forked from steaphangreene/acidmud
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathobject_acid.cpp
More file actions
448 lines (382 loc) · 10.8 KB
/
object_acid.cpp
File metadata and controls
448 lines (382 loc) · 10.8 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
// *************************************************************************
// This file is part of AcidMUD by Steaphan Greene
//
// Copyright 1999-2022 Steaphan Greene <steaphan@gmail.com>
//
// AcidMUD 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 3 of the License, or
// (at your option) any later version.
//
// AcidMUD 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 AcidMUD (see the file named "COPYING");
// If not, see <http://www.gnu.org/licenses/>.
//
// *************************************************************************
#include <map>
#include "color.hpp"
#include "dice.hpp"
#include "enum_utils.hpp"
#include "infile.hpp"
#include "log.hpp"
#include "mind.hpp"
#include "object.hpp"
#include "outfile.hpp"
#include "properties.hpp"
#include "utils.hpp"
#include "version.hpp"
const struct {
char8_t c;
gender_t g;
} gender_save[4] = {
{'N', gender_t::NONE},
{'M', gender_t::MALE},
{'F', gender_t::FEMALE},
{'O', gender_t::NEITHER}};
static std::vector<Object*> todo;
static std::map<int, Object*> num2obj;
Object* getbynum(int num) {
if (num2obj.count(num) < 1)
num2obj[num] = new Object();
return num2obj[num];
}
static int last_object_number = 0;
static std::map<Object*, int> obj2num;
int getonum(Object* obj) {
if (!obj2num.count(obj))
obj2num[obj] = ++last_object_number;
return obj2num[obj];
}
int Object::Save(const std::u8string& fn) {
outfile fl(fn);
if (!fl)
return -1;
fl.append(u8"{:08X}\n", CurrentVersion.savefile_version_object);
// Save 'prop_names' hashes and strings.
save_prop_names_to(fl);
obj2num.clear();
obj2num[nullptr] = 0;
last_object_number = 0;
if (SaveTo(fl)) {
return -1;
}
return 0;
}
int Object::SaveTo(outfile& fl) {
// loge(u8"Saving {}", Name());
fl.append(u8"{}\n", getonum(this));
fl.append(u8"{}{:c}\n", ShortDesc(), 0);
fl.append(u8"{}{:c}\n", Name(), 0);
if (HasDesc()) {
fl.append(u8"{}{:c}\n", Desc(), 0);
} else {
fl.append(u8"{:c}\n", 0);
}
if (HasLongDesc()) {
fl.append(u8"{}{:c}\n", LongDesc(), 0);
} else {
fl.append(u8"{:c}\n", 0);
}
char8_t gen_char = 'N';
for (auto g : gender_save) {
if (gender == g.g) {
gen_char = g.c;
}
}
fl.append(u8"{} {} {} {} {:c}\n", weight, size, volume, value, gen_char);
for (auto ind : known) {
fl.append(u8";{}", ind);
}
fl.append(u8":\n");
for (auto ind : completed) {
fl.append(u8";{}", ind);
}
fl.append(u8":\n");
fl.append(u8" {}\n", sexp);
fl.append(
u8"{},{},{},{},{},{},{},{},{};{}",
attr[0],
attr[1],
attr[2],
attr[3],
attr[4],
attr[5],
phys,
stun,
stru,
IsActive());
for (const auto& sk : skills)
fl.append(u8"|{:08X}|{}", sk.first, sk.second);
if (cur_skill != prhash(u8"None")) { // Added current skill to end in v0x13
fl.append(u8"|{:08X}", cur_skill);
}
fl.append(u8";\n");
fl.append(u8"{}\n", quantity);
fl.append(u8"{}\n", (int)(contents.size()));
for (auto cind : contents) {
fl.append(u8"{}\n", getonum(cind));
}
fl.append(u8"{}\n", enum_save(position));
fl.append(u8"{}\n", (int)(actions.size()));
for (auto aind : actions) {
fl.append(u8"{};{}\n", enum_save(aind.act()), getonum(aind.obj()));
}
fl.append(u8"\n");
for (auto cind : contents) {
cind->SaveTo(fl);
}
// loge(u8"Saved {}", Name());
return 0;
}
static unsigned int ver;
int Object::Load(const std::u8string& fn) {
infile fl(fn);
if (!fl)
return -1;
ver = nexthex(fl);
skipspace(fl);
// Load 'prop_names' hashes and strings.
load_prop_names_from(fl);
todo.clear();
num2obj.clear();
num2obj[0] = nullptr;
num2obj[1] = this;
if (LoadFrom(fl)) {
return -1;
}
// Purge old skill name hashes
if (ver < 0x0019) {
purge_invalid_prop_names();
}
for (auto ind : todo) {
std::vector<act_t> killacts;
for (auto aind : ind->actions) {
if (aind.act() != act_t::SPECIAL_ACTEE) {
if (aind.obj()) {
aind.obj()->NowTouching(ind);
} else if (aind.act() <= act_t::REST) { // Targetless Actions
aind.set_obj(nullptr);
} else { // Act Targ No Longer Exists (u8"junkrestart", I hope)!
killacts.push_back(aind.act());
}
if (aind.act() == act_t::FIGHT) {
ind->BusyFor(500, ind->Tactics());
}
}
}
for (auto kill : killacts) { // Kill Actions on Non-Existent
StopAct(kill);
}
if (ind->IsUsing(prhash(u8"Lumberjack"))) { // FIXME: All long-term skills?
ind->BusyFor(500, u8"use Lumberjack");
}
}
todo.clear();
return 0;
}
int Object::LoadFrom(std::u8string_view& fl) {
// static std::u8string debug_indent = u8"";
int num = nextnum(fl);
skipspace(fl);
if (num2obj[num] != this) {
loger(u8"Error: Acid number mismatch ({})!", num);
}
todo.push_back(this);
std::u8string_view sd = getuntil(fl, 0);
skipspace(fl);
std::u8string_view n = u8"";
if (ver >= 0x001B) {
n = getuntil(fl, 0);
skipspace(fl);
}
std::u8string_view d = getuntil(fl, 0);
skipspace(fl);
std::u8string_view ld = getuntil(fl, 0);
skipspace(fl);
SetDescs(sd, n, d, ld);
// loge(u8"{}Loading {}:{}", debug_indent, num, buf);
weight = nextnum(fl);
skipspace(fl);
size = nextnum(fl);
skipspace(fl);
volume = nextnum(fl);
skipspace(fl);
value = nextnum(fl);
skipspace(fl);
gender = gender_t::NONE;
char8_t gen_char = nextchar(fl);
skipspace(fl);
for (auto g : gender_save) {
if (gen_char == g.c) {
gender = g.g;
}
}
while (nextchar(fl) == ';') {
known.push_back(nextnum(fl));
}
skipspace(fl);
while (nextchar(fl) == ';') {
completed.push_back(nextnum(fl));
}
skipspace(fl);
sexp = nextnum(fl);
skipspace(fl);
attr[0] = nextnum(fl);
nextchar(fl); // Comma
attr[1] = nextnum(fl);
nextchar(fl); // Comma
attr[2] = nextnum(fl);
nextchar(fl); // Comma
attr[3] = nextnum(fl);
nextchar(fl); // Comma
attr[4] = nextnum(fl);
nextchar(fl); // Comma
attr[5] = nextnum(fl);
nextchar(fl); // Comma
phys = nextnum(fl);
nextchar(fl); // Comma
stun = nextnum(fl);
nextchar(fl); // Comma
stru = nextnum(fl);
char8_t delim = nextchar(fl);
if (delim == ';') {
int do_tick = nextnum(fl);
if (do_tick)
Activate();
delim = nextchar(fl);
}
while (delim == '|') {
uint32_t stok = nexthex(fl);
delim = nextchar(fl); // The '|' Separator
if (ver < 0x0019) { // Update to new hash algo
auto name = SkillName(stok);
stok = crc32c(name);
insert_skill_hash(stok, name);
}
if (delim == '|') {
int val = nextnum(fl);
delim = nextchar(fl);
if (stok == prhash(u8"Quantity")) { // Before v0x001C Quantity was saved as a Skill
quantity = val;
} else {
SetSkill(stok, val);
}
} else { // Only the skill name, so "currently used skill"
StartUsing(stok);
}
}
if (delim != ';') {
logerr(u8"ERROR[{}]: Object skill list parse error (delim='{:c}').", ShortDesc(), delim);
}
skipspace(fl);
if (ver >= 0x001C) {
quantity = nextnum(fl);
skipspace(fl);
}
num = nextnum(fl);
skipspace(fl);
std::vector<Object*> toload;
contents.reserve(num);
for (int ctr = 0; ctr < num; ++ctr) {
Object* obj = getbynum(nextnum(fl));
obj->parent = this;
contents.push_back(obj);
toload.push_back(obj);
skipspace(fl);
}
if (ver >= 0x001D) {
position = enum_load<pos_t>(getgraph(fl));
} else { // Prior to v0x001D, position was saved as a raw int
position = static_cast<pos_t>(nextnum(fl));
}
skipspace(fl);
num = nextnum(fl);
skipspace(fl);
for (int ctr = 0; ctr < num; ++ctr) {
auto acts = getuntil(fl, ';');
int onum = nextnum(fl);
skipspace(fl);
act_t a = enum_load<act_t>(acts);
if (a != act_t::SPECIAL_ACTEE && a != act_t::NONE) {
AddAct(a, getbynum(onum));
}
}
if (IsAct(act_t::SPECIAL_WORK)) {
Attach(std::make_shared<Mind>(mind_t::NPC));
} else if (Skill(prhash(u8"Personality"))) {
Attach(get_mob_mind());
} else if (Skill(prhash(u8"TBAAction"))) {
Attach(get_tbamob_mind());
}
if (Skill(prhash(u8"TBAScriptType")) & 2) { // Random/Permanent Triggers
std::shared_ptr<Mind> trig = new_mind(mind_t::TBATRIG, this);
trig->Suspend(Dice::Rand(3000, 15999)); // 3-16 Seconds
}
// int num_loaded = 0;
// if(parent && (!(parent->parent))) {
// loge(u8"Loading: {}", ShortDesc());
// }
for (auto cind : toload) {
// loge(u8"{}Calling loader from {}", debug_indent,
// ShortDesc());
// std::u8string tmp = debug_indent;
// debug_indent += u8" ";
cind->LoadFrom(fl);
// debug_indent = tmp;
// loge(u8"{}Called loader from {}", debug_indent,
// ShortDesc());
// if(parent && (!(parent->parent))) {
// loge(u8"\rLoaded: {}/{} ({}) ",
// ++num_loaded, int(toload.size()), cind->ShortDesc()
// );
// }
}
// if(parent && (!(parent->parent))) {
// loge(u8"\nLoaded.");
// }
// loge(u8"{}Loaded {}", debug_indent, ShortDesc());
// if(HasSkill(prhash(u8"Drink"))) {
// SetSkill(prhash(u8"Drink"), Skill(prhash(u8"Drink")) * 15);
// SetSkill(prhash(u8"Food"), Skill(prhash(u8"Food")) * 15);
// }
// else if(HasSkill(prhash(u8"Food"))) {
// SetSkill(prhash(u8"Food"), Skill(prhash(u8"Food")) * 60);
// }
// if(HasSkill(prhash(u8"Heal Effect"))) {
// SetSkill(prhash(u8"Heal Spell"), Skill(prhash(u8"Heal Effect")));
// ClearSkill(prhash(u8"Heal Effect"));
// }
// if(HasSkill(prhash(u8"Open"))) {
// SetSkill(prhash(u8"Open"), 1000);
// }
// if(HasSkill(prhash(u8"Day Time"))) {
// Activate();
// }
// if(IsAct(act_t::SPECIAL_NOTSHOWN)) {
// SetSkill(prhash(u8"Invisible"), 1000);
// }
// if(short_desc == u8"A passage exit.") {
// short_desc = u8"a passage exit";
// }
// if(HasSkill(prhash(u8"Evasion Penalty"))) {
// SetSkill(prhash(u8"Evasion Bonus"), Skill(prhash(u8"Evasion Penalty")));
// ClearSkill(prhash(u8"Evasion Penalty"));
// }
// else if(HasSkill(prhash(u8"Evasion Bonus"))) {
// SetSkill(prhash(u8"Evasion Penalty"), Skill(prhash(u8"Evasion Bonus")));
// ClearSkill(prhash(u8"Evasion Bonus"));
// }
// if(IsAct(act_t::SPECIAL_PREPARE)) {
// loge(u8"Found one!");
// StopAct(act_t::SPECIAL_PREPARE);
// }
// if(short_desc == u8"a gold piece") {
// SetSkill(prhash(u8"Money"), 1);
// }
return 0;
}