/
thinkers.cpp
533 lines (444 loc) · 12.5 KB
/
thinkers.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
/** @file thinkers.cpp World map thinker management.
*
* @authors Copyright © 2003-2013 Jaakko Keränen <jaakko.keranen@iki.fi>
* @authors Copyright © 2006-2015 Daniel Swanson <danij@dengine.net>
*
* @par License
* GPL: http://www.gnu.org/licenses/gpl.html
*
* <small>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; if not, see:
* http://www.gnu.org/licenses</small>
*/
#define DENG_NO_API_MACROS_THINKER
#include "de_base.h"
#include "world/thinkers.h"
#ifdef __CLIENT__
# include "client/cl_mobj.h"
# include "world/clientmobjthinkerdata.h"
#endif
#ifdef __SERVER__
# include "def_main.h"
# include "server/sv_pool.h"
# include <doomsday/world/mobjthinkerdata.h>
#endif
#include "world/map.h"
#include "world/p_object.h"
#include <de/memoryzone.h>
#include <QList>
#include <QtAlgorithms>
dd_bool Thinker_IsMobjFunc(thinkfunc_t func)
{
return (func && func == reinterpret_cast<thinkfunc_t>(gx.MobjThinker));
}
de::Map &Thinker_Map(thinker_t const & /*th*/)
{
/// @todo Do not assume the current map.
return App_WorldSystem().map();
}
namespace de {
struct ThinkerList
{
bool isPublic; ///< All thinkers in this list are visible publically.
Thinker sentinel;
ThinkerList(thinkfunc_t func, bool isPublic) : isPublic(isPublic)
{
sentinel.function = func;
sentinel.disable(); // Safety measure.
sentinel.prev = sentinel.next = sentinel;
}
void reinit()
{
sentinel.prev = sentinel.next = sentinel;
}
thinkfunc_t function() const
{
return sentinel.function;
}
// Link the thinker to the list.
void link(thinker_t &th)
{
sentinel.prev->next = &th;
th.next = sentinel;
th.prev = sentinel.prev;
sentinel.prev = &th;
}
dint count(dint *numInStasis) const
{
dint num = 0;
thinker_t *th = sentinel.next;
while(th != &sentinel.base() && th)
{
#ifdef LIBDENG_FAKE_MEMORY_ZONE
DENG2_ASSERT(th->next);
DENG2_ASSERT(th->prev);
#endif
num += 1;
if(numInStasis && Thinker_InStasis(th))
{
(*numInStasis) += 1;
}
th = th->next;
}
return num;
}
void releaseAll()
{
for(thinker_t *th = sentinel.next; th != &sentinel.base() && th; th = th->next)
{
Thinker::release(*th);
}
}
};
DENG2_PIMPL(Thinkers)
{
dint idtable[2048]; ///< 65536 bits telling which IDs are in use.
dushort iddealer = 0;
QList<ThinkerList *> lists;
QHash<thid_t, mobj_t *> mobjIdLookup; ///< public only
bool inited = false;
Instance(Public *i) : Base(i)
{
clearMobjIds();
}
~Instance()
{
// Make sure the private instances of thinkers are released.
releaseAllThinkers();
// Note that most thinkers are allocated from the memory zone
// so there is no memory leak here as this memory will be purged
// automatically when the map is "unloaded".
qDeleteAll(lists);
}
void releaseAllThinkers()
{
for(ThinkerList *list : lists)
{
list->releaseAll();
}
}
void clearMobjIds()
{
de::zap(idtable);
idtable[0] |= 1; // ID zero is always "used" (it's not a valid ID).
mobjIdLookup.clear();
}
thid_t newMobjId()
{
// Increment the ID dealer until a free ID is found.
/// @todo fixme: What if all IDs are in use? 65535 thinkers!?
while(self.isUsedMobjId(++iddealer)) {}
// Mark this ID as used.
self.setMobjId(iddealer);
return iddealer;
}
ThinkerList *listForThinkFunc(thinkfunc_t func, bool makePublic = true,
bool canCreate = false)
{
for(dint i = 0; i < lists.count(); ++i)
{
ThinkerList *list = lists[i];
if(list->function() == func && list->isPublic == makePublic)
return list;
}
if(!canCreate) return nullptr;
// A new thinker type.
lists.append(new ThinkerList(func, makePublic));
return lists.last();
}
};
Thinkers::Thinkers() : d(new Instance(this))
{}
bool Thinkers::isUsedMobjId(thid_t id)
{
return d->idtable[id >> 5] & (1 << (id & 31) /*(id % 32) */ );
}
void Thinkers::setMobjId(thid_t id, bool inUse)
{
dint c = id >> 5, bit = 1 << (id & 31); //(id % 32);
if(inUse) d->idtable[c] |= bit;
else d->idtable[c] &= ~bit;
}
struct mobj_s *Thinkers::mobjById(dint id)
{
auto found = d->mobjIdLookup.constFind(id);
if(found != d->mobjIdLookup.constEnd())
{
return found.value();
}
return nullptr;
}
void Thinkers::add(thinker_t &th, bool makePublic)
{
if(!th.function)
throw Error("Thinkers::add", "Invalid thinker function");
// Will it need an ID?
if(Thinker_IsMobjFunc(th.function))
{
// It is a mobj, give it an ID (not for client mobjs, though, they
// already have an id).
#ifdef __CLIENT__
if(!Cl_IsClientMobj(reinterpret_cast<mobj_t *>(&th)))
#endif
{
th.id = d->newMobjId();
}
if(makePublic && th.id)
{
d->mobjIdLookup.insert(th.id, reinterpret_cast<mobj_t *>(&th));
}
}
else
{
th.id = 0; // Zero is not a valid ID.
}
// Link the thinker to the thinker list.
ThinkerList *list = d->listForThinkFunc(th.function, makePublic, true /*can create*/);
list->link(th);
}
void Thinkers::remove(thinker_t &th)
{
// Has got an ID?
if(th.id)
{
// Flag the identifier as free.
setMobjId(th.id, false);
d->mobjIdLookup.remove(th.id);
#ifdef __SERVER__
// Then it must be a mobj.
auto *mob = reinterpret_cast<mobj_t *>(&th);
// If the state of the mobj is the NULL state, this is a
// predictable mobj removal (result of animation reaching its
// end) and shouldn't be included in netGame deltas.
if(!mob->state || !runtimeDefs.states.indexOf(mob->state))
{
Sv_MobjRemoved(th.id);
}
#endif
}
th.function = (thinkfunc_t) -1;
Thinker::release(th);
}
void Thinkers::initLists(dbyte flags)
{
if(!d->inited)
{
d->lists.clear();
}
else
{
for(dint i = 0; i < d->lists.count(); ++i)
{
ThinkerList *list = d->lists[i];
if(list->isPublic && !(flags & 0x1)) continue;
if(!list->isPublic && !(flags & 0x2)) continue;
list->reinit();
}
}
d->clearMobjIds();
d->inited = true;
}
bool Thinkers::isInited() const
{
return d->inited;
}
LoopResult Thinkers::forAll(dbyte flags, std::function<LoopResult (thinker_t *)> func) const
{
if(!d->inited) return LoopContinue;
for(dint i = 0; i < d->lists.count(); ++i)
{
ThinkerList *list = d->lists[i];
if( list->isPublic && !(flags & 0x1)) continue;
if(!list->isPublic && !(flags & 0x2)) continue;
thinker_t *th = list->sentinel.next;
while(th != &list->sentinel.base() && th)
{
#ifdef LIBDENG_FAKE_MEMORY_ZONE
DENG2_ASSERT(th->next);
DENG2_ASSERT(th->prev);
#endif
thinker_t *next = th->next;
if(auto result = func(th))
return result;
th = next;
}
}
return LoopContinue;
}
LoopResult Thinkers::forAll(thinkfunc_t thinkFunc, dbyte flags, std::function<LoopResult (thinker_t *)> func) const
{
if(!d->inited) return LoopContinue;
if(!thinkFunc)
{
return forAll(flags, func);
}
if(flags & 0x1 /*public*/)
{
if(ThinkerList *list = d->listForThinkFunc(thinkFunc))
{
thinker_t *th = list->sentinel.next;
while(th != &list->sentinel.base() && th)
{
#ifdef LIBDENG_FAKE_MEMORY_ZONE
DENG2_ASSERT(th->next);
DENG2_ASSERT(th->prev);
#endif
thinker_t *next = th->next;
if(auto result = func(th))
return result;
th = next;
}
}
}
if(flags & 0x2 /*private*/)
{
if(ThinkerList *list = d->listForThinkFunc(thinkFunc, false /*private*/))
{
thinker_t *th = list->sentinel.next;
while(th != &list->sentinel.base() && th)
{
#ifdef LIBDENG_FAKE_MEMORY_ZONE
DENG2_ASSERT(th->next);
DENG2_ASSERT(th->prev);
#endif
thinker_t *next = th->next;
if(auto result = func(th))
return result;
th = next;
}
}
}
return LoopContinue;
}
dint Thinkers::count(dint *numInStasis) const
{
dint total = 0;
if(isInited())
{
for(dint i = 0; i < d->lists.count(); ++i)
{
ThinkerList *list = d->lists[i];
total += list->count(numInStasis);
}
}
return total;
}
static void unlinkThinkerFromList(thinker_t *th)
{
th->next->prev = th->prev;
th->prev->next = th->next;
}
} // namespace de
using namespace de;
void Thinker_InitPrivateData(thinker_t *th)
{
DENG2_ASSERT(th->d == nullptr);
/// @todo The game should be asked to create its own private data. -jk
if(Thinker_IsMobjFunc(th->function))
{
#ifdef __CLIENT__
th->d = new ClientMobjThinkerData;
#else
th->d = new MobjThinkerData;
#endif
}
else
{
// Generic thinker data (Doomsday Script namespace, etc.).
th->d = new ThinkerData;
}
if(th->d)
{
auto &thinkerData = THINKER_DATA(*th, ThinkerData);
thinkerData.setThinker(th);
thinkerData.initBindings();
}
}
/**
* Locates a mobj by it's unique identifier in the CURRENT map.
*/
#undef Mobj_ById
DENG_EXTERN_C struct mobj_s *Mobj_ById(dint id)
{
/// @todo fixme: Do not assume the current map.
if(!App_WorldSystem().hasMap()) return nullptr;
return App_WorldSystem().map().thinkers().mobjById(id);
}
#undef Thinker_Init
void Thinker_Init()
{
/// @todo fixme: Do not assume the current map.
if(!App_WorldSystem().hasMap()) return;
App_WorldSystem().map().thinkers().initLists(0x1); // Init the public thinker lists.
}
#undef Thinker_Run
void Thinker_Run()
{
/// @todo fixme: Do not assume the current map.
if(!App_WorldSystem().hasMap()) return;
App_WorldSystem().map().thinkers().forAll(0x1 | 0x2, [] (thinker_t *th)
{
if(Thinker_InStasis(th)) return LoopContinue; // Skip.
// Time to remove it?
if(th->function == (thinkfunc_t) -1)
{
unlinkThinkerFromList(th);
if(th->id)
{
// Recycle for reduced allocation overhead.
P_MobjRecycle((mobj_t *) th);
}
else
{
// Non-mobjs are just deleted right away.
Thinker::destroy(th);
}
}
else if(th->function)
{
// Create a private data instance of appropriate type.
if(th->d == nullptr) Thinker_InitPrivateData(th);
// Public thinker callback.
th->function(th);
// Private thinking.
if(th->d) THINKER_DATA(*th, Thinker::IData).think();
}
return LoopContinue;
});
}
#undef Thinker_Add
void Thinker_Add(thinker_t *th)
{
if(!th) return;
Thinker_Map(*th).thinkers().add(*th);
}
#undef Thinker_Remove
void Thinker_Remove(thinker_t *th)
{
if(!th) return;
Thinker_Map(*th).thinkers().remove(*th);
}
#undef Thinker_Iterate
dint Thinker_Iterate(thinkfunc_t func, dint (*callback) (thinker_t *, void *), void *context)
{
if(!App_WorldSystem().hasMap()) return false; // Continue iteration.
return App_WorldSystem().map().thinkers().forAll(func, 0x1, [&callback, &context] (thinker_t *th)
{
return callback(th, context);
});
}
DENG_DECLARE_API(Thinker) =
{
{ DE_API_THINKER },
Thinker_Init,
Thinker_Run,
Thinker_Add,
Thinker_Remove,
Thinker_Iterate
};