-
Notifications
You must be signed in to change notification settings - Fork 34
Expand file tree
/
Copy pathCoreModule.cpp
More file actions
474 lines (405 loc) · 11.9 KB
/
Copy pathCoreModule.cpp
File metadata and controls
474 lines (405 loc) · 11.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
#include <windows.h>
#include <stdio.h>
#include <string>
#include <time.h>
#include <math.h>
#include <list>
#include <map>
#include <algorithm>
#include <FLHook.h>
#include <plugin.h>
#include <PluginUtilities.h>
#include "Main.h"
#include <hookext_exports.h>
CoreModule::CoreModule(PlayerBase* the_base) : Module(TYPE_CORE), base(the_base), space_obj(0), dont_eat(false),
dont_rust(false), wasDamagedSinceLastUpdate(false), undergoingDestruction(false)
{
}
CoreModule::~CoreModule()
{
if (space_obj)
{
pub::SpaceObj::Destroy(space_obj, DestroyType::VANISH);
spaceobj_modules.erase(space_obj);
space_obj = 0;
}
}
void CoreModule::Spawn()
{
if (!space_obj)
{
pub::SpaceObj::SolarInfo si;
memset(&si, 0, sizeof(si));
si.iFlag = 4;
char archname[100];
if (base->basesolar == "legacy")
{
_snprintf(archname, sizeof(archname), "dsy_playerbase_%02u", base->base_level);
}
else if (base->basesolar == "modern")
{
_snprintf(archname, sizeof(archname), "dsy_playerbase_modern_%02u", base->base_level);
}
else
{
_snprintf(archname, sizeof(archname), base->basesolar.c_str());
}
si.iArchID = CreateID(archname);
if (base->basesolar == "legacy" || base->basesolar == "modern")
{
si.iLoadoutID = CreateID(archname);
}
else
{
si.iLoadoutID = CreateID(base->baseloadout.c_str());
}
si.iHitPointsLeft = 1;
si.iSystemID = base->system;
si.mOrientation = base->rotation;
si.vPos = base->position;
si.Costume.head = CreateID("pi_pirate2_head");
si.Costume.body = CreateID("pi_pirate8_body");
si.Costume.lefthand = 0;
si.Costume.righthand = 0;
si.Costume.accessories = 0;
si.iVoiceID = CreateID("atc_leg_m01");
strncpy_s(si.cNickName, sizeof(si.cNickName), base->nickname.c_str(), base->nickname.size());
// Check to see if the hook IDS limit has been reached
static uint solar_ids = 526000;
if (++solar_ids > 526999)
{
solar_ids = 0;
return;
}
// Send the base name to all players that are online
base->solar_ids = solar_ids;
wstring basename = base->basename;
//if (base->affiliation)
//{
// basename = HkGetWStringFromIDS(Reputation::get_name(base->affiliation)) + L" - " + base->basename;
//}
struct PlayerData* pd = 0;
while (pd = Players.traverse_active(pd))
{
HkChangeIDSString(pd->iOnlineID, base->solar_ids, basename);
}
// Set the base name
FmtStr infoname(solar_ids, 0);
infoname.begin_mad_lib(solar_ids); // scanner name
infoname.end_mad_lib();
FmtStr infocard(solar_ids, 0);
infocard.begin_mad_lib(solar_ids); // infocard
infocard.end_mad_lib();
pub::Reputation::Alloc(si.iRep, infoname, infocard);
CreateSolar::SpawnSolar(space_obj, si);
spaceobj_modules[space_obj] = this;
// Set base health to reflect saved value unless this is a new base with
// a health of zero in which case we set it to 5% of the maximum and let
// players repair it.
float current;
pub::SpaceObj::GetHealth(space_obj, current, base->max_base_health);
if (base->base_health <= 0)
{
if (base->isFreshlyBuilt)
{
base->base_health = base->max_base_health * 0.5f;
}
else
{
AddLog("ERROR: Failed to load health for base %s: read health: %f, compare with today and yesterday backups.\n", wstos(base->basename).c_str(), base->base_health);
base->base_health = base->max_base_health;
}
}
else if (base->base_health > base->max_base_health)
{
base->base_health = base->max_base_health;
}
pub::SpaceObj::SetRelativeHealth(space_obj, base->base_health / base->max_base_health);
base->baseCSolar = (CSolar*)CObject::Find(space_obj, CObject::CSOLAR_OBJECT);
base->baseCSolar->Release();
if (shield_reinforcement_threshold_map.count(base->base_level))
base->base_shield_reinforcement_threshold = shield_reinforcement_threshold_map[base->base_level];
else
base->base_shield_reinforcement_threshold = FLT_MAX;
base->SyncReputationForBaseObject(space_obj);
if (set_plugin_debug > 1)
ConPrint(L"CoreModule::created space_obj=%u health=%f\n", space_obj, base->base_health);
pub::AI::SetPersonalityParams pers = CreateSolar::MakePersonality();
pub::AI::SubmitState(space_obj, &pers);
if (mapArchs[base->basetype].mining)
{
HookExt::AddMiningObj(space_obj, mapArchs[base->basetype].miningevent);
spaceobj_modules[space_obj]->mining = true;
}
else
{
spaceobj_modules[space_obj]->mining = false;
}
}
}
wstring CoreModule::GetInfo(bool xml)
{
return L"Core";
}
void CoreModule::LoadState(INI_Reader& ini)
{
while (ini.read_value())
{
if (ini.is_value("dont_eat"))
{
dont_eat = (ini.get_value_int(0) == 1);
}
else if (ini.is_value("dont_rust"))
{
dont_rust = (ini.get_value_int(0) == 1);
}
}
}
void CoreModule::SaveState(FILE* file)
{
fprintf(file, "[CoreModule]\n");
fprintf(file, "dont_eat = %d\n", dont_eat);
fprintf(file, "dont_rust = %d\n", dont_rust);
}
void CoreModule::RepairDamage(float max_base_health)
{
// no food & no water & no oxygen = RIOTS
if (!base->isCrewSupplied)
{
return;
}
// The bigger the base the more damage can be repaired.
for (REPAIR_ITEM& item : set_base_repair_items)
{
if (base->base_health >= max_base_health)
return;
if (base->HasMarketItem(item.good) >= item.quantity)
{
base->RemoveMarketGood(item.good, item.quantity);
base->base_health += repair_per_repair_cycle * base->base_level;
}
}
}
void CoreModule::EnableShieldFuse(bool shieldEnabled)
{
if (space_obj)
{
uint dummy;
IObjInspectImpl* inspect;
if (GetShipInspect(space_obj, inspect, dummy))
{
HkUnLightFuse((IObjRW*)inspect, shield_fuse, 0);
if (shieldEnabled)
{
HkLightFuse((IObjRW*)inspect, shield_fuse, 0.0f, 0.0f, 0.0f);
}
}
}
}
bool CoreModule::Timer(uint time)
{
// Disable shield if time elapsed
if (base->shield_timeout && base->shield_timeout < time)
{
base->shield_timeout = 0;
base->isShieldOn = false;
EnableShieldFuse(false);
}
if (set_holiday_mode)
{
return false;
}
if ((time % set_tick_time) != 0)
{
return false;
}
if (!space_obj)
{
return false;
}
if ((base->logic == 0) && (base->invulnerable == 1))
{
return false;
}
uint number_of_crew = base->HasMarketItem(set_base_crew_type);
bool isCrewSufficient = number_of_crew >= (base->base_level * 200);
pub::SpaceObj::GetHealth(space_obj, base->base_health, base->max_base_health);
if (!dont_rust && ((time % set_damage_tick_time) == 0))
{
float no_crew_penalty = isCrewSufficient ? 1.0f : no_crew_damage_multiplier;
// Reduce hitpoints to reflect wear and tear. This will eventually
// destroy the base unless it is able to repair itself.
float damage_taken = set_damage_per_tick * no_crew_penalty;
base->base_health -= damage_taken;
}
// Repair damage if we have sufficient crew on the base.
if (isCrewSufficient)
{
RepairDamage(base->max_base_health);
}
if (base->base_health > base->max_base_health)
{
base->base_health = base->max_base_health;
}
else if (base->base_health <= 0)
{
base->base_health = 0;
return SpaceObjDestroyed(space_obj);
}
pub::SpaceObj::SetRelativeHealth(space_obj, base->base_health / base->max_base_health);
// Humans use commodity_oxygen, commodity_water. Consume these for
// the crew or kill 10 crew off and repeat this every 12 hours.
if (!dont_eat && time % set_crew_check_frequency == 0)
{
bool passedFoodCheck = true;
uint peopleToFeed = 0;
for (uint humanCargoType : humanCargoList)
{
peopleToFeed += base->HasMarketItem(humanCargoType);
}
for (uint item : set_base_crew_consumption_items)
{
// Use water and oxygen.
if (base->HasMarketItem(item) >= peopleToFeed)
{
base->RemoveMarketGood(item, peopleToFeed);
}
else
{
base->RemoveMarketGood(item, base->HasMarketItem(item));
passedFoodCheck = false;
}
}
// Humans use food but may eat one of a number of types.
for (uint item : set_base_crew_food_items)
{
if (!peopleToFeed)
{
break;
}
uint food_available = base->HasMarketItem(item);
if (food_available)
{
uint food_to_use = min(food_available, peopleToFeed);
base->RemoveMarketGood(item, food_to_use);
peopleToFeed -= food_to_use;
}
}
// Insufficent food so fail check
if (peopleToFeed)
{
passedFoodCheck = false;
}
base->isCrewSupplied = passedFoodCheck;
}
return false;
}
float CoreModule::SpaceObjDamaged(uint space_obj, uint attacking_space_obj, float curr_hitpoints, float new_hitpoints)
{
base->shield_timeout = time(nullptr) + 60;
if (!base->isShieldOn)
{
base->isShieldOn = true;
EnableShieldFuse(true);
}
if (!base->vulnerableWindowStatus || base->invulnerable == 1 || base->shield_strength_multiplier >= 1.0f)
{
// base invulnerable, keep current health value
return curr_hitpoints;
}
float damageTaken;
if (!siegeWeaponryMap.empty())
{
const auto& siegeDamageIter = siegeWeaponryMap.find(iDmgMunitionID);
if (siegeDamageIter == siegeWeaponryMap.end())
{
//Siege gun(s) defined, but this is not one of them, no damage dealt
return curr_hitpoints;
}
else
{
//Even with siege gun damage override, it still takes shield strength into the account
damageTaken = siegeDamageIter->second * (1.0f - base->shield_strength_multiplier);
}
}
else
{
damageTaken = ((curr_hitpoints - new_hitpoints) * (1.0f - base->shield_strength_multiplier));
}
base->damage_taken_since_last_threshold += damageTaken;
if (base->damage_taken_since_last_threshold >= base->base_shield_reinforcement_threshold)
{
base->damage_taken_since_last_threshold -= base->base_shield_reinforcement_threshold;
base->shield_strength_multiplier += shield_reinforcement_increment;
}
if (!wasDamagedSinceLastUpdate)
{
base->baseCSolar->set_hit_pts(base->base_health);
wasDamagedSinceLastUpdate = true;
}
float newHealth = max(0, curr_hitpoints - damageTaken);
base->SpaceObjDamaged(space_obj, attacking_space_obj, curr_hitpoints, newHealth);
base->base_health = newHealth;
return newHealth;
}
bool CoreModule::SpaceObjDestroyed(uint space_obj, bool moveFile, bool broadcastDeath)
{
if (this->space_obj == space_obj && !undergoingDestruction)
{
undergoingDestruction = true;
if (set_plugin_debug > 1)
ConPrint(L"CoreModule::destroyed space_obj=%u\n", space_obj);
pub::SpaceObj::LightFuse(space_obj, "player_base_explode_fuse", 0);
spaceobj_modules.erase(space_obj);
this->space_obj = 0;
//chunk of code begins here
//No need to calculate health in this scenario, go straight to drama
Siege::SiegeAudioCalc(base->base, base->system, base->position, 0);
//List all players in the system at the time
list<string> CharsInSystem;
struct PlayerData* pd = 0;
while (pd = Players.traverse_active(pd))
{
if (broadcastDeath)
{
PrintUserCmdText(pd->iOnlineID, L"Base %s destroyed", base->basename.c_str());
}
if (pd->iSystemID == base->system)
{
const wstring& charname = (const wchar_t*)Players.GetActiveCharacterName(pd->iOnlineID);
CharsInSystem.emplace_back(wstos(charname));
}
}
// Logging
wstring wscMsg = L": Base %b destroyed";
wscMsg = ReplaceStr(wscMsg, L"%b", base->basename.c_str());
string scText = wstos(wscMsg);
BaseLogging("%s", scText.c_str());
//Base specific logging
string msg = "Base destroyed. Players in system: ";
for each (string player in CharsInSystem)
{
msg += (player + "; ");
}
Log::LogBaseAction(wstos(base->basename), msg.c_str());
ConPrint(L"BASE: Base %s destroyed\n", base->basename.c_str());
// Unspawn, delete base and save file.
DeleteBase(base, moveFile);
// Careful not to access this as this object will have been deleted by now.
return true;
}
return false;
}
void CoreModule::SetReputation(int player_rep, float attitude)
{
if (space_obj)
{
EnableShieldFuse(base->isShieldOn);
int obj_rep;
pub::SpaceObj::GetRep(this->space_obj, obj_rep);
if (set_plugin_debug > 1)
ConPrint(L"CoreModule::SetReputation player_rep=%u obj_rep=%u attitude=%f base=%08x\n",
player_rep, obj_rep, attitude, base->base);
pub::Reputation::SetAttitude(obj_rep, player_rep, attitude);
}
}