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
;
38 // ////////////////////////////////////////////////////////////////////////// //
39 private extern (C
) void _d_print_throwable (Throwable t
);
42 // ////////////////////////////////////////////////////////////////////////// //
47 // ////////////////////////////////////////////////////////////////////////// //
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;
69 public void plrKeyUp (uint plidx
, uint mask
) {
70 if (mask
== 0 || plidx
> 0) return;
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 '", mod
.rmoduletype
.classtype
, ":", mod
.rmoduletype
.classname
, "'");
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
) {
116 auto res
= loadTextFile(fname
);
117 conwriteln("loading DACS module file '", fname
, "' (", res
.length
, " bytes)");
118 if (res
is null) res
= "";
120 } catch (Exception
) {}
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
;
133 string
[] parts
= ["scripts"];
135 while (mn
.length
> 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
];
147 import std
.array
: join
;
148 string path
= parts
.join("/")~".dacs";
149 res
= getTextFile(path
);
150 if (res
!is null) return res
;
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
);
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];
172 } catch (Exception e
) { return; }
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
);
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
);
192 public void scriptLoadingComplete () {
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;
215 //if (lo > 0x7fffffff) lo -= 0x7fffffff; // should be >=, actually
216 lo
= (lo
&0x7FFFFFFF)+(lo
>>31); // same as previous code, but branch-less
221 // "synced" random, using common seed for all players
222 public uint syncrandu31 () {
223 pragma(inline
, true);
224 return (prngSyncSeed
= prngR31next(prngSyncSeed
));
228 // "unsynced" random, seed isn't saved
229 public uint unsyncrandu31 () {
230 pragma(inline
, true);
231 return (prngSeed
= prngR31next(prngSeed
));
235 // ////////////////////////////////////////////////////////////////////////// //
236 version(dacs_use_vm
) {
237 // this is VAT function, argument order is reversed
238 void doWrite(bool donl
) (FuncPool
.FuncInfo fi
, DACSVM vm
) {
240 auto count
= vm
.popInt();
241 while (count
-- > 0) {
242 auto type
= vm
.popInt();
243 switch (cast(VATArgType
)type
) {
244 case VATArgType
.Int
: write(vm
.popInt()); break;
245 case VATArgType
.Uint
: write(vm
.popUint()); break;
246 case VATArgType
.Float
: write(vm
.popFloat()); break;
247 case VATArgType
.StrId
: write(StrId(vm
.popUint()).get
); break;
248 case VATArgType
.ActorId
: write("<actor>"); vm
.popUint(); break; //TODO
249 default: write("<invalid-type>"); vm
.popUint(); break; //TODO
252 static if (donl
) writeln();
253 // push dummy return value
257 extern(C
) void doWrite(bool donl
) (uint argc
, ...) {
260 mainloop
: while (argc
>= 2) {
262 int tp
= va_arg
!int(_argptr
);
265 auto v
= va_arg
!int(_argptr
);
268 case VATArgType
.Uint
:
269 auto v
= va_arg
!uint(_argptr
);
272 case VATArgType
.Float
:
273 auto v
= va_arg
!float(_argptr
);
276 case VATArgType
.StrId
:
277 auto v
= StrId(va_arg
!uint(_argptr
)).get
;
280 case VATArgType
.ActorId
:
281 auto v
= ActorId(va_arg
!uint(_argptr
));
282 write("<actor:", v
.valid
, ":", v
.id
, ">");
285 default: write("<invalid-type>"); break mainloop
;
288 static if (donl
) writeln();
293 // ////////////////////////////////////////////////////////////////////////// //
294 void animClearFrames (StrId classtype
, StrId classname
, StrId state
) {
295 auto adef
= findActorDef(classtype
.get
, classname
.get
);
296 if (adef
is null) throw new Exception("animClearFrames: actor '"~classtype
.get
~":"~classname
.get
~"' not found!");
297 adef
.clearFrames(state
);
301 void animAddFrame (StrId classtype
, StrId classname
, StrId state
, uint dir
, StrId sprname
) {
302 auto adef
= findActorDef(classtype
.get
, classname
.get
);
303 if (adef
is null) throw new Exception("animAddFrame: actor '"~classtype
.get
~":"~classname
.get
~"' not found!");
304 if (dir
!= 0) dir
= 1; //TODO: process mirror flag here
305 adef
.addFrame(state
, dir
, sprname
);
309 // ////////////////////////////////////////////////////////////////////////// //
311 conwriteln("setting up D API");
313 FuncPool
["write"] = &doWrite
!false;
314 FuncPool
["writeln"] = &doWrite
!true;
316 FuncPool
["syncrandu31"] = &syncrandu31
;
318 FuncPool
["animClearFrames"] = &animClearFrames
;
319 FuncPool
["animAddFrame"] = &animAddFrame
;
321 FuncPool
["actorSetAnimation"] = function void (ActorId me
, StrId state
) {
322 if (!me
.valid
) return;
323 if (auto adef
= findActorDef(me
.classtype
!string
, me
.classname
!string
)) {
324 me
.zAnimstate
= state
;
329 //FuncPool["getPlayer"] = function ActorId () { assert(0); };
330 //FuncPool["isPlayer"] = function int (ActorId me) { return (me == players.ptr[0] ? 1 : 0); };
332 FuncPool
["getPlayerCount"] = function int () => 1;
334 FuncPool
["getPlayerActor"] = function ActorId (uint pnum
) {
335 if (pnum
== 1) return players
.ptr
[0];
339 FuncPool
["getPlayerButtons"] = function uint (uint pidx
) { return (pidx
== 1 ? plrKeysLast
: 0); };
341 FuncPool
["mapGetTypeTile"] = function int (int x
, int y
) {
342 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);
345 FuncPool
["mapGetTile"] = function int (uint layer
, int x
, int y
) {
346 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);
349 FuncPool
["mapSetTypeTile"] = function void (int x
, int y
, int tid
) {
350 if (map
is null || x
< 0 || y
< 0 || x
>= map
.width || y
>= map
.height || tid
< 0 || tid
> 255) return;
351 auto p
= map
.tiles
.ptr
[LevelMap
.Type
].ptr
+y
*map
.width
+x
;
354 import xmain_d2d
: mapDirty
;
355 mapDirty((1<<LevelMap
.Type
)|
(1<<LevelMap
.LightMask
));
359 FuncPool
["mapSetTile"] = function void (uint layer
, int x
, int y
, int tid
) {
360 if (map
is null || layer
< 0 || layer
> 1 || x
< 0 || y
< 0 || x
>= map
.width || y
>= map
.height || tid
< 0 || tid
> 255) return;
361 auto p
= map
.tiles
.ptr
[LevelMap
.Front
+layer
].ptr
+y
*map
.width
+x
;
364 import xmain_d2d
: mapDirty
;
367 mapDirty((1<<LevelMap
.Front
)|
(1<<LevelMap
.AllLiquids
)|
(1<<LevelMap
.LiquidMask
));
370 mapDirty((1<<LevelMap
.Back
)|
(1<<LevelMap
.AllLiquids
)|
(1<<LevelMap
.LiquidMask
));
375 FuncPool
["mapGetWaterTexture"] = function int (int fg
) {
376 if (fg
< 0 || fg
>= map
.wallnames
.length
) return 0;
377 switch (map
.wallnames
.ptr
[fg
]) {
378 case "_water_0": return 1;
379 case "_water_1": return 2;
380 case "_water_2": return 3;
386 FuncPool
["getMapViewHeight"] = function int () {
387 import xmain_d2d
: vlWidth
, vlHeight
, scale
;
388 return vlHeight
/scale
;
391 FuncPool
["setMapViewPos"] = function void (int x
, int y
) {
392 import xmain_d2d
: setMapViewPos
;
396 FuncPool
["actorsOverlap"] = function int (ActorId a
, ActorId b
) { return (actorsOverlap(a
, b
) ?
1 : 0); };
398 FuncPool
["actorRemove"] = function void (ActorId me
) { Actor
.remove(me
); };
400 FuncPool
["rewindTouchList"] = &rewindTouchList
;
401 FuncPool
["getNextTouchListItem"] = &getNextTouchListItem
;
403 FuncPool
["getCheatNoDoors"] = function int () { import xmain_d2d
: cheatNoDoors
; return (cheatNoDoors ?
1 : 0); };
405 FuncPool
["addMessage"] = function void (string text
, int pause
, bool noreplace
) {
406 import xmain_d2d
: addMessage
;
407 addMessage(text
, pause
, noreplace
);
410 import d2dparts
: dotAddBlood
, dotAddSpark
, dotAddWater
;
412 FuncPool
["dotAddBlood"] = &dotAddBlood
;
413 FuncPool
["dotAddSpark"] = &dotAddSpark
;
414 FuncPool
["dotAddWater"] = &dotAddWater
;
416 function void (int x, int y, int xv, int yv, int n, int color) {
417 conwriteln("dotAddWater: x=", x, "; y=", y, "; xv=", xv, "; yv=", yv, "; n=", n, "; color=", color);
418 dotAddWater(x, y, xv, yv, n, color);
424 public void registerAPI () {
425 //Actor.actorSize += 256;
426 conwriteln("actor size is ", Actor
.actorSize
, " bytes");
428 version(dacs_use_vm
) FuncPool
.vm
= new DACSVM();
429 //dvm = new DACSVM();
431 // initialize actor animation
432 ActorDef
.forEach((adef
) => adef
.callAnimInit());
434 conwriteln("API registered");
438 // ////////////////////////////////////////////////////////////////////////// //
439 mixin(Actor
.FieldGetMixin
!("classtype", StrId
)); // fget_classtype
440 mixin(Actor
.FieldGetMixin
!("classname", StrId
)); // fget_classname
441 mixin(Actor
.FieldGetMixin
!("x", int));
442 mixin(Actor
.FieldGetMixin
!("y", int));
443 mixin(Actor
.FieldGetMixin
!("dir", uint));
444 mixin(Actor
.FieldGetMixin
!("height", int));
445 mixin(Actor
.FieldGetMixin
!("radius", int));
446 mixin(Actor
.FieldGetMixin
!("flags", uint));
447 mixin(Actor
.FieldGetMixin
!("zAnimstate", StrId
));
448 mixin(Actor
.FieldGetMixin
!("zAnimidx", int));
450 mixin(Actor
.FieldSetMixin
!("zAnimidx", int));
453 public bool actorsOverlap (ActorId a
, ActorId b
) {
454 if (!a
.valid ||
!b
.valid
) return false;
455 if (a
.id
== b
.id
) return false; // no self-overlap
459 int ar
= a
.fget_radius
;
460 int br
= b
.fget_radius
;
462 if (ax
-ar
> bx
+br || ax
+ar
< bx
-br
) return false;
466 int ah
= a
.fget_height
;
467 int bh
= b
.fget_height
;
469 //return (ay > by-bh && ay-ah < by);
470 return (by
-bh
<= ay
&& by
>= ay
-ah
);
474 // ////////////////////////////////////////////////////////////////////////// //
475 __gshared ActorId
[2] players
;
477 public void loadMapMonsters () {
478 assert(map
!is null);
480 players
[] = ActorId(0);
481 //conwriteln(players[0].valid, "; ", players[0].id);
482 foreach (ref thing
; map
.things
) {
483 if (thing
.dmonly
) continue;
484 if (auto did
= thing
.type
in d2dactordefsById
) {
485 //if (did.classtype == "item") continue;
486 if (did
.classtype
== "playerstart") {
487 if (thing
.type
== 1 || thing
.type
== 2) {
488 int pnum
= thing
.type
-1;
489 if (!players
[pnum
].valid
) {
490 auto aid
= Actor
.alloc
;
491 aid
.classtype
= StrPool
.intern("monster");
492 aid
.classname
= StrPool
.intern("Player");
493 aid
.plrnum
= cast(uint)(pnum
+1);
494 aid
.state
= StrPool
.MNST_SLEEP
;
495 aid
.x
= cast(int)thing
.x
;
496 aid
.y
= cast(int)thing
.y
;
497 aid
.dir
= cast(uint)(thing
.right ?
1 : 0);
498 auto adef
= findD2DActorDef(thing
.type
);
499 if (adef
is null) assert(0);
501 if ((aid
.flags
!uint&AF_NOCOLLISION
) == 0) ugActorModify
!true(aid
);
503 conwriteln("player #", pnum
+1, " aid is ", aid
.id
);
507 import xmain_d2d : setMapViewPos;
508 setMapViewPos(aid.x!int, aid.y!int);
516 auto adef
= findD2DActorDef(thing
.type
);
518 if (auto did
= thing
.type
in d2dactordefsById
) {
519 conwriteln("ignoring D2D thing '", did
.classtype
.get
, ":", did
.classname
.get
, "'");
521 conwriteln("ignoring unknown D2D thing with mapid ", thing
.type
);
525 // load graphics (we'll load all graphics)
526 //adef.loadGraphics();
527 // create actor and initialize it
528 auto aid
= Actor
.alloc
;
529 aid
.classtype
= StrPool
.intern(adef
.classtype
);
530 aid
.classname
= StrPool
.intern(adef
.classname
);
531 //conwriteln("found '", aid.classtype.get, ":", aid.classname.get, "'");
532 if (auto did
= thing
.type
in d2dactordefsById
) {
533 assert(did
.classtype
== adef
.classtype
);
534 assert(did
.classname
== adef
.classname
);
535 conwriteln("mapid=", thing
.type
, "; ", adef
.classtype
, ":", adef
.classname
, "; id=", aid
.id
);
536 assert(aid
.classtype
!string
== adef
.classtype
);
537 assert(aid
.classname
!string
== adef
.classname
);
541 aid
.state
= StrPool
.MNST_SLEEP
;
542 aid
.x
= cast(int)thing
.x
;
543 aid
.y
= cast(int)thing
.y
;
544 aid
.dir
= cast(uint)(thing
.right ?
1 : 0);
545 //if (aid.classtype!string == "item" && aid.x!int < 64) { aid.x = 92; aid.y = aid.y!int-16; conwriteln("!!!"); }
547 if ((aid
.flags
!uint&AF_NOCOLLISION
) == 0) ugActorModify
!true(aid
);
551 foreach (ref sw
; map
.switches
) {
552 if (sw
.type
== 0) continue; // just in case
553 auto swname
= getD2DSwitchClassName(sw
.type
);
554 if (swname
.length
== 0) {
555 conwriteln("unknown switch type ", sw
.type
);
558 if (auto adef
= findActorDef("switch", swname
)) {
559 auto aid
= Actor
.alloc
;
560 aid
.classtype
= StrPool
.intern("switch");
561 aid
.classname
= StrPool
.intern(swname
);
562 // nocollision, 'cause collision checking will be processed in switch thinker, but other actors should not touch switches
563 aid
.flags
= AF_NOCOLLISION|AF_NOGRAVITY|AF_NOONTOUCH|AF_NODRAW|AF_NOLIGHT|AF_NOANIMATE
;
566 aid
.switchabf
= (sw
.a
<<16)|
(sw
.b
<<8)|sw
.flags
;
568 if ((aid
.flags
!uint&AF_NOCOLLISION
) == 0) ugActorModify
!true(aid
); // just in case
570 conwriteln("switch definition 'switch:", swname
, "' not found");
574 ActorDef
.forEach((adef
) {
575 conwriteln("loading graphics for '", adef
.classtype
.get
, ":", adef
.classname
.get
, "'");
578 //Actor.dumpActors();
580 conwriteln("initial snapshot size: ", Actor
.snapshotSize
, " bytes");
584 // ////////////////////////////////////////////////////////////////////////// //
585 __gshared
uint[65536] touchList
; // aids
586 __gshared
uint[] realTouchList
;
587 __gshared
uint realTouchListIndex
= uint.max
;
588 __gshared ActorId curThinkingActor
;
589 __gshared
bool touchListAllowed
= false;
593 void rewindTouchList () {
594 if (!touchListAllowed
) return;
595 realTouchListIndex
= 0;
596 realTouchList
= ugActorHitList(curThinkingActor
, touchList
[]);
601 ActorId
getNextTouchListItem () {
602 if (!touchListAllowed
) return ActorId(0);
603 if (realTouchListIndex
== uint.max
) rewindTouchList();
604 while (realTouchListIndex
< realTouchList
.length
) {
605 auto aid
= ActorId(realTouchList
.ptr
[realTouchListIndex
]);
606 ++realTouchListIndex
;
607 if (aid
.valid
&& (aid
.flags
!uint&AF_NOONTOUCH
) == 0) return aid
;
613 public void doActorsThink () {
615 //static uint fflags = uint.max;
616 //if (fflags == uint.max) fflags = Actor.fields["flags"].ofs;
618 // we have too much memory!
619 __gshared
uint[65536] validActorsList
;
621 touchListAllowed
= true;
622 scope(exit
) touchListAllowed
= false;
623 foreach (uint xid
; Actor
.getValidList(validActorsList
[])) {
624 auto me
= ActorId(xid
);
626 auto flags
= me
.fget_flags
;
627 if ((flags
&AF_NOCOLLISION
) == 0) ugActorModify
!false(me
); // remove from grid
628 if (auto adef
= findActorDef(me
)) {
629 if ((flags
&AF_NOTHINK
) == 0) {
630 realTouchListIndex
= uint.max
;
631 curThinkingActor
= me
;
633 //if (me.x!int < 32) conwriteln("actor: ", me.id, "; attLightRGBX=", me.attLightRGBX!uint);
634 if (!me
.valid
) continue; // we are dead
635 flags
= me
.fget_flags
; // in case script updated flags
637 if ((flags
&AF_NOANIMATE
) == 0) {
638 int aidx
= me
.fget_zAnimidx
;
639 int nidx
= adef
.nextAnimIdx(me
.fget_zAnimstate
, me
.fget_dir
, aidx
);
640 //conwriteln("actor ", me.id, " (", me.classtype!string, me.classname!string, "): state=", me.zAnimstate!string, "; aidx=", aidx, "; nidx=", nidx);
641 me
.fset_zAnimidx
= nidx
;
642 //assert(me.fget_zAnimidx == nidx);
645 if ((flags
&AF_NOCOLLISION
) == 0) ugActorModify
!true(me
);
650 //{ import std.stdio : stdout; stdout.writeln("========================================="); }
651 //Actor.dumpActors();