forked from nedbrek/Atlantis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathobject.cpp
529 lines (450 loc) · 10.6 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
// 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 "items.h"
#include "skills.h"
#include "gamedata.h"
#include "faction.h"
#include "unit.h"
#include "gamedefs.h"
#include "fileio.h"
#include "astring.h"
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()
{
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.Num());
forlist ((&units))
((Unit*)elem)->Writeout(f);
}
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;
}
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);
}
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()
{
Unit *owner = (Unit*)units.First();
while (owner && !owner->GetMen())
{
owner = (Unit*)units.Next(owner);
}
return owner;
}
void Object::Report(Areport *f, Faction *fac, int obs, int truesight,
int detfac, int passobs, int passtrue, int passdetfac, int present)
{
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)
{
AString temp = AString("+ ") + *name + " : " + ob->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 (describe)
temp += AString("; ") + *describe;
if (!(ob->flags & ObjectType::CANENTER))
temp += ", closed to player units";
temp += ".";
f->PutStr(temp);
f->AddTab();
}
//foreach unit in this
forlist ((&units))
{
Unit *const u = (Unit*)elem;
if (u->faction == fac)
{
// self-report
u->WriteReport(f, -1, 1, 1, 1);
}
else
{
if (present)
{
// foreign unit present
u->WriteReport(f, obs, truesight, detfac, type != O_DUMMY);
}
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)))
{
u->WriteReport(f, passobs, passtrue, passdetfac, type != O_DUMMY);
}
}
}
}
f->EndLine();
if (type != O_DUMMY)
f->DropTab();
}
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()
{
return (O_ROADN <= type && type <= O_ROADS) ? 1 : 0;
}
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;
}