"no doors" cheat
[dd2d.git] / dengapi.d
blob866b9d11c7ecf434157af4f775eaa84eee4dfd7a
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;
38 // ////////////////////////////////////////////////////////////////////////// //
39 private extern (C) void _d_print_throwable (Throwable t);
42 // ////////////////////////////////////////////////////////////////////////// //
43 import arsd.color;
44 import arsd.png;
47 // ////////////////////////////////////////////////////////////////////////// //
48 public {
49 enum PLK_UP = (1<<0);
50 enum PLK_DOWN = (1<<1);
51 enum PLK_LEFT = (1<<2);
52 enum PLK_RIGHT = (1<<3);
53 enum PLK_FIRE = (1<<4);
54 enum PLK_JUMP = (1<<5);
55 enum PLK_USE = (1<<6);
58 __gshared uint plrKeysLast;
59 __gshared uint plrKeyState;
62 public void plrKeyDown (uint plidx, uint mask) {
63 if (mask == 0 || plidx > 0) return;
64 plrKeysLast |= mask;
65 plrKeyState |= mask;
69 public void plrKeyUp (uint plidx, uint mask) {
70 if (mask == 0 || plidx > 0) return;
71 plrKeyState &= ~mask;
75 public void plrKeyUpDown (uint plidx, uint mask, bool down) {
76 if (down) plrKeyDown(plidx, mask); else plrKeyUp(plidx, mask);
80 void plrKeysFix (uint plidx) {
81 if (plidx > 0) return;
82 //conwritefln!"plrKeyState=0x%02x; plrKeysLast=0x%02x"(plrKeyState, plrKeysLast);
83 foreach (uint n; 0..12) {
84 if ((plrKeyState&(1<<n)) == 0) plrKeysLast &= ~(1<<n);
89 // ////////////////////////////////////////////////////////////////////////// //
90 void updateActorClasses (int iteration) {
91 foreach (string ctype; dacsClassTypes(iteration)) {
92 foreach (auto mod; dacsClassModules(ctype, iteration)) {
93 auto adef = registerActorDef(mod.rmoduletype.classtype, mod.rmoduletype.classname);
94 conwriteln("found info for actor '", adef.fullname, "'");
96 auto animInitFn = FuncPool.findByFQMG(mod.name~".initializeAnim", ":void");
97 if (animInitFn !is null) adef.setAnimInitFunc(animInitFn);
100 auto initFn = FuncPool.findByFQMG(mod.name~".initialize", ":void:Actor");
101 if (initFn !is null) adef.setInitFunc(initFn);
104 auto thinkFn = FuncPool.findByFQMG(mod.name~".think", ":void:Actor");
105 if (thinkFn !is null) adef.setThinkFunc(thinkFn);
112 // ////////////////////////////////////////////////////////////////////////// //
113 private string modLoader (string modname) {
114 static string getTextFile (string fname) {
115 try {
116 auto res = loadTextFile(fname);
117 conwriteln("loading DACS module file '", fname, "' (", res.length, " bytes)");
118 if (res is null) res = "";
119 return res;
120 } catch (Exception) {}
121 return null;
124 string res;
126 //res = getTextFile(modname~".dacs");
127 //if (res !is null) return res;
129 res = getTextFile("scripts/"~modname~".dacs");
130 if (res !is null) return res;
131 assert(res is null);
133 string[] parts = ["scripts"];
134 string mn = modname;
135 while (mn.length > 0) {
136 int pos = 0;
137 if (mn[0] >= 'A' && mn[0] <= 'Z') ++pos;
138 while (pos < mn.length && (mn[pos] < 'A' || mn[pos] > 'Z')) ++pos;
139 if (mn[0] >= 'A' && mn[0] <= 'Z') {
140 parts ~= cast(char)(mn[0]+32)~mn[1..pos];
141 } else {
142 parts ~= mn[0..pos];
144 mn = mn[pos..$];
147 import std.array : join;
148 string path = parts.join("/")~".dacs";
149 res = getTextFile(path);
150 if (res !is null) return res;
151 assert(res is null);
153 assert(res is null);
154 throw new Exception("module '"~modname~"' not found");
158 __gshared int modIteration = 0;
160 public void loadWadScripts () {
161 if (moduleLoader is null) moduleLoader = (string modname) => modLoader(modname);
162 string[] mainmods;
163 try {
164 import std.array : split;
165 auto t = loadTextFile("scripts/dacsmain.txt");
166 foreach (string line; t.split('\n')) {
167 while (line.length && line[0] <= ' ') line = line[1..$];
168 if (line.length == 0 || line[0] == ';') continue;
169 while (line.length && line[$-1] <= ' ') line = line[0..$-1];
170 mainmods ~= line;
172 } catch (Exception e) { return; }
173 try {
174 //conwriteln("loading main DACS module '", mainmod, "'");
175 auto iter = modIteration++;
176 conwriteln("iteration=", iter);
177 foreach (string mod; mainmods) {
178 parseModule(mod, iter);
180 conwriteln("calling parseComplete; iteration=", iter);
181 parseComplete();
182 updateActorClasses(iter);
183 } catch (CompilerException e) {
184 _d_print_throwable(e);
185 conwriteln("PARSE ERROR: ", e.file, " at ", e.line);
186 conwriteln(e.toString);
187 assert(0);
192 public void scriptLoadingComplete () {
193 setupDAPI();
194 dacsFinalizeCompiler();
198 // ////////////////////////////////////////////////////////////////////////// //
199 public __gshared LevelMap map;
200 //public __gshared DACSVM dvm;
201 public __gshared uint prngSyncSeed = 0x29a;
202 public __gshared uint prngSeed = 0x29a; // unsynced
205 // ////////////////////////////////////////////////////////////////////////// //
206 // Park-Miller-Carta Pseudo-Random Number Generator, based on David G. Carta paper
207 // 31 bit of randomness
208 // seed is previous result, as usual
209 public uint prngR31next (uint seed) {
210 if (seed == 0) seed = 0x29a;
211 uint lo = 16807*(seed&0xffff);
212 uint hi = 16807*(seed>>16);
213 lo += (hi&0x7fff)<<16;
214 lo += hi>>15;
215 //if (lo > 0x7fffffff) lo -= 0x7fffffff; // should be >=, actually
216 lo = (lo&0x7FFFFFFF)+(lo>>31); // same as previous code, but branch-less
217 return lo;
221 // "synced" random, using common seed for all players
222 public uint syncrandu31 () {
223 pragma(inline, true);
224 return (prngSyncSeed = prngR31next(prngSyncSeed));
228 // ////////////////////////////////////////////////////////////////////////// //
229 version(dacs_use_vm) {
230 // this is VAT function, argument order is reversed
231 void doWrite(bool donl) (FuncPool.FuncInfo fi, DACSVM vm) {
232 import std.stdio;
233 auto count = vm.popInt();
234 while (count-- > 0) {
235 auto type = vm.popInt();
236 switch (cast(VATArgType)type) {
237 case VATArgType.Int: write(vm.popInt()); break;
238 case VATArgType.Uint: write(vm.popUint()); break;
239 case VATArgType.Float: write(vm.popFloat()); break;
240 case VATArgType.StrId: write(StrId(vm.popUint()).get); break;
241 case VATArgType.ActorId: write("<actor>"); vm.popUint(); break; //TODO
242 default: write("<invalid-type>"); vm.popUint(); break; //TODO
245 static if (donl) writeln();
246 // push dummy return value
247 vm.pushInt(0);
249 } else {
250 extern(C) void doWrite(bool donl) (uint argc, ...) {
251 import core.vararg;
252 import std.stdio;
253 mainloop: while (argc >= 2) {
254 argc -= 2;
255 int tp = va_arg!int(_argptr);
256 switch (tp) {
257 case VATArgType.Int:
258 auto v = va_arg!int(_argptr);
259 write(v);
260 break;
261 case VATArgType.Uint:
262 auto v = va_arg!uint(_argptr);
263 write(v);
264 break;
265 case VATArgType.Float:
266 auto v = va_arg!float(_argptr);
267 write(v);
268 break;
269 case VATArgType.StrId:
270 auto v = StrId(va_arg!uint(_argptr)).get;
271 write(v);
272 break;
273 case VATArgType.ActorId:
274 auto v = ActorId(va_arg!uint(_argptr));
275 write("<actor:", v.valid, ":", v.id, ">");
276 //write("<actor>");
277 break;
278 default: write("<invalid-type>"); break mainloop;
281 static if (donl) writeln();
286 // ////////////////////////////////////////////////////////////////////////// //
287 void animClearFrames (StrId classtype, StrId classname, StrId state) {
288 auto adef = findActorDef(classtype.get, classname.get);
289 if (adef is null) throw new Exception("animClearFrames: actor '"~classtype.get~":"~classname.get~"' not found!");
290 adef.clearFrames(state);
294 void animAddFrame (StrId classtype, StrId classname, StrId state, uint dir, StrId sprname) {
295 auto adef = findActorDef(classtype.get, classname.get);
296 if (adef is null) throw new Exception("animAddFrame: actor '"~classtype.get~":"~classname.get~"' not found!");
297 if (dir != 0) dir = 1; //TODO: process mirror flag here
298 adef.addFrame(state, dir, sprname);
302 // ////////////////////////////////////////////////////////////////////////// //
303 void setupDAPI () {
304 conwriteln("setting up D API");
306 FuncPool["write"] = &doWrite!false;
307 FuncPool["writeln"] = &doWrite!true;
309 FuncPool["syncrandu31"] = &syncrandu31;
311 FuncPool["animClearFrames"] = &animClearFrames;
312 FuncPool["animAddFrame"] = &animAddFrame;
314 FuncPool["actorSetAnimation"] = function void (ActorId me, StrId state) {
315 if (!me.valid) return;
316 if (auto adef = findActorDef(me.classtype!string, me.classname!string)) {
317 me.zAnimstate = state;
318 me.zAnimidx = 0;
322 //FuncPool["getPlayer"] = function ActorId () { assert(0); };
324 FuncPool["isPlayer"] = function int (ActorId me) { return (me == players.ptr[0] ? 1 : 0); };
326 FuncPool["getPlayerButtons"] = function uint (uint pidx) { return (pidx == 0 ? plrKeysLast : 0); };
328 FuncPool["mapGetTypeTile"] = function int (int x, int y) {
329 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);
332 FuncPool["mapGetTile"] = function int (uint layer, int x, int y) {
333 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);
336 FuncPool["getMapViewHeight"] = function int () {
337 import xmain_d2d : vlWidth, vlHeight, scale;
338 return vlHeight/scale;
341 FuncPool["setMapViewPos"] = function void (int x, int y) {
342 import xmain_d2d : setMapViewPos;
343 setMapViewPos(x, y);
346 FuncPool["actorsOverlap"] = function int (ActorId a, ActorId b) { return (actorsOverlap(a, b) ? 1 : 0); };
348 FuncPool["actorRemove"] = function void (ActorId me) { Actor.remove(me); };
350 FuncPool["rewindTouchList"] = &rewindTouchList;
351 FuncPool["getNextTouchListItem"] = &getNextTouchListItem;
353 FuncPool["getCheatNoDoors"] = function int () { import xmain_d2d : cheatNoDoors; return (cheatNoDoors ? 1 : 0); };
357 public void registerAPI () {
358 //Actor.actorSize += 256;
359 conwriteln("actor size is ", Actor.actorSize, " bytes");
361 version(dacs_use_vm) FuncPool.vm = new DACSVM();
362 //dvm = new DACSVM();
364 // initialize actor animation
365 foreach (ActorDef adef; actordefs.byValue) {
366 adef.callAnimInit();
369 conwriteln("API registered");
373 // ////////////////////////////////////////////////////////////////////////// //
374 mixin(Actor.FieldGetMixin!("classtype", StrId)); // fget_classtype
375 mixin(Actor.FieldGetMixin!("classname", StrId)); // fget_classname
376 mixin(Actor.FieldGetMixin!("x", int));
377 mixin(Actor.FieldGetMixin!("y", int));
378 mixin(Actor.FieldGetMixin!("dir", uint));
379 mixin(Actor.FieldGetMixin!("height", int));
380 mixin(Actor.FieldGetMixin!("radius", int));
381 mixin(Actor.FieldGetMixin!("flags", uint));
382 mixin(Actor.FieldGetMixin!("zAnimstate", StrId));
383 mixin(Actor.FieldGetMixin!("zAnimidx", int));
385 mixin(Actor.FieldSetMixin!("zAnimidx", int));
388 public bool actorsOverlap (ActorId a, ActorId b) {
389 if (!a.valid || !b.valid) return false;
390 if (a.id == b.id) return false; // no self-overlap
392 int ax = a.fget_x;
393 int bx = b.fget_x;
394 int ar = a.fget_radius;
395 int br = b.fget_radius;
397 if (ax-ar > bx+br || ax+ar < bx-br) return false;
399 int ay = a.fget_y;
400 int by = b.fget_y;
401 int ah = a.fget_height;
402 int bh = b.fget_height;
404 //return (ay > by-bh && ay-ah < by);
405 return (by-bh <= ay && by >= ay-ah);
409 // ////////////////////////////////////////////////////////////////////////// //
410 __gshared ActorId[2] players;
412 public void loadMapMonsters () {
413 assert(map !is null);
414 ugClear();
415 players[] = ActorId(0);
416 //conwriteln(players[0].valid, "; ", players[0].id);
417 foreach (ref thing; map.things) {
418 if (thing.dmonly) continue;
419 if (auto did = thing.type in d2dactordefsById) {
420 //if (did.classtype == "item") continue;
421 if (did.classtype == "playerstart") {
422 if (thing.type == 1 || thing.type == 2) {
423 int pnum = thing.type-1;
424 if (!players[pnum].valid) {
425 auto aid = Actor.alloc;
426 aid.classtype = StrPool.intern("monster");
427 aid.classname = StrPool.intern("Player");
428 aid.plrnum = cast(uint)pnum;
429 aid.state = StrPool.MNST_SLEEP;
430 aid.x = cast(int)thing.x;
431 aid.y = cast(int)thing.y;
432 aid.dir = cast(uint)(thing.right ? 1 : 0);
433 auto adef = findD2DActorDef(thing.type);
434 if (adef is null) assert(0);
435 adef.callInit(aid);
436 if ((aid.flags!uint&AF_NOCOLLISION) == 0) ugActorModify!true(aid);
437 players[pnum] = aid;
438 conwriteln("player #", pnum+1, " aid is ", aid.id);
439 //FIXME: HACK!
441 if (pnum == 0) {
442 import xmain_d2d : setMapViewPos;
443 setMapViewPos(aid.x!int, aid.y!int);
448 continue;
451 auto adef = findD2DActorDef(thing.type);
452 if (adef is null) {
453 if (auto did = thing.type in d2dactordefsById) {
454 conwriteln("ignoring D2D thing '", did.fullname, "'");
455 } else {
456 conwriteln("ignoring unknown D2D thing with mapid ", thing.type);
458 continue;
460 // load graphics (we'll load all graphics)
461 //adef.loadGraphics();
462 // create actor and initialize it
463 auto aid = Actor.alloc;
464 aid.classtype = StrPool.intern(adef.classtype);
465 aid.classname = StrPool.intern(adef.classname);
466 //conwriteln("found '", aid.classtype.get, ":", aid.classname.get, "'");
467 if (auto did = thing.type in d2dactordefsById) {
468 assert(did.classtype == adef.classtype);
469 assert(did.classname == adef.classname);
470 conwriteln("mapid=", thing.type, "; ", adef.classtype, ":", adef.classname, "; id=", aid.id);
471 assert(aid.classtype!string == adef.classtype);
472 assert(aid.classname!string == adef.classname);
473 } else {
474 assert(0);
476 aid.state = StrPool.MNST_SLEEP;
477 aid.x = cast(int)thing.x;
478 aid.y = cast(int)thing.y;
479 aid.dir = cast(uint)(thing.right ? 1 : 0);
480 //if (aid.x!int < 64) { aid.x = 64; conwriteln("!!!"); }
481 adef.callInit(aid);
482 if ((aid.flags!uint&AF_NOCOLLISION) == 0) ugActorModify!true(aid);
485 foreach (ActorDef adef; actordefs.byValue) {
486 conwriteln("loading graphics for '", adef.fullname, "'");
487 adef.loadGraphics();
489 //Actor.dumpActors();
491 conwriteln("initial snapshot size: ", Actor.snapshotSize, " bytes");
495 // ////////////////////////////////////////////////////////////////////////// //
496 __gshared uint[65536] touchList; // aids
497 __gshared uint[] realTouchList;
498 __gshared uint realTouchListIndex = uint.max;
499 __gshared ActorId curThinkingActor;
500 __gshared bool touchListAllowed = false;
503 // dacs API
504 void rewindTouchList () {
505 if (!touchListAllowed) return;
506 realTouchListIndex = 0;
507 realTouchList = ugActorHitList(curThinkingActor, touchList[]);
511 // dacs API
512 ActorId getNextTouchListItem () {
513 if (!touchListAllowed) return ActorId(0);
514 if (realTouchListIndex == uint.max) rewindTouchList();
515 while (realTouchListIndex < realTouchList.length) {
516 auto aid = ActorId(realTouchList.ptr[realTouchListIndex]);
517 ++realTouchListIndex;
518 if (aid.valid && (aid.flags!uint&AF_NOONTOUCH) == 0) return aid;
520 return ActorId(0);
524 public void doActorsThink () {
525 //FIXME
526 //static uint fflags = uint.max;
527 //if (fflags == uint.max) fflags = Actor.fields["flags"].ofs;
529 // we have too much memory!
530 __gshared uint[65536] validActorsList;
532 touchListAllowed = true;
533 scope(exit) touchListAllowed = false;
534 foreach (uint xid; Actor.getValidList(validActorsList[])) {
535 auto me = ActorId(xid);
536 if (me.valid) {
537 auto flags = me.fget_flags;
538 if ((flags&AF_NOCOLLISION) == 0) ugActorModify!false(me); // remove from grid
539 if (auto adef = findActorDef(me)) {
540 if ((flags&AF_NOTHINK) == 0) {
541 realTouchListIndex = uint.max;
542 curThinkingActor = me;
543 adef.callThink(me);
544 //if (me.x!int < 32) conwriteln("actor: ", me.id, "; attLightRGBX=", me.attLightRGBX!uint);
545 if (!me.valid) continue; // we are dead
546 flags = me.fget_flags; // in case script updated flags
548 if ((flags&AF_NOANIMATE) == 0) {
549 int aidx = me.fget_zAnimidx;
550 int nidx = adef.nextAnimIdx(me.fget_zAnimstate, me.fget_dir, aidx);
551 //conwriteln("actor ", me.id, " (", me.classtype!string, me.classname!string, "): state=", me.zAnimstate!string, "; aidx=", aidx, "; nidx=", nidx);
552 me.fset_zAnimidx = nidx;
553 //assert(me.fget_zAnimidx == nidx);
555 // put back to grid
556 if ((flags&AF_NOCOLLISION) == 0) ugActorModify!true(me);
560 plrKeysFix(0);
561 //{ import std.stdio : stdout; stdout.writeln("========================================="); }
562 //Actor.dumpActors();