updated with new script loading scheme
[dd2d.git] / dengapi.d
blob61e9652e3055057c22f8a333bc24ac0923335a65
1 /* DooM2D: Midnight on the Firing Line
2 * coded by Ketmar // Invisible Vector <ketmar@ketmar.no-ip.org>
3 * Understanding is not required. Only obedience.
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 module dengapi is aliced;
20 private:
21 import core.atomic;
22 import core.thread;
23 import core.time;
25 import iv.glbinds;
26 import glutils;
27 import console;
28 import wadarc;
30 import d2dmap;
31 import d2dadefs;
32 import d2dgfx;
33 import dacs;
35 import d2dunigrid;
36 import render;
39 // ////////////////////////////////////////////////////////////////////////// //
40 private extern (C) void _d_print_throwable (Throwable t);
43 // ////////////////////////////////////////////////////////////////////////// //
44 import arsd.color;
45 import arsd.png;
48 // ////////////////////////////////////////////////////////////////////////// //
49 public void addInternalActorFields () {
50 // &0xffff: position in drawlist
51 // (>>16)&0xff: drawlist index
52 //Actor.addField("0drawlistpos", Actor.Field.Type.Uint);
56 // ////////////////////////////////////////////////////////////////////////// //
57 public {
58 enum PLK_UP = (1<<0);
59 enum PLK_DOWN = (1<<1);
60 enum PLK_LEFT = (1<<2);
61 enum PLK_RIGHT = (1<<3);
62 enum PLK_FIRE = (1<<4);
63 enum PLK_JUMP = (1<<5);
64 enum PLK_USE = (1<<6);
67 __gshared uint plrKeysLast;
68 __gshared uint plrKeyState;
71 public void plrKeyDown (uint plidx, uint mask) {
72 if (mask == 0 || plidx > 0) return;
73 plrKeysLast |= mask;
74 plrKeyState |= mask;
78 public void plrKeyUp (uint plidx, uint mask) {
79 if (mask == 0 || plidx > 0) return;
80 plrKeyState &= ~mask;
84 public void plrKeyUpDown (uint plidx, uint mask, bool down) {
85 if (down) plrKeyDown(plidx, mask); else plrKeyUp(plidx, mask);
89 void plrKeysFix (uint plidx) {
90 if (plidx > 0) return;
91 //conwritefln!"plrKeyState=0x%02x; plrKeysLast=0x%02x"(plrKeyState, plrKeysLast);
92 foreach (uint n; 0..12) {
93 if ((plrKeyState&(1<<n)) == 0) plrKeysLast &= ~(1<<n);
98 // ////////////////////////////////////////////////////////////////////////// //
99 void updateActorClasses () {
100 foreach (string ctype; dacsClassTypes) {
101 foreach (auto mod; dacsClassModules(ctype)) {
102 auto adef = registerActorDef(mod.rmoduletype.classtype, mod.rmoduletype.classname);
103 conwriteln("found info for actor '", mod.rmoduletype.classtype, ":", mod.rmoduletype.classname, "'");
105 auto animInitFn = FuncPool.findByFQMG(mod.name~".initializeAnim", ":void");
106 if (animInitFn !is null) adef.setAnimInitFunc(animInitFn);
109 auto initFn = FuncPool.findByFQMG(mod.name~".initialize", ":void:Actor");
110 if (initFn !is null) adef.setInitFunc(initFn);
113 auto thinkFn = FuncPool.findByFQMG(mod.name~".think", ":void:Actor");
114 if (thinkFn !is null) adef.setThinkFunc(thinkFn);
121 // ////////////////////////////////////////////////////////////////////////// //
122 private string modLoader (string modname) {
123 static string getTextFile (string fname) {
124 try {
125 auto res = loadTextFile(fname);
126 conwriteln("loading DACS module file '", fname, "' (", res.length, " bytes)");
127 if (res is null) res = "";
128 return res;
129 } catch (Exception) {}
130 return null;
133 string res;
135 //res = getTextFile(modname~".dacs");
136 //if (res !is null) return res;
138 res = getTextFile("scripts/"~modname~".dacs");
139 if (res !is null) return res;
140 assert(res is null);
142 string[] parts = ["scripts"];
143 string mn = modname;
144 while (mn.length > 0) {
145 int pos = 0;
146 if (mn[0] >= 'A' && mn[0] <= 'Z') ++pos;
147 while (pos < mn.length && (mn[pos] < 'A' || mn[pos] > 'Z')) ++pos;
148 if (mn[0] >= 'A' && mn[0] <= 'Z') {
149 parts ~= cast(char)(mn[0]+32)~mn[1..pos];
150 } else {
151 parts ~= mn[0..pos];
153 mn = mn[pos..$];
156 import std.array : join;
157 string path = parts.join("/")~".dacs";
158 res = getTextFile(path);
159 if (res !is null) return res;
160 assert(res is null);
162 assert(res is null);
163 throw new Exception("module '"~modname~"' not found");
167 __gshared string[] dacsModules;
169 public void registerWadScripts () {
170 try {
171 import std.array : split;
172 auto t = loadTextFile("scripts/dacsmain.txt");
173 foreach (string line; t.split('\n')) {
174 while (line.length && line[0] <= ' ') line = line[1..$];
175 if (line.length == 0 || line[0] == ';') continue;
176 while (line.length && line[$-1] <= ' ') line = line[0..$-1];
177 import std.algorithm : canFind;
178 if (!dacsModules.canFind(line)) dacsModules ~= line;
180 } catch (Exception e) { return; }
184 public void loadWadScripts () {
185 if (moduleLoader is null) moduleLoader = (string modname) => modLoader(modname);
186 try {
187 //conwriteln("loading main DACS module '", mainmod, "'");
188 foreach (string mod; dacsModules) parseModule(mod);
189 parseComplete();
190 updateActorClasses();
191 setupDAPI();
192 dacsFinalizeCompiler();
193 } catch (CompilerException e) {
194 _d_print_throwable(e);
195 conwriteln("PARSE ERROR: ", e.file, " at ", e.line);
196 conwriteln(e.toString);
197 assert(0);
202 // ////////////////////////////////////////////////////////////////////////// //
203 public __gshared LevelMap map;
204 //public __gshared DACSVM dvm;
205 public __gshared uint prngSyncSeed = 0x29a;
206 public __gshared uint prngSeed = 0x29a; // unsynced
207 public __gshared string curmapname;
208 public __gshared string nextmapname; // nonempty: go to next level
211 // generate next map name for exit
212 public string genNextMapName (int lnum) {
213 import std.algorithm : endsWith;
214 import std.path;
215 string ext;
216 string nn = curmapname;
217 if (nn.endsWith(".d2m")) {
218 ext = ".d2m";
219 nn = nn[0..$-4];
221 if (nn[$-1] < '0' || nn[$-1] > '9') return null;
222 uint mapnum = 0, mmul = 1;
223 while (nn.length > 0 && nn[$-1] >= '0' && nn[$-1] <= '9') {
224 mapnum += (nn[$-1]-'0')*mmul;
225 mmul *= 10;
226 nn = nn[0..$-1];
228 ++mapnum;
229 if (lnum > 0 && lnum < 100) mapnum = lnum;
230 if (mapnum > 99) return null; // alas
231 import std.string : format;
232 return "%s%02u%s".format(nn, mapnum, ext);
236 // ////////////////////////////////////////////////////////////////////////// //
237 // Park-Miller-Carta Pseudo-Random Number Generator, based on David G. Carta paper
238 // 31 bit of randomness
239 // seed is previous result, as usual
240 public uint prngR31next (uint seed) {
241 if (seed == 0) seed = 0x29a;
242 uint lo = 16807*(seed&0xffff);
243 uint hi = 16807*(seed>>16);
244 lo += (hi&0x7fff)<<16;
245 lo += hi>>15;
246 //if (lo > 0x7fffffff) lo -= 0x7fffffff; // should be >=, actually
247 lo = (lo&0x7FFFFFFF)+(lo>>31); // same as previous code, but branch-less
248 return lo;
252 // "synced" random, using common seed for all players
253 public uint syncrandu31 () {
254 pragma(inline, true);
255 return (prngSyncSeed = prngR31next(prngSyncSeed));
259 // "unsynced" random, seed isn't saved
260 public uint unsyncrandu31 () {
261 pragma(inline, true);
262 return (prngSeed = prngR31next(prngSeed));
266 // ////////////////////////////////////////////////////////////////////////// //
267 version(dacs_use_vm) {
268 // this is VAT function, argument order is reversed
269 void doWrite(bool donl) (FuncPool.FuncInfo fi, DACSVM vm) {
270 import std.stdio;
271 auto count = vm.popInt();
272 while (count-- > 0) {
273 auto type = vm.popInt();
274 switch (cast(VATArgType)type) {
275 case VATArgType.Int: write(vm.popInt()); break;
276 case VATArgType.Uint: write(vm.popUint()); break;
277 case VATArgType.Float: write(vm.popFloat()); break;
278 case VATArgType.StrId: write(StrId(vm.popUint()).get); break;
279 case VATArgType.ActorId: write("<actor>"); vm.popUint(); break; //TODO
280 default: write("<invalid-type>"); vm.popUint(); break; //TODO
283 static if (donl) writeln();
284 // push dummy return value
285 vm.pushInt(0);
287 } else {
288 extern(C) void doWrite(bool donl) (uint argc, ...) {
289 import core.vararg;
290 import std.stdio;
291 mainloop: while (argc >= 2) {
292 argc -= 2;
293 int tp = va_arg!int(_argptr);
294 switch (tp) {
295 case VATArgType.Int:
296 auto v = va_arg!int(_argptr);
297 write(v);
298 break;
299 case VATArgType.Uint:
300 auto v = va_arg!uint(_argptr);
301 write(v);
302 break;
303 case VATArgType.Float:
304 auto v = va_arg!float(_argptr);
305 write(v);
306 break;
307 case VATArgType.StrId:
308 auto v = StrId(va_arg!uint(_argptr)).get;
309 write(v);
310 break;
311 case VATArgType.ActorId:
312 auto v = ActorId(va_arg!uint(_argptr));
313 write("<actor:", v.valid, ":", v.id, ">");
314 //write("<actor>");
315 break;
316 default: write("<invalid-type>"); break mainloop;
319 static if (donl) writeln();
324 // ////////////////////////////////////////////////////////////////////////// //
325 void animClearFrames (StrId classtype, StrId classname, StrId state) {
326 auto adef = findActorDef(classtype.get, classname.get);
327 if (adef is null) throw new Exception("animClearFrames: actor '"~classtype.get~":"~classname.get~"' not found!");
328 adef.clearFrames(state);
332 void animAddFrame (StrId classtype, StrId classname, StrId state, uint dir, StrId sprname) {
333 auto adef = findActorDef(classtype.get, classname.get);
334 if (adef is null) throw new Exception("animAddFrame: actor '"~classtype.get~":"~classname.get~"' not found!");
335 if (dir != 0) dir = 1; //TODO: process mirror flag here
336 adef.addFrame(state, dir, sprname);
340 // ////////////////////////////////////////////////////////////////////////// //
341 void setupDAPI () {
342 conwriteln("setting up D API");
344 FuncPool["write"] = &doWrite!false;
345 FuncPool["writeln"] = &doWrite!true;
347 FuncPool["syncrandu31"] = &syncrandu31;
349 FuncPool["animClearFrames"] = &animClearFrames;
350 FuncPool["animAddFrame"] = &animAddFrame;
352 FuncPool["actorSetAnimation"] = function void (ActorId me, StrId state) {
353 if (!me.valid) return;
354 if (auto adef = findActorDef(me.classtype!string, me.classname!string)) {
355 me.zAnimstate = state;
356 me.zAnimidx = 0;
360 //FuncPool["getPlayer"] = function ActorId () { assert(0); };
361 //FuncPool["isPlayer"] = function int (ActorId me) { return (me == players.ptr[0] ? 1 : 0); };
363 FuncPool["getPlayerCount"] = function int () => 1;
365 FuncPool["getPlayerActor"] = function ActorId (uint pnum) {
366 if (pnum == 1) return players.ptr[0];
367 return ActorId(0);
370 FuncPool["getPlayerButtons"] = function uint (uint pidx) { return (pidx == 1 ? plrKeysLast : 0); };
372 FuncPool["mapGetTypeTile"] = function int (int x, int y) {
373 int res = 0;
374 if (map !is null && x >= 0 && y >= 0 && x < map.width && y < map.height) {
375 res = map.tiles.ptr[LevelMap.Type].ptr[y*map.width+x];
376 if (res != LevelMap.TILE_ACTTRAP) res &= 0x7f;
378 return res;
381 FuncPool["mapGetTile"] = function int (uint layer, int x, int y) {
382 return (map !is null && x >= 0 && y >= 0 && x < map.width && y < map.height && layer >= 0 && layer <= 1 ? map.tiles.ptr[LevelMap.Front+layer].ptr[y*map.width+x] : 0);
385 FuncPool["mapSetTypeTile"] = function void (int x, int y, int tid) {
386 if (map is null || x < 0 || y < 0 || x >= map.width || y >= map.height || tid < 0 || tid > 255) return;
387 auto p = map.tiles.ptr[LevelMap.Type].ptr+y*map.width+x;
388 if (*p != tid) {
389 *p = cast(ubyte)tid;
390 import render : mapDirty;
391 mapDirty((1<<LevelMap.Type)|(1<<LevelMap.LightMask));
395 FuncPool["mapSetTile"] = function void (uint layer, int x, int y, int tid) {
396 if (map is null || layer < 0 || layer > 1 || x < 0 || y < 0 || x >= map.width || y >= map.height || tid < 0 || tid > 255) return;
397 auto p = map.tiles.ptr[LevelMap.Front+layer].ptr+y*map.width+x;
398 if (*p != tid) {
399 *p = cast(ubyte)tid;
400 import render : mapDirty;
401 if (layer == 0) {
402 // front
403 mapDirty((1<<LevelMap.Front)|(1<<LevelMap.AllLiquids)|(1<<LevelMap.LiquidMask));
404 } else {
405 // back
406 mapDirty((1<<LevelMap.Back)|(1<<LevelMap.AllLiquids)|(1<<LevelMap.LiquidMask));
411 FuncPool["mapGetWaterTexture"] = function int (int fg) {
412 if (fg < 0 || fg >= map.wallnames.length) return 0;
413 switch (map.wallnames.ptr[fg]) {
414 case "_water_0": return 1;
415 case "_water_1": return 2;
416 case "_water_2": return 3;
417 default:
419 return 0;
422 FuncPool["getMapViewHeight"] = function int () {
423 import render : vlWidth, vlHeight, getScale;
424 return vlHeight/getScale;
427 FuncPool["setMapViewPos"] = function void (int x, int y) {
428 import render : setMapViewPos;
429 setMapViewPos(x, y);
432 FuncPool["actorsOverlap"] = function int (ActorId a, ActorId b) { return (actorsOverlap(a, b) ? 1 : 0); };
434 FuncPool["actorRemove"] = function void (ActorId me) {
435 if (me.valid) {
436 if ((me.fget_flags&AF_NOCOLLISION) == 0) ugActorModify!false(me); // remove from grid
437 Actor.remove(me);
441 FuncPool["rewindTouchList"] = &rewindTouchList;
442 FuncPool["getNextTouchListItem"] = &getNextTouchListItem;
444 FuncPool["actorListRewind"] = &xactorListRewind;
445 FuncPool["actorListNext"] = &xactorListNext;
447 FuncPool["getCheatNoDoors"] = function int () { import render : cheatNoDoors; return (cheatNoDoors ? 1 : 0); };
448 FuncPool["getCheatNoWallClip"] = function int () { import render : cheatNoWallClip; return (cheatNoWallClip ? 1 : 0); };
449 FuncPool["getCheatNoCeilClip"] = function int () { return 0; };
450 FuncPool["getCheatNoLiftClip"] = function int () { return 0; };
452 FuncPool["addMessage"] = function void (string text, int pause, bool noreplace) {
453 import render : postAddMessage;
454 postAddMessage(text, pause, noreplace);
457 import d2dparts : dotAddBlood, dotAddSpark, dotAddWater;
459 FuncPool["dotAddBlood"] = &dotAddBlood;
460 FuncPool["dotAddSpark"] = &dotAddSpark;
461 FuncPool["dotAddWater"] = &dotAddWater;
463 function void (int x, int y, int xv, int yv, int n, int color) {
464 conwriteln("dotAddWater: x=", x, "; y=", y, "; xv=", xv, "; yv=", yv, "; n=", n, "; color=", color);
465 dotAddWater(x, y, xv, yv, n, color);
469 FuncPool["gactLevelExit"] = function void (int lnum) {
470 if (nextmapname.length == 0) {
471 string nmname = genNextMapName(lnum);
472 if (nmname.length == 0) assert(0, "no level!");
473 nextmapname = nmname;
477 FuncPool["actorSpawn"] = &actorSpawn;
481 public void registerAPI () {
482 //Actor.actorSize += 256;
483 conwriteln("actor size is ", Actor.actorSize, " bytes");
485 version(dacs_use_vm) FuncPool.vm = new DACSVM();
486 //dvm = new DACSVM();
488 // initialize actor animation
489 ActorDef.forEach((adef) => adef.callAnimInit());
491 conwriteln("API registered");
495 // ////////////////////////////////////////////////////////////////////////// //
496 mixin(Actor.FieldGetMixin!("classtype", StrId)); // fget_classtype
497 mixin(Actor.FieldGetMixin!("classname", StrId)); // fget_classname
498 mixin(Actor.FieldGetMixin!("x", int));
499 mixin(Actor.FieldGetMixin!("y", int));
500 mixin(Actor.FieldGetMixin!("dir", uint));
501 mixin(Actor.FieldGetMixin!("height", int));
502 mixin(Actor.FieldGetMixin!("radius", int));
503 mixin(Actor.FieldGetMixin!("flags", uint));
504 mixin(Actor.FieldGetMixin!("zAnimstate", StrId));
505 mixin(Actor.FieldGetMixin!("zAnimidx", int));
507 mixin(Actor.FieldSetMixin!("zAnimidx", int));
510 public bool actorsOverlap (ActorId a, ActorId b) {
511 if (!a.valid || !b.valid) return false;
512 if (a.id == b.id) return false; // no self-overlap
514 int ax = a.fget_x;
515 int bx = b.fget_x;
516 int ar = a.fget_radius;
517 int br = b.fget_radius;
519 if (ax-ar > bx+br || ax+ar < bx-br) return false;
521 int ay = a.fget_y;
522 int by = b.fget_y;
523 int ah = a.fget_height;
524 int bh = b.fget_height;
526 //return (ay > by-bh && ay-ah < by);
527 return (by-bh <= ay && by >= ay-ah);
531 // ////////////////////////////////////////////////////////////////////////// //
532 public __gshared ActorId[2] players;
535 public void loadAllMonsterGraphics () {
536 ActorDef.forEach((adef) {
537 conwriteln("loading graphics for '", adef.classtype.get, ":", adef.classname.get, "'");
538 adef.loadGraphics();
540 //Actor.dumpActors();
541 realiseSpriteAtlases();
545 ActorId actorSpawn (StrId classtype, StrId classname, int x, int y, uint dir) {
546 auto adef = findActorDef(classtype, classname);
547 if (adef is null) return ActorId(0);
548 auto aid = Actor.alloc;
549 aid.classtype = classtype;
550 aid.classname = classname;
551 aid.state = StrPool.MNST_SLEEP;
552 aid.x = x;
553 aid.y = y;
554 aid.dir = (dir ? 1 : 0);
555 adef.callInit(aid);
556 if ((aid.fget_flags&AF_NOCOLLISION) == 0) ugActorModify!true(aid);
557 return aid;
561 public void loadMapMonsters () {
562 assert(map !is null);
563 ugClear();
564 players[] = ActorId(0);
565 //conwriteln(players[0].valid, "; ", players[0].id);
566 foreach (ref thing; map.things) {
567 if (thing.dmonly) continue;
568 auto did = (thing.type&0x7fff) in d2dactordefsById;
569 if (did !is null && did.classtype == "playerstart") {
570 if ((thing.type&0x7fff) == 1 || (thing.type&0x7fff) == 2) {
571 int pnum = thing.type-1;
572 if (!players[pnum].valid) {
573 auto aid = Actor.alloc;
574 aid.classtype = StrPool.intern("monster");
575 aid.classname = StrPool.intern("Player");
576 aid.plrnum = cast(uint)(pnum+1);
577 aid.state = StrPool.MNST_SLEEP;
578 aid.x = cast(int)thing.x;
579 aid.y = cast(int)thing.y;
580 aid.dir = cast(uint)(thing.right ? 1 : 0);
581 if (pnum == 0) aid.flags = aid.fget_flags|AF_CAMERACHICK;
582 auto adef = findD2DActorDef(thing.type);
583 if (adef is null) assert(0);
584 adef.callInit(aid);
585 if ((aid.fget_flags&AF_NOCOLLISION) == 0) ugActorModify!true(aid);
586 players[pnum] = aid;
587 conwriteln("player #", pnum+1, " aid is ", aid.id);
590 continue;
592 auto adef = findD2DActorDef(thing.type&0x7fff);
593 if (adef is null) {
594 if (did !is null) {
595 conwriteln("ignoring D2D thing '", did.classtype.get, ":", did.classname.get, "'");
596 } else {
597 conwriteln("ignoring unknown D2D thing with mapid ", thing.type);
599 continue;
601 // create actor and initialize it
602 auto aid = Actor.alloc;
603 aid.classtype = StrPool.intern(adef.classtype);
604 aid.classname = StrPool.intern(adef.classname);
605 //conwriteln("found '", aid.classtype.get, ":", aid.classname.get, "'");
606 if (did !is null) {
607 assert(did.classtype == adef.classtype);
608 assert(did.classname == adef.classname);
609 conwriteln("mapid=", thing.type, "; ", adef.classtype, ":", adef.classname, "; id=", aid.id);
610 assert(aid.classtype!string == adef.classtype);
611 assert(aid.classname!string == adef.classname);
612 } else {
613 assert(0);
615 aid.state = StrPool.MNST_SLEEP;
616 aid.x = cast(int)thing.x;
617 aid.y = cast(int)thing.y;
618 aid.dir = cast(uint)(thing.right ? 1 : 0);
619 if (thing.type&0x8000) aid.flags = aid.fget_flags|AF_NOGRAVITY;
620 //if (aid.classtype!string == "item" && aid.x!int < 64) { aid.x = 92; aid.y = aid.y!int-16; conwriteln("!!!"); }
621 adef.callInit(aid);
622 if ((aid.fget_flags&AF_NOCOLLISION) == 0) ugActorModify!true(aid);
625 // create switches
626 foreach (ref sw; map.switches) {
627 if (sw.type == 0) continue; // just in case
628 auto swname = getD2DSwitchClassName(sw.type);
629 if (swname.length == 0) {
630 conwriteln("unknown switch type ", sw.type);
631 continue;
633 if (auto adef = findActorDef("switch", swname)) {
634 auto aid = Actor.alloc;
635 aid.classtype = StrPool.intern("switch");
636 aid.classname = StrPool.intern(swname);
637 // nocollision, 'cause collision checking will be processed in switch thinker, but other actors should not touch switches
638 aid.flags = /*AF_NOCOLLISION|*/AF_NOGRAVITY|/*AF_NOONTOUCH|*/AF_NODRAW|AF_NOLIGHT|AF_NOANIMATE;
639 aid.x = sw.x*8;
640 aid.y = sw.y*8;
641 aid.switchabf = (sw.a<<16)|(sw.b<<8)|sw.flags;
642 // should be enough
643 aid.radius = 16;
644 aid.height = 16;
645 adef.callInit(aid);
646 if ((aid.fget_flags&AF_NOCOLLISION) == 0) ugActorModify!true(aid); // just in case
647 } else {
648 conwriteln("switch definition 'switch:", swname, "' not found");
652 conwriteln("initial snapshot size: ", Actor.snapshotSize, " bytes");
656 // ////////////////////////////////////////////////////////////////////////// //
657 __gshared ActorId[65536] xactorList; // aids
658 __gshared uint xactorListCount, xactorListIndex = uint.max;
661 // dacs API
662 void xactorListRewind () {
663 if (!touchListAllowed) return; // it's ok to reuse it here
664 xactorListCount = xactorListIndex = 0;
665 Actor.forEach((ActorId me) {
666 if (me.fget_classtype != StrPool.X_X) xactorList.ptr[xactorListCount++] = me;
671 // dacs API
672 ActorId xactorListNext () {
673 if (!touchListAllowed) return ActorId(0); // it's ok to reuse it here
674 if (xactorListIndex == uint.max) xactorListRewind();
675 while (xactorListIndex < xactorListCount) {
676 auto aid = xactorList.ptr[xactorListIndex];
677 ++xactorListIndex;
678 if (aid.fget_classtype == StrPool.X_X) continue;
679 return aid;
681 return ActorId(0);
685 // ////////////////////////////////////////////////////////////////////////// //
686 __gshared uint[65536] touchList; // aids
687 __gshared uint[] realTouchList;
688 __gshared uint realTouchListIndex = uint.max;
689 __gshared ActorId curThinkingActor;
690 __gshared bool touchListAllowed = false;
693 // dacs API
694 void rewindTouchList () {
695 if (!touchListAllowed) return;
696 realTouchListIndex = 0;
697 realTouchList = ugActorHitList(curThinkingActor, touchList[]);
701 // dacs API
702 ActorId getNextTouchListItem () {
703 if (!touchListAllowed) return ActorId(0);
704 if (realTouchListIndex == uint.max) rewindTouchList();
705 while (realTouchListIndex < realTouchList.length) {
706 auto aid = ActorId(realTouchList.ptr[realTouchListIndex]);
707 ++realTouchListIndex;
708 if (aid.fget_classtype == StrPool.X_X) continue;
709 if (aid.valid && (aid.fget_flags&AF_NOONTOUCH) == 0) return aid;
711 return ActorId(0);
715 public void doActorsThink () {
716 // we have too much memory!
717 __gshared uint[65536] validActorsList;
718 __gshared ActorId[65536] postponedDeath;
719 __gshared uint pdcount;
721 touchListAllowed = true;
722 scope(exit) touchListAllowed = false;
723 pdcount = 0;
724 foreach (uint xid; Actor.getValidList(validActorsList[])) {
725 auto me = ActorId(xid);
726 if (me.valid) {
727 if (me.fget_classtype == StrPool.X_X) { postponedDeath.ptr[pdcount++] = me; continue; }
728 auto flags = me.fget_flags;
729 if ((flags&AF_NOCOLLISION) == 0) ugActorModify!false(me); // remove from grid
730 if (auto adef = findActorDef(me)) {
731 if ((flags&AF_NOTHINK) == 0) {
732 realTouchListIndex = uint.max;
733 xactorListIndex = uint.max;
734 curThinkingActor = me;
735 adef.callThink(me);
736 //if (me.x!int < 32) conwriteln("actor: ", me.id, "; attLightRGBX=", me.attLightRGBX!uint);
737 if (!me.valid) continue; // we are dead
738 if (me.fget_classtype == StrPool.X_X) { postponedDeath.ptr[pdcount++] = me; continue; }
739 flags = me.fget_flags; // in case script updated flags
741 if ((flags&AF_NOANIMATE) == 0) {
742 int aidx = me.fget_zAnimidx;
743 int nidx = adef.nextAnimIdx(me.fget_zAnimstate, me.fget_dir, aidx);
744 //conwriteln("actor ", me.id, " (", me.classtype!string, me.classname!string, "): state=", me.zAnimstate!string, "; aidx=", aidx, "; nidx=", nidx);
745 me.fset_zAnimidx = nidx;
746 //assert(me.fget_zAnimidx == nidx);
748 // put back to grid
749 if ((flags&AF_NOCOLLISION) == 0) ugActorModify!true(me);
753 // process scheduled death
754 foreach (ActorId aid; postponedDeath[0..pdcount]) {
755 /*if ((flags&AF_NOCOLLISION) == 0)*/ ugActorModify!false(aid); // remove from grid
756 Actor.remove(aid);
758 // reset player keys
759 plrKeysFix(0);
760 //{ import std.stdio : stdout; stdout.writeln("========================================="); }
761 //Actor.dumpActors();