more switches
[dd2d.git] / dengapi.d
blob62b81ad0833fd6803a93aa81383498973309cf5b
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 (int iteration) {
100 foreach (string ctype; dacsClassTypes(iteration)) {
101 foreach (auto mod; dacsClassModules(ctype, iteration)) {
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 int modIteration = 0;
169 public void loadWadScripts () {
170 if (moduleLoader is null) moduleLoader = (string modname) => modLoader(modname);
171 string[] mainmods;
172 try {
173 import std.array : split;
174 auto t = loadTextFile("scripts/dacsmain.txt");
175 foreach (string line; t.split('\n')) {
176 while (line.length && line[0] <= ' ') line = line[1..$];
177 if (line.length == 0 || line[0] == ';') continue;
178 while (line.length && line[$-1] <= ' ') line = line[0..$-1];
179 mainmods ~= line;
181 } catch (Exception e) { return; }
182 try {
183 //conwriteln("loading main DACS module '", mainmod, "'");
184 auto iter = modIteration++;
185 conwriteln("iteration=", iter);
186 foreach (string mod; mainmods) {
187 parseModule(mod, iter);
189 conwriteln("calling parseComplete; iteration=", iter);
190 parseComplete();
191 updateActorClasses(iter);
192 } catch (CompilerException e) {
193 _d_print_throwable(e);
194 conwriteln("PARSE ERROR: ", e.file, " at ", e.line);
195 conwriteln(e.toString);
196 assert(0);
201 public void scriptLoadingComplete () {
202 setupDAPI();
203 dacsFinalizeCompiler();
207 // ////////////////////////////////////////////////////////////////////////// //
208 public __gshared LevelMap map;
209 //public __gshared DACSVM dvm;
210 public __gshared uint prngSyncSeed = 0x29a;
211 public __gshared uint prngSeed = 0x29a; // unsynced
212 public __gshared string curmapname;
213 public __gshared string nextmapname; // nonempty: go to next level
216 // generate next map name for exit
217 public string genNextMapName (int lnum) {
218 import std.algorithm : endsWith;
219 import std.path;
220 string ext;
221 string nn = curmapname;
222 if (nn.endsWith(".d2m")) {
223 ext = ".d2m";
224 nn = nn[0..$-4];
226 if (nn[$-1] < '0' || nn[$-1] > '9') return null;
227 uint mapnum = 0, mmul = 1;
228 while (nn.length > 0 && nn[$-1] >= '0' && nn[$-1] <= '9') {
229 mapnum += (nn[$-1]-'0')*mmul;
230 mmul *= 10;
231 nn = nn[0..$-1];
233 ++mapnum;
234 if (lnum > 0 && lnum < 100) mapnum = lnum;
235 if (mapnum > 99) return null; // alas
236 import std.string : format;
237 return "%s%02u%s".format(nn, mapnum, ext);
241 // ////////////////////////////////////////////////////////////////////////// //
242 // Park-Miller-Carta Pseudo-Random Number Generator, based on David G. Carta paper
243 // 31 bit of randomness
244 // seed is previous result, as usual
245 public uint prngR31next (uint seed) {
246 if (seed == 0) seed = 0x29a;
247 uint lo = 16807*(seed&0xffff);
248 uint hi = 16807*(seed>>16);
249 lo += (hi&0x7fff)<<16;
250 lo += hi>>15;
251 //if (lo > 0x7fffffff) lo -= 0x7fffffff; // should be >=, actually
252 lo = (lo&0x7FFFFFFF)+(lo>>31); // same as previous code, but branch-less
253 return lo;
257 // "synced" random, using common seed for all players
258 public uint syncrandu31 () {
259 pragma(inline, true);
260 return (prngSyncSeed = prngR31next(prngSyncSeed));
264 // "unsynced" random, seed isn't saved
265 public uint unsyncrandu31 () {
266 pragma(inline, true);
267 return (prngSeed = prngR31next(prngSeed));
271 // ////////////////////////////////////////////////////////////////////////// //
272 version(dacs_use_vm) {
273 // this is VAT function, argument order is reversed
274 void doWrite(bool donl) (FuncPool.FuncInfo fi, DACSVM vm) {
275 import std.stdio;
276 auto count = vm.popInt();
277 while (count-- > 0) {
278 auto type = vm.popInt();
279 switch (cast(VATArgType)type) {
280 case VATArgType.Int: write(vm.popInt()); break;
281 case VATArgType.Uint: write(vm.popUint()); break;
282 case VATArgType.Float: write(vm.popFloat()); break;
283 case VATArgType.StrId: write(StrId(vm.popUint()).get); break;
284 case VATArgType.ActorId: write("<actor>"); vm.popUint(); break; //TODO
285 default: write("<invalid-type>"); vm.popUint(); break; //TODO
288 static if (donl) writeln();
289 // push dummy return value
290 vm.pushInt(0);
292 } else {
293 extern(C) void doWrite(bool donl) (uint argc, ...) {
294 import core.vararg;
295 import std.stdio;
296 mainloop: while (argc >= 2) {
297 argc -= 2;
298 int tp = va_arg!int(_argptr);
299 switch (tp) {
300 case VATArgType.Int:
301 auto v = va_arg!int(_argptr);
302 write(v);
303 break;
304 case VATArgType.Uint:
305 auto v = va_arg!uint(_argptr);
306 write(v);
307 break;
308 case VATArgType.Float:
309 auto v = va_arg!float(_argptr);
310 write(v);
311 break;
312 case VATArgType.StrId:
313 auto v = StrId(va_arg!uint(_argptr)).get;
314 write(v);
315 break;
316 case VATArgType.ActorId:
317 auto v = ActorId(va_arg!uint(_argptr));
318 write("<actor:", v.valid, ":", v.id, ">");
319 //write("<actor>");
320 break;
321 default: write("<invalid-type>"); break mainloop;
324 static if (donl) writeln();
329 // ////////////////////////////////////////////////////////////////////////// //
330 void animClearFrames (StrId classtype, StrId classname, StrId state) {
331 auto adef = findActorDef(classtype.get, classname.get);
332 if (adef is null) throw new Exception("animClearFrames: actor '"~classtype.get~":"~classname.get~"' not found!");
333 adef.clearFrames(state);
337 void animAddFrame (StrId classtype, StrId classname, StrId state, uint dir, StrId sprname) {
338 auto adef = findActorDef(classtype.get, classname.get);
339 if (adef is null) throw new Exception("animAddFrame: actor '"~classtype.get~":"~classname.get~"' not found!");
340 if (dir != 0) dir = 1; //TODO: process mirror flag here
341 adef.addFrame(state, dir, sprname);
345 // ////////////////////////////////////////////////////////////////////////// //
346 void setupDAPI () {
347 conwriteln("setting up D API");
349 FuncPool["write"] = &doWrite!false;
350 FuncPool["writeln"] = &doWrite!true;
352 FuncPool["syncrandu31"] = &syncrandu31;
354 FuncPool["animClearFrames"] = &animClearFrames;
355 FuncPool["animAddFrame"] = &animAddFrame;
357 FuncPool["actorSetAnimation"] = function void (ActorId me, StrId state) {
358 if (!me.valid) return;
359 if (auto adef = findActorDef(me.classtype!string, me.classname!string)) {
360 me.zAnimstate = state;
361 me.zAnimidx = 0;
365 //FuncPool["getPlayer"] = function ActorId () { assert(0); };
366 //FuncPool["isPlayer"] = function int (ActorId me) { return (me == players.ptr[0] ? 1 : 0); };
368 FuncPool["getPlayerCount"] = function int () => 1;
370 FuncPool["getPlayerActor"] = function ActorId (uint pnum) {
371 if (pnum == 1) return players.ptr[0];
372 return ActorId(0);
375 FuncPool["getPlayerButtons"] = function uint (uint pidx) { return (pidx == 1 ? plrKeysLast : 0); };
377 FuncPool["mapGetTypeTile"] = function int (int x, int y) {
378 int res = 0;
379 if (map !is null && x >= 0 && y >= 0 && x < map.width && y < map.height) {
380 res = map.tiles.ptr[LevelMap.Type].ptr[y*map.width+x];
381 if (res != LevelMap.TILE_ACTTRAP) res &= 0x7f;
383 return res;
386 FuncPool["mapGetTile"] = function int (uint layer, int x, int y) {
387 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);
390 FuncPool["mapSetTypeTile"] = function void (int x, int y, int tid) {
391 if (map is null || x < 0 || y < 0 || x >= map.width || y >= map.height || tid < 0 || tid > 255) return;
392 auto p = map.tiles.ptr[LevelMap.Type].ptr+y*map.width+x;
393 if (*p != tid) {
394 *p = cast(ubyte)tid;
395 import render : mapDirty;
396 mapDirty((1<<LevelMap.Type)|(1<<LevelMap.LightMask));
400 FuncPool["mapSetTile"] = function void (uint layer, int x, int y, int tid) {
401 if (map is null || layer < 0 || layer > 1 || x < 0 || y < 0 || x >= map.width || y >= map.height || tid < 0 || tid > 255) return;
402 auto p = map.tiles.ptr[LevelMap.Front+layer].ptr+y*map.width+x;
403 if (*p != tid) {
404 *p = cast(ubyte)tid;
405 import render : mapDirty;
406 if (layer == 0) {
407 // front
408 mapDirty((1<<LevelMap.Front)|(1<<LevelMap.AllLiquids)|(1<<LevelMap.LiquidMask));
409 } else {
410 // back
411 mapDirty((1<<LevelMap.Back)|(1<<LevelMap.AllLiquids)|(1<<LevelMap.LiquidMask));
416 FuncPool["mapGetWaterTexture"] = function int (int fg) {
417 if (fg < 0 || fg >= map.wallnames.length) return 0;
418 switch (map.wallnames.ptr[fg]) {
419 case "_water_0": return 1;
420 case "_water_1": return 2;
421 case "_water_2": return 3;
422 default:
424 return 0;
427 FuncPool["getMapViewHeight"] = function int () {
428 import render : vlWidth, vlHeight, getScale;
429 return vlHeight/getScale;
432 FuncPool["setMapViewPos"] = function void (int x, int y) {
433 import render : setMapViewPos;
434 setMapViewPos(x, y);
437 FuncPool["actorsOverlap"] = function int (ActorId a, ActorId b) { return (actorsOverlap(a, b) ? 1 : 0); };
439 FuncPool["actorRemove"] = function void (ActorId me) {
440 if (me.valid) {
441 if ((me.fget_flags&AF_NOCOLLISION) == 0) ugActorModify!false(me); // remove from grid
442 Actor.remove(me);
446 FuncPool["rewindTouchList"] = &rewindTouchList;
447 FuncPool["getNextTouchListItem"] = &getNextTouchListItem;
449 FuncPool["actorListRewind"] = &xactorListRewind;
450 FuncPool["actorListNext"] = &xactorListNext;
452 FuncPool["getCheatNoDoors"] = function int () { import render : cheatNoDoors; return (cheatNoDoors ? 1 : 0); };
453 FuncPool["getCheatNoWallClip"] = function int () { import render : cheatNoWallClip; return (cheatNoWallClip ? 1 : 0); };
454 FuncPool["getCheatNoCeilClip"] = function int () { return 0; };
455 FuncPool["getCheatNoLiftClip"] = function int () { return 0; };
457 FuncPool["addMessage"] = function void (string text, int pause, bool noreplace) {
458 import render : postAddMessage;
459 postAddMessage(text, pause, noreplace);
462 import d2dparts : dotAddBlood, dotAddSpark, dotAddWater;
464 FuncPool["dotAddBlood"] = &dotAddBlood;
465 FuncPool["dotAddSpark"] = &dotAddSpark;
466 FuncPool["dotAddWater"] = &dotAddWater;
468 function void (int x, int y, int xv, int yv, int n, int color) {
469 conwriteln("dotAddWater: x=", x, "; y=", y, "; xv=", xv, "; yv=", yv, "; n=", n, "; color=", color);
470 dotAddWater(x, y, xv, yv, n, color);
474 FuncPool["gactLevelExit"] = function void (int lnum) {
475 if (nextmapname.length == 0) {
476 string nmname = genNextMapName(lnum);
477 if (nmname.length == 0) assert(0, "no level!");
478 nextmapname = nmname;
482 FuncPool["actorSpawn"] = &actorSpawn;
486 public void registerAPI () {
487 //Actor.actorSize += 256;
488 conwriteln("actor size is ", Actor.actorSize, " bytes");
490 version(dacs_use_vm) FuncPool.vm = new DACSVM();
491 //dvm = new DACSVM();
493 // initialize actor animation
494 ActorDef.forEach((adef) => adef.callAnimInit());
496 conwriteln("API registered");
500 // ////////////////////////////////////////////////////////////////////////// //
501 mixin(Actor.FieldGetMixin!("classtype", StrId)); // fget_classtype
502 mixin(Actor.FieldGetMixin!("classname", StrId)); // fget_classname
503 mixin(Actor.FieldGetMixin!("x", int));
504 mixin(Actor.FieldGetMixin!("y", int));
505 mixin(Actor.FieldGetMixin!("dir", uint));
506 mixin(Actor.FieldGetMixin!("height", int));
507 mixin(Actor.FieldGetMixin!("radius", int));
508 mixin(Actor.FieldGetMixin!("flags", uint));
509 mixin(Actor.FieldGetMixin!("zAnimstate", StrId));
510 mixin(Actor.FieldGetMixin!("zAnimidx", int));
512 mixin(Actor.FieldSetMixin!("zAnimidx", int));
515 public bool actorsOverlap (ActorId a, ActorId b) {
516 if (!a.valid || !b.valid) return false;
517 if (a.id == b.id) return false; // no self-overlap
519 int ax = a.fget_x;
520 int bx = b.fget_x;
521 int ar = a.fget_radius;
522 int br = b.fget_radius;
524 if (ax-ar > bx+br || ax+ar < bx-br) return false;
526 int ay = a.fget_y;
527 int by = b.fget_y;
528 int ah = a.fget_height;
529 int bh = b.fget_height;
531 //return (ay > by-bh && ay-ah < by);
532 return (by-bh <= ay && by >= ay-ah);
536 // ////////////////////////////////////////////////////////////////////////// //
537 public __gshared ActorId[2] players;
540 public void loadAllMonsterGraphics () {
541 ActorDef.forEach((adef) {
542 conwriteln("loading graphics for '", adef.classtype.get, ":", adef.classname.get, "'");
543 adef.loadGraphics();
545 //Actor.dumpActors();
546 realiseSpriteAtlases();
550 ActorId actorSpawn (StrId classtype, StrId classname, int x, int y, uint dir) {
551 auto adef = findActorDef(classtype, classname);
552 if (adef is null) return ActorId(0);
553 auto aid = Actor.alloc;
554 aid.classtype = classtype;
555 aid.classname = classname;
556 aid.state = StrPool.MNST_SLEEP;
557 aid.x = x;
558 aid.y = y;
559 aid.dir = (dir ? 1 : 0);
560 adef.callInit(aid);
561 if ((aid.fget_flags&AF_NOCOLLISION) == 0) ugActorModify!true(aid);
562 return aid;
566 public void loadMapMonsters () {
567 assert(map !is null);
568 ugClear();
569 players[] = ActorId(0);
570 //conwriteln(players[0].valid, "; ", players[0].id);
571 foreach (ref thing; map.things) {
572 if (thing.dmonly) continue;
573 auto did = (thing.type&0x7fff) in d2dactordefsById;
574 if (did !is null && did.classtype == "playerstart") {
575 if ((thing.type&0x7fff) == 1 || (thing.type&0x7fff) == 2) {
576 int pnum = thing.type-1;
577 if (!players[pnum].valid) {
578 auto aid = Actor.alloc;
579 aid.classtype = StrPool.intern("monster");
580 aid.classname = StrPool.intern("Player");
581 aid.plrnum = cast(uint)(pnum+1);
582 aid.state = StrPool.MNST_SLEEP;
583 aid.x = cast(int)thing.x;
584 aid.y = cast(int)thing.y;
585 aid.dir = cast(uint)(thing.right ? 1 : 0);
586 if (pnum == 0) aid.flags = aid.fget_flags|AF_CAMERACHICK;
587 auto adef = findD2DActorDef(thing.type);
588 if (adef is null) assert(0);
589 adef.callInit(aid);
590 if ((aid.fget_flags&AF_NOCOLLISION) == 0) ugActorModify!true(aid);
591 players[pnum] = aid;
592 conwriteln("player #", pnum+1, " aid is ", aid.id);
595 continue;
597 auto adef = findD2DActorDef(thing.type&0x7fff);
598 if (adef is null) {
599 if (did !is null) {
600 conwriteln("ignoring D2D thing '", did.classtype.get, ":", did.classname.get, "'");
601 } else {
602 conwriteln("ignoring unknown D2D thing with mapid ", thing.type);
604 continue;
606 // create actor and initialize it
607 auto aid = Actor.alloc;
608 aid.classtype = StrPool.intern(adef.classtype);
609 aid.classname = StrPool.intern(adef.classname);
610 //conwriteln("found '", aid.classtype.get, ":", aid.classname.get, "'");
611 if (did !is null) {
612 assert(did.classtype == adef.classtype);
613 assert(did.classname == adef.classname);
614 conwriteln("mapid=", thing.type, "; ", adef.classtype, ":", adef.classname, "; id=", aid.id);
615 assert(aid.classtype!string == adef.classtype);
616 assert(aid.classname!string == adef.classname);
617 } else {
618 assert(0);
620 aid.state = StrPool.MNST_SLEEP;
621 aid.x = cast(int)thing.x;
622 aid.y = cast(int)thing.y;
623 aid.dir = cast(uint)(thing.right ? 1 : 0);
624 if (thing.type&0x8000) aid.flags = aid.fget_flags|AF_NOGRAVITY;
625 //if (aid.classtype!string == "item" && aid.x!int < 64) { aid.x = 92; aid.y = aid.y!int-16; conwriteln("!!!"); }
626 adef.callInit(aid);
627 if ((aid.fget_flags&AF_NOCOLLISION) == 0) ugActorModify!true(aid);
630 // create switches
631 foreach (ref sw; map.switches) {
632 if (sw.type == 0) continue; // just in case
633 auto swname = getD2DSwitchClassName(sw.type);
634 if (swname.length == 0) {
635 conwriteln("unknown switch type ", sw.type);
636 continue;
638 if (auto adef = findActorDef("switch", swname)) {
639 auto aid = Actor.alloc;
640 aid.classtype = StrPool.intern("switch");
641 aid.classname = StrPool.intern(swname);
642 // nocollision, 'cause collision checking will be processed in switch thinker, but other actors should not touch switches
643 aid.flags = /*AF_NOCOLLISION|*/AF_NOGRAVITY|/*AF_NOONTOUCH|*/AF_NODRAW|AF_NOLIGHT|AF_NOANIMATE;
644 aid.x = sw.x*8;
645 aid.y = sw.y*8;
646 aid.switchabf = (sw.a<<16)|(sw.b<<8)|sw.flags;
647 // should be enough
648 aid.radius = 16;
649 aid.height = 16;
650 adef.callInit(aid);
651 if ((aid.fget_flags&AF_NOCOLLISION) == 0) ugActorModify!true(aid); // just in case
652 } else {
653 conwriteln("switch definition 'switch:", swname, "' not found");
657 conwriteln("initial snapshot size: ", Actor.snapshotSize, " bytes");
661 // ////////////////////////////////////////////////////////////////////////// //
662 __gshared ActorId[65536] xactorList; // aids
663 __gshared uint xactorListCount, xactorListIndex = uint.max;
666 // dacs API
667 void xactorListRewind () {
668 if (!touchListAllowed) return; // it's ok to reuse it here
669 xactorListCount = xactorListIndex = 0;
670 Actor.forEach((ActorId me) {
671 if (me.fget_classtype != StrPool.X_X) xactorList.ptr[xactorListCount++] = me;
676 // dacs API
677 ActorId xactorListNext () {
678 if (!touchListAllowed) return ActorId(0); // it's ok to reuse it here
679 if (xactorListIndex == uint.max) xactorListRewind();
680 while (xactorListIndex < xactorListCount) {
681 auto aid = xactorList.ptr[xactorListIndex];
682 ++xactorListIndex;
683 if (aid.fget_classtype == StrPool.X_X) continue;
684 return aid;
686 return ActorId(0);
690 // ////////////////////////////////////////////////////////////////////////// //
691 __gshared uint[65536] touchList; // aids
692 __gshared uint[] realTouchList;
693 __gshared uint realTouchListIndex = uint.max;
694 __gshared ActorId curThinkingActor;
695 __gshared bool touchListAllowed = false;
698 // dacs API
699 void rewindTouchList () {
700 if (!touchListAllowed) return;
701 realTouchListIndex = 0;
702 realTouchList = ugActorHitList(curThinkingActor, touchList[]);
706 // dacs API
707 ActorId getNextTouchListItem () {
708 if (!touchListAllowed) return ActorId(0);
709 if (realTouchListIndex == uint.max) rewindTouchList();
710 while (realTouchListIndex < realTouchList.length) {
711 auto aid = ActorId(realTouchList.ptr[realTouchListIndex]);
712 ++realTouchListIndex;
713 if (aid.fget_classtype == StrPool.X_X) continue;
714 if (aid.valid && (aid.fget_flags&AF_NOONTOUCH) == 0) return aid;
716 return ActorId(0);
720 public void doActorsThink () {
721 // we have too much memory!
722 __gshared uint[65536] validActorsList;
723 __gshared ActorId[65536] postponedDeath;
724 __gshared uint pdcount;
726 touchListAllowed = true;
727 scope(exit) touchListAllowed = false;
728 pdcount = 0;
729 foreach (uint xid; Actor.getValidList(validActorsList[])) {
730 auto me = ActorId(xid);
731 if (me.valid) {
732 if (me.fget_classtype == StrPool.X_X) { postponedDeath.ptr[pdcount++] = me; continue; }
733 auto flags = me.fget_flags;
734 if ((flags&AF_NOCOLLISION) == 0) ugActorModify!false(me); // remove from grid
735 if (auto adef = findActorDef(me)) {
736 if ((flags&AF_NOTHINK) == 0) {
737 realTouchListIndex = uint.max;
738 xactorListIndex = uint.max;
739 curThinkingActor = me;
740 adef.callThink(me);
741 //if (me.x!int < 32) conwriteln("actor: ", me.id, "; attLightRGBX=", me.attLightRGBX!uint);
742 if (!me.valid) continue; // we are dead
743 if (me.fget_classtype == StrPool.X_X) { postponedDeath.ptr[pdcount++] = me; continue; }
744 flags = me.fget_flags; // in case script updated flags
746 if ((flags&AF_NOANIMATE) == 0) {
747 int aidx = me.fget_zAnimidx;
748 int nidx = adef.nextAnimIdx(me.fget_zAnimstate, me.fget_dir, aidx);
749 //conwriteln("actor ", me.id, " (", me.classtype!string, me.classname!string, "): state=", me.zAnimstate!string, "; aidx=", aidx, "; nidx=", nidx);
750 me.fset_zAnimidx = nidx;
751 //assert(me.fget_zAnimidx == nidx);
753 // put back to grid
754 if ((flags&AF_NOCOLLISION) == 0) ugActorModify!true(me);
758 // process scheduled death
759 foreach (ActorId aid; postponedDeath[0..pdcount]) {
760 Actor.remove(aid);
762 // reset player keys
763 plrKeysFix(0);
764 //{ import std.stdio : stdout; stdout.writeln("========================================="); }
765 //Actor.dumpActors();