map03 is working now
[dd2d.git] / dengapi.d
blob6b1f7c0a8fa6fe8a4eca522ab3760c71cb402de2
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.flags!uint&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;
484 public void registerAPI () {
485 //Actor.actorSize += 256;
486 conwriteln("actor size is ", Actor.actorSize, " bytes");
488 version(dacs_use_vm) FuncPool.vm = new DACSVM();
489 //dvm = new DACSVM();
491 // initialize actor animation
492 ActorDef.forEach((adef) => adef.callAnimInit());
494 conwriteln("API registered");
498 // ////////////////////////////////////////////////////////////////////////// //
499 mixin(Actor.FieldGetMixin!("classtype", StrId)); // fget_classtype
500 mixin(Actor.FieldGetMixin!("classname", StrId)); // fget_classname
501 mixin(Actor.FieldGetMixin!("x", int));
502 mixin(Actor.FieldGetMixin!("y", int));
503 mixin(Actor.FieldGetMixin!("dir", uint));
504 mixin(Actor.FieldGetMixin!("height", int));
505 mixin(Actor.FieldGetMixin!("radius", int));
506 mixin(Actor.FieldGetMixin!("flags", uint));
507 mixin(Actor.FieldGetMixin!("zAnimstate", StrId));
508 mixin(Actor.FieldGetMixin!("zAnimidx", int));
510 mixin(Actor.FieldSetMixin!("zAnimidx", int));
513 public bool actorsOverlap (ActorId a, ActorId b) {
514 if (!a.valid || !b.valid) return false;
515 if (a.id == b.id) return false; // no self-overlap
517 int ax = a.fget_x;
518 int bx = b.fget_x;
519 int ar = a.fget_radius;
520 int br = b.fget_radius;
522 if (ax-ar > bx+br || ax+ar < bx-br) return false;
524 int ay = a.fget_y;
525 int by = b.fget_y;
526 int ah = a.fget_height;
527 int bh = b.fget_height;
529 //return (ay > by-bh && ay-ah < by);
530 return (by-bh <= ay && by >= ay-ah);
534 // ////////////////////////////////////////////////////////////////////////// //
535 public __gshared ActorId[2] players;
538 public void loadAllMonsterGraphics () {
539 ActorDef.forEach((adef) {
540 conwriteln("loading graphics for '", adef.classtype.get, ":", adef.classname.get, "'");
541 adef.loadGraphics();
543 //Actor.dumpActors();
544 realiseSpriteAtlases();
548 public void loadMapMonsters () {
549 assert(map !is null);
550 ugClear();
551 players[] = ActorId(0);
552 //conwriteln(players[0].valid, "; ", players[0].id);
553 foreach (ref thing; map.things) {
554 if (thing.dmonly) continue;
555 auto did = (thing.type&0x7fff) in d2dactordefsById;
556 if (did !is null && did.classtype == "playerstart") {
557 if ((thing.type&0x7fff) == 1 || (thing.type&0x7fff) == 2) {
558 int pnum = thing.type-1;
559 if (!players[pnum].valid) {
560 auto aid = Actor.alloc;
561 aid.classtype = StrPool.intern("monster");
562 aid.classname = StrPool.intern("Player");
563 aid.plrnum = cast(uint)(pnum+1);
564 aid.state = StrPool.MNST_SLEEP;
565 aid.x = cast(int)thing.x;
566 aid.y = cast(int)thing.y;
567 aid.dir = cast(uint)(thing.right ? 1 : 0);
568 if (pnum == 0) aid.flags = aid.flags!uint|AF_CAMERACHICK;
569 auto adef = findD2DActorDef(thing.type);
570 if (adef is null) assert(0);
571 adef.callInit(aid);
572 if ((aid.flags!uint&AF_NOCOLLISION) == 0) ugActorModify!true(aid);
573 players[pnum] = aid;
574 conwriteln("player #", pnum+1, " aid is ", aid.id);
577 continue;
579 auto adef = findD2DActorDef(thing.type&0x7fff);
580 if (adef is null) {
581 if (did !is null) {
582 conwriteln("ignoring D2D thing '", did.classtype.get, ":", did.classname.get, "'");
583 } else {
584 conwriteln("ignoring unknown D2D thing with mapid ", thing.type);
586 continue;
588 // create actor and initialize it
589 auto aid = Actor.alloc;
590 aid.classtype = StrPool.intern(adef.classtype);
591 aid.classname = StrPool.intern(adef.classname);
592 //conwriteln("found '", aid.classtype.get, ":", aid.classname.get, "'");
593 if (did !is null) {
594 assert(did.classtype == adef.classtype);
595 assert(did.classname == adef.classname);
596 conwriteln("mapid=", thing.type, "; ", adef.classtype, ":", adef.classname, "; id=", aid.id);
597 assert(aid.classtype!string == adef.classtype);
598 assert(aid.classname!string == adef.classname);
599 } else {
600 assert(0);
602 aid.state = StrPool.MNST_SLEEP;
603 aid.x = cast(int)thing.x;
604 aid.y = cast(int)thing.y;
605 aid.dir = cast(uint)(thing.right ? 1 : 0);
606 if (thing.type&0x8000) aid.flags = aid.flags!uint|AF_NOGRAVITY;
607 //if (aid.classtype!string == "item" && aid.x!int < 64) { aid.x = 92; aid.y = aid.y!int-16; conwriteln("!!!"); }
608 adef.callInit(aid);
609 if ((aid.flags!uint&AF_NOCOLLISION) == 0) ugActorModify!true(aid);
612 // create switches
613 foreach (ref sw; map.switches) {
614 if (sw.type == 0) continue; // just in case
615 auto swname = getD2DSwitchClassName(sw.type);
616 if (swname.length == 0) {
617 conwriteln("unknown switch type ", sw.type);
618 continue;
620 if (auto adef = findActorDef("switch", swname)) {
621 auto aid = Actor.alloc;
622 aid.classtype = StrPool.intern("switch");
623 aid.classname = StrPool.intern(swname);
624 // nocollision, 'cause collision checking will be processed in switch thinker, but other actors should not touch switches
625 aid.flags = /*AF_NOCOLLISION|*/AF_NOGRAVITY|/*AF_NOONTOUCH|*/AF_NODRAW|AF_NOLIGHT|AF_NOANIMATE;
626 aid.x = sw.x*8;
627 aid.y = sw.y*8;
628 aid.switchabf = (sw.a<<16)|(sw.b<<8)|sw.flags;
629 // should be enough
630 aid.radius = 16;
631 aid.height = 16;
632 adef.callInit(aid);
633 if ((aid.flags!uint&AF_NOCOLLISION) == 0) ugActorModify!true(aid); // just in case
634 } else {
635 conwriteln("switch definition 'switch:", swname, "' not found");
639 conwriteln("initial snapshot size: ", Actor.snapshotSize, " bytes");
643 // ////////////////////////////////////////////////////////////////////////// //
644 __gshared ActorId[65536] xactorList; // aids
645 __gshared uint xactorListCount, xactorListIndex = uint.max;
648 // dacs API
649 void xactorListRewind () {
650 if (!touchListAllowed) return; // it's ok to reuse it here
651 xactorListCount = xactorListIndex = 0;
652 Actor.forEach((ActorId me) {
653 if (me.fget_classtype != StrPool.X_X) xactorList.ptr[xactorListCount++] = me;
658 // dacs API
659 ActorId xactorListNext () {
660 if (!touchListAllowed) return ActorId(0); // it's ok to reuse it here
661 if (xactorListIndex == uint.max) xactorListRewind();
662 while (xactorListIndex < xactorListCount) {
663 auto aid = xactorList.ptr[xactorListIndex];
664 ++xactorListIndex;
665 if (aid.fget_classtype == StrPool.X_X) continue;
666 return aid;
668 return ActorId(0);
672 // ////////////////////////////////////////////////////////////////////////// //
673 __gshared uint[65536] touchList; // aids
674 __gshared uint[] realTouchList;
675 __gshared uint realTouchListIndex = uint.max;
676 __gshared ActorId curThinkingActor;
677 __gshared bool touchListAllowed = false;
680 // dacs API
681 void rewindTouchList () {
682 if (!touchListAllowed) return;
683 realTouchListIndex = 0;
684 realTouchList = ugActorHitList(curThinkingActor, touchList[]);
688 // dacs API
689 ActorId getNextTouchListItem () {
690 if (!touchListAllowed) return ActorId(0);
691 if (realTouchListIndex == uint.max) rewindTouchList();
692 while (realTouchListIndex < realTouchList.length) {
693 auto aid = ActorId(realTouchList.ptr[realTouchListIndex]);
694 ++realTouchListIndex;
695 if (aid.fget_classtype == StrPool.X_X) continue;
696 if (aid.valid && (aid.fget_flags&AF_NOONTOUCH) == 0) return aid;
698 return ActorId(0);
702 public void doActorsThink () {
703 // we have too much memory!
704 __gshared uint[65536] validActorsList;
705 __gshared ActorId[65536] postponedDeath;
706 __gshared uint pdcount;
708 touchListAllowed = true;
709 scope(exit) touchListAllowed = false;
710 pdcount = 0;
711 foreach (uint xid; Actor.getValidList(validActorsList[])) {
712 auto me = ActorId(xid);
713 if (me.valid) {
714 if (me.fget_classtype == StrPool.X_X) { postponedDeath.ptr[pdcount++] = me; continue; }
715 auto flags = me.fget_flags;
716 if ((flags&AF_NOCOLLISION) == 0) ugActorModify!false(me); // remove from grid
717 if (auto adef = findActorDef(me)) {
718 if ((flags&AF_NOTHINK) == 0) {
719 realTouchListIndex = uint.max;
720 xactorListIndex = uint.max;
721 curThinkingActor = me;
722 adef.callThink(me);
723 //if (me.x!int < 32) conwriteln("actor: ", me.id, "; attLightRGBX=", me.attLightRGBX!uint);
724 if (!me.valid) continue; // we are dead
725 if (me.fget_classtype == StrPool.X_X) { postponedDeath.ptr[pdcount++] = me; continue; }
726 flags = me.fget_flags; // in case script updated flags
728 if ((flags&AF_NOANIMATE) == 0) {
729 int aidx = me.fget_zAnimidx;
730 int nidx = adef.nextAnimIdx(me.fget_zAnimstate, me.fget_dir, aidx);
731 //conwriteln("actor ", me.id, " (", me.classtype!string, me.classname!string, "): state=", me.zAnimstate!string, "; aidx=", aidx, "; nidx=", nidx);
732 me.fset_zAnimidx = nidx;
733 //assert(me.fget_zAnimidx == nidx);
735 // put back to grid
736 if ((flags&AF_NOCOLLISION) == 0) ugActorModify!true(me);
740 // process scheduled death
741 foreach (ActorId aid; postponedDeath[0..pdcount]) {
742 Actor.remove(aid);
744 // reset player keys
745 plrKeysFix(0);
746 //{ import std.stdio : stdout; stdout.writeln("========================================="); }
747 //Actor.dumpActors();