forked from Atlantis-PBEM/Atlantis
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathobject.cpp
More file actions
756 lines (649 loc) · 14.5 KB
/
Copy pathobject.cpp
File metadata and controls
756 lines (649 loc) · 14.5 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
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
// START A3HEADER
//
// This source file is part of the Atlantis PBM game program.
// Copyright (C) 1995-1999 Geoff Dunbar
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program, in the file license.txt. If not, write
// to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
// Boston, MA 02111-1307, USA.
//
// See the Atlantis Project web page for details:
// http://www.prankster.com/project
//
// END A3HEADER
#include "object.h"
#include "aregion.h"
#include "items.h"
#include "skills.h"
#include "gamedata.h"
#include "faction.h"
#include "unit.h"
#include "gamedefs.h"
#include "fileio.h"
#include "astring.h"
#include "gameio.h"
#include <algorithm>
#include <map>
int ParseObject(AString *token)
{
for (int i = O_DUMMY+1; i < NOBJECTS; ++i)
{
if (*token == ObjectDefs[i].name)
{
if (ObjectDefs[i].flags & ObjectType::DISABLED)
return -1;
return i;
}
}
return -1;
}
int ObjectIsShip(int ot)
{
if (ot < 0 || ot >= NOBJECTS)
return 0;
return ObjectDefs[ot].capacity ? 1 : 0;
}
//----------------------------------------------------------------------------
Object::Object(ARegion *reg)
{
name = new AString("Dummy");
describe = NULL;
region = reg;
inner = -1;
num = 0;
type = O_DUMMY;
incomplete = 0;
capacity = 0;
runes = 0;
prevdir = -1;
mages = 0;
}
Object::~Object()
{
if (!objects.empty())
std::cerr << "Warning sub-objects not empty for " << name << std::endl;
delete name;
delete describe;
region = NULL;
}
void Object::Writeout(Aoutfile *f)
{
f->PutInt(num);
f->PutInt(type);
f->PutInt(incomplete);
f->PutStr(*name);
if (describe)
{
f->PutStr(*describe);
}
else
{
f->PutStr("none");
}
f->PutInt(inner);
f->PutInt(-1);
if (Globals->PREVENT_SAIL_THROUGH && !Globals->ALLOW_TRIVIAL_PORTAGE)
f->PutInt(prevdir);
else
f->PutInt(-1);
f->PutInt(runes);
f->PutInt(units.size());
for (auto &u : units)
u->Writeout(f);
f->PutInt(objects.size());
{
for (auto op : objects)
{
op->Writeout(f);
//delete op;
}
}
objects.clear();
}
void Object::Readin(Ainfile *f, AList *facs, ATL_VER v)
{
num = f->GetInt();
type = f->GetInt();
incomplete = f->GetInt();
delete name;
name = f->GetStr();
delete describe;
describe = f->GetStr();
if (*describe == "none")
{
delete describe;
describe = NULL;
}
inner = f->GetInt();
const int dummy = f->GetInt();
if (dummy == -1)
{
prevdir = f->GetInt();
runes = f->GetInt();
}
else
runes = dummy;
// Now, fix up a save file if ALLOW_TRIVIAL_PORTAGE is allowed, just
// in case it wasn't when the save file was made.
if (Globals->ALLOW_TRIVIAL_PORTAGE)
prevdir = -1;
const int i = f->GetInt();
for (int j = 0; j < i; ++j)
{
Unit *temp = new Unit;
temp->Readin(f, facs, v);
temp->MoveUnit(this);
}
mages = ObjectDefs[type].maxMages;
if (v >= MAKE_ATL_VER(4, 2, 86))
{
const int num_objects = f->GetInt();
if (num_objects > 0)
{
std::map<int, Unit*> unit_map;
for (Unit *u : units)
{
unit_map[u->num] = u;
}
// read sub objects
for (int i = 0; i < num_objects; ++i)
{
Object *op = new Object(region);
op->Readin(f, facs, v);
op->parent = this;
objects.push_back(op);
// remap units
std::vector<Unit*> new_units;
bool all_good = true;
for (Unit *u : op->units)
{
auto umi = unit_map.find(u->num);
if (umi == unit_map.end())
{
std::cerr << "Error in object data " << op->name->str() << ' ' << u->name->str() << std::endl;
all_good = false;
}
else
{
Unit *base_unit = umi->second;
new_units.push_back(base_unit);
base_unit->object = this;
}
}
if (all_good)
op->units = new_units;
}
}
}
}
void Object::SetName(AString *s)
{
if (!s)
return;
if (!CanModify())
{
delete s;
return;
}
AString *newname = s->getlegal();
delete s;
if (!newname)
return;
*newname += AString(" [") + num + "]";
delete name;
name = newname;
}
void Object::SetDescribe(AString *s)
{
if (!CanModify())
{
delete s;
return;
}
delete describe;
if (s)
{
AString *newname = s->getlegal();
delete s;
describe = newname;
}
else
describe = NULL;
}
int Object::IsBoat()
{
return ObjectIsShip(type);
}
bool Object::hasBoats() const
{
if (ObjectIsShip(type))
return true;
if (type != O_ARMY)
return false;
for (auto op : objects)
{
if (ObjectIsShip(op->type))
return true;
}
return false;
}
int Object::IsBuilding()
{
return ObjectDefs[type].protect ? 1 : 0;
}
int Object::CanModify()
{
return (ObjectDefs[type].flags & ObjectType::CANMODIFY) ? 1 : 0;
}
Unit* Object::GetUnit(int num)
{
return Unit::findByNum(units, num);
}
Unit* Object::GetUnitAlias(int alias, int faction)
{
return Unit::findByFaction(units, alias, faction);
}
Unit* Object::GetUnitId(const UnitId *id, int faction)
{
if (id == NULL)
return NULL;
return id->find(units, faction);
}
int Object::CanEnter(ARegion *reg, Unit *u)
{
if (!(ObjectDefs[type].flags & ObjectType::CANENTER) &&
(u->type == U_MAGE || u->type == U_NORMAL || u->type == U_APPRENTICE))
{
return 0;
}
return 1;
}
Unit* Object::ForbiddenBy(ARegion *reg, Unit *u)
{
Unit *owner = GetOwner();
if (!owner)
return NULL;
if (owner->GetAttitude(reg, u) < A_FRIENDLY)
return owner;
return NULL;
}
Unit* Object::GetOwner()
{
if (units.empty())
return nullptr;
auto i = units.begin();
Unit *owner = *i;
while (i != units.end() && !(**i).GetMen())
{
owner = *i;
++i;
}
return owner;
}
void Object::Report(JsonReport &of, Faction *fac, int obs, int truesight,
int detfac, int passobs, int passtrue, int passdetfac, int present)
{
Areport *f = of.plainFile();
ObjectType *ob = &ObjectDefs[type];
if (type != O_DUMMY && !present)
{
if (IsBuilding() &&
!(Globals->TRANSIT_REPORT & GameDefs::REPORT_SHOW_BUILDINGS))
{
// This is a building and we don't see buildings in transit
return;
}
if (IsBoat() &&
!(Globals->TRANSIT_REPORT & GameDefs::REPORT_SHOW_SHIPS))
{
// This is a ship and we don't see ships in transit
return;
}
if (IsRoad() &&
!(Globals->TRANSIT_REPORT & GameDefs::REPORT_SHOW_ROADS))
{
// This is a road and we don't see roads in transit
return;
}
}
if (type != O_DUMMY)
{
of.startDict(NULL);
bool is_fleet = false;
AString temp = AString("+ ") + *name + " : ";
if (type != O_ARMY || objects.empty())
temp += ob->name;
else
{
is_fleet = true;
temp += "Fleet";
std::map<int, unsigned> obj_ct;
for (Object *op : objects)
{
obj_ct[op->type]++;
}
for (const auto &mi : obj_ct)
{
temp += ", ";
temp += AString(mi.second);
temp += " ";
temp += ObjectDefs[mi.first].name;
}
}
if (incomplete > 0)
{
temp += AString(", needs ") + incomplete;
}
else if (Globals->DECAY &&
!(ob->flags & ObjectType::NEVERDECAY) && incomplete < 1)
{
if (incomplete > (0 - ob->maxMonthlyDecay))
{
temp += ", about to decay";
}
else if (incomplete > (0 - ob->maxMaintenance/2))
{
temp += ", needs maintenance";
}
}
if (inner != -1)
temp += ", contains an inner location";
if (runes)
temp += ", engraved with Runes of Warding";
if (is_fleet || describe)
temp += AString("; ");
if (is_fleet)
{
bool first = true;
for (const auto &op : objects)
{
if (!first)
temp += ", ";
temp += *op->name;
temp += " ";
temp += ObjectDefs[op->type].name;
first = false;
}
}
if (describe)
{
if (is_fleet)
temp += ". ";
temp += *describe;
}
if (!(ob->flags & ObjectType::CANENTER))
temp += ", closed to player units";
temp += ".";
f->PutStr(temp);
of.putPairStr("name", temp.str());
of.putPairStr("type", ob->name);
f->AddTab();
}
//foreach unit in this
bool first = true;
for (const auto &u : units)
{
if (u->faction == fac)
{
// self-report
if (first) {
first = false;
of.startArray("units");
}
u->WriteReport(of, -1, 1, 1, 1, region);
}
else
{
if (present)
{
// foreign unit present
if (first) {
first = false;
of.startArray("units");
}
u->WriteReport(of, obs, truesight, detfac, type != O_DUMMY, region);
}
else
{
// foreign unit passing through
if ((type == O_DUMMY && (Globals->TRANSIT_REPORT & GameDefs::REPORT_SHOW_OUTDOOR_UNITS)) ||
(type != O_DUMMY && (Globals->TRANSIT_REPORT & GameDefs::REPORT_SHOW_INDOOR_UNITS)) ||
(u->guard == GUARD_GUARD && (Globals->TRANSIT_REPORT & GameDefs::REPORT_SHOW_GUARDS)))
{
if (first) {
first = false;
of.startArray("units");
}
u->WriteReport(of, passobs, passtrue, passdetfac, type != O_DUMMY, region);
}
}
}
}
if (!first) {
of.endArray();
}
f->EndLine();
if (type != O_DUMMY)
of.endDict();
if (type != O_DUMMY)
f->DropTab();
}
void Object::SetPrevDir(int newdir)
{
prevdir = newdir;
}
void Object::MoveObject(ARegion *toreg)
{
region->objects.remove(this);
region = toreg;
for (auto o : objects)
{
o->region = toreg;
}
toreg->objects.push_back(this);
}
int Object::IsRoad()
{
return (O_ROADN <= type && type <= O_ROADS) ? 1 : 0;
}
void Object::addUnit(Unit *u)
{
units.push_back(u);
}
void Object::prependUnit(Unit *u)
{
units.insert(units.begin(), u);
}
void Object::removeUnit(Unit *u, bool remove_from_sub)
{
units.erase(std::remove(units.begin(), units.end(), u), units.end());
if (!remove_from_sub)
return;
// check sub-objects
for (auto o : objects)
{
o->units.erase(std::remove(o->units.begin(), o->units.end(), u), o->units.end());
}
}
Object* Object::findUnitSubObject(Unit *u)
{
Object *ret = nullptr;
for (auto o : objects)
{
for(auto &ou : o->units)
{
if (ou == u)
ret = o;
}
}
return ret;
}
void Object::DoDecayClicks(ARegionList *pRegs)
{
if (ObjectDefs[type].flags & ObjectType::NEVERDECAY)
return;
int clicks = getrandom(region->GetMaxClicks()) + region->PillageCheck();
if (clicks > ObjectDefs[type].maxMonthlyDecay)
clicks = ObjectDefs[type].maxMonthlyDecay;
incomplete += clicks;
if (incomplete > 0)
{
// trigger decay event
region->RunDecayEvent(this, pRegs);
}
// apply to subojects
for (auto o : objects)
o->DoDecayClicks(pRegs);
}
bool Object::canFly() const
{
if (type == O_ARMY && !objects.empty())
{
// all ships must fly
for (Object *op : objects)
{
if (op->type != O_BALLOON)
return false;
}
return true;
}
return type == O_BALLOON; // TODO: generalize
}
AString* ObjectDescription(int obj)
{
if (ObjectDefs[obj].flags & ObjectType::DISABLED)
return NULL;
ObjectType *o = &ObjectDefs[obj];
AString *temp = new AString;
*temp += AString(o->name) + ": ";
if (o->capacity)
*temp += AString("This is a ship with capacity ") + o->capacity + ".";
else
*temp += "This is a building.";
if (Globals->LAIR_MONSTERS_EXIST && o->monster != -1)
{
*temp += " Monsters can potentially lair in this structure.";
if (o->flags & ObjectType::NOMONSTERGROWTH)
*temp += " Monsters in this structures will never regenerate.";
}
if (o->flags & ObjectType::CANENTER)
*temp += " Units may enter this structure.";
if (o->protect)
{
*temp += AString(" This structure provides defense to the first ") +
o->protect + " men inside it.";
}
// Handle all the specials
for (int i = 0; i < NUMSPECIALS; ++i)
{
SpecialType *spd = &SpecialDefs[i];
if (!(spd->targflags & SpecialType::HIT_BUILDINGIF) &&
!(spd->targflags & SpecialType::HIT_BUILDINGEXCEPT))
{
continue;
}
bool match = false;
for (int j = 0; j < 3; ++j)
if (spd->buildings[j] == obj)
match = true;
if (!match)
continue;
*temp += " Units in this structure are";
if (spd->targflags & SpecialType::HIT_BUILDINGEXCEPT)
*temp += " not";
*temp += AString(" affected by ") + spd->specialname + ".";
}
if (o->sailors)
{
*temp += AString(" This ship requires ") + o->sailors +
" total levels of sailing skill to sail.";
}
if (o->maxMages && Globals->LIMITED_MAGES_PER_BUILDING)
{
*temp += AString(" This structure will allow up to ") + o->maxMages +
" mages to study above level 2.";
}
// can this object be built
bool buildable = true;
// if there are no inputs, or no skill to build
if (o->item == -1 || o->skill == -1)
buildable = false;
// if the skill is disabled
if (SkillDefs[o->skill].flags & SkillType::DISABLED)
buildable = false;
// wood or stone requires two checks and'ed together
if (o->item != I_WOOD_OR_STONE &&
(ItemDefs[o->item].flags & ItemType::DISABLED))
buildable = false;
// check wood and stone disabled
if (o->item == I_WOOD_OR_STONE &&
(ItemDefs[I_WOOD].flags & ItemType::DISABLED) &&
(ItemDefs[I_STONE].flags & ItemType::DISABLED))
buildable = false;
if (!buildable)
{
*temp += " This structure cannot be built by players.";
}
else
{
*temp += AString(" This structure is built using ") +
SkillStrs(o->skill) + " " + o->level + " and requires " +
o->cost + " ";
if (o->item == I_WOOD_OR_STONE)
*temp += "wood or stone";
else
*temp += ItemDefs[o->item].name;
*temp += " to build.";
}
// check if object is a building that increases production
if (o->productionAided != -1 &&
!(ItemDefs[o->productionAided].flags & ItemType::DISABLED))
{
*temp += " This trade structure increases the amount of ";
if (o->productionAided == I_SILVER)
*temp += "entertainment";
else
*temp += ItemDefs[o->productionAided].names;
*temp += " available in the region.";
}
if (Globals->DECAY)
{
if (o->flags & ObjectType::NEVERDECAY)
{
*temp += " This structure will never decay.";
}
else
{
*temp += AString(" This structure can take ") + o->maxMaintenance +
" units of damage before it begins to decay.";
*temp += AString(" Damage can occur at a maximum rate of ") +
o->maxMonthlyDecay + " units per month.";
if (buildable)
{
*temp += AString(" Repair of damage is accomplished at ") +
"a rate of " + o->maintFactor + " damage units per " +
"unit of ";
if (o->item == I_WOOD_OR_STONE)
{
*temp += "wood or stone.";
}
else
{
*temp += ItemDefs[o->item].name;
}
}
}
}
return temp;
}