-
Notifications
You must be signed in to change notification settings - Fork 11
/
util.cpp
250 lines (228 loc) · 5.53 KB
/
util.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
#include "stdafx.h"
#include "util.h"
#include "adv.h"
#include "funcs.h"
#include "rooms.h"
#include "parser.h"
#include "dung.h"
#include "memq.h"
bool always_lit = false;
const HackP &get_demon(const char *id)
{
const ObjectP &obj = find_obj(id);
return *std::find_if(demons.cbegin(), demons.cend(), [&obj](const HackP &h)
{
return h->hobj() == obj;
});
}
ObjList splice_out(const ObjectP &op, const ObjList &al)
{
ObjList new_list;
std::copy_if(al.begin(), al.end(), std::back_inserter(new_list), [&op](const ObjectP& o)
{
return o != op;
});
return new_list;
}
bool remove_object(const ObjectP &obj, const AdvP &winner)
{
// Remove it from the object that it's contained in.
if (auto &ocan = obj->ocan())
{
ocan->ocontents() = splice_out(obj, ocan->ocontents());
}
else if (auto &oroom = obj->oroom())
{
oroom->robjs() = splice_out(obj, oroom->robjs());
}
else if (memq(obj, winner->aobjs()))
{
winner->aobjs() = splice_out(obj, winner->aobjs());
}
obj->oroom(RoomP());
obj->ocan(ObjectP());
// Return value is never used, except to make a conditional statement continue.
return true;
}
bool insert_object(const ObjectP &obj, const RoomP &room)
{
obj->oroom(room);
room->robjs().push_front(obj);
return true;
}
void insert_into(const ObjectP &cnt, const ObjectP &obj)
{
cnt->ocontents().push_front(obj);
obj->ocan(cnt);
obj->oroom(RoomP());
}
void remove_from(const ObjectP &cnt, const ObjectP &obj)
{
cnt->ocontents() = splice_out(obj, cnt->ocontents());
obj->ocan(ObjectP());
}
void take_object(const ObjectP &obj, const AdvP &winner)
{
tro(obj, touchbit);
obj->oroom(RoomP());
winner->aobjs().push_front(obj);
}
void drop_object(const ObjectP &obj, const AdvP &winner)
{
winner->aobjs() = splice_out(obj, winner->aobjs());
}
bool drop_if(const ObjectP &obj, const AdvP &winner)
{
bool rv = false;
if (memq(obj, winner->aobjs()))
{
drop_object(obj, winner);
rv = true;
}
return rv;
}
const ObjectP &snarf_object(const ObjectP &who, const ObjectP &what)
{
if (what->ocan() != who &&
(what->oroom() || what->ocan()))
{
remove_object(what);
insert_into(who, what);
}
return who;
}
bool in_room(const ObjectP &obj, const RoomP &here)
{
bool found = false;
const ObjectP &tobj = obj->ocan();
if (tobj)
{
if (tobj->oroom() == here)
{
found = true;
}
else if (trnn(tobj, searchbit))
{
found = in_room(tobj, here);
}
}
else
{
found = obj->oroom() == here;
}
return found;
}
bool hackable(const ObjectP &obj, const RoomP &rm)
{
bool h = false;
const AdvP &winner = *::winner;
const ObjectP &av = winner->avehicle();
if (av)
{
h = search_list(obj->oid(), av->ocontents(), AdjectiveP()).first != ObjectP();
}
else
{
h = search_list(obj->oid(), rm->robjs(), AdjectiveP()).first != ObjectP();
}
return h;
}
bool lfcn(const ObjList &l)
{
for (auto &x : l)
{
if (trnn(x, onbit))
return true;
if (trnn(x, ovison) && (trnn(x, openbit) || trnn(x, transbit)))
{
for (auto &x2 : x->ocontents())
{
if (trnn(x2, onbit))
{
return true;
}
}
}
if (trnn(x, actorbit) && lfcn((*x->oactor())->aobjs()))
{
return true;
}
}
return false;
}
bool lit(const RoomP &rm)
{
const AdvP &win = *winner;
bool is_lit = false;
if (rtrnn(rm, rlightbit) ||
lfcn(rm->robjs()) ||
rm == here && lfcn(win->aobjs()) ||
win != player() && here == player()->aroom() || lfcn(player()->aobjs()) ||
always_lit)
{
is_lit = true;
}
return is_lit;
}
bool prob(int goodluck, std::optional<int> badluck)
{
if (!badluck.has_value())
badluck = goodluck;
int val = rand() % 100;
return val < (flags[lucky] ? goodluck : badluck);
}
bool perform(rapplic fcn, VerbP vb, ObjectP obj1, ObjectP obj2)
{
bool rv = false;
ParseVec &pv = prsvec;
pv[0] = vb;
pv[1] = obj1 ? obj1 : ParseVecVal();
pv[2] = obj2 ? obj2 : ParseVecVal();
rv = (*fcn)();
return rv;
}
bool yes_no(bool no_is_bad)
{
std::string inbuf;
readst(inbuf, "");
bool rv;
if (no_is_bad)
{
rv = inbuf.find_first_of("NnfF") == std::string::npos;
}
else
{
rv = inbuf.find_first_of("YyTt") != std::string::npos;
}
return rv;
}
ObjList rob_adv(const AdvP &win, ObjList newlist)
{
// First move all non-sacred valuables to the front of
// the list, then splice them into newlist.
ObjList &aobjs = win->aobjs();
auto end_val = std::partition(aobjs.begin(), aobjs.end(), [](const ObjectP &o)
{
return o->otval() > 0 && !trnn(o, sacredbit);
});
newlist.splice(newlist.begin(), aobjs, aobjs.begin(), end_val);
return newlist;
}
ObjList rob_room(const RoomP &rm, ObjList newlist, int prob)
{
ObjList robjs = rm->robjs();
for (const ObjectP &x : robjs)
{
if (x->otval() > 0 && !trnn(x, sacredbit) && trnn(x, ovison) && ::prob(prob))
{
remove_object(x);
tro(x, touchbit);
newlist.push_front(x);
}
else if (x->oactor())
{
newlist = rob_adv((*x->oactor()), newlist);
}
}
return newlist;
}