0be98e38498e8d47720d62c152186c5246530dd6
1 (* Copyright (C) Doom 2D: Forever Developers
3 * This program is free software: you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation, version 3 of the License ONLY.
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
12 * You should have received a copy of the GNU General Public License
13 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 {$INCLUDE ../shared/a_modes.inc}
16 {.$DEFINE D2F_DEBUG_FALL_MPLAT}
17 {/$DEFINE D2F_DEBUG_PART_AWAKE}
39 MARK_BLOCKED
= MARK_WALL
or MARK_DOOR
;
40 MARK_LIQUID
= MARK_WATER
or MARK_ACID
;
41 MARK_LIFT
= MARK_LIFTDOWN
or MARK_LIFTUP
or MARK_LIFTLEFT
or MARK_LIFTRIGHT
;
46 R_GFX_EXPLODE_ROCKET
= 3;
47 R_GFX_EXPLODE_BFG
= 4;
50 R_GFX_ITEM_RESPAWN
= 7;
52 R_GFX_EXPLODE_SKELFIRE
= 9;
53 R_GFX_EXPLODE_PLASMA
= 10;
54 R_GFX_EXPLODE_BSPFIRE
= 11;
55 R_GFX_EXPLODE_IMPFIRE
= 12;
56 R_GFX_EXPLODE_CACOFIRE
= 13;
57 R_GFX_EXPLODE_BARONFIRE
= 14;
58 R_GFX_TELEPORT_FAST
= 15;
59 R_GFX_SMOKE_TRANS
= 16;
60 R_GFX_FLAME_RAND
= 17;
63 R_GFX_FLAME_WIDTH
= 32;
64 R_GFX_FLAME_HEIGHT
= 32;
65 R_GFX_SMOKE_WIDTH
= 32;
66 R_GFX_SMOKE_HEIGHT
= 32;
68 procedure g_GFX_Init ();
69 procedure g_GFX_Free ();
71 procedure g_GFX_Blood (fX
, fY
: Integer; count
: Word; vx
, vy
: Integer;
72 devX
, devY
: Word; cr
, cg
, cb
: Byte; kind
: Byte=BLOOD_NORMAL
);
73 procedure g_GFX_Spark (fX
, fY
: Integer; count
: Word; angle
: SmallInt; devX
, devY
: Byte);
74 procedure g_GFX_Water (fX
, fY
: Integer; count
: Word; fVelX
, fVelY
: Single; devX
, devY
, color
: Byte;
75 simple
: Boolean=false; cr
: Byte=0; cg
: Byte=0; cb
: Byte=0);
76 procedure g_GFX_SimpleWater (fX
, fY
: Integer; count
: Word; fVelX
, fVelY
: Single; defColor
, cr
, cg
, cb
: Byte);
77 procedure g_GFX_Bubbles (fX
, fY
: Integer; count
: Word; devX
, devY
: Byte);
79 procedure g_GFX_SetMax (count
: Integer);
80 function g_GFX_GetMax (): Integer;
82 procedure g_Mark (x
, y
, Width
, Height
: Integer; t
: Byte; st
: Boolean=true);
84 procedure g_GFX_QueueEffect (AnimType
, X
, Y
: Integer);
86 procedure g_GFX_Update ();
89 gpart_dbg_enabled
: Boolean = true;
90 gpart_dbg_phys_enabled
: Boolean = true;
93 //WARNING: only for Holmes!
94 function awmIsSetHolmes (x
, y
: Integer): Boolean; inline;
96 type (* private state *)
97 TPartType
= (Blood
, Spark
, Bubbles
, Water
);
98 TPartState
= (Free
, Normal
, Stuck
, Sleeping
);
99 TFloorType
= (Wall
, LiquidIn
, LiquidOut
);
100 // Wall: floorY is just before floor
101 // LiquidIn: floorY is liquid *start* (i.e. just in a liquid)
102 // LiquidOut: floorY is liquid *end* (i.e. just out of a liquid)
103 TEnvType
= (EAir
, ELiquid
, EWall
); // where particle is now
105 // note: this MUST be record, so we can keep it in
106 // dynamic array and has sequential memory access pattern
107 PParticle
= ^TParticle
;
112 accelX
, accelY
: Single;
114 particleType
: TPartType
;
115 red
, green
, blue
: Byte;
117 time
, liveTime
, waitTime
: Word;
118 stickDX
: Integer; // STATE_STICK: -1,1: stuck to a wall; 0: stuck to ceiling
119 justSticked
: Boolean; // not used
120 floorY
: Integer; // actually, floor-1; `Unknown`: unknown
121 floorType
: TFloorType
;
122 env
: TEnvType
; // where particle is now
123 ceilingY
: Integer; // actually, ceiling+1; `Unknown`: unknown
124 wallEndY
: Integer; // if we stuck to a wall, this is where wall ends
126 //k8: sorry, i have to emulate virtual methods this way, 'cause i haet `Object`
127 procedure thinkerBloodAndWater ();
128 procedure thinkerSpark ();
129 procedure thinkerBubble ();
131 procedure findFloor (force
: Boolean=false); // this updates `floorY` if forced or Unknown
132 procedure findCeiling (force
: Boolean=false); // this updates `ceilingY` if forced or Unknown
134 procedure freeze (); inline; // remove velocities and acceleration
135 procedure sleep (); inline; // switch to sleep mode
137 function checkAirStreams (): Boolean; // `true`: affected by air stream
139 function alive (): Boolean; inline;
140 procedure die (); inline;
141 procedure think (); inline;
144 var (* private state *)
145 Particles
: array of TParticle
= nil;
150 {$IFDEF ENABLE_RENDER}
153 g_map
, g_panel
, Math
, utils
,
154 g_options
, SysUtils
, MAPDEF
159 Unknown
= Integer($7fffffff);
162 MaxParticles
: Integer = 0;
163 CurrentParticle
: Integer = 0;
164 // awakeMap has one bit for each map grid cell; on g_Mark,
165 // corresponding bits will be set, and in `think()` all particles
166 // in marked cells will be awaken
167 awakeMap
: packed array of LongWord = nil;
168 awakeMapH
: Integer = -1;
169 awakeMapW
: Integer = -1;
170 awakeMinX
, awakeMinY
: Integer;
171 awakeDirty
: Boolean = false;
172 {$IF DEFINED(D2F_DEBUG_PART_AWAKE)}
173 awakeMapHlm
: packed array of LongWord = nil;
176 procedure g_GFX_QueueEffect (AnimType
, X
, Y
: Integer);
178 {$IFDEF ENABLE_RENDER}
179 r_Render_QueueEffect(AnimType
, X
, Y
)
183 // ////////////////////////////////////////////////////////////////////////// //
184 function awmIsSetHolmes (x
, y
: Integer): Boolean; inline;
186 {$IF DEFINED(D2F_DEBUG_PART_AWAKE)}
187 if (Length(awakeMapHlm
) = 0) then begin result
:= false; exit
; end;
188 x
:= (x
-awakeMinX
) div mapGrid
.tileSize
;
189 y
:= (y
-awakeMinY
) div mapGrid
.tileSize
;
190 if (x
>= 0) and (y
>= 0) and (x
div 32 < awakeMapW
) and (y
< awakeMapH
) then
192 if (y
*awakeMapW
+x
div 32 < Length(awakeMapHlm
)) then
194 result
:= ((awakeMapHlm
[y
*awakeMapW
+x
div 32] and (LongWord(1) shl (x
mod 32))) <> 0);
211 // ////////////////////////////////////////////////////////////////////////// //
212 // HACK! using mapgrid
213 procedure awmClear (); inline;
215 {$IF DEFINED(D2F_DEBUG_PART_AWAKE)}
216 if (Length(awakeMap
) > 0) then
218 if (Length(awakeMapHlm
) <> Length(awakeMap
)) then SetLength(awakeMapHlm
, Length(awakeMap
));
219 Move(awakeMap
[0], awakeMapHlm
[0], Length(awakeMap
)*sizeof(awakeMap
[0]));
222 if awakeDirty
and (awakeMapW
> 0) then
224 FillDWord(awakeMap
[0], Length(awakeMap
), 0);
230 procedure awmSetup ();
232 assert(mapGrid
<> nil);
233 awakeMapW
:= (mapGrid
.gridWidth
+mapGrid
.tileSize
-1) div mapGrid
.tileSize
;
234 awakeMapW
:= (awakeMapW
+31) div 32; // LongWord has 32 bits ;-)
235 awakeMapH
:= (mapGrid
.gridHeight
+mapGrid
.tileSize
-1) div mapGrid
.tileSize
;
236 awakeMinX
:= mapGrid
.gridX0
;
237 awakeMinY
:= mapGrid
.gridY0
;
238 SetLength(awakeMap
, awakeMapW
*awakeMapH
);
239 {$IF DEFINED(D2F_DEBUG_PART_AWAKE)}
240 SetLength(awakeMapHlm
, awakeMapW
*awakeMapH
);
241 FillDWord(awakeMapHlm
[0], Length(awakeMapHlm
), 0);
243 //{$IF DEFINED(D2F_DEBUG)}
244 e_LogWritefln('particle awake map: %sx%s (for grid of size %sx%s)', [awakeMapW
, awakeMapH
, mapGrid
.gridWidth
, mapGrid
.gridHeight
]);
251 function awmIsSet (x
, y
: Integer): Boolean; inline;
253 x
:= (x
-awakeMinX
) div mapGrid
.tileSize
;
254 y
:= (y
-awakeMinY
) div mapGrid
.tileSize
;
255 if (x
>= 0) and (y
>= 0) and (x
div 32 < awakeMapW
) and (y
< awakeMapH
) then
257 {$IF DEFINED(D2F_DEBUG)}
258 assert(y
*awakeMapW
+x
div 32 < Length(awakeMap
));
260 result
:= ((awakeMap
[y
*awakeMapW
+x
div 32] and (LongWord(1) shl (x
mod 32))) <> 0);
269 procedure awmSet (x
, y
: Integer); inline;
273 x
:= (x
-awakeMinX
) div mapGrid
.tileSize
;
274 y
:= (y
-awakeMinY
) div mapGrid
.tileSize
;
275 if (x
>= 0) and (y
>= 0) and (x
div 32 < awakeMapW
) and (y
< awakeMapH
) then
277 {$IF DEFINED(D2F_DEBUG)}
278 assert(y
*awakeMapW
+x
div 32 < Length(awakeMap
));
280 v
:= @awakeMap
[y
*awakeMapW
+x
div 32];
281 v
^ := v
^ or (LongWord(1) shl (x
mod 32));
287 // ////////////////////////////////////////////////////////////////////////// //
291 procedure g_Mark (x
, y
, Width
, Height
: Integer; t
: Byte; st
: Boolean=true);
294 dx
, dy
, ex
, ey
: Integer;
297 if (not gpart_dbg_enabled
) or (not gpart_dbg_phys_enabled
) then exit
;
298 if (awakeMapW
< 1) or (awakeMapH
< 1) then exit
;
300 if (Width
< 1) or (Height
< 1) then exit
;
302 // make some border, so we'll hit particles around the panel
303 ex
:= x
+Width
+Extrude
-1-awakeMinX
;
304 ey
:= y
+Height
+Extrude
-1-awakeMinY
;
305 x
:= (x
-Extrude
)-awakeMinX
;
306 y
:= (y
-Extrude
)-awakeMinY
;
308 x
:= x
div mapGrid
.tileSize
;
309 y
:= y
div mapGrid
.tileSize
;
310 ex
:= ex
div mapGrid
.tileSize
;
311 ey
:= ey
div mapGrid
.tileSize
;
313 // has something to do?
314 if (ex
< 0) or (ey
< 0) or (x
>= awakeMapW
*32) or (y
>= awakeMapH
) then exit
;
315 if (x
< 0) then x
:= 0;
316 if (y
< 0) then y
:= 0;
317 if (ex
>= awakeMapW
*32) then ex
:= awakeMapW
*32-1;
318 if (ey
>= awakeMapH
) then ey
:= awakeMapH
;
325 {$IF DEFINED(D2F_DEBUG)}
326 assert((dx
>= 0) and (dy
>= 0) and (dx
div 32 < awakeMapW
) and (dy
< awakeMapH
));
327 assert(dy
*awakeMapW
+dx
div 32 < Length(awakeMap
));
329 v
:= @awakeMap
[dy
*awakeMapW
+dx
div 32];
330 v
^ := v
^ or (LongWord(1) shl (dx
mod 32));
336 // ////////////////////////////////////////////////////////////////////////// //
337 function TParticle
.alive (): Boolean; inline; begin result
:= (state
<> TPartState
.Free
); end;
338 procedure TParticle
.die (); inline; begin state
:= TPartState
.Free
; end;
340 // remove velocities and acceleration
341 procedure TParticle
.freeze (); inline;
343 // stop right there, you criminal scum!
351 // `true`: affected by air stream
352 function TParticle
.checkAirStreams (): Boolean;
357 pan
:= g_Map_PanelAtPoint(x
, y
, GridTagLift
);
358 result
:= (pan
<> nil) and WordBool(pan
.PanelType
and (PANEL_LIFTUP
or PANEL_LIFTDOWN
or PANEL_LIFTLEFT
or PANEL_LIFTRIGHT
));
365 if (velY
> -1-r
) then velY
-= 0.8;
366 if (abs(velX
) > 0.1) then velX
-= velX
/10.0;
367 velX
+= (Random
-Random
)*0.2;
372 if (velY
< 1+r
) then velY
+= 0.8;
377 if (velX
> -8-r
) then velX
-= (8+r
) div 2;
382 if (velX
< 8+r
) then velX
+= (8+r
) div 2;
389 if result
and (state
= TPartState
.Sleeping
) then state
:= TPartState
.Normal
;
394 // switch to sleep mode
395 procedure TParticle
.sleep (); inline;
397 if not checkAirStreams() then
399 state
:= TPartState
.Sleeping
;
405 procedure TParticle
.findFloor (force
: Boolean=false);
410 if (not force
) and (floorY
<> Unknown
) then exit
;
411 // stuck in the wall? rescan, 'cause it can be mplat
412 if (env
= TEnvType
.EWall
) then
414 pan
:= g_Map_PanelAtPoint(x
, y
, (GridTagObstacle
or GridTagLiquid
));
417 // either in a wall, or in a liquid
418 if ((pan
.tag
and GridTagObstacle
) <> 0) then
420 // we are in the wall, wtf?!
422 env
:= TEnvType
.EWall
;
423 floorType
:= TFloorType
.Wall
;
424 state
:= TPartState
.Sleeping
; // anyway
427 // we are in liquid, trace to liquid end
428 env
:= TEnvType
.ELiquid
;
431 // are we in a liquid?
432 if (env
= TEnvType
.ELiquid
) then
434 // trace out of the liquid
435 //env := TEnvType.ELiquid;
436 floorType
:= TFloorType
.LiquidOut
;
437 //e_LogWritefln('tracing out of a liquid; floorY=%s; y=%s', [floorY, y]);
438 mapGrid
.traceOrthoRayWhileIn(ex
, floorY
, x
, y
, x
, g_Map_MaxY
, GridTagLiquid
);
439 floorY
+= 1; // so `floorY` is just out of a liquid
440 //e_LogWritefln(' traced out of a liquid; floorY=%s; y=%s', [floorY, y]);
445 assert(env
= TEnvType
.EAir
);
446 //env := TEnvType.EAir;
447 pan
:= g_Map_traceToNearest(x
, y
, x
, g_Map_MaxY
, (GridTagObstacle
or GridTagLiquid
), @ex
, @floorY
);
451 if ((pan
.tag
and GridTagObstacle
) <> 0) then
454 floorType
:= TFloorType
.Wall
;
459 floorType
:= TFloorType
.LiquidIn
; // entering liquid
460 floorY
+= 1; // so `floorY` is just in a liquid
465 // out of the level; assume wall, but it doesn't really matter
466 floorType
:= TFloorType
.Wall
;
467 floorY
:= g_Map_MaxY
+2;
473 procedure TParticle
.findCeiling (force
: Boolean=false);
477 if (not force
) and (ceilingY
<> Unknown
) then exit
;
478 if (nil = g_Map_traceToNearest(x
, y
, x
, g_Map_MinY
, GridTagSolid
, @ex
, @ceilingY
)) then
480 ceilingY
:= g_Map_MinY
-2;
485 procedure TParticle
.think (); inline;
488 if (state
= TPartState
.Stuck
) then
490 //writeln('awaking particle at (', x, ',', y, ')');
491 if (stickDX
= 0) then
493 state
:= TPartState
.Normal
; // stuck to a ceiling
497 // stuck to a wall, check if wall is still there
498 if (wallEndY
<> Unknown
) then
501 if (g_Map_PanelAtPoint(x
+stickDX
, y
, GridTagObstacle
) = nil) then
503 // a wall was moved out, start falling
504 state
:= TPartState
.Normal
;
505 if (velY
= 0) then velY
:= 0.1;
506 if (accelY
= 0) then accelY
:= 0.5;
513 state
:= TPartState
.Normal
;
514 if (velY
= 0) then velY
:= 0.1;
515 if (accelY
= 0) then accelY
:= 0.5;
524 // awake sleeping particle, if necessary
527 if awmIsSet(x
, y
) then awake();
530 TPartState.Sleeping, TPartState.Stuck:
531 if awmIsSet(x, y) then awake();
533 if (env = TEnvType.EWall) and awmIsSet(x, y) then awake();
538 TPartType
.Blood
, TPartType
.Water
: thinkerBloodAndWater();
539 TPartType
.Spark
: thinkerSpark();
540 TPartType
.Bubbles
: thinkerBubble();
545 // ////////////////////////////////////////////////////////////////////////// //
546 procedure TParticle
.thinkerBloodAndWater ();
547 procedure stickToCeiling ();
549 state
:= TPartState
.Stuck
;
552 ceilingY
:= y
; // yep
555 procedure stickToWall (dx
: Integer);
559 state
:= TPartState
.Stuck
;
560 if (dx
> 0) then stickDX
:= 1 else stickDX
:= -1;
562 // find next floor transition
565 mapGrid
.traceOrthoRayWhileIn(ex
, wallEndY
, x
+stickDX
, y
, x
+stickDX
, floorY
+1, (GridTagWall
or GridTagDoor
or GridTagStep
));
568 procedure hitAFloor ();
570 state
:= TPartState
.Sleeping
; // we aren't moving anymore
573 floorType
:= TFloorType
.Wall
; // yep
576 // `true`: didn't, get outa thinker
577 function drip (): Boolean;
580 TPartType
.Blood
: result
:= (Random(200) = 100);
581 TPartType
.Water
: result
:= (Random(30) = 15);
582 else raise Exception
.Create('internal error in particle engine: drip');
588 // if we're falling from ceiling, switch to normal mode
589 if (state
= TPartState
.Stuck
) and (stickDX
= 0) then state
:= TPartState
.Normal
;
593 // switch to freefall mode
594 procedure freefall ();
596 state
:= TPartState
.Normal
;
601 procedure applyGravity (inLiquid
: Boolean);
603 state
:= TPartState
.Normal
;
617 _done
, _gravityagain
, _stuckagain
;
622 checkEnv
, inAir
, inStep
: Boolean;
623 floorJustTraced
: Boolean;
624 {$IF DEFINED(D2F_DEBUG_FALL_MPLAT)}
628 if not gpart_dbg_phys_enabled
then begin x
+= round(velX
); y
+= round(velY
); goto _done
; end;
632 // still check for air streams when sleeping (no)
633 if (state
= TPartState
.Sleeping
) then begin {checkAirStreams();} goto _done
; end; // so blood will dissolve
635 // process stuck particles
636 if (state
= TPartState
.Stuck
) then
638 // stuck to a ceiling?
639 if (stickDX
= 0) then
641 // yeah, stuck to a ceiling
642 if (ceilingY
= Unknown
) then findCeiling();
643 // dropped from a ceiling?
644 if (y
> ceilingY
) then
649 state
:= TPartState
.Normal
;
653 // otherwise, try to drip
654 if drip() then goto _done
;
660 if (wallEndY
= Unknown
) then
662 // this can happen if mplat was moved out; find new `wallEndY`
663 findFloor(true); // force trace, just in case
664 if (floorType
= TFloorType
.LiquidOut
) then env
:= TEnvType
.ELiquid
else env
:= TEnvType
.EAir
;
665 mapGrid
.traceOrthoRayWhileIn(ex
, wallEndY
, x
+stickDX
, y
, x
+stickDX
, floorY
+1, (GridTagWall
or GridTagDoor
or GridTagStep
));
669 if (wallEndY
<= floorY
) and (y
>= floorY
) then
673 TFloorType
.Wall
: // hit the ground
675 // check if our ground wasn't moved since the last scan
676 findFloor(true); // force trace
680 goto _done
; // nothing to do anymore
682 // otherwise, do it again
685 TFloorType
.LiquidIn
: // entering the liquid
687 // rescan, so we'll know when we'll exit the liquid
688 findFloor(true); // force rescan
690 TFloorType
.LiquidOut
: // exiting the liquid
692 // rescan, so we'll know when we'll enter something interesting
693 findFloor(true); // force rescan
694 if (floorType
= TFloorType
.Wall
) and (floorY
= y
) then begin sleep(); goto _done
; end;
699 if (floorY
<= wallEndY
) and (y
>= wallEndY
) then
701 // just unstuck from the wall, switch to freefall mode
707 // otherwise, try to drip
708 if drip() then goto _done
;
711 // nope, process as usual
714 // it is important to have it here
718 inAir
:= checkAirStreams();
720 // gravity, if not stuck
721 if (state
<> TPartState
.Stuck
) and (abs(velX
) < 0.1) and (abs(velY
) < 0.1) then
723 floorJustTraced
:= (floorY
= Unknown
);
724 if floorJustTraced
then findFloor();
730 TFloorType
.Wall
: // hit the ground
732 // check if our ground wasn't moved since the last scan
733 if not floorJustTraced
then
735 findFloor(true); // force trace
736 if (floorType
= TFloorType
.LiquidOut
) then env
:= TEnvType
.ELiquid
else env
:= TEnvType
.EAir
;
737 if (y
<> floorY
) then goto _gravityagain
;
739 // otherwise, nothing to do
741 TFloorType
.LiquidIn
: // entering the liquid
743 // rescan, so we'll know when we'll exit the liquid
744 findFloor(true); // force rescan
747 TFloorType
.LiquidOut
: // exiting the liquid
749 // rescan, so we'll know when we'll enter something interesting
750 findFloor(true); // force rescan
751 if (floorType
<> TFloorType
.Wall
) or (floorY
<> y
) then applyGravity(floorType
= TFloorType
.LiquidIn
);
757 // looks like we're in the air
765 // has some horizontal velocity
767 pan
:= g_Map_traceToNearest(x
, y
, x
+dx
, y
+dy
, GridTagSolid
, @ex
, @ey
);
768 if (pan
= nil) and (dy
>= 0) then
770 // do not stuck inside step
771 if g_Map_traceToNearest(x
, y
, x
, y
, GridTagStep
, nil, nil) = nil then
772 // check for step panel below
773 pan
:= g_Map_traceToNearest(x
, y
, x
, y
+dy
, GridTagStep
, nil, @ey
);
774 inStep
:= pan
<> nil;
777 // stick to panel edges
780 else if ex
> pan
.X
+ pan
.Width
- 1 then
781 ex
:= pan
.X
+ pan
.Width
- 1;
784 checkEnv
:= (x
<> ex
);
792 // check environment (air/liquid)
793 if (g_Map_PanelAtPoint(x
, y
, GridTagLiquid
) <> nil) then env
:= TEnvType
.ELiquid
else env
:= TEnvType
.EAir
;
802 // the only case when we can have both ceiling and wall is corner; stick to wall in this case
803 // check if we stuck to a wall
804 if (dx
< 0) then dx
:= -1 else dx
:= 1;
805 if (g_Map_PanelAtPoint(x
+dx
, y
, GridTagSolid
) <> nil) then
812 // stuck to a ceiling
818 else if (dy
<> 0) then
820 // has only vertical velocity
824 if (ceilingY
= Unknown
) then findCeiling(); // need to do this anyway
826 if (y
<= ceilingY
) then begin y
:= ceilingY
; stickToCeiling(); end; // oops, hit a ceiling
827 // environment didn't changed
834 floorJustTraced
:= (floorY
= Unknown
);
835 if floorJustTraced
then findFloor();
836 if (floorType
= TFloorType
.LiquidOut
) then env
:= TEnvType
.ELiquid
else env
:= TEnvType
.EAir
;
838 //e_LogWritefln('floorY=%s; newy=%s; dY=%s; floorType=%s', [floorY, y, dY, floorType]);
839 if (y
>= floorY
) then
844 //e_LogWritefln(' HIT FLOORY: floorY=%s; newy=%s; dY=%s; floorType=%s', [floorY, y, dY, floorType]);
846 TFloorType
.Wall
: // hit the ground
848 // check if our ground wasn't moved since the last scan
849 if not floorJustTraced
then
851 {$IF DEFINED(D2F_DEBUG_FALL_MPLAT)}
854 findFloor(true); // force trace
855 {$IF DEFINED(D2F_DEBUG_FALL_MPLAT)}
856 if (floorY
<> oldFloorY
) then
858 e_LogWritefln('force rescanning vpart at (%s,%s); oldFloorY=%s; floorY=%s', [x
, y
, oldFloorY
, floorY
]);
861 if (floorType
= TFloorType
.LiquidOut
) then env
:= TEnvType
.ELiquid
else env
:= TEnvType
.EAir
;
862 if (y
<> floorY
) then continue
;
864 // environment didn't changed
865 if not inAir
then hitAFloor();
866 break
; // done with vertical movement
868 TFloorType
.LiquidIn
: // entering the liquid
870 // we're entered the liquid
871 env
:= TEnvType
.ELiquid
;
872 // rescan, so we'll know when we'll exit the liquid
873 findFloor(true); // force rescan
875 TFloorType
.LiquidOut
: // exiting the liquid
877 // we're exited the liquid
878 env
:= TEnvType
.EAir
;
879 // rescan, so we'll know when we'll enter something interesting
880 findFloor(true); // force rescan
881 if (floorType
= TFloorType
.Wall
) and (floorY
= y
) then
883 if not inAir
then hitAFloor();
884 break
; // done with vertical movement
891 break
; // done with vertical movement
904 if (g_Map_PanelAtPoint(x
, y
, GridTagObstacle
) <> nil) then begin die(); exit
; end;
908 if (x
< g_Map_MinX
) or (y
< g_Map_MinY
) or (x
> g_Map_MaxX
) or (y
> g_Map_MaxY
) then begin die(); end;
913 // blood will dissolve in other liquids
914 if (particleType
= TPartType
.Blood
) then
916 if (env
= TEnvType
.ELiquid
) then
922 if (liveTime
<= 0) then begin die(); exit
; end;
923 ex
:= 255-trunc(255.0*time
/liveTime
);
924 if (ex
<= 10) then begin die(); exit
; end;
925 if (ex
> 250) then ex
:= 255;
931 // water will disappear in any liquid
932 if (env
= TEnvType
.ELiquid
) then begin die(); exit
; end;
938 if (liveTime
<= 0) then begin die(); exit
; end;
939 ex
:= 255-trunc(255.0*time
/liveTime
);
940 if (ex
<= 10) then begin die(); exit
; end;
941 if (ex
> 250) then ex
:= 255;
947 // ////////////////////////////////////////////////////////////////////////// //
948 procedure g_GFX_SparkVel (fX
, fY
: Integer; count
: Word; vx
, vy
: Integer; devX
, devY
: Byte); forward;
950 procedure g_GFX_Blood (fX
, fY
: Integer; count
: Word; vx
, vy
: Integer;
951 devX
, devY
: Word; cr
, cg
, cb
: Byte; kind
: Byte = BLOOD_NORMAL
);
953 function genColor (cbase
, crnd
: Integer; def
: Byte=0): Byte;
958 if (cbase
< 0) then result
:= 0
959 else if (cbase
> 255) then result
:= 255
960 else result
:= Byte(cbase
);
970 devX1
, devX2
, devY1
, devY2
: Integer;
975 if not gpart_dbg_enabled
then exit
;
977 if (kind
= BLOOD_SPARKS
) then
979 g_GFX_SparkVel(fX
, fY
, 2+Random(2), -vx
div 2, -vy
div 2, devX
, devY
);
982 else if (kind
= BLOOD_CSPARKS
) OR (kind
= BLOOD_COMBINE
) then
984 g_GFX_SparkVel(fX
, fY
, count
, -vx
div 2, -vy
div 2, devX
, devY
);
985 if kind
<> BLOOD_COMBINE
then exit
988 l
:= Length(Particles
);
989 if (l
= 0) then exit
;
990 if (count
> l
) then count
:= l
;
997 for a
:= 1 to count
do
999 with Particles
[CurrentParticle
] do
1001 x
:= fX
-devX1
+Random(devX2
);
1002 y
:= fY
-devY1
+Random(devY2
);
1006 // check for level bounds
1007 if (x
< g_Map_MinX
) or (y
< g_Map_MinY
) or (x
> g_Map_MaxX
) or (y
> g_Map_MaxY
) then continue
;
1009 // in what environment we are starting in?
1010 pan
:= g_Map_PanelAtPoint(x
, y
, (GridTagSolid
or GridTagLiquid
));
1011 if (pan
<> nil) then
1013 // either in a wall, or in a liquid
1014 if ((pan
.tag
and GridTagSolid
) <> 0) then continue
; // don't spawn in walls
1015 env
:= TEnvType
.ELiquid
;
1019 env
:= TEnvType
.EAir
;
1022 velX
:= vx
+(Random
-Random
)*3;
1023 velY
:= vy
+(Random
-Random
)*3;
1027 if (velY
-4 < -4) then velY
:= -4 else velY
:= velY
-4;
1030 accelX
:= -sign(velX
)*Random
/100;
1033 crnd
:= 20*Random(6)-50;
1035 red
:= genColor(cr
, CRnd
, 0);
1036 green
:= genColor(cg
, CRnd
, 0);
1037 blue
:= genColor(cb
, CRnd
, 0);
1040 particleType
:= TPartType
.Blood
;
1041 state
:= TPartState
.Normal
;
1043 liveTime
:= 120+Random(40);
1046 ceilingY
:= Unknown
;
1049 if (CurrentParticle
>= MaxParticles
-1) then CurrentParticle
:= 0 else CurrentParticle
+= 1;
1054 procedure g_GFX_Water (fX
, fY
: Integer; count
: Word; fVelX
, fVelY
: Single; devX
, devY
, color
: Byte;
1055 simple
: Boolean=false; cr
: Byte=0; cg
: Byte=0; cb
: Byte=0);
1058 devX1
, devX2
, devY1
, devY2
: Integer;
1062 if not gpart_dbg_enabled
then exit
;
1064 l
:= Length(Particles
);
1065 if (l
= 0) then exit
;
1066 if (count
> l
) then count
:= l
;
1068 if (abs(fVelX
) < 3.0) then fVelX
:= 3.0-6.0*Random
;
1070 devX1
:= devX
div 2;
1072 devY1
:= devY
div 2;
1075 if (not simple
) and (color
> 3) then color
:= 0;
1077 for a
:= 1 to count
do
1079 with Particles
[CurrentParticle
] do
1083 x
:= fX
-devX1
+Random(devX2
);
1084 y
:= fY
-devY1
+Random(devY2
);
1086 if (abs(fVelX
) < 0.5) then velX
:= 1.0-2.0*Random
else velX
:= fVelX
*Random
;
1087 if (Random(10) < 7) then velX
:= -velX
;
1088 velY
:= fVelY
*Random
;
1106 // check for level bounds
1107 if (x
< g_Map_MinX
) or (y
< g_Map_MinY
) or (x
> g_Map_MaxX
) or (y
> g_Map_MaxY
) then continue
;
1109 // this hack will allow water spawned in water to fly out
1110 // it can happen when player fell from a huge height (see "DOOM2D.WAD:\MAP03", for example)
1111 if (fVelY
>= 0) then
1113 // in what environment we are starting in?
1114 pan
:= g_Map_PanelAtPoint(x
, y
, (GridTagObstacle
or GridTagLiquid
));
1118 pan
:= g_Map_PanelAtPoint(x
, y
, GridTagObstacle
);
1120 if (pan
<> nil) then continue
;
1121 env
:= TEnvType
.EAir
;
1127 red
:= 155+Random(9)*10;
1128 green
:= trunc(150*Random
);
1133 red
:= trunc(150*Random
);
1134 green
:= 175+Random(9)*10;
1139 red
:= trunc(200*Random
);
1141 blue
:= 175+Random(9)*10;
1143 4: // Ñâîé öâåò, ñâåòëåå
1145 red
:= 20+Random(19)*10;
1148 red
:= nmin(red
+cr
, 255);
1149 green
:= nmin(green
+cg
, 255);
1150 blue
:= nmin(blue
+cb
, 255);
1152 5: // Ñâîé öâåò, òåìÃåå
1154 red
:= 20+Random(19)*10;
1157 red
:= nmax(cr
-red
, 0);
1158 green
:= nmax(cg
-green
, 0);
1159 blue
:= nmax(cb
-blue
, 0);
1163 red
:= 90+random(12)*10;
1170 particleType
:= TPartType
.Water
;
1171 state
:= TPartState
.Normal
;
1173 liveTime
:= 60+Random(60);
1176 ceilingY
:= Unknown
;
1179 if (CurrentParticle
>= MaxParticles
-1) then CurrentParticle
:= 0 else CurrentParticle
+= 1;
1184 procedure g_GFX_SimpleWater (fX
, fY
: Integer; count
: Word; fVelX
, fVelY
: Single; defColor
, cr
, cg
, cb
: Byte);
1186 g_GFX_Water(fX
, fY
, count
, 0, 0, 0, 0, defColor
, true, cr
, cg
, cb
);
1190 // ////////////////////////////////////////////////////////////////////////// //
1191 procedure TParticle
.thinkerBubble ();
1202 if (y
<= ceilingY
) then begin die(); exit
; end;
1206 if (y
>= floorY
) then begin die(); exit
; end;
1208 if (y
< g_Map_MinY
) or (y
> g_Map_MaxY
) then begin die(); exit
; end;
1211 if (velY
> -4) then velY
+= accelY
;
1213 if waitTime
> 0 then
1220 {.$DEFINE D2F_DEBUG_BUBBLES}
1221 procedure g_GFX_Bubbles (fX
, fY
: Integer; count
: Word; devX
, devY
: Byte);
1223 a
, liquidx
: Integer;
1224 devX1
, devX2
, devY1
, devY2
: Integer;
1226 {$IF DEFINED(D2F_DEBUG_BUBBLES)}
1231 if not gpart_dbg_enabled
then exit
;
1233 l
:= Length(Particles
);
1234 if (l
= 0) then exit
;
1235 if (count
> l
) then count
:= l
;
1237 devX1
:= devX
div 2;
1239 devY1
:= devY
div 2;
1242 for a
:= 1 to count
do
1244 with Particles
[CurrentParticle
] do
1246 x
:= fX
-devX1
+Random(devX2
);
1247 y
:= fY
-devY1
+Random(devY2
);
1251 // check for level bounds
1252 if (x
< g_Map_MinX
) or (y
< g_Map_MinY
) or (x
> g_Map_MaxX
) or (y
> g_Map_MaxY
) then continue
;
1255 // don't spawn bubbles outside of the liquid
1256 if not isLiquidAt(X, Y) {ByteBool(gCollideMap[Y, X] and MARK_LIQUID)} then
1260 // trace liquid, so we'll know where it ends; do it in 8px steps for speed
1261 // tracer will return `false` if we started outside of the liquid
1263 {$IF DEFINED(D2F_DEBUG_BUBBLES)}
1264 stt
:= getTimeMicro();
1265 ptr
:= mapGrid
.traceOrthoRayWhileIn(liquidx
, liquidTopY
, x
, y
, x
, 0, GridTagWater
or GridTagAcid1
or GridTagAcid2
);
1266 stt
:= getTimeMicro()-stt
;
1267 e_LogWritefln('traceOrthoRayWhileIn: time=%s (%s); liquidTopY=%s', [Integer(stt
), ptr
, liquidTopY
]);
1269 stt
:= getTimeMicro();
1270 nptr
:= g_Map_TraceLiquidNonPrecise(x
, y
, 0, -8, liquidx
, liquidTopY
);
1271 stt
:= getTimeMicro()-stt
;
1272 e_LogWritefln('g_Map_TraceLiquidNonPrecise: time=%s (%s); liquidTopY=%s', [Integer(stt
), nptr
, liquidTopY
]);
1273 if not nptr
then continue
;
1275 if not g_Map_TraceLiquidNonPrecise(x
, y
, 0, -8, liquidx
, ceilingY
) then continue
;
1276 if not g_Map_TraceLiquidNonPrecise(x
, y
, 0, +8, liquidx
, floorY
) then continue
;
1289 state
:= TPartState
.Normal
;
1290 particleType
:= TPartType
.Bubbles
;
1296 if (CurrentParticle
>= MaxParticles
-1) then CurrentParticle
:= 0 else CurrentParticle
+= 1;
1301 // ////////////////////////////////////////////////////////////////////////// //
1302 procedure TParticle
.thinkerSpark ();
1310 if not gpart_dbg_phys_enabled
then begin x
+= round(velX
); y
+= round(velY
); goto _done
; end;
1315 //writeln('spark0: pos=(', x, ',', y, '); delta=(', dx, ',', dy, '); state=', state, '; ceilingY=', ceilingY, '; floorY=', floorY);
1318 if (abs(velX
) < 0.1) and (abs(velY
) < 0.1) then
1327 // has some horizontal velocity
1328 pan
:= g_Map_traceToNearest(x
, y
, x
+dx
, y
+dy
, (GridTagSolid
or GridTagLiquid
), @ex
, @ey
);
1329 if (x
<> ex
) then begin floorY
:= Unknown
; ceilingY
:= Unknown
; end; // dunno yet
1332 if (pan
<> nil) then
1334 if ((pan
.tag
and GridTagLiquid
) <> 0) then begin die(); exit
; end; // die in liquid
1335 // hit the wall; falling down vertically
1340 else if (dy
<> 0) then
1342 // has some vertical velocity
1346 if (ceilingY
= Unknown
) then findCeiling(); // need to do this anyway
1348 if (y
<= ceilingY
) then
1350 // oops, hit a ceiling
1353 accelY
:= abs(accelY
);
1355 // environment didn't changed
1360 if (floorY
= Unknown
) then findFloor(); // need to do this anyway
1362 if (y
>= floorY
) then
1364 // hit something except a floor?
1365 if (floorType
<> TFloorType
.Wall
) then begin die(); exit
; end; // yep: just die
1366 // otherwise, go to sleep
1369 // environment didn't changed
1375 if (x
< g_Map_MinX
) or (y
< g_Map_MinY
) or (x
> g_Map_MaxX
) or (y
> g_Map_MaxY
) then begin die(); end;
1377 if (velX
<> 0.0) then velX
+= accelX
;
1379 if (velY
<> 0.0) then
1381 if (accelY
< 10) then accelY
+= 0.08;
1385 //writeln('spark1: pos=(', x, ',', y, '); delta=(', velX:6:3, ',', velY:6:3, '); state=', state, '; ceilingY=', ceilingY, '; floorY=', floorY);
1387 if waitTime
> 0 then
1394 // ////////////////////////////////////////////////////////////////////////// //
1395 procedure g_GFX_SparkVel (fX
, fY
: Integer; count
: Word; vx
, vy
: Integer; devX
, devY
: Byte);
1398 devX1
, devX2
, devY1
, devY2
: Integer;
1402 if not gpart_dbg_enabled
then exit
;
1404 l
:= Length(Particles
);
1405 if (l
= 0) then exit
;
1406 if (count
> l
) then count
:= l
;
1408 devX1
:= devX
div 2;
1410 devY1
:= devY
div 2;
1413 for a
:= 1 to count
do
1415 with Particles
[CurrentParticle
] do
1417 x
:= fX
-devX1
+Random(devX2
);
1418 y
:= fY
-devY1
+Random(devY2
);
1422 // check for level bounds
1423 if (x
< g_Map_MinX
) or (y
< g_Map_MinY
) or (x
> g_Map_MaxX
) or (y
> g_Map_MaxY
) then continue
;
1425 // in what environment we are starting in?
1426 pan
:= g_Map_PanelAtPoint(x
, y
, (GridTagSolid
or GridTagLiquid
));
1427 if (pan
<> nil) then
1429 // either in a wall, or in a liquid
1430 //if ((pan.tag and GridTagSolid) <> 0) then continue; // don't spawn in walls
1431 //env := TEnvType.ELiquid;
1436 env
:= TEnvType
.EAir
;
1439 velX
:= vx
+(Random
-Random
)*3;
1440 velY
:= vy
+(Random
-Random
)*3;
1444 if (velY
-4 < -4) then velY
:= -4 else velY
:= velY
-4;
1447 accelX
:= -sign(velX
)*Random
/100;
1451 green
:= 100+Random(155);
1455 particleType
:= TPartType
.Spark
;
1456 state
:= TPartState
.Normal
;
1458 liveTime
:= 30+Random(60);
1461 ceilingY
:= Unknown
;
1464 if (CurrentParticle
>= MaxParticles
-1) then CurrentParticle
:= 0 else CurrentParticle
+= 1;
1469 procedure g_GFX_Spark (fX
, fY
: Integer; count
: Word; angle
: SmallInt; devX
, devY
: Byte);
1473 devX1
, devX2
, devY1
, devY2
: Integer;
1474 baseVelX
, baseVelY
: Single;
1478 if not gpart_dbg_enabled
then exit
;
1480 l
:= Length(Particles
);
1481 if (l
= 0) then exit
;
1482 if (count
> l
) then count
:= l
;
1486 devX1
:= devX
div 2;
1488 devY1
:= devY
div 2;
1491 b
:= DegToRad(angle
);
1493 baseVelY
:= 1.6*sin(b
);
1494 if (abs(baseVelX
) < 0.01) then baseVelX
:= 0.0;
1495 if (abs(baseVelY
) < 0.01) then baseVelY
:= 0.0;
1497 for a
:= 1 to count
do
1499 with Particles
[CurrentParticle
] do
1501 x
:= fX
-devX1
+Random(devX2
);
1502 y
:= fY
-devY1
+Random(devY2
);
1506 // check for level bounds
1507 if (x
< g_Map_MinX
) or (y
< g_Map_MinY
) or (x
> g_Map_MaxX
) or (y
> g_Map_MaxY
) then continue
;
1509 // in what environment we are starting in?
1510 pan
:= g_Map_PanelAtPoint(x
, y
, (GridTagSolid
or GridTagLiquid
));
1511 if (pan
<> nil) then
1513 // either in a wall, or in a liquid
1514 //if ((pan.tag and GridTagSolid) <> 0) then continue; // don't spawn in walls
1515 //env := TEnvType.ELiquid;
1520 env
:= TEnvType
.EAir
;
1523 velX
:= baseVelX
*Random
;
1524 velY
:= baseVelY
-Random
;
1529 green
:= 100+Random(155);
1533 particleType
:= TPartType
.Spark
;
1534 state
:= TPartState
.Normal
;
1536 liveTime
:= 30+Random(60);
1539 ceilingY
:= Unknown
;
1542 if (CurrentParticle
>= MaxParticles
-1) then CurrentParticle
:= 0 else CurrentParticle
+= 1;
1547 // ////////////////////////////////////////////////////////////////////////// //
1548 procedure g_GFX_SetMax (count
: Integer);
1552 if count
> 50000 then count
:= 50000;
1553 if (count
< 1) then count
:= 1;
1554 SetLength(Particles
, count
);
1555 for a
:= 0 to High(Particles
) do Particles
[a
].die();
1556 MaxParticles
:= count
;
1557 CurrentParticle
:= 0;
1561 function g_GFX_GetMax (): Integer;
1563 result
:= MaxParticles
;
1566 // ////////////////////////////////////////////////////////////////////////// //
1567 procedure g_GFX_Init ();
1569 //g_Game_SetLoadingText(_lc[I_LOAD_COLLIDE_MAP]+' 1/6', 0, False);
1570 //SetLength(gCollideMap, gMapInfo.Height+1);
1571 //for a := 0 to High(gCollideMap) do SetLength(gCollideMap[a], gMapInfo.Width+1);
1574 gpart_dbg_enabled
:= false;
1579 procedure g_GFX_Free ();
1584 SetLength(Particles
, MaxParticles
);
1585 for a
:= 0 to High(Particles
) do Particles
[a
].die();
1586 CurrentParticle
:= 0;
1595 // ////////////////////////////////////////////////////////////////////////// //
1596 procedure g_GFX_Update ();
1602 if not gpart_dbg_enabled
then exit
;
1604 if (Particles
<> nil) then
1606 w
:= gMapInfo
.Width
;
1607 h
:= gMapInfo
.Height
;
1609 len
:= High(Particles
);
1611 for a
:= 0 to len
do
1613 if Particles
[a
].alive
then
1615 with Particles
[a
] do
1617 if (time
= liveTime
) then begin die(); continue
; end;
1618 if (x
+1 >= w
) or (y
+1 >= h
) or (x
<= 0) or (y
<= 0) then begin die(); end;
1623 end; // Particles <> nil