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}
43 MARK_BLOCKED
= MARK_WALL
or MARK_DOOR
;
44 MARK_LIQUID
= MARK_WATER
or MARK_ACID
;
45 MARK_LIFT
= MARK_LIFTDOWN
or MARK_LIFTUP
or MARK_LIFTLEFT
or MARK_LIFTRIGHT
;
48 procedure g_GFX_Init ();
49 procedure g_GFX_Free ();
51 procedure g_GFX_Blood (fX
, fY
: Integer; count
: Word; vx
, vy
: Integer;
52 devX
, devY
: Word; cr
, cg
, cb
: Byte; kind
: Byte=BLOOD_NORMAL
);
53 procedure g_GFX_Spark (fX
, fY
: Integer; count
: Word; angle
: SmallInt
; devX
, devY
: Byte);
54 procedure g_GFX_Water (fX
, fY
: Integer; count
: Word; fVelX
, fVelY
: Single; devX
, devY
, color
: Byte;
55 simple
: Boolean=false; cr
: Byte=0; cg
: Byte=0; cb
: Byte=0);
56 procedure g_GFX_SimpleWater (fX
, fY
: Integer; count
: Word; fVelX
, fVelY
: Single; defColor
, cr
, cg
, cb
: Byte);
57 procedure g_GFX_Bubbles (fX
, fY
: Integer; count
: Word; devX
, devY
: Byte);
59 procedure g_GFX_SetMax (count
: Integer);
60 function g_GFX_GetMax (): Integer;
62 procedure g_GFX_OnceAnim (X
, Y
: Integer; Anim
: TAnimation
; AnimType
: Byte = 0);
64 procedure g_Mark (x
, y
, Width
, Height
: Integer; t
: Byte; st
: Boolean=true);
66 procedure g_GFX_Update ();
67 procedure g_GFX_Draw ();
71 gpart_dbg_enabled
: Boolean = true;
72 gpart_dbg_phys_enabled
: Boolean = true;
75 //WARNING: only for Holmes!
76 function awmIsSetHolmes (x
, y
: Integer): Boolean; inline;
82 {$INCLUDE ../nogl/noGLuses.inc}
83 g_map
, g_panel
, g_basic
, Math
, e_graphics
,
84 g_options
, g_console
, SysUtils
, g_triggers
, MAPDEF
,
85 g_game
, g_language
, g_net
, utils
, xprofiler
;
89 Unknown
= Integer($7fffffff);
93 TPartType
= (Blood
, Spark
, Bubbles
, Water
);
94 TPartState
= (Free
, Normal
, Stuck
, Sleeping
);
95 TFloorType
= (Wall
, LiquidIn
, LiquidOut
);
96 // Wall: floorY is just before floor
97 // LiquidIn: floorY is liquid *start* (i.e. just in a liquid)
98 // LiquidOut: floorY is liquid *end* (i.e. just out of a liquid)
99 TEnvType
= (EAir
, ELiquid
, EWall
); // where particle is now
101 // note: this MUST be record, so we can keep it in
102 // dynamic array and has sequential memory access pattern
103 PParticle
= ^TParticle
;
108 accelX
, accelY
: Single;
110 particleType
: TPartType
;
111 red
, green
, blue
: Byte;
113 time
, liveTime
, waitTime
: Word;
114 stickDX
: Integer; // STATE_STICK: -1,1: stuck to a wall; 0: stuck to ceiling
115 justSticked
: Boolean; // not used
116 floorY
: Integer; // actually, floor-1; `Unknown`: unknown
117 floorType
: TFloorType
;
118 env
: TEnvType
; // where particle is now
119 ceilingY
: Integer; // actually, ceiling+1; `Unknown`: unknown
120 wallEndY
: Integer; // if we stuck to a wall, this is where wall ends
122 //k8: sorry, i have to emulate virtual methods this way, 'cause i haet `Object`
123 procedure thinkerBloodAndWater ();
124 procedure thinkerSpark ();
125 procedure thinkerBubble ();
127 procedure findFloor (force
: Boolean=false); // this updates `floorY` if forced or Unknown
128 procedure findCeiling (force
: Boolean=false); // this updates `ceilingY` if forced or Unknown
130 procedure freeze (); inline; // remove velocities and acceleration
131 procedure sleep (); inline; // switch to sleep mode
133 function checkAirStreams (): Boolean; // `true`: affected by air stream
135 function alive (): Boolean; inline;
136 procedure die (); inline;
137 procedure think (); inline;
144 Animation
: TAnimation
;
149 Particles
: array of TParticle
= nil;
150 OnceAnims
: array of TOnceAnim
= nil;
151 MaxParticles
: Integer = 0;
152 CurrentParticle
: Integer = 0;
153 // awakeMap has one bit for each map grid cell; on g_Mark,
154 // corresponding bits will be set, and in `think()` all particles
155 // in marked cells will be awaken
156 awakeMap
: packed array of LongWord
= nil;
157 awakeMapH
: Integer = -1;
158 awakeMapW
: Integer = -1;
159 awakeMinX
, awakeMinY
: Integer;
160 awakeDirty
: Boolean = false;
161 {$IF DEFINED(D2F_DEBUG_PART_AWAKE)}
162 awakeMapHlm
: packed array of LongWord
= nil;
166 // ////////////////////////////////////////////////////////////////////////// //
167 function awmIsSetHolmes (x
, y
: Integer): Boolean; inline;
169 {$IF DEFINED(D2F_DEBUG_PART_AWAKE)}
170 if (Length(awakeMapHlm
) = 0) then begin result
:= false; exit
; end;
171 x
:= (x
-awakeMinX
) div mapGrid
.tileSize
;
172 y
:= (y
-awakeMinY
) div mapGrid
.tileSize
;
173 if (x
>= 0) and (y
>= 0) and (x
div 32 < awakeMapW
) and (y
< awakeMapH
) then
175 if (y
*awakeMapW
+x
div 32 < Length(awakeMapHlm
)) then
177 result
:= ((awakeMapHlm
[y
*awakeMapW
+x
div 32] and (LongWord(1) shl (x
mod 32))) <> 0);
194 // ////////////////////////////////////////////////////////////////////////// //
195 // HACK! using mapgrid
196 procedure awmClear (); inline;
198 {$IF DEFINED(D2F_DEBUG_PART_AWAKE)}
199 if (Length(awakeMap
) > 0) then
201 if (Length(awakeMapHlm
) <> Length(awakeMap
)) then SetLength(awakeMapHlm
, Length(awakeMap
));
202 Move(awakeMap
[0], awakeMapHlm
[0], Length(awakeMap
)*sizeof(awakeMap
[0]));
205 if awakeDirty
and (awakeMapW
> 0) then
207 FillDWord(awakeMap
[0], Length(awakeMap
), 0);
213 procedure awmSetup ();
215 assert(mapGrid
<> nil);
216 awakeMapW
:= (mapGrid
.gridWidth
+mapGrid
.tileSize
-1) div mapGrid
.tileSize
;
217 awakeMapW
:= (awakeMapW
+31) div 32; // LongWord has 32 bits ;-)
218 awakeMapH
:= (mapGrid
.gridHeight
+mapGrid
.tileSize
-1) div mapGrid
.tileSize
;
219 awakeMinX
:= mapGrid
.gridX0
;
220 awakeMinY
:= mapGrid
.gridY0
;
221 SetLength(awakeMap
, awakeMapW
*awakeMapH
);
222 {$IF DEFINED(D2F_DEBUG_PART_AWAKE)}
223 SetLength(awakeMapHlm
, awakeMapW
*awakeMapH
);
224 FillDWord(awakeMapHlm
[0], Length(awakeMapHlm
), 0);
226 //{$IF DEFINED(D2F_DEBUG)}
227 e_LogWritefln('particle awake map: %sx%s (for grid of size %sx%s)', [awakeMapW
, awakeMapH
, mapGrid
.gridWidth
, mapGrid
.gridHeight
]);
234 function awmIsSet (x
, y
: Integer): Boolean; inline;
236 x
:= (x
-awakeMinX
) div mapGrid
.tileSize
;
237 y
:= (y
-awakeMinY
) div mapGrid
.tileSize
;
238 if (x
>= 0) and (y
>= 0) and (x
div 32 < awakeMapW
) and (y
< awakeMapH
) then
240 {$IF DEFINED(D2F_DEBUG)}
241 assert(y
*awakeMapW
+x
div 32 < Length(awakeMap
));
243 result
:= ((awakeMap
[y
*awakeMapW
+x
div 32] and (LongWord(1) shl (x
mod 32))) <> 0);
252 procedure awmSet (x
, y
: Integer); inline;
256 x
:= (x
-awakeMinX
) div mapGrid
.tileSize
;
257 y
:= (y
-awakeMinY
) div mapGrid
.tileSize
;
258 if (x
>= 0) and (y
>= 0) and (x
div 32 < awakeMapW
) and (y
< awakeMapH
) then
260 {$IF DEFINED(D2F_DEBUG)}
261 assert(y
*awakeMapW
+x
div 32 < Length(awakeMap
));
263 v
:= @awakeMap
[y
*awakeMapW
+x
div 32];
264 v
^ := v
^ or (LongWord(1) shl (x
mod 32));
270 // ////////////////////////////////////////////////////////////////////////// //
274 procedure g_Mark (x
, y
, Width
, Height
: Integer; t
: Byte; st
: Boolean=true);
277 dx
, dy
, ex
, ey
: Integer;
280 if (not gpart_dbg_enabled
) or (not gpart_dbg_phys_enabled
) then exit
;
281 if (awakeMapW
< 1) or (awakeMapH
< 1) then exit
;
283 if (Width
< 1) or (Height
< 1) then exit
;
285 // make some border, so we'll hit particles around the panel
286 ex
:= x
+Width
+Extrude
-1-awakeMinX
;
287 ey
:= y
+Height
+Extrude
-1-awakeMinY
;
288 x
:= (x
-Extrude
)-awakeMinX
;
289 y
:= (y
-Extrude
)-awakeMinY
;
291 x
:= x
div mapGrid
.tileSize
;
292 y
:= y
div mapGrid
.tileSize
;
293 ex
:= ex
div mapGrid
.tileSize
;
294 ey
:= ey
div mapGrid
.tileSize
;
296 // has something to do?
297 if (ex
< 0) or (ey
< 0) or (x
>= awakeMapW
*32) or (y
>= awakeMapH
) then exit
;
298 if (x
< 0) then x
:= 0;
299 if (y
< 0) then y
:= 0;
300 if (ex
>= awakeMapW
*32) then ex
:= awakeMapW
*32-1;
301 if (ey
>= awakeMapH
) then ey
:= awakeMapH
;
308 {$IF DEFINED(D2F_DEBUG)}
309 assert((dx
>= 0) and (dy
>= 0) and (dx
div 32 < awakeMapW
) and (dy
< awakeMapH
));
310 assert(dy
*awakeMapW
+dx
div 32 < Length(awakeMap
));
312 v
:= @awakeMap
[dy
*awakeMapW
+dx
div 32];
313 v
^ := v
^ or (LongWord(1) shl (dx
mod 32));
319 // ////////////////////////////////////////////////////////////////////////// //
320 function TParticle
.alive (): Boolean; inline; begin result
:= (state
<> TPartState
.Free
); end;
321 procedure TParticle
.die (); inline; begin state
:= TPartState
.Free
; end;
323 // remove velocities and acceleration
324 procedure TParticle
.freeze (); inline;
326 // stop right there, you criminal scum!
334 // `true`: affected by air stream
335 function TParticle
.checkAirStreams (): Boolean;
340 pan
:= g_Map_PanelAtPoint(x
, y
, GridTagLift
);
341 result
:= (pan
<> nil) and WordBool(pan
.PanelType
and (PANEL_LIFTUP
or PANEL_LIFTDOWN
or PANEL_LIFTLEFT
or PANEL_LIFTRIGHT
));
348 if (velY
> -1-r
) then velY
-= 0.8;
349 if (abs(velX
) > 0.1) then velX
-= velX
/10.0;
350 velX
+= (Random
-Random
)*0.2;
355 if (velY
< 1+r
) then velY
+= 0.8;
360 if (velX
> -8-r
) then velX
-= (8+r
) div 2;
365 if (velX
< 8+r
) then velX
+= (8+r
) div 2;
372 if result
and (state
= TPartState
.Sleeping
) then state
:= TPartState
.Normal
;
377 // switch to sleep mode
378 procedure TParticle
.sleep (); inline;
380 if not checkAirStreams() then
382 state
:= TPartState
.Sleeping
;
388 procedure TParticle
.findFloor (force
: Boolean=false);
393 if (not force
) and (floorY
<> Unknown
) then exit
;
394 // stuck in the wall? rescan, 'cause it can be mplat
395 if (env
= TEnvType
.EWall
) then
397 pan
:= g_Map_PanelAtPoint(x
, y
, (GridTagObstacle
or GridTagLiquid
));
400 // either in a wall, or in a liquid
401 if ((pan
.tag
and GridTagObstacle
) <> 0) then
403 // we are in the wall, wtf?!
405 env
:= TEnvType
.EWall
;
406 floorType
:= TFloorType
.Wall
;
407 state
:= TPartState
.Sleeping
; // anyway
410 // we are in liquid, trace to liquid end
411 env
:= TEnvType
.ELiquid
;
414 // are we in a liquid?
415 if (env
= TEnvType
.ELiquid
) then
417 // trace out of the liquid
418 //env := TEnvType.ELiquid;
419 floorType
:= TFloorType
.LiquidOut
;
420 //e_LogWritefln('tracing out of a liquid; floorY=%s; y=%s', [floorY, y]);
421 mapGrid
.traceOrthoRayWhileIn(ex
, floorY
, x
, y
, x
, g_Map_MaxY
, GridTagLiquid
);
422 floorY
+= 1; // so `floorY` is just out of a liquid
423 //e_LogWritefln(' traced out of a liquid; floorY=%s; y=%s', [floorY, y]);
428 assert(env
= TEnvType
.EAir
);
429 //env := TEnvType.EAir;
430 pan
:= g_Map_traceToNearest(x
, y
, x
, g_Map_MaxY
, (GridTagObstacle
or GridTagLiquid
), @ex
, @floorY
);
434 if ((pan
.tag
and GridTagObstacle
) <> 0) then
437 floorType
:= TFloorType
.Wall
;
442 floorType
:= TFloorType
.LiquidIn
; // entering liquid
443 floorY
+= 1; // so `floorY` is just in a liquid
448 // out of the level; assume wall, but it doesn't really matter
449 floorType
:= TFloorType
.Wall
;
450 floorY
:= g_Map_MaxY
+2;
456 procedure TParticle
.findCeiling (force
: Boolean=false);
460 if (not force
) and (ceilingY
<> Unknown
) then exit
;
461 if (nil = g_Map_traceToNearest(x
, y
, x
, g_Map_MinY
, GridTagSolid
, @ex
, @ceilingY
)) then
463 ceilingY
:= g_Map_MinY
-2;
468 procedure TParticle
.think (); inline;
471 if (state
= TPartState
.Stuck
) then
473 //writeln('awaking particle at (', x, ',', y, ')');
474 if (stickDX
= 0) then
476 state
:= TPartState
.Normal
; // stuck to a ceiling
480 // stuck to a wall, check if wall is still there
481 if (wallEndY
<> Unknown
) then
484 if (g_Map_PanelAtPoint(x
+stickDX
, y
, GridTagObstacle
) = nil) then
486 // a wall was moved out, start falling
487 state
:= TPartState
.Normal
;
488 if (velY
= 0) then velY
:= 0.1;
489 if (accelY
= 0) then accelY
:= 0.5;
496 state
:= TPartState
.Normal
;
497 if (velY
= 0) then velY
:= 0.1;
498 if (accelY
= 0) then accelY
:= 0.5;
507 // awake sleeping particle, if necessary
510 if awmIsSet(x
, y
) then awake();
513 TPartState.Sleeping, TPartState.Stuck:
514 if awmIsSet(x, y) then awake();
516 if (env = TEnvType.EWall) and awmIsSet(x, y) then awake();
521 TPartType
.Blood
, TPartType
.Water
: thinkerBloodAndWater();
522 TPartType
.Spark
: thinkerSpark();
523 TPartType
.Bubbles
: thinkerBubble();
528 // ////////////////////////////////////////////////////////////////////////// //
529 procedure TParticle
.thinkerBloodAndWater ();
530 procedure stickToCeiling ();
532 state
:= TPartState
.Stuck
;
535 ceilingY
:= y
; // yep
538 procedure stickToWall (dx
: Integer);
542 state
:= TPartState
.Stuck
;
543 if (dx
> 0) then stickDX
:= 1 else stickDX
:= -1;
545 // find next floor transition
548 mapGrid
.traceOrthoRayWhileIn(ex
, wallEndY
, x
+stickDX
, y
, x
+stickDX
, floorY
+1, (GridTagWall
or GridTagDoor
or GridTagStep
));
551 procedure hitAFloor ();
553 state
:= TPartState
.Sleeping
; // we aren't moving anymore
556 floorType
:= TFloorType
.Wall
; // yep
559 // `true`: didn't, get outa thinker
560 function drip (): Boolean;
563 TPartType
.Blood
: result
:= (Random(200) = 100);
564 TPartType
.Water
: result
:= (Random(30) = 15);
565 else raise Exception
.Create('internal error in particle engine: drip');
571 // if we're falling from ceiling, switch to normal mode
572 if (state
= TPartState
.Stuck
) and (stickDX
= 0) then state
:= TPartState
.Normal
;
576 // switch to freefall mode
577 procedure freefall ();
579 state
:= TPartState
.Normal
;
584 procedure applyGravity (inLiquid
: Boolean);
586 state
:= TPartState
.Normal
;
600 _done
, _gravityagain
, _stuckagain
;
605 checkEnv
, inAir
, inStep
: Boolean;
606 floorJustTraced
: Boolean;
607 {$IF DEFINED(D2F_DEBUG_FALL_MPLAT)}
611 if not gpart_dbg_phys_enabled
then begin x
+= round(velX
); y
+= round(velY
); goto _done
; end;
615 // still check for air streams when sleeping (no)
616 if (state
= TPartState
.Sleeping
) then begin {checkAirStreams();} goto _done
; end; // so blood will dissolve
618 // process stuck particles
619 if (state
= TPartState
.Stuck
) then
621 // stuck to a ceiling?
622 if (stickDX
= 0) then
624 // yeah, stuck to a ceiling
625 if (ceilingY
= Unknown
) then findCeiling();
626 // dropped from a ceiling?
627 if (y
> ceilingY
) then
632 state
:= TPartState
.Normal
;
636 // otherwise, try to drip
637 if drip() then goto _done
;
643 if (wallEndY
= Unknown
) then
645 // this can happen if mplat was moved out; find new `wallEndY`
646 findFloor(true); // force trace, just in case
647 if (floorType
= TFloorType
.LiquidOut
) then env
:= TEnvType
.ELiquid
else env
:= TEnvType
.EAir
;
648 mapGrid
.traceOrthoRayWhileIn(ex
, wallEndY
, x
+stickDX
, y
, x
+stickDX
, floorY
+1, (GridTagWall
or GridTagDoor
or GridTagStep
));
652 if (wallEndY
<= floorY
) and (y
>= floorY
) then
656 TFloorType
.Wall
: // hit the ground
658 // check if our ground wasn't moved since the last scan
659 findFloor(true); // force trace
663 goto _done
; // nothing to do anymore
665 // otherwise, do it again
668 TFloorType
.LiquidIn
: // entering the liquid
670 // rescan, so we'll know when we'll exit the liquid
671 findFloor(true); // force rescan
673 TFloorType
.LiquidOut
: // exiting the liquid
675 // rescan, so we'll know when we'll enter something interesting
676 findFloor(true); // force rescan
677 if (floorType
= TFloorType
.Wall
) and (floorY
= y
) then begin sleep(); goto _done
; end;
682 if (floorY
<= wallEndY
) and (y
>= wallEndY
) then
684 // just unstuck from the wall, switch to freefall mode
690 // otherwise, try to drip
691 if drip() then goto _done
;
694 // nope, process as usual
697 // it is important to have it here
701 inAir
:= checkAirStreams();
703 // gravity, if not stuck
704 if (state
<> TPartState
.Stuck
) and (abs(velX
) < 0.1) and (abs(velY
) < 0.1) then
706 floorJustTraced
:= (floorY
= Unknown
);
707 if floorJustTraced
then findFloor();
713 TFloorType
.Wall
: // hit the ground
715 // check if our ground wasn't moved since the last scan
716 if not floorJustTraced
then
718 findFloor(true); // force trace
719 if (floorType
= TFloorType
.LiquidOut
) then env
:= TEnvType
.ELiquid
else env
:= TEnvType
.EAir
;
720 if (y
<> floorY
) then goto _gravityagain
;
722 // otherwise, nothing to do
724 TFloorType
.LiquidIn
: // entering the liquid
726 // rescan, so we'll know when we'll exit the liquid
727 findFloor(true); // force rescan
730 TFloorType
.LiquidOut
: // exiting the liquid
732 // rescan, so we'll know when we'll enter something interesting
733 findFloor(true); // force rescan
734 if (floorType
<> TFloorType
.Wall
) or (floorY
<> y
) then applyGravity(floorType
= TFloorType
.LiquidIn
);
740 // looks like we're in the air
748 // has some horizontal velocity
750 pan
:= g_Map_traceToNearest(x
, y
, x
+dx
, y
+dy
, GridTagSolid
, @ex
, @ey
);
751 if (pan
= nil) and (dy
>= 0) then
753 // do not stuck inside step
754 if g_Map_traceToNearest(x
, y
, x
, y
, GridTagStep
, nil, nil) = nil then
755 // check for step panel below
756 pan
:= g_Map_traceToNearest(x
, y
, x
, y
+dy
, GridTagStep
, nil, @ey
);
757 inStep
:= pan
<> nil;
760 // stick to panel edges
763 else if ex
> pan
.X
+ pan
.Width
- 1 then
764 ex
:= pan
.X
+ pan
.Width
- 1;
767 checkEnv
:= (x
<> ex
);
775 // check environment (air/liquid)
776 if (g_Map_PanelAtPoint(x
, y
, GridTagLiquid
) <> nil) then env
:= TEnvType
.ELiquid
else env
:= TEnvType
.EAir
;
785 // the only case when we can have both ceiling and wall is corner; stick to wall in this case
786 // check if we stuck to a wall
787 if (dx
< 0) then dx
:= -1 else dx
:= 1;
788 if (g_Map_PanelAtPoint(x
+dx
, y
, GridTagSolid
) <> nil) then
795 // stuck to a ceiling
801 else if (dy
<> 0) then
803 // has only vertical velocity
807 if (ceilingY
= Unknown
) then findCeiling(); // need to do this anyway
809 if (y
<= ceilingY
) then begin y
:= ceilingY
; stickToCeiling(); end; // oops, hit a ceiling
810 // environment didn't changed
817 floorJustTraced
:= (floorY
= Unknown
);
818 if floorJustTraced
then findFloor();
819 if (floorType
= TFloorType
.LiquidOut
) then env
:= TEnvType
.ELiquid
else env
:= TEnvType
.EAir
;
821 //e_LogWritefln('floorY=%s; newy=%s; dY=%s; floorType=%s', [floorY, y, dY, floorType]);
822 if (y
>= floorY
) then
827 //e_LogWritefln(' HIT FLOORY: floorY=%s; newy=%s; dY=%s; floorType=%s', [floorY, y, dY, floorType]);
829 TFloorType
.Wall
: // hit the ground
831 // check if our ground wasn't moved since the last scan
832 if not floorJustTraced
then
834 {$IF DEFINED(D2F_DEBUG_FALL_MPLAT)}
837 findFloor(true); // force trace
838 {$IF DEFINED(D2F_DEBUG_FALL_MPLAT)}
839 if (floorY
<> oldFloorY
) then
841 e_LogWritefln('force rescanning vpart at (%s,%s); oldFloorY=%s; floorY=%s', [x
, y
, oldFloorY
, floorY
]);
844 if (floorType
= TFloorType
.LiquidOut
) then env
:= TEnvType
.ELiquid
else env
:= TEnvType
.EAir
;
845 if (y
<> floorY
) then continue
;
847 // environment didn't changed
848 if not inAir
then hitAFloor();
849 break
; // done with vertical movement
851 TFloorType
.LiquidIn
: // entering the liquid
853 // we're entered the liquid
854 env
:= TEnvType
.ELiquid
;
855 // rescan, so we'll know when we'll exit the liquid
856 findFloor(true); // force rescan
858 TFloorType
.LiquidOut
: // exiting the liquid
860 // we're exited the liquid
861 env
:= TEnvType
.EAir
;
862 // rescan, so we'll know when we'll enter something interesting
863 findFloor(true); // force rescan
864 if (floorType
= TFloorType
.Wall
) and (floorY
= y
) then
866 if not inAir
then hitAFloor();
867 break
; // done with vertical movement
874 break
; // done with vertical movement
887 if (g_Map_PanelAtPoint(x
, y
, GridTagObstacle
) <> nil) then begin die(); exit
; end;
891 if (x
< g_Map_MinX
) or (y
< g_Map_MinY
) or (x
> g_Map_MaxX
) or (y
> g_Map_MaxY
) then begin die(); end;
896 // blood will dissolve in other liquids
897 if (particleType
= TPartType
.Blood
) then
899 if (env
= TEnvType
.ELiquid
) then
905 if (liveTime
<= 0) then begin die(); exit
; end;
906 ex
:= 255-trunc(255.0*time
/liveTime
);
907 if (ex
<= 10) then begin die(); exit
; end;
908 if (ex
> 250) then ex
:= 255;
914 // water will disappear in any liquid
915 if (env
= TEnvType
.ELiquid
) then begin die(); exit
; end;
921 if (liveTime
<= 0) then begin die(); exit
; end;
922 ex
:= 255-trunc(255.0*time
/liveTime
);
923 if (ex
<= 10) then begin die(); exit
; end;
924 if (ex
> 250) then ex
:= 255;
930 // ////////////////////////////////////////////////////////////////////////// //
931 procedure g_GFX_SparkVel (fX
, fY
: Integer; count
: Word; vx
, vy
: Integer; devX
, devY
: Byte); forward;
933 procedure g_GFX_Blood (fX
, fY
: Integer; count
: Word; vx
, vy
: Integer;
934 devX
, devY
: Word; cr
, cg
, cb
: Byte; kind
: Byte = BLOOD_NORMAL
);
936 function genColor (cbase
, crnd
: Integer; def
: Byte=0): Byte;
941 if (cbase
< 0) then result
:= 0
942 else if (cbase
> 255) then result
:= 255
943 else result
:= Byte(cbase
);
953 devX1
, devX2
, devY1
, devY2
: Integer;
958 if not gpart_dbg_enabled
then exit
;
960 if (kind
= BLOOD_SPARKS
) then
962 g_GFX_SparkVel(fX
, fY
, 2+Random(2), -vx
div 2, -vy
div 2, devX
, devY
);
965 else if (kind
= BLOOD_CSPARKS
) OR (kind
= BLOOD_COMBINE
) then
967 g_GFX_SparkVel(fX
, fY
, count
, -vx
div 2, -vy
div 2, devX
, devY
);
968 if kind
<> BLOOD_COMBINE
then exit
971 l
:= Length(Particles
);
972 if (l
= 0) then exit
;
973 if (count
> l
) then count
:= l
;
980 for a
:= 1 to count
do
982 with Particles
[CurrentParticle
] do
984 x
:= fX
-devX1
+Random(devX2
);
985 y
:= fY
-devY1
+Random(devY2
);
989 // check for level bounds
990 if (x
< g_Map_MinX
) or (y
< g_Map_MinY
) or (x
> g_Map_MaxX
) or (y
> g_Map_MaxY
) then continue
;
992 // in what environment we are starting in?
993 pan
:= g_Map_PanelAtPoint(x
, y
, (GridTagSolid
or GridTagLiquid
));
996 // either in a wall, or in a liquid
997 if ((pan
.tag
and GridTagSolid
) <> 0) then continue
; // don't spawn in walls
998 env
:= TEnvType
.ELiquid
;
1002 env
:= TEnvType
.EAir
;
1005 velX
:= vx
+(Random
-Random
)*3;
1006 velY
:= vy
+(Random
-Random
)*3;
1010 if (velY
-4 < -4) then velY
:= -4 else velY
:= velY
-4;
1013 accelX
:= -sign(velX
)*Random
/100;
1016 crnd
:= 20*Random(6)-50;
1018 red
:= genColor(cr
, CRnd
, 0);
1019 green
:= genColor(cg
, CRnd
, 0);
1020 blue
:= genColor(cb
, CRnd
, 0);
1023 particleType
:= TPartType
.Blood
;
1024 state
:= TPartState
.Normal
;
1026 liveTime
:= 120+Random(40);
1029 ceilingY
:= Unknown
;
1032 if (CurrentParticle
>= MaxParticles
-1) then CurrentParticle
:= 0 else CurrentParticle
+= 1;
1037 procedure g_GFX_Water (fX
, fY
: Integer; count
: Word; fVelX
, fVelY
: Single; devX
, devY
, color
: Byte;
1038 simple
: Boolean=false; cr
: Byte=0; cg
: Byte=0; cb
: Byte=0);
1041 devX1
, devX2
, devY1
, devY2
: Integer;
1045 if not gpart_dbg_enabled
then exit
;
1047 l
:= Length(Particles
);
1048 if (l
= 0) then exit
;
1049 if (count
> l
) then count
:= l
;
1051 if (abs(fVelX
) < 3.0) then fVelX
:= 3.0-6.0*Random
;
1053 devX1
:= devX
div 2;
1055 devY1
:= devY
div 2;
1058 if (not simple
) and (color
> 3) then color
:= 0;
1060 for a
:= 1 to count
do
1062 with Particles
[CurrentParticle
] do
1066 x
:= fX
-devX1
+Random(devX2
);
1067 y
:= fY
-devY1
+Random(devY2
);
1069 if (abs(fVelX
) < 0.5) then velX
:= 1.0-2.0*Random
else velX
:= fVelX
*Random
;
1070 if (Random(10) < 7) then velX
:= -velX
;
1071 velY
:= fVelY
*Random
;
1089 // check for level bounds
1090 if (x
< g_Map_MinX
) or (y
< g_Map_MinY
) or (x
> g_Map_MaxX
) or (y
> g_Map_MaxY
) then continue
;
1092 // this hack will allow water spawned in water to fly out
1093 // it can happen when player fell from a huge height (see "DOOM2D.WAD:\MAP03", for example)
1094 if (fVelY
>= 0) then
1096 // in what environment we are starting in?
1097 pan
:= g_Map_PanelAtPoint(x
, y
, (GridTagObstacle
or GridTagLiquid
));
1101 pan
:= g_Map_PanelAtPoint(x
, y
, GridTagObstacle
);
1103 if (pan
<> nil) then continue
;
1104 env
:= TEnvType
.EAir
;
1110 red
:= 155+Random(9)*10;
1111 green
:= trunc(150*Random
);
1116 red
:= trunc(150*Random
);
1117 green
:= 175+Random(9)*10;
1122 red
:= trunc(200*Random
);
1124 blue
:= 175+Random(9)*10;
1126 4: // Ñâîé öâåò, ñâåòëåå
1128 red
:= 20+Random(19)*10;
1131 red
:= nmin(red
+cr
, 255);
1132 green
:= nmin(green
+cg
, 255);
1133 blue
:= nmin(blue
+cb
, 255);
1135 5: // Ñâîé öâåò, òåìÃåå
1137 red
:= 20+Random(19)*10;
1140 red
:= nmax(cr
-red
, 0);
1141 green
:= nmax(cg
-green
, 0);
1142 blue
:= nmax(cb
-blue
, 0);
1146 red
:= 90+random(12)*10;
1153 particleType
:= TPartType
.Water
;
1154 state
:= TPartState
.Normal
;
1156 liveTime
:= 60+Random(60);
1159 ceilingY
:= Unknown
;
1162 if (CurrentParticle
>= MaxParticles
-1) then CurrentParticle
:= 0 else CurrentParticle
+= 1;
1167 procedure g_GFX_SimpleWater (fX
, fY
: Integer; count
: Word; fVelX
, fVelY
: Single; defColor
, cr
, cg
, cb
: Byte);
1169 g_GFX_Water(fX
, fY
, count
, 0, 0, 0, 0, defColor
, true, cr
, cg
, cb
);
1173 // ////////////////////////////////////////////////////////////////////////// //
1174 procedure TParticle
.thinkerBubble ();
1185 if (y
<= ceilingY
) then begin die(); exit
; end;
1189 if (y
>= floorY
) then begin die(); exit
; end;
1191 if (y
< g_Map_MinY
) or (y
> g_Map_MaxY
) then begin die(); exit
; end;
1194 if (velY
> -4) then velY
+= accelY
;
1196 if waitTime
> 0 then
1203 {.$DEFINE D2F_DEBUG_BUBBLES}
1204 procedure g_GFX_Bubbles (fX
, fY
: Integer; count
: Word; devX
, devY
: Byte);
1206 a
, liquidx
: Integer;
1207 devX1
, devX2
, devY1
, devY2
: Integer;
1209 {$IF DEFINED(D2F_DEBUG_BUBBLES)}
1214 if not gpart_dbg_enabled
then exit
;
1216 l
:= Length(Particles
);
1217 if (l
= 0) then exit
;
1218 if (count
> l
) then count
:= l
;
1220 devX1
:= devX
div 2;
1222 devY1
:= devY
div 2;
1225 for a
:= 1 to count
do
1227 with Particles
[CurrentParticle
] do
1229 x
:= fX
-devX1
+Random(devX2
);
1230 y
:= fY
-devY1
+Random(devY2
);
1234 // check for level bounds
1235 if (x
< g_Map_MinX
) or (y
< g_Map_MinY
) or (x
> g_Map_MaxX
) or (y
> g_Map_MaxY
) then continue
;
1238 // don't spawn bubbles outside of the liquid
1239 if not isLiquidAt(X, Y) {ByteBool(gCollideMap[Y, X] and MARK_LIQUID)} then
1243 // trace liquid, so we'll know where it ends; do it in 8px steps for speed
1244 // tracer will return `false` if we started outside of the liquid
1246 {$IF DEFINED(D2F_DEBUG_BUBBLES)}
1247 stt
:= getTimeMicro();
1248 ptr
:= mapGrid
.traceOrthoRayWhileIn(liquidx
, liquidTopY
, x
, y
, x
, 0, GridTagWater
or GridTagAcid1
or GridTagAcid2
);
1249 stt
:= getTimeMicro()-stt
;
1250 e_LogWritefln('traceOrthoRayWhileIn: time=%s (%s); liquidTopY=%s', [Integer(stt
), ptr
, liquidTopY
]);
1252 stt
:= getTimeMicro();
1253 nptr
:= g_Map_TraceLiquidNonPrecise(x
, y
, 0, -8, liquidx
, liquidTopY
);
1254 stt
:= getTimeMicro()-stt
;
1255 e_LogWritefln('g_Map_TraceLiquidNonPrecise: time=%s (%s); liquidTopY=%s', [Integer(stt
), nptr
, liquidTopY
]);
1256 if not nptr
then continue
;
1258 if not g_Map_TraceLiquidNonPrecise(x
, y
, 0, -8, liquidx
, ceilingY
) then continue
;
1259 if not g_Map_TraceLiquidNonPrecise(x
, y
, 0, +8, liquidx
, floorY
) then continue
;
1272 state
:= TPartState
.Normal
;
1273 particleType
:= TPartType
.Bubbles
;
1279 if (CurrentParticle
>= MaxParticles
-1) then CurrentParticle
:= 0 else CurrentParticle
+= 1;
1284 // ////////////////////////////////////////////////////////////////////////// //
1285 procedure TParticle
.thinkerSpark ();
1293 if not gpart_dbg_phys_enabled
then begin x
+= round(velX
); y
+= round(velY
); goto _done
; end;
1298 //writeln('spark0: pos=(', x, ',', y, '); delta=(', dx, ',', dy, '); state=', state, '; ceilingY=', ceilingY, '; floorY=', floorY);
1301 if (abs(velX
) < 0.1) and (abs(velY
) < 0.1) then
1310 // has some horizontal velocity
1311 pan
:= g_Map_traceToNearest(x
, y
, x
+dx
, y
+dy
, (GridTagSolid
or GridTagLiquid
), @ex
, @ey
);
1312 if (x
<> ex
) then begin floorY
:= Unknown
; ceilingY
:= Unknown
; end; // dunno yet
1315 if (pan
<> nil) then
1317 if ((pan
.tag
and GridTagLiquid
) <> 0) then begin die(); exit
; end; // die in liquid
1318 // hit the wall; falling down vertically
1323 else if (dy
<> 0) then
1325 // has some vertical velocity
1329 if (ceilingY
= Unknown
) then findCeiling(); // need to do this anyway
1331 if (y
<= ceilingY
) then
1333 // oops, hit a ceiling
1336 accelY
:= abs(accelY
);
1338 // environment didn't changed
1343 if (floorY
= Unknown
) then findFloor(); // need to do this anyway
1345 if (y
>= floorY
) then
1347 // hit something except a floor?
1348 if (floorType
<> TFloorType
.Wall
) then begin die(); exit
; end; // yep: just die
1349 // otherwise, go to sleep
1352 // environment didn't changed
1358 if (x
< g_Map_MinX
) or (y
< g_Map_MinY
) or (x
> g_Map_MaxX
) or (y
> g_Map_MaxY
) then begin die(); end;
1360 if (velX
<> 0.0) then velX
+= accelX
;
1362 if (velY
<> 0.0) then
1364 if (accelY
< 10) then accelY
+= 0.08;
1368 //writeln('spark1: pos=(', x, ',', y, '); delta=(', velX:6:3, ',', velY:6:3, '); state=', state, '; ceilingY=', ceilingY, '; floorY=', floorY);
1370 if waitTime
> 0 then
1377 // ////////////////////////////////////////////////////////////////////////// //
1378 procedure g_GFX_SparkVel (fX
, fY
: Integer; count
: Word; vx
, vy
: Integer; devX
, devY
: Byte);
1381 devX1
, devX2
, devY1
, devY2
: Integer;
1385 if not gpart_dbg_enabled
then exit
;
1387 l
:= Length(Particles
);
1388 if (l
= 0) then exit
;
1389 if (count
> l
) then count
:= l
;
1391 devX1
:= devX
div 2;
1393 devY1
:= devY
div 2;
1396 for a
:= 1 to count
do
1398 with Particles
[CurrentParticle
] do
1400 x
:= fX
-devX1
+Random(devX2
);
1401 y
:= fY
-devY1
+Random(devY2
);
1405 // check for level bounds
1406 if (x
< g_Map_MinX
) or (y
< g_Map_MinY
) or (x
> g_Map_MaxX
) or (y
> g_Map_MaxY
) then continue
;
1408 // in what environment we are starting in?
1409 pan
:= g_Map_PanelAtPoint(x
, y
, (GridTagSolid
or GridTagLiquid
));
1410 if (pan
<> nil) then
1412 // either in a wall, or in a liquid
1413 //if ((pan.tag and GridTagSolid) <> 0) then continue; // don't spawn in walls
1414 //env := TEnvType.ELiquid;
1419 env
:= TEnvType
.EAir
;
1422 velX
:= vx
+(Random
-Random
)*3;
1423 velY
:= vy
+(Random
-Random
)*3;
1427 if (velY
-4 < -4) then velY
:= -4 else velY
:= velY
-4;
1430 accelX
:= -sign(velX
)*Random
/100;
1434 green
:= 100+Random(155);
1438 particleType
:= TPartType
.Spark
;
1439 state
:= TPartState
.Normal
;
1441 liveTime
:= 30+Random(60);
1444 ceilingY
:= Unknown
;
1447 if (CurrentParticle
>= MaxParticles
-1) then CurrentParticle
:= 0 else CurrentParticle
+= 1;
1452 procedure g_GFX_Spark (fX
, fY
: Integer; count
: Word; angle
: SmallInt
; devX
, devY
: Byte);
1456 devX1
, devX2
, devY1
, devY2
: Integer;
1457 baseVelX
, baseVelY
: Single;
1461 if not gpart_dbg_enabled
then exit
;
1463 l
:= Length(Particles
);
1464 if (l
= 0) then exit
;
1465 if (count
> l
) then count
:= l
;
1469 devX1
:= devX
div 2;
1471 devY1
:= devY
div 2;
1474 b
:= DegToRad(angle
);
1476 baseVelY
:= 1.6*sin(b
);
1477 if (abs(baseVelX
) < 0.01) then baseVelX
:= 0.0;
1478 if (abs(baseVelY
) < 0.01) then baseVelY
:= 0.0;
1480 for a
:= 1 to count
do
1482 with Particles
[CurrentParticle
] do
1484 x
:= fX
-devX1
+Random(devX2
);
1485 y
:= fY
-devY1
+Random(devY2
);
1489 // check for level bounds
1490 if (x
< g_Map_MinX
) or (y
< g_Map_MinY
) or (x
> g_Map_MaxX
) or (y
> g_Map_MaxY
) then continue
;
1492 // in what environment we are starting in?
1493 pan
:= g_Map_PanelAtPoint(x
, y
, (GridTagSolid
or GridTagLiquid
));
1494 if (pan
<> nil) then
1496 // either in a wall, or in a liquid
1497 //if ((pan.tag and GridTagSolid) <> 0) then continue; // don't spawn in walls
1498 //env := TEnvType.ELiquid;
1503 env
:= TEnvType
.EAir
;
1506 velX
:= baseVelX
*Random
;
1507 velY
:= baseVelY
-Random
;
1512 green
:= 100+Random(155);
1516 particleType
:= TPartType
.Spark
;
1517 state
:= TPartState
.Normal
;
1519 liveTime
:= 30+Random(60);
1522 ceilingY
:= Unknown
;
1525 if (CurrentParticle
>= MaxParticles
-1) then CurrentParticle
:= 0 else CurrentParticle
+= 1;
1530 // ////////////////////////////////////////////////////////////////////////// //
1531 procedure g_GFX_SetMax (count
: Integer);
1535 if count
> 50000 then count
:= 50000;
1536 if (count
< 1) then count
:= 1;
1537 SetLength(Particles
, count
);
1538 for a
:= 0 to High(Particles
) do Particles
[a
].die();
1539 MaxParticles
:= count
;
1540 CurrentParticle
:= 0;
1544 function g_GFX_GetMax (): Integer;
1546 result
:= MaxParticles
;
1550 function FindOnceAnim (): DWORD
;
1554 if OnceAnims
<> nil then
1555 for i
:= 0 to High(OnceAnims
) do
1556 if OnceAnims
[i
].Animation
= nil then
1562 if OnceAnims
= nil then
1564 SetLength(OnceAnims
, 16);
1569 Result
:= High(OnceAnims
) + 1;
1570 SetLength(OnceAnims
, Length(OnceAnims
) + 16);
1575 procedure g_GFX_OnceAnim (x
, y
: Integer; Anim
: TAnimation
; AnimType
: Byte);
1579 if not gpart_dbg_enabled
then exit
;
1581 if (Anim
= nil) then exit
;
1583 find_id
:= FindOnceAnim();
1585 OnceAnims
[find_id
].AnimType
:= AnimType
;
1586 OnceAnims
[find_id
].Animation
:= TAnimation
.Create(Anim
.FramesID
, Anim
.Loop
, Anim
.Speed
);
1587 OnceAnims
[find_id
].Animation
.Blending
:= Anim
.Blending
;
1588 OnceAnims
[find_id
].Animation
.alpha
:= Anim
.alpha
;
1589 OnceAnims
[find_id
].x
:= x
;
1590 OnceAnims
[find_id
].y
:= y
;
1594 // ////////////////////////////////////////////////////////////////////////// //
1595 procedure g_GFX_Init ();
1597 //g_Game_SetLoadingText(_lc[I_LOAD_COLLIDE_MAP]+' 1/6', 0, False);
1598 //SetLength(gCollideMap, gMapInfo.Height+1);
1599 //for a := 0 to High(gCollideMap) do SetLength(gCollideMap[a], gMapInfo.Width+1);
1602 gpart_dbg_enabled
:= false;
1607 procedure g_GFX_Free ();
1612 SetLength(Particles
, MaxParticles
);
1613 for a
:= 0 to High(Particles
) do Particles
[a
].die();
1614 CurrentParticle
:= 0;
1616 if (OnceAnims
<> nil) then
1618 for a
:= 0 to High(OnceAnims
) do OnceAnims
[a
].Animation
.Free();
1629 // ////////////////////////////////////////////////////////////////////////// //
1630 procedure g_GFX_Update ();
1636 if not gpart_dbg_enabled
then exit
;
1638 if (Particles
<> nil) then
1640 w
:= gMapInfo
.Width
;
1641 h
:= gMapInfo
.Height
;
1643 len
:= High(Particles
);
1645 for a
:= 0 to len
do
1647 if Particles
[a
].alive
then
1649 with Particles
[a
] do
1651 if (time
= liveTime
) then begin die(); continue
; end;
1652 if (x
+1 >= w
) or (y
+1 >= h
) or (x
<= 0) or (y
<= 0) then begin die(); end;
1657 end; // Particles <> nil
1662 if OnceAnims
<> nil then
1664 for a
:= 0 to High(OnceAnims
) do
1665 if OnceAnims
[a
].Animation
<> nil then
1667 OnceAnims
[a
].oldx
:= OnceAnims
[a
].x
;
1668 OnceAnims
[a
].oldy
:= OnceAnims
[a
].y
;
1670 case OnceAnims
[a
].AnimType
of
1673 if Random(3) = 0 then
1674 OnceAnims
[a
].x
:= OnceAnims
[a
].x
-1+Random(3);
1675 if Random(2) = 0 then
1676 OnceAnims
[a
].y
:= OnceAnims
[a
].y
-Random(2);
1680 if OnceAnims
[a
].Animation
.Played
then
1682 OnceAnims
[a
].Animation
.Free();
1683 OnceAnims
[a
].Animation
:= nil;
1686 OnceAnims
[a
].Animation
.Update();
1692 procedure g_GFX_Draw ();
1694 a
, len
, fx
, fy
: Integer;
1696 if not gpart_dbg_enabled
then exit
;
1698 if (Particles
<> nil) then
1700 glDisable(GL_TEXTURE_2D
);
1701 if (g_dbg_scale
< 0.6) then glPointSize(1)
1702 else if (g_dbg_scale
> 1.3) then glPointSize(g_dbg_scale
+1)
1703 else glPointSize(2);
1704 glDisable(GL_POINT_SMOOTH
);
1707 glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
1711 len
:= High(Particles
);
1712 for a
:= 0 to len
do
1714 with Particles
[a
] do
1716 if not alive
then continue
;
1717 if (x
>= sX
) and (y
>= sY
) and (x
<= sX
+sWidth
) and (sY
<= sY
+sHeight
) then
1719 fx
:= nlerp(oldx
, x
, gLerpFactor
);
1720 fy
:= nlerp(oldy
, y
, gLerpFactor
);
1721 glColor4ub(red
, green
, blue
, alpha
);
1722 glVertex2f(fx
+0.37, fy
+0.37);
1729 glDisable(GL_BLEND
);
1732 if (OnceAnims
<> nil) then
1734 len
:= High(OnceAnims
);
1735 for a
:= 0 to len
do
1737 if (OnceAnims
[a
].Animation
<> nil) then
1739 with OnceAnims
[a
] do
1741 fx
:= nlerp(oldx
, x
, gLerpFactor
);
1742 fy
:= nlerp(oldy
, y
, gLerpFactor
);
1743 Animation
.Draw(x
, y
, TMirrorType
.None
);