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
;
39 // ////////////////////////////////////////////////////////////////////////// //
40 private extern (C
) void _d_print_throwable (Throwable t
);
43 // ////////////////////////////////////////////////////////////////////////// //
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 // ////////////////////////////////////////////////////////////////////////// //
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;
82 public void plrKeyUp (uint plidx
, uint mask
) {
83 if (mask
== 0 || plidx
> 0) return;
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 if (mod
.rmoduletype
.classtype
== "map") {
107 // map scripts, not here
109 auto adef
= registerActorDef(mod
.rmoduletype
.classtype
, mod
.rmoduletype
.classname
);
110 conwriteln("found info for actor '", mod
.rmoduletype
.classtype
, ":", mod
.rmoduletype
.classname
, "'");
112 auto animInitFn
= FuncPool
.findByFQMG(mod
.name
~".initializeAnim", ":void");
113 if (animInitFn
!is null) adef
.setAnimInitFunc(animInitFn
);
116 auto initFn
= FuncPool
.findByFQMG(mod
.name
~".initialize", ":void:Actor");
117 if (initFn
!is null) adef
.setInitFunc(initFn
);
120 auto thinkFn
= FuncPool
.findByFQMG(mod
.name
~".think", ":void:Actor");
121 if (thinkFn
!is null) adef
.setThinkFunc(thinkFn
);
129 // ////////////////////////////////////////////////////////////////////////// //
130 private string
modLoader (string modname
) {
131 static string
getTextFile (string fname
) {
133 auto res
= loadTextFile(fname
);
134 conwriteln("loading DACS module file '", fname
, "' (", res
.length
, " bytes)");
135 if (res
is null) res
= "";
137 } catch (Exception
) {}
143 //res = getTextFile(modname~".dacs");
144 //if (res !is null) return res;
146 res
= getTextFile("scripts/"~modname
~".dacs");
147 if (res
!is null) return res
;
150 string
[] parts
= ["scripts"];
152 while (mn
.length
> 0) {
154 if (mn
[0] >= 'A' && mn
[0] <= 'Z') ++pos
;
155 while (pos
< mn
.length
&& (mn
[pos
] < 'A' || mn
[pos
] > 'Z')) ++pos
;
156 if (mn
[0] >= 'A' && mn
[0] <= 'Z') {
157 parts
~= cast(char)(mn
[0]+32)~mn
[1..pos
];
164 import std
.array
: join
;
165 string path
= parts
.join("/")~".dacs";
166 res
= getTextFile(path
);
167 if (res
!is null) return res
;
171 throw new Exception("module '"~modname
~"' not found");
175 __gshared string
[] dacsModules
;
177 public void registerWadScripts () {
179 import std
.array
: split
;
180 auto t
= loadTextFile("scripts/dacsmain.txt");
181 foreach (string line
; t
.split('\n')) {
182 while (line
.length
&& line
[0] <= ' ') line
= line
[1..$];
183 if (line
.length
== 0 || line
[0] == ';' || line
[0] == '#') continue;
184 while (line
.length
&& line
[$-1] <= ' ') line
= line
[0..$-1];
185 import std
.algorithm
: canFind
;
186 if (!dacsModules
.canFind(line
)) dacsModules
~= line
;
188 } catch (Exception e
) { return; }
192 public void loadWadScripts () {
193 if (moduleLoader
is null) moduleLoader
= (string modname
) => modLoader(modname
);
195 //conwriteln("loading main DACS module '", mainmod, "'");
196 foreach (string mod
; dacsModules
) parseModule(mod
);
198 updateActorClasses();
200 dacsFinalizeCompiler();
201 } catch (CompilerException e
) {
202 _d_print_throwable(e
);
203 conwriteln("PARSE ERROR: ", e
.file
, " at ", e
.line
);
204 conwriteln(e
.toString
);
210 // ////////////////////////////////////////////////////////////////////////// //
211 public __gshared LevelMap map
;
212 //public __gshared DACSVM dvm;
213 public __gshared
uint prngSyncSeed
= 0x29a;
214 public __gshared
uint prngSeed
= 0x29a; // unsynced
215 public __gshared string curmapname
;
216 public __gshared string nextmapname
; // nonempty: go to next level
219 FuncPool
.FuncInfo fiInit
; // called after level map is loaded
220 FuncPool
.FuncInfo fiLoaded
; // called after mosters set
221 FuncPool
.FuncInfo fiUnloading
; // called before level is unloaded (i.e. player finished the level)
222 FuncPool
.FuncInfo fiPreThink
; // called before monster think
223 FuncPool
.FuncInfo fiPostThink
; // called after monster think
225 void runInit () { if (fiInit
!is null) fiInit(); }
226 void runLoaded () { if (fiLoaded
!is null) fiLoaded(); }
227 void runUnloading () { if (fiUnloading
!is null) fiUnloading(); }
228 void runPreThink () { if (fiPreThink
!is null) fiPreThink(); }
229 void runPostThink () { if (fiPostThink
!is null) fiPostThink(); }
232 public __gshared MapScripts mapscripts
;
235 // generate next map name for exit
236 public string
genNextMapName (int lnum
) {
237 import std
.algorithm
: endsWith
;
240 string nn
= curmapname
;
241 if (nn
.endsWith(".d2m")) {
245 if (nn
[$-1] < '0' || nn
[$-1] > '9') return null;
246 uint mapnum
= 0, mmul
= 1;
247 while (nn
.length
> 0 && nn
[$-1] >= '0' && nn
[$-1] <= '9') {
248 mapnum
+= (nn
[$-1]-'0')*mmul
;
253 if (lnum
> 0 && lnum
< 100) mapnum
= lnum
;
254 if (mapnum
> 99) return null; // alas
255 import std
.string
: format
;
256 return "%s%02u%s".format(nn
, mapnum
, ext
);
260 public void clearMapScripts () {
261 mapscripts
= mapscripts
.init
; // remove old scripts
265 public void setupMapScripts () {
266 void setupFromModule(T
) (T mod
) {
267 mapscripts
.fiInit
= FuncPool
.findByFQMG(mod
.name
~".initialize", ":void");
268 mapscripts
.fiLoaded
= FuncPool
.findByFQMG(mod
.name
~".loaded", ":void");
269 mapscripts
.fiUnloading
= FuncPool
.findByFQMG(mod
.name
~".unloading", ":void");
270 mapscripts
.fiPreThink
= FuncPool
.findByFQMG(mod
.name
~".prethink", ":void");
271 mapscripts
.fiPostThink
= FuncPool
.findByFQMG(mod
.name
~".postthink", ":void");
274 import std
.path
: baseName
, setExtension
;
276 string mapname
= curmapname
.baseName
.setExtension("");
277 foreach (string ctype
; dacsClassTypes
) {
278 foreach (auto mod
; dacsClassModules(ctype
)) {
279 if (mod
.rmoduletype
.classtype
!= "map") continue;
280 if (mod
.rmoduletype
.classname
!= mapname
) continue;
281 conwriteln("found module for map '", mapname
, "'");
282 setupFromModule(mod
);
286 // not found, try "unnamed map"
287 foreach (string ctype
; dacsClassTypes
) {
288 foreach (auto mod
; dacsClassModules(ctype
)) {
289 if (mod
.rmoduletype
.classtype
!= "map") continue;
290 if (mod
.rmoduletype
.classname
!= " ") continue;
291 conwriteln("using module for unknownmap '", mapname
, "'");
292 setupFromModule(mod
);
299 // ////////////////////////////////////////////////////////////////////////// //
300 // Park-Miller-Carta Pseudo-Random Number Generator, based on David G. Carta paper
301 // 31 bit of randomness
302 // seed is previous result, as usual
303 public uint prngR31next (uint seed
) {
304 if (seed
== 0) seed
= 0x29a;
305 uint lo
= 16807*(seed
&0xffff);
306 uint hi
= 16807*(seed
>>16);
307 lo
+= (hi
&0x7fff)<<16;
309 //if (lo > 0x7fffffff) lo -= 0x7fffffff; // should be >=, actually
310 lo
= (lo
&0x7FFFFFFF)+(lo
>>31); // same as previous code, but branch-less
315 // "synced" random, using common seed for all players
316 public uint syncrandu31 () {
317 pragma(inline
, true);
318 return (prngSyncSeed
= prngR31next(prngSyncSeed
));
322 // "unsynced" random, seed isn't saved
323 public uint unsyncrandu31 () {
324 pragma(inline
, true);
325 return (prngSeed
= prngR31next(prngSeed
));
329 // ////////////////////////////////////////////////////////////////////////// //
330 version(dacs_use_vm
) {
331 // this is VAT function, argument order is reversed
332 void doWrite(bool donl
) (FuncPool
.FuncInfo fi
, DACSVM vm
) {
334 auto count
= vm
.popInt();
335 while (count
-- > 0) {
336 auto type
= vm
.popInt();
337 switch (cast(VATArgType
)type
) {
338 case VATArgType
.Int
: write(vm
.popInt()); break;
339 case VATArgType
.Uint
: write(vm
.popUint()); break;
340 case VATArgType
.Float
: write(vm
.popFloat()); break;
341 case VATArgType
.StrId
: write(StrId(vm
.popUint()).get
); break;
342 case VATArgType
.ActorId
: write("<actor>"); vm
.popUint(); break; //TODO
343 default: write("<invalid-type>"); vm
.popUint(); break; //TODO
346 static if (donl
) writeln();
347 // push dummy return value
351 extern(C
) void doWrite(bool donl
) (uint argc
, ...) {
354 mainloop
: while (argc
>= 2) {
356 int tp
= va_arg
!int(_argptr
);
359 auto v
= va_arg
!int(_argptr
);
362 case VATArgType
.Uint
:
363 auto v
= va_arg
!uint(_argptr
);
366 case VATArgType
.Float
:
367 auto v
= va_arg
!float(_argptr
);
370 case VATArgType
.StrId
:
371 auto v
= StrId(va_arg
!uint(_argptr
)).get
;
374 case VATArgType
.ActorId
:
375 auto v
= ActorId(va_arg
!uint(_argptr
));
376 write("<actor:", v
.valid
, ":", v
.id
, ">");
379 default: write("<invalid-type>"); break mainloop
;
382 static if (donl
) writeln();
387 // ////////////////////////////////////////////////////////////////////////// //
388 void animClearFrames (StrId classtype
, StrId classname
, StrId state
) {
389 auto adef
= findActorDef(classtype
.get
, classname
.get
);
390 if (adef
is null) throw new Exception("animClearFrames: actor '"~classtype
.get
~":"~classname
.get
~"' not found!");
391 adef
.clearFrames(state
);
395 void animAddFrame (StrId classtype
, StrId classname
, StrId state
, uint dir
, StrId sprname
) {
396 auto adef
= findActorDef(classtype
.get
, classname
.get
);
397 if (adef
is null) throw new Exception("animAddFrame: actor '"~classtype
.get
~":"~classname
.get
~"' not found!");
398 if (dir
!= 0) dir
= 1; //TODO: process mirror flag here
399 adef
.addFrame(state
, dir
, sprname
);
403 // ////////////////////////////////////////////////////////////////////////// //
405 conwriteln("setting up D API");
407 FuncPool
["write"] = &doWrite
!false;
408 FuncPool
["writeln"] = &doWrite
!true;
410 FuncPool
["syncrandu31"] = &syncrandu31
;
412 FuncPool
["animClearFrames"] = &animClearFrames
;
413 FuncPool
["animAddFrame"] = &animAddFrame
;
415 FuncPool
["actorSetAnimation"] = function void (ActorId me
, StrId state
) {
416 if (!me
.valid
) return;
417 if (auto adef
= findActorDef(me
.classtype
!string
, me
.classname
!string
)) {
418 me
.zAnimstate
= state
;
423 //FuncPool["getPlayer"] = function ActorId () { assert(0); };
424 //FuncPool["isPlayer"] = function int (ActorId me) { return (me == players.ptr[0] ? 1 : 0); };
426 FuncPool
["getPlayerCount"] = function int () => 1;
428 FuncPool
["getPlayerActor"] = function ActorId (uint pnum
) {
429 if (pnum
== 1) return players
.ptr
[0];
433 FuncPool
["getPlayerButtons"] = function uint (uint pidx
) { return (pidx
== 1 ? plrKeysLast
: 0); };
435 FuncPool
["mapGetTypeTile"] = function int (int x
, int y
) {
437 if (map
!is null && x
>= 0 && y
>= 0 && x
< map
.width
&& y
< map
.height
) {
438 res
= map
.tiles
.ptr
[LevelMap
.Type
].ptr
[y
*map
.width
+x
];
439 if (res
!= LevelMap
.TILE_ACTTRAP
) res
&= 0x7f;
444 FuncPool
["mapGetTile"] = function int (uint layer
, int x
, int y
) {
445 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);
448 FuncPool
["mapSetTypeTile"] = function void (int x
, int y
, int tid
) {
449 if (map
is null || x
< 0 || y
< 0 || x
>= map
.width || y
>= map
.height || tid
< 0 || tid
> 255) return;
450 auto p
= map
.tiles
.ptr
[LevelMap
.Type
].ptr
+y
*map
.width
+x
;
453 import render
: mapDirty
;
454 mapDirty((1<<LevelMap
.Type
)|
(1<<LevelMap
.LightMask
));
458 FuncPool
["mapSetTile"] = function void (uint layer
, int x
, int y
, int tid
) {
459 if (map
is null || layer
< 0 || layer
> 1 || x
< 0 || y
< 0 || x
>= map
.width || y
>= map
.height || tid
< 0 || tid
> 255) return;
460 auto p
= map
.tiles
.ptr
[LevelMap
.Front
+layer
].ptr
+y
*map
.width
+x
;
463 import render
: mapDirty
;
466 mapDirty((1<<LevelMap
.Front
)|
(1<<LevelMap
.AllLiquids
)|
(1<<LevelMap
.LiquidMask
));
469 mapDirty((1<<LevelMap
.Back
)|
(1<<LevelMap
.AllLiquids
)|
(1<<LevelMap
.LiquidMask
));
474 FuncPool
["mapGetWaterTexture"] = function int (int fg
) {
475 if (fg
< 0 || fg
>= map
.wallnames
.length
) return 0;
476 switch (map
.wallnames
.ptr
[fg
]) {
477 case "_water_0": return 1;
478 case "_water_1": return 2;
479 case "_water_2": return 3;
485 FuncPool
["getMapViewHeight"] = function int () {
486 import render
: vlWidth
, vlHeight
, getScale
;
487 return vlHeight
/getScale
;
490 FuncPool
["actorsOverlap"] = function int (ActorId a
, ActorId b
) { return (actorsOverlap(a
, b
) ?
1 : 0); };
492 FuncPool
["actorRemove"] = function void (ActorId me
) {
494 if ((me
.fget_flags
&AF_NOCOLLISION
) == 0) ugActorModify
!false(me
); // remove from grid
499 FuncPool
["rewindTouchList"] = &rewindTouchList
;
500 FuncPool
["getNextTouchListItem"] = &getNextTouchListItem
;
502 FuncPool
["actorListRewind"] = &xactorListRewind
;
503 FuncPool
["actorListNext"] = &xactorListNext
;
505 FuncPool
["getCheatNoDoors"] = function int () { import render
: cheatNoDoors
; return (cheatNoDoors ?
1 : 0); };
506 FuncPool
["getCheatNoWallClip"] = function int () { import render
: cheatNoWallClip
; return (cheatNoWallClip ?
1 : 0); };
507 FuncPool
["getCheatNoCeilClip"] = function int () { return 0; };
508 FuncPool
["getCheatNoLiftClip"] = function int () { return 0; };
510 FuncPool
["addMessage"] = function void (string text
, int pause
, bool noreplace
) {
511 import render
: postAddMessage
;
512 postAddMessage(text
, pause
, noreplace
);
515 import d2dparts
: dotAddBlood
, dotAddSpark
, dotAddWater
;
517 FuncPool
["dotAddBlood"] = &dotAddBlood
;
518 FuncPool
["dotAddSpark"] = &dotAddSpark
;
519 FuncPool
["dotAddWater"] = &dotAddWater
;
521 function void (int x, int y, int xv, int yv, int n, int color) {
522 conwriteln("dotAddWater: x=", x, "; y=", y, "; xv=", xv, "; yv=", yv, "; n=", n, "; color=", color);
523 dotAddWater(x, y, xv, yv, n, color);
527 FuncPool
["gactLevelExit"] = function void (int lnum
) {
528 if (nextmapname
.length
== 0) {
529 string nmname
= genNextMapName(lnum
);
530 if (nmname
.length
== 0) assert(0, "no level!");
531 nextmapname
= nmname
;
535 FuncPool
["actorSpawn"] = &actorSpawn
;
537 // return previous one
538 FuncPool
["setCameraChick"] = function ActorId (ActorId act
) {
539 auto res
= cameraChick
;
544 FuncPool
["MapWidth"] = function int () { return (map
!is null ? map
.width
: 1); };
545 FuncPool
["MapHeight"] = function int () { return (map
!is null ? map
.height
: 1); };
549 public void registerAPI () {
550 //Actor.actorSize += 256;
551 conwriteln("actor size is ", Actor
.actorSize
, " bytes");
553 version(dacs_use_vm
) FuncPool
.vm
= new DACSVM();
554 //dvm = new DACSVM();
556 // initialize actor animation
557 ActorDef
.forEach((adef
) => adef
.callAnimInit());
559 conwriteln("API registered");
563 // ////////////////////////////////////////////////////////////////////////// //
564 mixin(Actor
.FieldGetMixin
!("classtype", StrId
)); // fget_classtype
565 mixin(Actor
.FieldGetMixin
!("classname", StrId
)); // fget_classname
566 mixin(Actor
.FieldGetMixin
!("x", int));
567 mixin(Actor
.FieldGetMixin
!("y", int));
568 mixin(Actor
.FieldGetMixin
!("dir", uint));
569 mixin(Actor
.FieldGetMixin
!("height", int));
570 mixin(Actor
.FieldGetMixin
!("radius", int));
571 mixin(Actor
.FieldGetMixin
!("flags", uint));
572 mixin(Actor
.FieldGetMixin
!("zAnimstate", StrId
));
573 mixin(Actor
.FieldGetMixin
!("zAnimidx", int));
575 mixin(Actor
.FieldSetMixin
!("zAnimidx", int));
578 public bool actorsOverlap (ActorId a
, ActorId b
) {
579 if (!a
.valid ||
!b
.valid
) return false;
580 if (a
.id
== b
.id
) return false; // no self-overlap
584 int ar
= a
.fget_radius
;
585 int br
= b
.fget_radius
;
587 if (ax
-ar
> bx
+br || ax
+ar
< bx
-br
) return false;
591 int ah
= a
.fget_height
;
592 int bh
= b
.fget_height
;
594 //return (ay > by-bh && ay-ah < by);
595 return (by
-bh
<= ay
&& by
>= ay
-ah
);
599 // ////////////////////////////////////////////////////////////////////////// //
600 public __gshared ActorId
[2] players
;
603 public void loadAllMonsterGraphics () {
604 ActorDef
.forEach((adef
) {
605 conwriteln("loading graphics for '", adef
.classtype
.get
, ":", adef
.classname
.get
, "'");
608 //Actor.dumpActors();
609 realiseSpriteAtlases();
613 ActorId
actorSpawn (StrId classtype
, StrId classname
, int x
, int y
, uint dir
) {
614 auto adef
= findActorDef(classtype
, classname
);
615 if (adef
is null) return ActorId(0);
616 auto aid
= Actor
.alloc
;
617 aid
.classtype
= classtype
;
618 aid
.classname
= classname
;
619 aid
.state
= StrPool
.MNST_SLEEP
;
622 aid
.dir
= (dir ?
1 : 0);
624 if ((aid
.fget_flags
&AF_NOCOLLISION
) == 0) ugActorModify
!true(aid
);
629 public void loadMapMonsters () {
630 assert(map
!is null);
632 players
[] = ActorId(0);
633 //conwriteln(players[0].valid, "; ", players[0].id);
634 foreach (ref thing
; map
.things
) {
635 if (thing
.dmonly
) continue;
636 auto did
= (thing
.type
&0x7fff) in d2dactordefsById
;
637 if (did
!is null && did
.classtype
== "playerstart") {
638 if ((thing
.type
&0x7fff) == 1 ||
(thing
.type
&0x7fff) == 2) {
639 int pnum
= thing
.type
-1;
640 if (!players
[pnum
].valid
) {
641 auto aid
= Actor
.alloc
;
642 aid
.classtype
= StrPool
.intern("monster");
643 aid
.classname
= StrPool
.intern("Player");
644 aid
.plrnum
= cast(uint)(pnum
+1);
645 aid
.state
= StrPool
.MNST_SLEEP
;
646 aid
.x
= cast(int)thing
.x
;
647 aid
.y
= cast(int)thing
.y
;
648 aid
.dir
= cast(uint)(thing
.right ?
1 : 0);
649 auto adef
= findD2DActorDef(thing
.type
);
650 if (adef
is null) assert(0);
652 if ((aid
.fget_flags
&AF_NOCOLLISION
) == 0) ugActorModify
!true(aid
);
654 conwriteln("player #", pnum
+1, " aid is ", aid
.id
);
659 auto adef
= findD2DActorDef(thing
.type
&0x7fff);
662 conwriteln("ignoring D2D thing '", did
.classtype
.get
, ":", did
.classname
.get
, "'");
664 conwriteln("ignoring unknown D2D thing with mapid ", thing
.type
);
668 // create actor and initialize it
669 auto aid
= Actor
.alloc
;
670 aid
.classtype
= StrPool
.intern(adef
.classtype
);
671 aid
.classname
= StrPool
.intern(adef
.classname
);
672 //conwriteln("found '", aid.classtype.get, ":", aid.classname.get, "'");
674 assert(did
.classtype
== adef
.classtype
);
675 assert(did
.classname
== adef
.classname
);
676 conwriteln("mapid=", thing
.type
, "; ", adef
.classtype
, ":", adef
.classname
, "; id=", aid
.id
);
677 assert(aid
.classtype
!string
== adef
.classtype
);
678 assert(aid
.classname
!string
== adef
.classname
);
682 aid
.state
= StrPool
.MNST_SLEEP
;
683 aid
.x
= cast(int)thing
.x
;
684 aid
.y
= cast(int)thing
.y
;
685 aid
.dir
= cast(uint)(thing
.right ?
1 : 0);
686 if (thing
.type
&0x8000) aid
.flags
= aid
.fget_flags|AF_NOGRAVITY
;
687 //if (aid.classtype!string == "item" && aid.x!int < 64) { aid.x = 92; aid.y = aid.y!int-16; conwriteln("!!!"); }
689 if ((aid
.fget_flags
&AF_NOCOLLISION
) == 0) ugActorModify
!true(aid
);
693 foreach (ref sw
; map
.switches
) {
694 if (sw
.type
== 0) continue; // just in case
695 auto swname
= getD2DSwitchClassName(sw
.type
);
696 if (swname
.length
== 0) {
697 conwriteln("unknown switch type ", sw
.type
);
700 if (auto adef
= findActorDef("switch", swname
)) {
701 auto aid
= Actor
.alloc
;
702 aid
.classtype
= StrPool
.intern("switch");
703 aid
.classname
= StrPool
.intern(swname
);
704 // nocollision, 'cause collision checking will be processed in switch thinker, but other actors should not touch switches
705 aid
.flags
= /*AF_NOCOLLISION|*/AF_NOGRAVITY|
/*AF_NOONTOUCH|*/AF_NODRAW|AF_NOLIGHT|AF_NOANIMATE
;
706 aid
.x
= sw
.x
*TileSize
;
707 aid
.y
= sw
.y
*TileSize
;
708 aid
.switchabf
= (sw
.a
<<16)|
(sw
.b
<<8)|sw
.flags
;
713 if ((aid
.fget_flags
&AF_NOCOLLISION
) == 0) ugActorModify
!true(aid
); // just in case
715 conwriteln("switch definition 'switch:", swname
, "' not found");
719 conwriteln("initial snapshot size: ", Actor
.snapshotSize
, " bytes");
723 // ////////////////////////////////////////////////////////////////////////// //
724 __gshared ActorId
[65536] xactorList
; // aids
725 __gshared
uint xactorListCount
, xactorListIndex
= uint.max
;
729 void xactorListRewind () {
730 if (!touchListAllowed
) return; // it's ok to reuse it here
731 xactorListCount
= xactorListIndex
= 0;
732 Actor
.forEach((ActorId me
) {
733 if (me
.fget_classtype
!= StrPool
.X_X
) xactorList
.ptr
[xactorListCount
++] = me
;
739 ActorId
xactorListNext () {
740 if (!touchListAllowed
) return ActorId(0); // it's ok to reuse it here
741 if (xactorListIndex
== uint.max
) xactorListRewind();
742 while (xactorListIndex
< xactorListCount
) {
743 auto aid
= xactorList
.ptr
[xactorListIndex
];
745 if (aid
.fget_classtype
== StrPool
.X_X
) continue;
752 // ////////////////////////////////////////////////////////////////////////// //
753 __gshared
uint[65536] touchList
; // aids
754 __gshared
uint[] realTouchList
;
755 __gshared
uint realTouchListIndex
= uint.max
;
756 __gshared ActorId curThinkingActor
;
757 __gshared
bool touchListAllowed
= false;
761 void rewindTouchList () {
762 if (!touchListAllowed
) return;
763 realTouchListIndex
= 0;
764 realTouchList
= ugActorHitList(curThinkingActor
, touchList
[]);
769 ActorId
getNextTouchListItem () {
770 if (!touchListAllowed
) return ActorId(0);
771 if (realTouchListIndex
== uint.max
) rewindTouchList();
772 while (realTouchListIndex
< realTouchList
.length
) {
773 auto aid
= ActorId(realTouchList
.ptr
[realTouchListIndex
]);
774 ++realTouchListIndex
;
775 if (aid
.fget_classtype
== StrPool
.X_X
) continue;
776 if (aid
.valid
&& (aid
.fget_flags
&AF_NOONTOUCH
) == 0) return aid
;
782 public void doActorsThink () {
783 // we have too much memory!
784 __gshared
uint[65536] validActorsList
;
785 __gshared ActorId
[65536] postponedDeath
;
786 __gshared
uint pdcount
;
788 touchListAllowed
= true;
789 scope(exit
) touchListAllowed
= false;
791 mapscripts
.runPreThink();
792 foreach (uint xid
; Actor
.getValidList(validActorsList
[])) {
793 auto me
= ActorId(xid
);
795 if (me
.fget_classtype
== StrPool
.X_X
) { postponedDeath
.ptr
[pdcount
++] = me
; continue; }
796 auto flags
= me
.fget_flags
;
797 if ((flags
&AF_NOCOLLISION
) == 0) ugActorModify
!false(me
); // remove from grid
798 if (auto adef
= findActorDef(me
)) {
799 if ((flags
&AF_NOTHINK
) == 0) {
800 realTouchListIndex
= uint.max
;
801 xactorListIndex
= uint.max
;
802 curThinkingActor
= me
;
804 //if (me.x!int < 32) conwriteln("actor: ", me.id, "; attLightRGBX=", me.attLightRGBX!uint);
805 if (!me
.valid
) continue; // we are dead
806 if (me
.fget_classtype
== StrPool
.X_X
) { postponedDeath
.ptr
[pdcount
++] = me
; continue; }
807 flags
= me
.fget_flags
; // in case script updated flags
809 if ((flags
&AF_NOANIMATE
) == 0) {
810 int aidx
= me
.fget_zAnimidx
;
811 int nidx
= adef
.nextAnimIdx(me
.fget_zAnimstate
, me
.fget_dir
, aidx
);
812 //conwriteln("actor ", me.id, " (", me.classtype!string, me.classname!string, "): state=", me.zAnimstate!string, "; aidx=", aidx, "; nidx=", nidx);
813 me
.fset_zAnimidx
= nidx
;
814 //assert(me.fget_zAnimidx == nidx);
817 if ((flags
&AF_NOCOLLISION
) == 0) ugActorModify
!true(me
);
821 mapscripts
.runPostThink();
822 // process scheduled death
823 foreach (ActorId aid
; postponedDeath
[0..pdcount
]) {
824 /*if ((flags&AF_NOCOLLISION) == 0)*/ ugActorModify
!false(aid
); // remove from grid
829 //{ import std.stdio : stdout; stdout.writeln("========================================="); }
830 //Actor.dumpActors();