max light radius is 255 now. no, really!
[dd2d.git] / dengapi.d
blobbe8e662ca1c06f788e2611c1778e8238fa6bd6e2
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 __gshared ActorId cameraChick;
52 // ////////////////////////////////////////////////////////////////////////// //
53 public void addInternalActorFields () {
54 // &0xffff: position in drawlist
55 // (>>16)&0xff: drawlist index
56 //Actor.addField("0drawlistpos", Actor.Field.Type.Uint);
60 // ////////////////////////////////////////////////////////////////////////// //
61 public {
62 enum PLK_UP = (1<<0);
63 enum PLK_DOWN = (1<<1);
64 enum PLK_LEFT = (1<<2);
65 enum PLK_RIGHT = (1<<3);
66 enum PLK_FIRE = (1<<4);
67 enum PLK_JUMP = (1<<5);
68 enum PLK_USE = (1<<6);
71 __gshared uint plrKeysLast;
72 __gshared uint plrKeyState;
75 public void plrKeyDown (uint plidx, uint mask) {
76 if (mask == 0 || plidx > 0) return;
77 plrKeysLast |= mask;
78 plrKeyState |= mask;
82 public void plrKeyUp (uint plidx, uint mask) {
83 if (mask == 0 || plidx > 0) return;
84 plrKeyState &= ~mask;
88 public void plrKeyUpDown (uint plidx, uint mask, bool down) {
89 if (down) plrKeyDown(plidx, mask); else plrKeyUp(plidx, mask);
93 void plrKeysFix (uint plidx) {
94 if (plidx > 0) return;
95 //conwritefln!"plrKeyState=0x%02x; plrKeysLast=0x%02x"(plrKeyState, plrKeysLast);
96 foreach (uint n; 0..12) {
97 if ((plrKeyState&(1<<n)) == 0) plrKeysLast &= ~(1<<n);
102 // ////////////////////////////////////////////////////////////////////////// //
103 void updateActorClasses () {
104 foreach (string ctype; dacsClassTypes) {
105 foreach (auto mod; dacsClassModules(ctype)) {
106 auto adef = registerActorDef(mod.rmoduletype.classtype, mod.rmoduletype.classname);
107 conwriteln("found info for actor '", mod.rmoduletype.classtype, ":", mod.rmoduletype.classname, "'");
109 auto animInitFn = FuncPool.findByFQMG(mod.name~".initializeAnim", ":void");
110 if (animInitFn !is null) adef.setAnimInitFunc(animInitFn);
113 auto initFn = FuncPool.findByFQMG(mod.name~".initialize", ":void:Actor");
114 if (initFn !is null) adef.setInitFunc(initFn);
117 auto thinkFn = FuncPool.findByFQMG(mod.name~".think", ":void:Actor");
118 if (thinkFn !is null) adef.setThinkFunc(thinkFn);
125 // ////////////////////////////////////////////////////////////////////////// //
126 private string modLoader (string modname) {
127 static string getTextFile (string fname) {
128 try {
129 auto res = loadTextFile(fname);
130 conwriteln("loading DACS module file '", fname, "' (", res.length, " bytes)");
131 if (res is null) res = "";
132 return res;
133 } catch (Exception) {}
134 return null;
137 string res;
139 //res = getTextFile(modname~".dacs");
140 //if (res !is null) return res;
142 res = getTextFile("scripts/"~modname~".dacs");
143 if (res !is null) return res;
144 assert(res is null);
146 string[] parts = ["scripts"];
147 string mn = modname;
148 while (mn.length > 0) {
149 int pos = 0;
150 if (mn[0] >= 'A' && mn[0] <= 'Z') ++pos;
151 while (pos < mn.length && (mn[pos] < 'A' || mn[pos] > 'Z')) ++pos;
152 if (mn[0] >= 'A' && mn[0] <= 'Z') {
153 parts ~= cast(char)(mn[0]+32)~mn[1..pos];
154 } else {
155 parts ~= mn[0..pos];
157 mn = mn[pos..$];
160 import std.array : join;
161 string path = parts.join("/")~".dacs";
162 res = getTextFile(path);
163 if (res !is null) return res;
164 assert(res is null);
166 assert(res is null);
167 throw new Exception("module '"~modname~"' not found");
171 __gshared string[] dacsModules;
173 public void registerWadScripts () {
174 try {
175 import std.array : split;
176 auto t = loadTextFile("scripts/dacsmain.txt");
177 foreach (string line; t.split('\n')) {
178 while (line.length && line[0] <= ' ') line = line[1..$];
179 if (line.length == 0 || line[0] == ';' || line[0] == '#') continue;
180 while (line.length && line[$-1] <= ' ') line = line[0..$-1];
181 import std.algorithm : canFind;
182 if (!dacsModules.canFind(line)) dacsModules ~= line;
184 } catch (Exception e) { return; }
188 public void loadWadScripts () {
189 if (moduleLoader is null) moduleLoader = (string modname) => modLoader(modname);
190 try {
191 //conwriteln("loading main DACS module '", mainmod, "'");
192 foreach (string mod; dacsModules) parseModule(mod);
193 parseComplete();
194 updateActorClasses();
195 setupDAPI();
196 dacsFinalizeCompiler();
197 } catch (CompilerException e) {
198 _d_print_throwable(e);
199 conwriteln("PARSE ERROR: ", e.file, " at ", e.line);
200 conwriteln(e.toString);
201 assert(0);
206 // ////////////////////////////////////////////////////////////////////////// //
207 public __gshared LevelMap map;
208 //public __gshared DACSVM dvm;
209 public __gshared uint prngSyncSeed = 0x29a;
210 public __gshared uint prngSeed = 0x29a; // unsynced
211 public __gshared string curmapname;
212 public __gshared string nextmapname; // nonempty: go to next level
215 // generate next map name for exit
216 public string genNextMapName (int lnum) {
217 import std.algorithm : endsWith;
218 import std.path;
219 string ext;
220 string nn = curmapname;
221 if (nn.endsWith(".d2m")) {
222 ext = ".d2m";
223 nn = nn[0..$-4];
225 if (nn[$-1] < '0' || nn[$-1] > '9') return null;
226 uint mapnum = 0, mmul = 1;
227 while (nn.length > 0 && nn[$-1] >= '0' && nn[$-1] <= '9') {
228 mapnum += (nn[$-1]-'0')*mmul;
229 mmul *= 10;
230 nn = nn[0..$-1];
232 ++mapnum;
233 if (lnum > 0 && lnum < 100) mapnum = lnum;
234 if (mapnum > 99) return null; // alas
235 import std.string : format;
236 return "%s%02u%s".format(nn, mapnum, ext);
240 // ////////////////////////////////////////////////////////////////////////// //
241 // Park-Miller-Carta Pseudo-Random Number Generator, based on David G. Carta paper
242 // 31 bit of randomness
243 // seed is previous result, as usual
244 public uint prngR31next (uint seed) {
245 if (seed == 0) seed = 0x29a;
246 uint lo = 16807*(seed&0xffff);
247 uint hi = 16807*(seed>>16);
248 lo += (hi&0x7fff)<<16;
249 lo += hi>>15;
250 //if (lo > 0x7fffffff) lo -= 0x7fffffff; // should be >=, actually
251 lo = (lo&0x7FFFFFFF)+(lo>>31); // same as previous code, but branch-less
252 return lo;
256 // "synced" random, using common seed for all players
257 public uint syncrandu31 () {
258 pragma(inline, true);
259 return (prngSyncSeed = prngR31next(prngSyncSeed));
263 // "unsynced" random, seed isn't saved
264 public uint unsyncrandu31 () {
265 pragma(inline, true);
266 return (prngSeed = prngR31next(prngSeed));
270 // ////////////////////////////////////////////////////////////////////////// //
271 version(dacs_use_vm) {
272 // this is VAT function, argument order is reversed
273 void doWrite(bool donl) (FuncPool.FuncInfo fi, DACSVM vm) {
274 import std.stdio;
275 auto count = vm.popInt();
276 while (count-- > 0) {
277 auto type = vm.popInt();
278 switch (cast(VATArgType)type) {
279 case VATArgType.Int: write(vm.popInt()); break;
280 case VATArgType.Uint: write(vm.popUint()); break;
281 case VATArgType.Float: write(vm.popFloat()); break;
282 case VATArgType.StrId: write(StrId(vm.popUint()).get); break;
283 case VATArgType.ActorId: write("<actor>"); vm.popUint(); break; //TODO
284 default: write("<invalid-type>"); vm.popUint(); break; //TODO
287 static if (donl) writeln();
288 // push dummy return value
289 vm.pushInt(0);
291 } else {
292 extern(C) void doWrite(bool donl) (uint argc, ...) {
293 import core.vararg;
294 import std.stdio;
295 mainloop: while (argc >= 2) {
296 argc -= 2;
297 int tp = va_arg!int(_argptr);
298 switch (tp) {
299 case VATArgType.Int:
300 auto v = va_arg!int(_argptr);
301 write(v);
302 break;
303 case VATArgType.Uint:
304 auto v = va_arg!uint(_argptr);
305 write(v);
306 break;
307 case VATArgType.Float:
308 auto v = va_arg!float(_argptr);
309 write(v);
310 break;
311 case VATArgType.StrId:
312 auto v = StrId(va_arg!uint(_argptr)).get;
313 write(v);
314 break;
315 case VATArgType.ActorId:
316 auto v = ActorId(va_arg!uint(_argptr));
317 write("<actor:", v.valid, ":", v.id, ">");
318 //write("<actor>");
319 break;
320 default: write("<invalid-type>"); break mainloop;
323 static if (donl) writeln();
328 // ////////////////////////////////////////////////////////////////////////// //
329 void animClearFrames (StrId classtype, StrId classname, StrId state) {
330 auto adef = findActorDef(classtype.get, classname.get);
331 if (adef is null) throw new Exception("animClearFrames: actor '"~classtype.get~":"~classname.get~"' not found!");
332 adef.clearFrames(state);
336 void animAddFrame (StrId classtype, StrId classname, StrId state, uint dir, StrId sprname) {
337 auto adef = findActorDef(classtype.get, classname.get);
338 if (adef is null) throw new Exception("animAddFrame: actor '"~classtype.get~":"~classname.get~"' not found!");
339 if (dir != 0) dir = 1; //TODO: process mirror flag here
340 adef.addFrame(state, dir, sprname);
344 // ////////////////////////////////////////////////////////////////////////// //
345 void setupDAPI () {
346 conwriteln("setting up D API");
348 FuncPool["write"] = &doWrite!false;
349 FuncPool["writeln"] = &doWrite!true;
351 FuncPool["syncrandu31"] = &syncrandu31;
353 FuncPool["animClearFrames"] = &animClearFrames;
354 FuncPool["animAddFrame"] = &animAddFrame;
356 FuncPool["actorSetAnimation"] = function void (ActorId me, StrId state) {
357 if (!me.valid) return;
358 if (auto adef = findActorDef(me.classtype!string, me.classname!string)) {
359 me.zAnimstate = state;
360 me.zAnimidx = 0;
364 //FuncPool["getPlayer"] = function ActorId () { assert(0); };
365 //FuncPool["isPlayer"] = function int (ActorId me) { return (me == players.ptr[0] ? 1 : 0); };
367 FuncPool["getPlayerCount"] = function int () => 1;
369 FuncPool["getPlayerActor"] = function ActorId (uint pnum) {
370 if (pnum == 1) return players.ptr[0];
371 return ActorId(0);
374 FuncPool["getPlayerButtons"] = function uint (uint pidx) { return (pidx == 1 ? plrKeysLast : 0); };
376 FuncPool["mapGetTypeTile"] = function int (int x, int y) {
377 int res = 0;
378 if (map !is null && x >= 0 && y >= 0 && x < map.width && y < map.height) {
379 res = map.tiles.ptr[LevelMap.Type].ptr[y*map.width+x];
380 if (res != LevelMap.TILE_ACTTRAP) res &= 0x7f;
382 return res;
385 FuncPool["mapGetTile"] = function int (uint layer, int x, int y) {
386 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);
389 FuncPool["mapSetTypeTile"] = function void (int x, int y, int tid) {
390 if (map is null || x < 0 || y < 0 || x >= map.width || y >= map.height || tid < 0 || tid > 255) return;
391 auto p = map.tiles.ptr[LevelMap.Type].ptr+y*map.width+x;
392 if (*p != tid) {
393 *p = cast(ubyte)tid;
394 import render : mapDirty;
395 mapDirty((1<<LevelMap.Type)|(1<<LevelMap.LightMask));
399 FuncPool["mapSetTile"] = function void (uint layer, int x, int y, int tid) {
400 if (map is null || layer < 0 || layer > 1 || x < 0 || y < 0 || x >= map.width || y >= map.height || tid < 0 || tid > 255) return;
401 auto p = map.tiles.ptr[LevelMap.Front+layer].ptr+y*map.width+x;
402 if (*p != tid) {
403 *p = cast(ubyte)tid;
404 import render : mapDirty;
405 if (layer == 0) {
406 // front
407 mapDirty((1<<LevelMap.Front)|(1<<LevelMap.AllLiquids)|(1<<LevelMap.LiquidMask));
408 } else {
409 // back
410 mapDirty((1<<LevelMap.Back)|(1<<LevelMap.AllLiquids)|(1<<LevelMap.LiquidMask));
415 FuncPool["mapGetWaterTexture"] = function int (int fg) {
416 if (fg < 0 || fg >= map.wallnames.length) return 0;
417 switch (map.wallnames.ptr[fg]) {
418 case "_water_0": return 1;
419 case "_water_1": return 2;
420 case "_water_2": return 3;
421 default:
423 return 0;
426 FuncPool["getMapViewHeight"] = function int () {
427 import render : vlWidth, vlHeight, getScale;
428 return vlHeight/getScale;
431 FuncPool["actorsOverlap"] = function int (ActorId a, ActorId b) { return (actorsOverlap(a, b) ? 1 : 0); };
433 FuncPool["actorRemove"] = function void (ActorId me) {
434 if (me.valid) {
435 if ((me.fget_flags&AF_NOCOLLISION) == 0) ugActorModify!false(me); // remove from grid
436 Actor.remove(me);
440 FuncPool["rewindTouchList"] = &rewindTouchList;
441 FuncPool["getNextTouchListItem"] = &getNextTouchListItem;
443 FuncPool["actorListRewind"] = &xactorListRewind;
444 FuncPool["actorListNext"] = &xactorListNext;
446 FuncPool["getCheatNoDoors"] = function int () { import render : cheatNoDoors; return (cheatNoDoors ? 1 : 0); };
447 FuncPool["getCheatNoWallClip"] = function int () { import render : cheatNoWallClip; return (cheatNoWallClip ? 1 : 0); };
448 FuncPool["getCheatNoCeilClip"] = function int () { return 0; };
449 FuncPool["getCheatNoLiftClip"] = function int () { return 0; };
451 FuncPool["addMessage"] = function void (string text, int pause, bool noreplace) {
452 import render : postAddMessage;
453 postAddMessage(text, pause, noreplace);
456 import d2dparts : dotAddBlood, dotAddSpark, dotAddWater;
458 FuncPool["dotAddBlood"] = &dotAddBlood;
459 FuncPool["dotAddSpark"] = &dotAddSpark;
460 FuncPool["dotAddWater"] = &dotAddWater;
462 function void (int x, int y, int xv, int yv, int n, int color) {
463 conwriteln("dotAddWater: x=", x, "; y=", y, "; xv=", xv, "; yv=", yv, "; n=", n, "; color=", color);
464 dotAddWater(x, y, xv, yv, n, color);
468 FuncPool["gactLevelExit"] = function void (int lnum) {
469 if (nextmapname.length == 0) {
470 string nmname = genNextMapName(lnum);
471 if (nmname.length == 0) assert(0, "no level!");
472 nextmapname = nmname;
476 FuncPool["actorSpawn"] = &actorSpawn;
478 // return previous one
479 FuncPool["setCameraChick"] = function ActorId (ActorId act) {
480 auto res = cameraChick;
481 cameraChick = act;
482 return res;
487 public void registerAPI () {
488 //Actor.actorSize += 256;
489 conwriteln("actor size is ", Actor.actorSize, " bytes");
491 version(dacs_use_vm) FuncPool.vm = new DACSVM();
492 //dvm = new DACSVM();
494 // initialize actor animation
495 ActorDef.forEach((adef) => adef.callAnimInit());
497 conwriteln("API registered");
501 // ////////////////////////////////////////////////////////////////////////// //
502 mixin(Actor.FieldGetMixin!("classtype", StrId)); // fget_classtype
503 mixin(Actor.FieldGetMixin!("classname", StrId)); // fget_classname
504 mixin(Actor.FieldGetMixin!("x", int));
505 mixin(Actor.FieldGetMixin!("y", int));
506 mixin(Actor.FieldGetMixin!("dir", uint));
507 mixin(Actor.FieldGetMixin!("height", int));
508 mixin(Actor.FieldGetMixin!("radius", int));
509 mixin(Actor.FieldGetMixin!("flags", uint));
510 mixin(Actor.FieldGetMixin!("zAnimstate", StrId));
511 mixin(Actor.FieldGetMixin!("zAnimidx", int));
513 mixin(Actor.FieldSetMixin!("zAnimidx", int));
516 public bool actorsOverlap (ActorId a, ActorId b) {
517 if (!a.valid || !b.valid) return false;
518 if (a.id == b.id) return false; // no self-overlap
520 int ax = a.fget_x;
521 int bx = b.fget_x;
522 int ar = a.fget_radius;
523 int br = b.fget_radius;
525 if (ax-ar > bx+br || ax+ar < bx-br) return false;
527 int ay = a.fget_y;
528 int by = b.fget_y;
529 int ah = a.fget_height;
530 int bh = b.fget_height;
532 //return (ay > by-bh && ay-ah < by);
533 return (by-bh <= ay && by >= ay-ah);
537 // ////////////////////////////////////////////////////////////////////////// //
538 public __gshared ActorId[2] players;
541 public void loadAllMonsterGraphics () {
542 ActorDef.forEach((adef) {
543 conwriteln("loading graphics for '", adef.classtype.get, ":", adef.classname.get, "'");
544 adef.loadGraphics();
546 //Actor.dumpActors();
547 realiseSpriteAtlases();
551 ActorId actorSpawn (StrId classtype, StrId classname, int x, int y, uint dir) {
552 auto adef = findActorDef(classtype, classname);
553 if (adef is null) return ActorId(0);
554 auto aid = Actor.alloc;
555 aid.classtype = classtype;
556 aid.classname = classname;
557 aid.state = StrPool.MNST_SLEEP;
558 aid.x = x;
559 aid.y = y;
560 aid.dir = (dir ? 1 : 0);
561 adef.callInit(aid);
562 if ((aid.fget_flags&AF_NOCOLLISION) == 0) ugActorModify!true(aid);
563 return aid;
567 public void loadMapMonsters () {
568 assert(map !is null);
569 ugClear();
570 players[] = ActorId(0);
571 //conwriteln(players[0].valid, "; ", players[0].id);
572 foreach (ref thing; map.things) {
573 if (thing.dmonly) continue;
574 auto did = (thing.type&0x7fff) in d2dactordefsById;
575 if (did !is null && did.classtype == "playerstart") {
576 if ((thing.type&0x7fff) == 1 || (thing.type&0x7fff) == 2) {
577 int pnum = thing.type-1;
578 if (!players[pnum].valid) {
579 auto aid = Actor.alloc;
580 aid.classtype = StrPool.intern("monster");
581 aid.classname = StrPool.intern("Player");
582 aid.plrnum = cast(uint)(pnum+1);
583 aid.state = StrPool.MNST_SLEEP;
584 aid.x = cast(int)thing.x;
585 aid.y = cast(int)thing.y;
586 aid.dir = cast(uint)(thing.right ? 1 : 0);
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 /*if ((flags&AF_NOCOLLISION) == 0)*/ ugActorModify!false(aid); // remove from grid
761 Actor.remove(aid);
763 // reset player keys
764 plrKeysFix(0);
765 //{ import std.stdio : stdout; stdout.writeln("========================================="); }
766 //Actor.dumpActors();