-
Notifications
You must be signed in to change notification settings - Fork 37
/
object.cpp
919 lines (832 loc) · 22.3 KB
/
object.cpp
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
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
// 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
// 13/06/04 changed Object class to handle fleets (ravanrooke)
#include "object.h"
#include "items.h"
#include "skills.h"
#include "gamedata.h"
#include "unit.h"
#include "indenter.hpp"
int LookupObject(AString *token)
{
for (int i = 0; i < NOBJECTS; i++) {
if (*token == ObjectDefs[i].name) return i;
}
return -1;
}
/* ParseObject checks for matching Object types AND
* for matching ship-type items (which are also
* produced using the build order) if the ships
* argument is given.
*/
int ParseObject(AString *token, int ships)
{
// Check for ship-type items:
if (ships > 0) {
for (int i=0; i<NITEMS; i++) {
if (ItemDefs[i].type & IT_SHIP) {
if ((*token == ItemDefs[i].name) ||
(*token == ItemDefs[i].abr)) {
if (ItemDefs[i].flags & ItemType::DISABLED) continue;
return -(i+1);
}
}
}
}
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 (ObjectDefs[ot].capacity) return 1;
return 0;
}
Object::Object(ARegion *reg)
{
num = 0;
type = O_DUMMY;
name = new AString("Dummy");
incomplete = 0;
describe = 0;
capacity = 0;
mages = 0;
inner = -1;
runes = 0;
region = reg;
prevdir = -1;
flying = 0;
destroyed = 0;
movepoints = Globals->PHASED_MOVE_OFFSET % Globals->MAX_SPEED;
ships.clear();
}
Object::~Object()
{
if (name) delete name;
if (describe) delete describe;
region = (ARegion *)NULL;
}
void Object::Writeout(std::ostream& f)
{
f << num << '\n';
f << (type == -1
? "NO_OBJECT"
: (IsFleet() ? ObjectDefs[O_FLEET].name : ObjectDefs[type].name)
) << '\n';
f << incomplete << '\n';
f << name->const_str() << '\n';
f << (describe ? describe->const_str() : "none") << '\n';
f << inner << '\n';
f << (Globals->PREVENT_SAIL_THROUGH && !Globals->ALLOW_TRIVIAL_PORTAGE ? prevdir : -1) << '\n';
f << runes << '\n';
f << units.size() << '\n';
for(auto u: units) u->Writeout(f);
WriteoutFleet(f);
}
void Object::Readin(std::istream& f, AList *facs)
{
AString temp;
f >> num;
f >> std::ws >> temp;
type = LookupObject(&temp);
f >> incomplete;
if (name) delete name;
f >> std::ws >> temp;
name = new AString(temp);
AString *tmp = name->stripnumber();
SetName(tmp);
f >> std::ws >> temp;
describe = temp.getlegal();
if (*describe == "none") {
delete describe;
describe = 0;
}
f >> inner;
f >> prevdir;
f >> runes;
// 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;
int i;
f >> i;
for (int j = 0; j < i; j++) {
Unit *temp = new Unit;
temp->Readin(f, facs);
if (!temp->faction)
continue;
temp->MoveUnit(this);
if (!(temp->faction->is_npc)) region->visited = 1;
}
mages = ObjectDefs[type].maxMages;
ReadinFleet(f);
}
void Object::SetName(AString *s)
{
if (s && (CanModify())) {
AString *newname = s->getlegal();
if (!newname) {
delete s;
return;
}
delete s;
delete name;
*newname += AString(" [") + num + "]";
name = newname;
}
}
void Object::SetDescribe(AString *s)
{
if (CanModify()) {
if (describe) delete describe;
if (s) {
AString *newname = s->getlegal();
delete s;
describe = newname;
} else describe = 0;
}
}
int Object::IsFleet()
{
if (type == O_FLEET) return 1;
if (ObjectDefs[type].sailors > 0) return 1;
if (ships.size() > 0) return 1;
return 0;
}
int Object::IsBuilding()
{
if (ObjectDefs[type].protect)
return 1;
return 0;
}
int Object::CanModify()
{
return (ObjectDefs[type].flags & ObjectType::CANMODIFY);
}
Unit *Object::GetUnit(int num)
{
for(auto u: units)
if (u->num == num)
return u;
return 0;
}
Unit *Object::GetUnitAlias(int alias, int faction)
{
// First search for units with the 'formfaction'
for(auto u: units) {
if (u->alias == alias && u->formfaction->num == faction)
return u;
}
// Now search against their current faction
for(auto u: units) {
if (u->alias == alias && u->faction->num == faction)
return u;
}
return 0;
}
Unit *Object::get_unit_id(UnitId *unitid, int faction)
{
if (unitid == 0) return 0;
if (unitid->unitnum) {
return GetUnit(unitid->unitnum);
} else {
if (unitid->faction) {
return GetUnitAlias(unitid->alias, unitid->faction);
} else {
return GetUnitAlias(unitid->alias, 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 || type == O_GATEWAY) {
return(0);
}
if (owner->GetAttitude(reg, u) < A_FRIENDLY) {
return owner;
}
return 0;
}
Unit *Object::GetOwner()
{
Unit *owner = nullptr;
if (units.size() > 0) owner = units[0];
return(owner);
}
void Object::build_json_report(json& j, Faction *fac, int obs, int truesight,
int detfac, int passobs, int passtrue, int passdetfac, int present)
{
// Exit early if no observable.
if ((type != O_DUMMY) && !present) {
if (IsFleet() && !(Globals->TRANSIT_REPORT & GameDefs::REPORT_SHOW_SHIPS)) return;
if (IsBuilding() && !(Globals->TRANSIT_REPORT & GameDefs::REPORT_SHOW_BUILDINGS)) return;
if (IsRoad() && !(Globals->TRANSIT_REPORT & GameDefs::REPORT_SHOW_ROADS)) return;
}
json container = json::object();
if (type != O_DUMMY) {
ObjectType& ob = ObjectDefs[type];
std::string s = name->const_str();
container["name"] = s.substr(0, s.find(" [")); // remove the object number.
container["number"] = num;
container["type"] = ob.name;
if (describe) container["description"] = describe->const_str();
if (IsFleet()) {
container["fleet"] = true;
if((GetOwner() && fac == GetOwner()->faction) || (obs > 9)){
container["load"] = FleetLoad();
container["capacity"] = FleetCapacity();
container["sailors"] = FleetSailingSkill(1);
container["fleet_size"] = GetFleetSize();
container["max_speed"] = GetFleetSpeed(1);
container["damage_percent"] = incomplete;
}
if (
Globals->PREVENT_SAIL_THROUGH && !Globals->ALLOW_TRIVIAL_PORTAGE
&& (flying < 1) && (TerrainDefs[region->type].similar_type != R_OCEAN)
) {
for (int dir = 0; dir < NDIRS; dir++) {
if (SailThroughCheck(dir) == 1) container["sail_directions"][DirectionAbrs[dir]] = true;
}
}
for(auto ship: ships) {
if (ship.type != -1 && ship.num > 0) {
ItemType item_def = ItemDefs[ship.type];
if (item_def.flags & ItemType::DISABLED) continue;
if (!(item_def.type & IT_SHIP)) continue;
container["ships"].push_back(
{ {"name", item_def.name}, {"number", ship.num}, { "plural", item_def.names } }
);
}
}
} else {
if (incomplete > 0) container["incomplete"] = incomplete;
if (inner != -1) container["inner_location"] = true;
if (runes) container["warding_runes"] = true;
if (!(ob.flags & ObjectType::CANENTER)) container["closed"] = true;
if (Globals->DECAY && !(ob.flags & ObjectType::NEVERDECAY) && incomplete < 1) {
if (incomplete > (0 - ob.maxMonthlyDecay)) {
container["decaying"] = true;
} else if (incomplete > (0 - ob.maxMaintenance/2)) {
container["needs_maintenance"] = true;
}
}
if (ob.flags & ObjectType::SACRIFICE && (incomplete < 0)) {
ItemType& def = ItemDefs[ob.sacrifice_item];
json item_obj = json{ { "name", def.name }, { "tag", def.abr }, { "plural", def.names } };
item_obj["amount"] = -incomplete;
container["sacrifice"] = item_obj;
}
if (ob.flags & ObjectType::GRANTSKILL) {
container["grantskill"] = {
{ "name", SkillDefs[ob.granted_skill].name },
{ "tag", SkillDefs[ob.granted_skill].abbr },
{ "level", ob.granted_level }
};
}
}
}
json& unit_container = (type == O_DUMMY) ? j["units"] : container["units"];
// Add units to container
for(auto u: units) {
json unit = json::object();
int attitude = fac->get_attitude(u->faction->num);
if (u->faction == fac) {
u->build_json_report(unit, -1, 1, 1, 1, attitude, fac->showunitattitudes);
} else {
if (present) {
u->build_json_report(unit, obs, truesight, detfac, type != O_DUMMY, attitude, fac->showunitattitudes);
} else {
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))) {
u->build_json_report(unit, passobs, passtrue, passdetfac, type != O_DUMMY, attitude, fac->showunitattitudes);
}
}
}
if (unit.empty()) continue;
unit_container.push_back(unit);
}
if (type != O_DUMMY) {
j["structures"].push_back(container);
}
}
void Object::SetPrevDir(int newdir)
{
prevdir = newdir;
}
void Object::MoveObject(ARegion *toreg)
{
region->objects.Remove(this);
region = toreg;
toreg->objects.Add(this);
}
int Object::IsRoad()
{
if (type >= O_ROADN && type <= O_ROADS) return 1;
return 0;
}
/* Performs a basic check on items for ship-types.
* (note: always fails for non-Fleet Objects.)
*/
int Object::CheckShip(int item)
{
if (item < 0) return 0;
if (!IsFleet()) return 0;
if (ItemDefs[item].type & IT_SHIP) return 1;
return 0;
}
void Object::WriteoutFleet(std::ostream& f)
{
if (!IsFleet()) return;
f << ships.size() << "\n";
for(auto ship: ships) ship.Writeout(f);
}
void Object::ReadinFleet(std::istream &f)
{
if (type != O_FLEET) return;
int nships;
f >> nships;
for (int i = 0; i < nships; i++) {
Item ship;
ship.Readin(f);
if (ship.type >= 0)
SetNumShips(ship.type, ship.num);
}
}
/* Returns the number of component ships of a given
* type.
*/
int Object::GetNumShips(int type)
{
if (CheckShip(type) != 0) {
for(auto ship: ships) {
if (ship.type == type) {
return ship.num;
}
}
}
return 0;
}
/* Erases possible previous entries for ship type
* and resets the number of ships.
*/
void Object::SetNumShips(int type, int num)
{
if (CheckShip(type) != 0) {
ships.SetNum(type, num);
FleetCapacity();
}
return;
}
/* Adds one ship of the given type.
*/
void Object::AddShip(int type)
{
if (CheckShip(type) == 0) return;
int num = GetNumShips(type);
num++;
SetNumShips(type, num);
}
/* Returns the String 'Fleet' for multi-ship fleets
* and the name of the ship for single ship fleets
*/
AString Object::FleetDefinition()
{
AString fleet;
int shiptype = -1;
int num = 0;
for (int i=0; i<NITEMS; i++) {
if (ItemDefs[i].type & IT_SHIP) {
int sn = GetNumShips(i);
if (sn > 0) {
num += sn;
shiptype = i;
}
}
}
if (num == 1) fleet = ItemDefs[shiptype].name;
else {
fleet = ObjectDefs[type].name;
// report ships:
for (int item=0; item<NITEMS; item++) {
num = GetNumShips(item);
if (num > 0) {
if (num > 1) {
fleet += AString(", ") + num + " " + ItemDefs[item].names;
} else {
fleet += AString(", ") + num + " " +ItemDefs[item].name;
}
}
}
}
return fleet;
}
/* Sets a fleet's sailing capacity.
*/
int Object::FleetCapacity()
{
AString *oname;
int ot;
capacity = 0;
// Calculate the maximum number of mages while we're at it
mages = 0;
if (!IsFleet()) return 0;
// Fleets are assumed to be flying, at least until we find any
// non-flying vessels in them
flying = 1;
for (int item=0; item < NITEMS; item++) {
int num = GetNumShips(item);
if (num < 1) continue;
if (ItemDefs[item].fly > 0) {
capacity += num * ItemDefs[item].fly;
} else {
capacity += num * ItemDefs[item].swim;
flying = 0;
}
oname = new AString(ItemDefs[item].name);
ot = LookupObject(oname);
delete oname;
if (ot > 0) {
mages += num * ObjectDefs[ot].maxMages;
}
}
return capacity;
}
/* Returns a fleet's load or -1 for non-fleet objects.
*/
int Object::FleetLoad()
{
int load = -1;
int wgt = 0;
if (IsFleet()) {
for(auto unit: units) {
wgt += unit->Weight();
}
load = wgt;
}
return load;
}
/* Return 1 if fleet can sail to a direction without sailing through land, or
* 0 if it cannot
*/
int Object::SailThroughCheck(int dir)
{
if (IsFleet()) {
// if target region doesn't exist, cannot be sailed into
if (!region->neighbors[dir]) {
return 0;
}
// flying fleets always can sail through
if (flying == 1) {
return 1;
}
// from ocean sailing is always possible
if (TerrainDefs[region->type].similar_type == R_OCEAN) {
return 1;
}
// fleet is not flying and it is in a land region. Check that it
// doesn's sail inland
if (TerrainDefs[region->neighbors[dir]->type].similar_type != R_OCEAN) {
return 0;
}
// sailing from land into ocean. If sail through is allowed, allow it
if (!Globals->PREVENT_SAIL_THROUGH) {
return 1;
}
// if the fleet hadn't sailed before, it can go in any direction
if (prevdir == -1) {
return 1;
}
// fleet can always sail backward
if (prevdir == dir) {
return 1;
}
// Now we have to check that fleet is not sailing through land
{
// Fleet is on land, it is not flying and comes from another region
// so check that the fleet goes not through land
int blocked1 = 0;
int blocked2 = 0;
int d1 = prevdir;
int d2 = dir;
if (d1 > d2) {
int tmp = d1;
d1 = d2;
d2 = tmp;
}
for (int k = d1+1; k < d2; k++) {
ARegion *land1 = region->neighbors[k];
if ((!land1) ||
(TerrainDefs[land1->type].similar_type !=
R_OCEAN))
blocked1 = 1;
}
int sides = NDIRS - 2 - (d2 - d1 - 1);
for (int l = d2+1; l <= d2 + sides; l++) {
int dl = l;
if (dl >= NDIRS) dl -= NDIRS;
ARegion *land2 = region->neighbors[dl];
if ((!land2) ||
(TerrainDefs[land2->type].similar_type !=
R_OCEAN))
blocked2 = 1;
}
if ((blocked1) && (blocked2)) {
return 0;
}
else {
return 1;
}
}
}
return 0;
}
/* Returns the total skill level of all sailors.
* If report is not 0, returns the total skill level of all
* units regardless if they have sail orders (for report
* purposes).
*/
int Object::FleetSailingSkill(int report)
{
int skill = -1;
int slvl = 0;
if (IsFleet()) {
for(auto unit: units) {
if ((report != 0) ||
(unit->monthorders && unit->monthorders->type == O_SAIL)) {
slvl += unit->GetSkill(S_SAILING) * unit->GetMen();
}
}
skill = slvl;
}
return skill;
}
/* Returns fleet size - which is the total of
* sailors needed to move the fleet.
*/
int Object::GetFleetSize()
{
if (!IsFleet()) return 0;
int inertia = 0;
for (int item=0; item<NITEMS; item++) {
int num = GetNumShips(item);
if (num > 0) inertia += num * ItemDefs[item].weight;
}
return (inertia / 50);
}
/* Returns the fleet speed - theoretical if report
* argument is greater than zero (which means all
* potential sailors issued a SAIL command). The
* latter is mainly for report purposes. Game
* functions for moving the fleet provide a zero
* argument.
*/
int Object::GetFleetSpeed(int report)
{
int tskill = FleetSailingSkill(report);
int speed = Globals->MAX_SPEED;
int weight = 0;
int capacity = 0;
int bonus;
int windbonus = 0;
if (!IsFleet()) return 0;
for (int item = 0; item < NITEMS; item++) {
int num = GetNumShips(item);
if (num > 0) {
weight += num * ItemDefs[item].weight;
if (ItemDefs[item].fly > 0) {
capacity += num * ItemDefs[item].fly;
} else {
capacity += num * ItemDefs[item].swim;
flying = 0;
}
// Fleets travel as fast as their slowest ship
if (ItemDefs[item].speed < speed)
speed = ItemDefs[item].speed;
}
}
// no ships no speed
if (weight < 1) return 0;
// check for sufficient sailing skill!
if (tskill < (weight / 50)) return 0;
// count wind mages
for(auto unit: units) {
int wb = unit->GetAttribute("wind");
if (wb > 0) {
windbonus += wb * 12 * Globals->FLEET_WIND_BOOST;
}
}
// speed gain through wind:
bonus = windbonus / (weight / 50);
if (bonus > Globals->FLEET_WIND_BOOST)
bonus = Globals->FLEET_WIND_BOOST;
speed += bonus;
// speed bonus due to more / better skilled sailors:
bonus = 0;
while (tskill >= (weight / 25)) {
bonus++;
tskill /= 2;
}
if (bonus > Globals->FLEET_CREW_BOOST)
bonus = Globals->FLEET_CREW_BOOST;
speed += bonus;
// check for being overloaded
if (FleetLoad() > capacity) return 0;
// speed bonus due to low load:
int loadfactor = (capacity / FleetLoad());
bonus = 0;
while (loadfactor >= 2) {
bonus++;
loadfactor /= 2;
}
if (bonus > Globals->FLEET_LOAD_BOOST)
bonus = Globals->FLEET_LOAD_BOOST;
speed += bonus;
// Cap everything at max speed
if (speed > Globals->MAX_SPEED) speed = Globals->MAX_SPEED;
return speed;
}
AString *ObjectDescription(int obj)
{
if (ObjectDefs[obj].flags & ObjectType::DISABLED)
return new AString("");
ObjectType *o = &ObjectDefs[obj];
AString *temp = new AString;
if (ObjectDefs[obj].flags & ObjectType::GROUP) {
*temp += "This is a group of ships.";
} else if (o->capacity) {
*temp += "This is a ship.";
} else {
*temp += "This is a building.";
}
if (o->flags & ObjectType::SACRIFICE) {
*temp += " This structure requires a sacrifice of ";
*temp += AString(o->sacrifice_amount) + " ";
*temp += plural(o->sacrifice_amount, ItemDefs[o->sacrifice_item].name, ItemDefs[o->sacrifice_item].names);
*temp += " [";
*temp += ItemDefs[o->sacrifice_item].abr;
*temp += "].";
}
if (o->flags & ObjectType::GRANTSKILL) {
*temp += " This structure grants the owner the skill " + SkillDefs[o->granted_skill].name + " [" +
SkillDefs[o->granted_skill].abbr + "] at a skill level of ";
*temp += AString(o->granted_level) + ".";
}
if (Globals->LAIR_MONSTERS_EXIST && (o->monster != -1)) {
*temp += AString(" ") + capitalize(ItemDefs[o->monster].names) + " 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.";
// Now do the defences. First, figure out how many to do.
int totaldef = 0;
for (int i=0; i<NUM_ATTACK_TYPES; i++) {
totaldef += (o->defenceArray[i] != 0);
}
// Now add the description to temp
*temp += AString(" This structure gives a defensive bonus of ");
for (int i=0; i<NUM_ATTACK_TYPES; i++) {
if (o->defenceArray[i]) {
totaldef--;
*temp += AString(o->defenceArray[i]) + " against " +
AttType(i) + AString(" attacks");
if (totaldef >= 2) {
*temp += AString(", ");
} else {
if (totaldef == 1) { // penultimate bonus
*temp += AString(" and ");
} else { // last bonus
*temp += AString(".");
}
} // end if
}
} // end for
// Capacity check prevents showing wrong details for ships
if (Globals->EXTENDED_FORT_DEFENCE && !o->capacity) {
*temp += AString(" This structure also protects in all adjacent regions.");
}
}
/*
* Handle all the specials
*/
for (int i = 0; i < NUMSPECIALS; i++) {
SpecialType *spd = &SpecialDefs[i];
AString effect = "are";
int match = 0;
if (!(spd->targflags & SpecialType::HIT_BUILDINGIF) &&
!(spd->targflags & SpecialType::HIT_BUILDINGEXCEPT)) {
continue;
}
for (int j = 0; j < SPECIAL_BUILDINGS; j++)
if (spd->buildings[j] == obj) match = 1;
if (!match) continue;
if (spd->targflags & SpecialType::HIT_BUILDINGEXCEPT) {
effect += " not";
}
*temp += " Units in this structure ";
*temp += effect + " 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 += " This structure will allow ";
if (o->maxMages > 1) {
*temp += "up to ";
*temp += o->maxMages;
*temp += " mages";
} else {
*temp += "one mage";
}
*temp += " to study above level 2.";
}
int buildable = 1;
SkillType *pS = NULL;
if (o->item == -1 || o->skill == NULL) buildable = 0;
if (o->skill != NULL) pS = FindSkill(o->skill);
if (!pS) buildable = 0;
if (pS && (pS->flags & SkillType::DISABLED)) buildable = 0;
if (o->item != I_WOOD_OR_STONE &&
(ItemDefs[o->item].flags & ItemType::DISABLED))
buildable = 0;
if (o->item == I_WOOD_OR_STONE &&
(ItemDefs[I_WOOD].flags & ItemType::DISABLED) &&
(ItemDefs[I_STONE].flags & ItemType::DISABLED))
buildable = 0;
if (!buildable && !(ObjectDefs[obj].flags & ObjectType::GROUP)) {
*temp += " This structure cannot be built by players.";
}
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;
}