rendering thread moved to it's own module; no more gshared hacks, all communications...
[dd2d.git] / dengapi.d
blob71ae951de46ceff289168698ffa430448e32379e
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
214 // ////////////////////////////////////////////////////////////////////////// //
215 // Park-Miller-Carta Pseudo-Random Number Generator, based on David G. Carta paper
216 // 31 bit of randomness
217 // seed is previous result, as usual
218 public uint prngR31next (uint seed) {
219 if (seed == 0) seed = 0x29a;
220 uint lo = 16807*(seed&0xffff);
221 uint hi = 16807*(seed>>16);
222 lo += (hi&0x7fff)<<16;
223 lo += hi>>15;
224 //if (lo > 0x7fffffff) lo -= 0x7fffffff; // should be >=, actually
225 lo = (lo&0x7FFFFFFF)+(lo>>31); // same as previous code, but branch-less
226 return lo;
230 // "synced" random, using common seed for all players
231 public uint syncrandu31 () {
232 pragma(inline, true);
233 return (prngSyncSeed = prngR31next(prngSyncSeed));
237 // "unsynced" random, seed isn't saved
238 public uint unsyncrandu31 () {
239 pragma(inline, true);
240 return (prngSeed = prngR31next(prngSeed));
244 // ////////////////////////////////////////////////////////////////////////// //
245 version(dacs_use_vm) {
246 // this is VAT function, argument order is reversed
247 void doWrite(bool donl) (FuncPool.FuncInfo fi, DACSVM vm) {
248 import std.stdio;
249 auto count = vm.popInt();
250 while (count-- > 0) {
251 auto type = vm.popInt();
252 switch (cast(VATArgType)type) {
253 case VATArgType.Int: write(vm.popInt()); break;
254 case VATArgType.Uint: write(vm.popUint()); break;
255 case VATArgType.Float: write(vm.popFloat()); break;
256 case VATArgType.StrId: write(StrId(vm.popUint()).get); break;
257 case VATArgType.ActorId: write("<actor>"); vm.popUint(); break; //TODO
258 default: write("<invalid-type>"); vm.popUint(); break; //TODO
261 static if (donl) writeln();
262 // push dummy return value
263 vm.pushInt(0);
265 } else {
266 extern(C) void doWrite(bool donl) (uint argc, ...) {
267 import core.vararg;
268 import std.stdio;
269 mainloop: while (argc >= 2) {
270 argc -= 2;
271 int tp = va_arg!int(_argptr);
272 switch (tp) {
273 case VATArgType.Int:
274 auto v = va_arg!int(_argptr);
275 write(v);
276 break;
277 case VATArgType.Uint:
278 auto v = va_arg!uint(_argptr);
279 write(v);
280 break;
281 case VATArgType.Float:
282 auto v = va_arg!float(_argptr);
283 write(v);
284 break;
285 case VATArgType.StrId:
286 auto v = StrId(va_arg!uint(_argptr)).get;
287 write(v);
288 break;
289 case VATArgType.ActorId:
290 auto v = ActorId(va_arg!uint(_argptr));
291 write("<actor:", v.valid, ":", v.id, ">");
292 //write("<actor>");
293 break;
294 default: write("<invalid-type>"); break mainloop;
297 static if (donl) writeln();
302 // ////////////////////////////////////////////////////////////////////////// //
303 void animClearFrames (StrId classtype, StrId classname, StrId state) {
304 auto adef = findActorDef(classtype.get, classname.get);
305 if (adef is null) throw new Exception("animClearFrames: actor '"~classtype.get~":"~classname.get~"' not found!");
306 adef.clearFrames(state);
310 void animAddFrame (StrId classtype, StrId classname, StrId state, uint dir, StrId sprname) {
311 auto adef = findActorDef(classtype.get, classname.get);
312 if (adef is null) throw new Exception("animAddFrame: actor '"~classtype.get~":"~classname.get~"' not found!");
313 if (dir != 0) dir = 1; //TODO: process mirror flag here
314 adef.addFrame(state, dir, sprname);
318 // ////////////////////////////////////////////////////////////////////////// //
319 void setupDAPI () {
320 conwriteln("setting up D API");
322 FuncPool["write"] = &doWrite!false;
323 FuncPool["writeln"] = &doWrite!true;
325 FuncPool["syncrandu31"] = &syncrandu31;
327 FuncPool["animClearFrames"] = &animClearFrames;
328 FuncPool["animAddFrame"] = &animAddFrame;
330 FuncPool["actorSetAnimation"] = function void (ActorId me, StrId state) {
331 if (!me.valid) return;
332 if (auto adef = findActorDef(me.classtype!string, me.classname!string)) {
333 me.zAnimstate = state;
334 me.zAnimidx = 0;
338 //FuncPool["getPlayer"] = function ActorId () { assert(0); };
339 //FuncPool["isPlayer"] = function int (ActorId me) { return (me == players.ptr[0] ? 1 : 0); };
341 FuncPool["getPlayerCount"] = function int () => 1;
343 FuncPool["getPlayerActor"] = function ActorId (uint pnum) {
344 if (pnum == 1) return players.ptr[0];
345 return ActorId(0);
348 FuncPool["getPlayerButtons"] = function uint (uint pidx) { return (pidx == 1 ? plrKeysLast : 0); };
350 FuncPool["mapGetTypeTile"] = function int (int x, int y) {
351 return (map !is null && x >= 0 && y >= 0 && x < map.width && y < map.height ? map.tiles.ptr[LevelMap.Type].ptr[y*map.width+x] : 0);
354 FuncPool["mapGetTile"] = function int (uint layer, int x, int y) {
355 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);
358 FuncPool["mapSetTypeTile"] = function void (int x, int y, int tid) {
359 if (map is null || x < 0 || y < 0 || x >= map.width || y >= map.height || tid < 0 || tid > 255) return;
360 auto p = map.tiles.ptr[LevelMap.Type].ptr+y*map.width+x;
361 if (*p != tid) {
362 *p = cast(ubyte)tid;
363 import render : mapDirty;
364 mapDirty((1<<LevelMap.Type)|(1<<LevelMap.LightMask));
368 FuncPool["mapSetTile"] = function void (uint layer, int x, int y, int tid) {
369 if (map is null || layer < 0 || layer > 1 || x < 0 || y < 0 || x >= map.width || y >= map.height || tid < 0 || tid > 255) return;
370 auto p = map.tiles.ptr[LevelMap.Front+layer].ptr+y*map.width+x;
371 if (*p != tid) {
372 *p = cast(ubyte)tid;
373 import render : mapDirty;
374 if (layer == 0) {
375 // front
376 mapDirty((1<<LevelMap.Front)|(1<<LevelMap.AllLiquids)|(1<<LevelMap.LiquidMask));
377 } else {
378 // back
379 mapDirty((1<<LevelMap.Back)|(1<<LevelMap.AllLiquids)|(1<<LevelMap.LiquidMask));
384 FuncPool["mapGetWaterTexture"] = function int (int fg) {
385 if (fg < 0 || fg >= map.wallnames.length) return 0;
386 switch (map.wallnames.ptr[fg]) {
387 case "_water_0": return 1;
388 case "_water_1": return 2;
389 case "_water_2": return 3;
390 default:
392 return 0;
395 FuncPool["getMapViewHeight"] = function int () {
396 import render : vlWidth, vlHeight, getScale;
397 return vlHeight/getScale;
400 FuncPool["setMapViewPos"] = function void (int x, int y) {
401 import render : setMapViewPos;
402 setMapViewPos(x, y);
405 FuncPool["actorsOverlap"] = function int (ActorId a, ActorId b) { return (actorsOverlap(a, b) ? 1 : 0); };
407 FuncPool["actorRemove"] = function void (ActorId me) { Actor.remove(me); };
409 FuncPool["rewindTouchList"] = &rewindTouchList;
410 FuncPool["getNextTouchListItem"] = &getNextTouchListItem;
412 FuncPool["getCheatNoDoors"] = function int () { import render : cheatNoDoors; return (cheatNoDoors ? 1 : 0); };
414 FuncPool["addMessage"] = function void (string text, int pause, bool noreplace) {
415 import render : postAddMessage;
416 postAddMessage(text, pause, noreplace);
419 import d2dparts : dotAddBlood, dotAddSpark, dotAddWater;
421 FuncPool["dotAddBlood"] = &dotAddBlood;
422 FuncPool["dotAddSpark"] = &dotAddSpark;
423 FuncPool["dotAddWater"] = &dotAddWater;
425 function void (int x, int y, int xv, int yv, int n, int color) {
426 conwriteln("dotAddWater: x=", x, "; y=", y, "; xv=", xv, "; yv=", yv, "; n=", n, "; color=", color);
427 dotAddWater(x, y, xv, yv, n, color);
433 public void registerAPI () {
434 //Actor.actorSize += 256;
435 conwriteln("actor size is ", Actor.actorSize, " bytes");
437 version(dacs_use_vm) FuncPool.vm = new DACSVM();
438 //dvm = new DACSVM();
440 // initialize actor animation
441 ActorDef.forEach((adef) => adef.callAnimInit());
443 conwriteln("API registered");
447 // ////////////////////////////////////////////////////////////////////////// //
448 mixin(Actor.FieldGetMixin!("classtype", StrId)); // fget_classtype
449 mixin(Actor.FieldGetMixin!("classname", StrId)); // fget_classname
450 mixin(Actor.FieldGetMixin!("x", int));
451 mixin(Actor.FieldGetMixin!("y", int));
452 mixin(Actor.FieldGetMixin!("dir", uint));
453 mixin(Actor.FieldGetMixin!("height", int));
454 mixin(Actor.FieldGetMixin!("radius", int));
455 mixin(Actor.FieldGetMixin!("flags", uint));
456 mixin(Actor.FieldGetMixin!("zAnimstate", StrId));
457 mixin(Actor.FieldGetMixin!("zAnimidx", int));
459 mixin(Actor.FieldSetMixin!("zAnimidx", int));
462 public bool actorsOverlap (ActorId a, ActorId b) {
463 if (!a.valid || !b.valid) return false;
464 if (a.id == b.id) return false; // no self-overlap
466 int ax = a.fget_x;
467 int bx = b.fget_x;
468 int ar = a.fget_radius;
469 int br = b.fget_radius;
471 if (ax-ar > bx+br || ax+ar < bx-br) return false;
473 int ay = a.fget_y;
474 int by = b.fget_y;
475 int ah = a.fget_height;
476 int bh = b.fget_height;
478 //return (ay > by-bh && ay-ah < by);
479 return (by-bh <= ay && by >= ay-ah);
483 // ////////////////////////////////////////////////////////////////////////// //
484 public __gshared ActorId[2] players;
487 public void loadAllMonsterGraphics () {
488 ActorDef.forEach((adef) {
489 conwriteln("loading graphics for '", adef.classtype.get, ":", adef.classname.get, "'");
490 adef.loadGraphics();
492 //Actor.dumpActors();
493 realiseSpriteAtlases();
497 public void loadMapMonsters () {
498 assert(map !is null);
499 ugClear();
500 players[] = ActorId(0);
501 //conwriteln(players[0].valid, "; ", players[0].id);
502 foreach (ref thing; map.things) {
503 if (thing.dmonly) continue;
504 auto did = (thing.type&0x7fff) in d2dactordefsById;
505 if (did !is null && did.classtype == "playerstart") {
506 if ((thing.type&0x7fff) == 1 || (thing.type&0x7fff) == 2) {
507 int pnum = thing.type-1;
508 if (!players[pnum].valid) {
509 auto aid = Actor.alloc;
510 aid.classtype = StrPool.intern("monster");
511 aid.classname = StrPool.intern("Player");
512 aid.plrnum = cast(uint)(pnum+1);
513 aid.state = StrPool.MNST_SLEEP;
514 aid.x = cast(int)thing.x;
515 aid.y = cast(int)thing.y;
516 aid.dir = cast(uint)(thing.right ? 1 : 0);
517 if (pnum == 0) aid.flags = aid.flags!uint|AF_CAMERACHICK;
518 auto adef = findD2DActorDef(thing.type);
519 if (adef is null) assert(0);
520 adef.callInit(aid);
521 if ((aid.flags!uint&AF_NOCOLLISION) == 0) ugActorModify!true(aid);
522 players[pnum] = aid;
523 conwriteln("player #", pnum+1, " aid is ", aid.id);
526 continue;
528 auto adef = findD2DActorDef(thing.type&0x7fff);
529 if (adef is null) {
530 if (did !is null) {
531 conwriteln("ignoring D2D thing '", did.classtype.get, ":", did.classname.get, "'");
532 } else {
533 conwriteln("ignoring unknown D2D thing with mapid ", thing.type);
535 continue;
537 // create actor and initialize it
538 auto aid = Actor.alloc;
539 aid.classtype = StrPool.intern(adef.classtype);
540 aid.classname = StrPool.intern(adef.classname);
541 //conwriteln("found '", aid.classtype.get, ":", aid.classname.get, "'");
542 if (did !is null) {
543 assert(did.classtype == adef.classtype);
544 assert(did.classname == adef.classname);
545 conwriteln("mapid=", thing.type, "; ", adef.classtype, ":", adef.classname, "; id=", aid.id);
546 assert(aid.classtype!string == adef.classtype);
547 assert(aid.classname!string == adef.classname);
548 } else {
549 assert(0);
551 aid.state = StrPool.MNST_SLEEP;
552 aid.x = cast(int)thing.x;
553 aid.y = cast(int)thing.y;
554 aid.dir = cast(uint)(thing.right ? 1 : 0);
555 if (thing.type&0x8000) aid.flags = aid.flags!uint|AF_NOGRAVITY;
556 //if (aid.classtype!string == "item" && aid.x!int < 64) { aid.x = 92; aid.y = aid.y!int-16; conwriteln("!!!"); }
557 adef.callInit(aid);
558 if ((aid.flags!uint&AF_NOCOLLISION) == 0) ugActorModify!true(aid);
561 // create switches
562 foreach (ref sw; map.switches) {
563 if (sw.type == 0) continue; // just in case
564 auto swname = getD2DSwitchClassName(sw.type);
565 if (swname.length == 0) {
566 conwriteln("unknown switch type ", sw.type);
567 continue;
569 if (auto adef = findActorDef("switch", swname)) {
570 auto aid = Actor.alloc;
571 aid.classtype = StrPool.intern("switch");
572 aid.classname = StrPool.intern(swname);
573 // nocollision, 'cause collision checking will be processed in switch thinker, but other actors should not touch switches
574 aid.flags = AF_NOCOLLISION|AF_NOGRAVITY|AF_NOONTOUCH|AF_NODRAW|AF_NOLIGHT|AF_NOANIMATE;
575 aid.x = sw.x*8;
576 aid.y = sw.y*8;
577 aid.switchabf = (sw.a<<16)|(sw.b<<8)|sw.flags;
578 adef.callInit(aid);
579 if ((aid.flags!uint&AF_NOCOLLISION) == 0) ugActorModify!true(aid); // just in case
580 } else {
581 conwriteln("switch definition 'switch:", swname, "' not found");
585 conwriteln("initial snapshot size: ", Actor.snapshotSize, " bytes");
589 // ////////////////////////////////////////////////////////////////////////// //
590 __gshared uint[65536] touchList; // aids
591 __gshared uint[] realTouchList;
592 __gshared uint realTouchListIndex = uint.max;
593 __gshared ActorId curThinkingActor;
594 __gshared bool touchListAllowed = false;
597 // dacs API
598 void rewindTouchList () {
599 if (!touchListAllowed) return;
600 realTouchListIndex = 0;
601 realTouchList = ugActorHitList(curThinkingActor, touchList[]);
605 // dacs API
606 ActorId getNextTouchListItem () {
607 if (!touchListAllowed) return ActorId(0);
608 if (realTouchListIndex == uint.max) rewindTouchList();
609 while (realTouchListIndex < realTouchList.length) {
610 auto aid = ActorId(realTouchList.ptr[realTouchListIndex]);
611 ++realTouchListIndex;
612 if (aid.valid && (aid.flags!uint&AF_NOONTOUCH) == 0) return aid;
614 return ActorId(0);
618 public void doActorsThink () {
619 //FIXME
620 //static uint fflags = uint.max;
621 //if (fflags == uint.max) fflags = Actor.fields["flags"].ofs;
623 // we have too much memory!
624 __gshared uint[65536] validActorsList;
626 touchListAllowed = true;
627 scope(exit) touchListAllowed = false;
628 foreach (uint xid; Actor.getValidList(validActorsList[])) {
629 auto me = ActorId(xid);
630 if (me.valid) {
631 auto flags = me.fget_flags;
632 if ((flags&AF_NOCOLLISION) == 0) ugActorModify!false(me); // remove from grid
633 if (auto adef = findActorDef(me)) {
634 if ((flags&AF_NOTHINK) == 0) {
635 realTouchListIndex = uint.max;
636 curThinkingActor = me;
637 adef.callThink(me);
638 //if (me.x!int < 32) conwriteln("actor: ", me.id, "; attLightRGBX=", me.attLightRGBX!uint);
639 if (!me.valid) continue; // we are dead
640 flags = me.fget_flags; // in case script updated flags
642 if ((flags&AF_NOANIMATE) == 0) {
643 int aidx = me.fget_zAnimidx;
644 int nidx = adef.nextAnimIdx(me.fget_zAnimstate, me.fget_dir, aidx);
645 //conwriteln("actor ", me.id, " (", me.classtype!string, me.classname!string, "): state=", me.zAnimstate!string, "; aidx=", aidx, "; nidx=", nidx);
646 me.fset_zAnimidx = nidx;
647 //assert(me.fget_zAnimidx == nidx);
649 // put back to grid
650 if ((flags&AF_NOCOLLISION) == 0) ugActorModify!true(me);
654 plrKeysFix(0);
655 //{ import std.stdio : stdout; stdout.writeln("========================================="); }
656 //Actor.dumpActors();