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, either version 3 of the License, or
6 * (at your option) any later version.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 {$INCLUDE ../shared/a_modes.inc}
17 {.$DEFINE D2F_DEBUG_FALL_MPLAT}
18 {/$DEFINE D2F_DEBUG_PART_AWAKE}
42 MARK_BLOCKED
= MARK_WALL
or MARK_DOOR
;
43 MARK_LIQUID
= MARK_WATER
or MARK_ACID
;
44 MARK_LIFT
= MARK_LIFTDOWN
or MARK_LIFTUP
or MARK_LIFTLEFT
or MARK_LIFTRIGHT
;
47 procedure g_GFX_Init ();
48 procedure g_GFX_Free ();
50 procedure g_GFX_Blood (fX
, fY
: Integer; count
: Word; vx
, vy
: Integer;
51 devX
, devY
: Word; cr
, cg
, cb
: Byte; kind
: Byte=BLOOD_NORMAL
);
52 procedure g_GFX_Spark (fX
, fY
: Integer; count
: Word; angle
: SmallInt
; devX
, devY
: Byte);
53 procedure g_GFX_Water (fX
, fY
: Integer; count
: Word; fVelX
, fVelY
: Single; devX
, devY
, color
: Byte;
54 simple
: Boolean=false; cr
: Byte=0; cg
: Byte=0; cb
: Byte=0);
55 procedure g_GFX_SimpleWater (fX
, fY
: Integer; count
: Word; fVelX
, fVelY
: Single; defColor
, cr
, cg
, cb
: Byte);
56 procedure g_GFX_Bubbles (fX
, fY
: Integer; count
: Word; devX
, devY
: Byte);
58 procedure g_GFX_SetMax (count
: Integer);
59 function g_GFX_GetMax (): Integer;
61 procedure g_GFX_OnceAnim (X
, Y
: Integer; Anim
: TAnimation
; AnimType
: Byte = 0);
63 procedure g_Mark (x
, y
, Width
, Height
: Integer; t
: Byte; st
: Boolean=true);
65 procedure g_GFX_Update ();
66 procedure g_GFX_Draw ();
70 gpart_dbg_enabled
: Boolean = true;
71 gpart_dbg_phys_enabled
: Boolean = true;
74 //WARNING: only for Holmes!
75 function awmIsSetHolmes (x
, y
: Integer): Boolean; inline;
81 {$INCLUDE ../nogl/noGLuses.inc}
82 g_map
, g_panel
, g_basic
, Math
, e_graphics
,
83 g_options
, g_console
, SysUtils
, g_triggers
, MAPDEF
,
84 g_game
, g_language
, g_net
, utils
, xprofiler
;
88 Unknown
= Integer($7fffffff);
92 TPartType
= (Blood
, Spark
, Bubbles
, Water
);
93 TPartState
= (Free
, Normal
, Stuck
, Sleeping
);
94 TFloorType
= (Wall
, LiquidIn
, LiquidOut
);
95 // Wall: floorY is just before floor
96 // LiquidIn: floorY is liquid *start* (i.e. just in a liquid)
97 // LiquidOut: floorY is liquid *end* (i.e. just out of a liquid)
98 TEnvType
= (EAir
, ELiquid
, EWall
); // where particle is now
100 // note: this MUST be record, so we can keep it in
101 // dynamic array and has sequential memory access pattern
102 PParticle
= ^TParticle
;
106 accelX
, accelY
: Single;
108 particleType
: TPartType
;
109 red
, green
, blue
: Byte;
111 time
, liveTime
: Word;
112 stickDX
: Integer; // STATE_STICK: -1,1: stuck to a wall; 0: stuck to ceiling
113 justSticked
: Boolean; // not used
114 floorY
: Integer; // actually, floor-1; `Unknown`: unknown
115 floorType
: TFloorType
;
116 env
: TEnvType
; // where particle is now
117 ceilingY
: Integer; // actually, ceiling+1; `Unknown`: unknown
118 wallEndY
: Integer; // if we stuck to a wall, this is where wall ends
120 //k8: sorry, i have to emulate virtual methods this way, 'cause i haet `Object`
121 procedure thinkerBloodAndWater ();
122 procedure thinkerSpark ();
123 procedure thinkerBubble ();
125 procedure findFloor (force
: Boolean=false); // this updates `floorY` if forced or Unknown
126 procedure findCeiling (force
: Boolean=false); // this updates `ceilingY` if forced or Unknown
128 procedure freeze (); inline; // remove velocities and acceleration
129 procedure sleep (); inline; // switch to sleep mode
131 function checkAirStreams (): Boolean; // `true`: affected by air stream
133 function alive (): Boolean; inline;
134 procedure die (); inline;
135 procedure think (); inline;
141 Animation
: TAnimation
;
146 Particles
: array of TParticle
= nil;
147 OnceAnims
: array of TOnceAnim
= nil;
148 MaxParticles
: Integer = 0;
149 CurrentParticle
: Integer = 0;
150 // awakeMap has one bit for each map grid cell; on g_Mark,
151 // corresponding bits will be set, and in `think()` all particles
152 // in marked cells will be awaken
153 awakeMap
: packed array of LongWord
= nil;
154 awakeMapH
: Integer = -1;
155 awakeMapW
: Integer = -1;
156 awakeMinX
, awakeMinY
: Integer;
157 awakeDirty
: Boolean = false;
158 {$IF DEFINED(D2F_DEBUG_PART_AWAKE)}
159 awakeMapHlm
: packed array of LongWord
= nil;
163 // ////////////////////////////////////////////////////////////////////////// //
164 function awmIsSetHolmes (x
, y
: Integer): Boolean; inline;
166 {$IF DEFINED(D2F_DEBUG_PART_AWAKE)}
167 if (Length(awakeMapHlm
) = 0) then begin result
:= false; exit
; end;
168 x
:= (x
-awakeMinX
) div mapGrid
.tileSize
;
169 y
:= (y
-awakeMinY
) div mapGrid
.tileSize
;
170 if (x
>= 0) and (y
>= 0) and (x
div 32 < awakeMapW
) and (y
< awakeMapH
) then
172 if (y
*awakeMapW
+x
div 32 < Length(awakeMapHlm
)) then
174 result
:= ((awakeMapHlm
[y
*awakeMapW
+x
div 32] and (LongWord(1) shl (x
mod 32))) <> 0);
191 // ////////////////////////////////////////////////////////////////////////// //
192 // HACK! using mapgrid
193 procedure awmClear (); inline;
195 {$IF DEFINED(D2F_DEBUG_PART_AWAKE)}
196 if (Length(awakeMap
) > 0) then
198 if (Length(awakeMapHlm
) <> Length(awakeMap
)) then SetLength(awakeMapHlm
, Length(awakeMap
));
199 Move(awakeMap
[0], awakeMapHlm
[0], Length(awakeMap
)*sizeof(awakeMap
[0]));
202 if awakeDirty
and (awakeMapW
> 0) then
204 FillDWord(awakeMap
[0], Length(awakeMap
), 0);
210 procedure awmSetup ();
212 assert(mapGrid
<> nil);
213 awakeMapW
:= (mapGrid
.gridWidth
+mapGrid
.tileSize
-1) div mapGrid
.tileSize
;
214 awakeMapW
:= (awakeMapW
+31) div 32; // LongWord has 32 bits ;-)
215 awakeMapH
:= (mapGrid
.gridHeight
+mapGrid
.tileSize
-1) div mapGrid
.tileSize
;
216 awakeMinX
:= mapGrid
.gridX0
;
217 awakeMinY
:= mapGrid
.gridY0
;
218 SetLength(awakeMap
, awakeMapW
*awakeMapH
);
219 {$IF DEFINED(D2F_DEBUG_PART_AWAKE)}
220 SetLength(awakeMapHlm
, awakeMapW
*awakeMapH
);
221 FillDWord(awakeMapHlm
[0], Length(awakeMapHlm
), 0);
223 //{$IF DEFINED(D2F_DEBUG)}
224 e_LogWritefln('particle awake map: %sx%s (for grid of size %sx%s)', [awakeMapW
, awakeMapH
, mapGrid
.gridWidth
, mapGrid
.gridHeight
]);
231 function awmIsSet (x
, y
: Integer): Boolean; inline;
233 x
:= (x
-awakeMinX
) div mapGrid
.tileSize
;
234 y
:= (y
-awakeMinY
) div mapGrid
.tileSize
;
235 if (x
>= 0) and (y
>= 0) and (x
div 32 < awakeMapW
) and (y
< awakeMapH
) then
237 {$IF DEFINED(D2F_DEBUG)}
238 assert(y
*awakeMapW
+x
div 32 < Length(awakeMap
));
240 result
:= ((awakeMap
[y
*awakeMapW
+x
div 32] and (LongWord(1) shl (x
mod 32))) <> 0);
249 procedure awmSet (x
, y
: Integer); 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 v
:= @awakeMap
[y
*awakeMapW
+x
div 32];
261 v
^ := v
^ or (LongWord(1) shl (x
mod 32));
267 // ////////////////////////////////////////////////////////////////////////// //
271 procedure g_Mark (x
, y
, Width
, Height
: Integer; t
: Byte; st
: Boolean=true);
274 dx
, dy
, ex
, ey
: Integer;
277 if (not gpart_dbg_enabled
) or (not gpart_dbg_phys_enabled
) then exit
;
278 if (awakeMapW
< 1) or (awakeMapH
< 1) then exit
;
280 if (Width
< 1) or (Height
< 1) then exit
;
282 // make some border, so we'll hit particles around the panel
283 ex
:= x
+Width
+Extrude
-1-awakeMinX
;
284 ey
:= y
+Height
+Extrude
-1-awakeMinY
;
285 x
:= (x
-Extrude
)-awakeMinX
;
286 y
:= (y
-Extrude
)-awakeMinY
;
288 x
:= x
div mapGrid
.tileSize
;
289 y
:= y
div mapGrid
.tileSize
;
290 ex
:= ex
div mapGrid
.tileSize
;
291 ey
:= ey
div mapGrid
.tileSize
;
293 // has something to do?
294 if (ex
< 0) or (ey
< 0) or (x
>= awakeMapW
*32) or (y
>= awakeMapH
) then exit
;
295 if (x
< 0) then x
:= 0;
296 if (y
< 0) then y
:= 0;
297 if (ex
>= awakeMapW
*32) then ex
:= awakeMapW
*32-1;
298 if (ey
>= awakeMapH
) then ey
:= awakeMapH
;
305 {$IF DEFINED(D2F_DEBUG)}
306 assert((dx
>= 0) and (dy
>= 0) and (dx
div 32 < awakeMapW
) and (dy
< awakeMapH
));
307 assert(dy
*awakeMapW
+dx
div 32 < Length(awakeMap
));
309 v
:= @awakeMap
[dy
*awakeMapW
+dx
div 32];
310 v
^ := v
^ or (LongWord(1) shl (dx
mod 32));
316 // ////////////////////////////////////////////////////////////////////////// //
317 function TParticle
.alive (): Boolean; inline; begin result
:= (state
<> TPartState
.Free
); end;
318 procedure TParticle
.die (); inline; begin state
:= TPartState
.Free
; end;
320 // remove velocities and acceleration
321 procedure TParticle
.freeze (); inline;
323 // stop right there, you criminal scum!
331 // `true`: affected by air stream
332 function TParticle
.checkAirStreams (): Boolean;
336 pan
:= g_Map_PanelAtPoint(x
, y
, GridTagLift
);
337 result
:= (pan
<> nil);
340 if ((pan
.PanelType
and PANEL_LIFTUP
) <> 0) then
342 if (velY
> -4-Random(3)) then velY
-= 0.8;
343 if (abs(velX
) > 0.1) then velX
-= velX
/10.0;
344 velX
+= (Random
-Random
)*0.2;
347 else if ((pan
.PanelType
and PANEL_LIFTLEFT
) <> 0) then
349 if (velX
> -8-Random(3)) then velX
-= 0.8;
352 else if ((pan
.PanelType
and PANEL_LIFTRIGHT
) <> 0) then
354 if (velX
< 8+Random(3)) then velX
+= 0.8;
362 if result
and (state
= TPartState
.Sleeping
) then state
:= TPartState
.Normal
;
367 // switch to sleep mode
368 procedure TParticle
.sleep (); inline;
370 if not checkAirStreams() then
372 state
:= TPartState
.Sleeping
;
378 procedure TParticle
.findFloor (force
: Boolean=false);
383 if (not force
) and (floorY
<> Unknown
) then exit
;
384 // stuck in the wall? rescan, 'cause it can be mplat
385 if (env
= TEnvType
.EWall
) then
387 pan
:= g_Map_PanelAtPoint(x
, y
, (GridTagObstacle
or GridTagLiquid
));
390 // either in a wall, or in a liquid
391 if ((pan
.tag
and GridTagObstacle
) <> 0) then
393 // we are in the wall, wtf?!
395 env
:= TEnvType
.EWall
;
396 floorType
:= TFloorType
.Wall
;
397 state
:= TPartState
.Sleeping
; // anyway
400 // we are in liquid, trace to liquid end
401 env
:= TEnvType
.ELiquid
;
404 // are we in a liquid?
405 if (env
= TEnvType
.ELiquid
) then
407 // trace out of the liquid
408 //env := TEnvType.ELiquid;
409 floorType
:= TFloorType
.LiquidOut
;
410 //e_LogWritefln('tracing out of a liquid; floorY=%s; y=%s', [floorY, y]);
411 mapGrid
.traceOrthoRayWhileIn(ex
, floorY
, x
, y
, x
, g_Map_MaxY
, GridTagLiquid
);
412 floorY
+= 1; // so `floorY` is just out of a liquid
413 //e_LogWritefln(' traced out of a liquid; floorY=%s; y=%s', [floorY, y]);
418 assert(env
= TEnvType
.EAir
);
419 //env := TEnvType.EAir;
420 pan
:= g_Map_traceToNearest(x
, y
, x
, g_Map_MaxY
, (GridTagObstacle
or GridTagLiquid
), @ex
, @floorY
);
424 if ((pan
.tag
and GridTagObstacle
) <> 0) then
427 floorType
:= TFloorType
.Wall
;
432 floorType
:= TFloorType
.LiquidIn
; // entering liquid
433 floorY
+= 1; // so `floorY` is just in a liquid
438 // out of the level; assume wall, but it doesn't really matter
439 floorType
:= TFloorType
.Wall
;
440 floorY
:= g_Map_MaxY
+2;
446 procedure TParticle
.findCeiling (force
: Boolean=false);
450 if (not force
) and (ceilingY
<> Unknown
) then exit
;
451 if (nil = g_Map_traceToNearest(x
, y
, x
, g_Map_MinY
, GridTagObstacle
, @ex
, @ceilingY
)) then
453 ceilingY
:= g_Map_MinY
-2;
458 procedure TParticle
.think (); inline;
461 if (state
= TPartState
.Stuck
) then
463 //writeln('awaking particle at (', x, ',', y, ')');
464 if (stickDX
= 0) then
466 state
:= TPartState
.Normal
; // stuck to a ceiling
470 // stuck to a wall, check if wall is still there
471 if (wallEndY
<> Unknown
) then
474 if (g_Map_PanelAtPoint(x
+stickDX
, y
, GridTagObstacle
) = nil) then
476 // a wall was moved out, start falling
477 state
:= TPartState
.Normal
;
478 if (velY
= 0) then velY
:= 0.1;
479 if (accelY
= 0) then accelY
:= 0.5;
486 state
:= TPartState
.Normal
;
487 if (velY
= 0) then velY
:= 0.1;
488 if (accelY
= 0) then accelY
:= 0.5;
495 // awake sleeping particle, if necessary
498 if awmIsSet(x
, y
) then awake();
501 TPartState.Sleeping, TPartState.Stuck:
502 if awmIsSet(x, y) then awake();
504 if (env = TEnvType.EWall) and awmIsSet(x, y) then awake();
509 TPartType
.Blood
, TPartType
.Water
: thinkerBloodAndWater();
510 TPartType
.Spark
: thinkerSpark();
511 TPartType
.Bubbles
: thinkerBubble();
516 // ////////////////////////////////////////////////////////////////////////// //
517 procedure TParticle
.thinkerBloodAndWater ();
518 procedure stickToCeiling ();
520 state
:= TPartState
.Stuck
;
523 ceilingY
:= y
; // yep
526 procedure stickToWall (dx
: Integer);
530 state
:= TPartState
.Stuck
;
531 if (dx
> 0) then stickDX
:= 1 else stickDX
:= -1;
533 // find next floor transition
536 mapGrid
.traceOrthoRayWhileIn(ex
, wallEndY
, x
+stickDX
, y
, x
+stickDX
, floorY
+1, (GridTagWall
or GridTagDoor
or GridTagStep
));
539 procedure hitAFloor ();
541 state
:= TPartState
.Sleeping
; // we aren't moving anymore
544 floorType
:= TFloorType
.Wall
; // yep
547 // `true`: didn't, get outa thinker
548 function drip (): Boolean;
551 TPartType
.Blood
: result
:= (Random(200) = 100);
552 TPartType
.Water
: result
:= (Random(30) = 15);
553 else raise Exception
.Create('internal error in particle engine: drip');
559 // if we're falling from ceiling, switch to normal mode
560 if (state
= TPartState
.Stuck
) and (stickDX
= 0) then state
:= TPartState
.Normal
;
564 // switch to freefall mode
565 procedure freefall ();
567 state
:= TPartState
.Normal
;
572 procedure applyGravity (inLiquid
: Boolean);
574 state
:= TPartState
.Normal
;
588 _done
, _gravityagain
, _stuckagain
;
594 floorJustTraced
: Boolean;
595 {$IF DEFINED(D2F_DEBUG_FALL_MPLAT)}
599 if not gpart_dbg_phys_enabled
then begin x
+= round(velX
); y
+= round(velY
); goto _done
; end;
603 // still check for air streams when sleeping (no)
604 if (state
= TPartState
.Sleeping
) then begin {checkAirStreams();} goto _done
; end; // so blood will dissolve
606 // process stuck particles
607 if (state
= TPartState
.Stuck
) then
609 // stuck to a ceiling?
610 if (stickDX
= 0) then
612 // yeah, stuck to a ceiling
613 if (ceilingY
= Unknown
) then findCeiling();
614 // dropped from a ceiling?
615 if (y
> ceilingY
) then
620 state
:= TPartState
.Normal
;
624 // otherwise, try to drip
625 if drip() then goto _done
;
631 if (wallEndY
= Unknown
) then
633 // this can happen if mplat was moved out; find new `wallEndY`
634 findFloor(true); // force trace, just in case
635 if (floorType
= TFloorType
.LiquidOut
) then env
:= TEnvType
.ELiquid
else env
:= TEnvType
.EAir
;
636 mapGrid
.traceOrthoRayWhileIn(ex
, wallEndY
, x
+stickDX
, y
, x
+stickDX
, floorY
+1, (GridTagWall
or GridTagDoor
or GridTagStep
));
640 if (wallEndY
<= floorY
) and (y
>= floorY
) then
644 TFloorType
.Wall
: // hit the ground
646 // check if our ground wasn't moved since the last scan
647 findFloor(true); // force trace
651 goto _done
; // nothing to do anymore
653 // otherwise, do it again
656 TFloorType
.LiquidIn
: // entering the liquid
658 // rescan, so we'll know when we'll exit the liquid
659 findFloor(true); // force rescan
661 TFloorType
.LiquidOut
: // exiting the liquid
663 // rescan, so we'll know when we'll enter something interesting
664 findFloor(true); // force rescan
665 if (floorType
= TFloorType
.Wall
) and (floorY
= y
) then begin sleep(); goto _done
; end;
670 if (floorY
<= wallEndY
) and (y
>= wallEndY
) then
672 // just unstuck from the wall, switch to freefall mode
678 // otherwise, try to drip
679 if drip() then goto _done
;
682 // nope, process as usual
685 // it is important to have it here
689 if (state
= TPartState
.Normal
) then checkAirStreams();
691 // gravity, if not stuck
692 if (state
<> TPartState
.Stuck
) and (abs(velX
) < 0.1) and (abs(velY
) < 0.1) then
694 floorJustTraced
:= (floorY
= Unknown
);
695 if floorJustTraced
then findFloor();
701 TFloorType
.Wall
: // hit the ground
703 // check if our ground wasn't moved since the last scan
704 if not floorJustTraced
then
706 findFloor(true); // force trace
707 if (floorType
= TFloorType
.LiquidOut
) then env
:= TEnvType
.ELiquid
else env
:= TEnvType
.EAir
;
708 if (y
<> floorY
) then goto _gravityagain
;
710 // otherwise, nothing to do
712 TFloorType
.LiquidIn
: // entering the liquid
714 // rescan, so we'll know when we'll exit the liquid
715 findFloor(true); // force rescan
718 TFloorType
.LiquidOut
: // exiting the liquid
720 // rescan, so we'll know when we'll enter something interesting
721 findFloor(true); // force rescan
722 if (floorType
<> TFloorType
.Wall
) or (floorY
<> y
) then applyGravity(floorType
= TFloorType
.LiquidIn
);
728 // looks like we're in the air
736 // has some horizontal velocity
737 pan
:= g_Map_traceToNearest(x
, y
, x
+dx
, y
+dy
, GridTagObstacle
, @ex
, @ey
);
738 checkEnv
:= (x
<> ex
);
746 // check environment (air/liquid)
747 if (g_Map_PanelAtPoint(x
, y
, GridTagLiquid
) <> nil) then env
:= TEnvType
.ELiquid
else env
:= TEnvType
.EAir
;
752 // the only case when we can have both ceiling and wall is corner; stick to wall in this case
753 // check if we stuck to a wall
754 if (dx
< 0) then dx
:= -1 else dx
:= 1;
755 if (g_Map_PanelAtPoint(x
+dx
, y
, GridTagObstacle
) <> nil) then
762 // stuck to a ceiling
767 else if (dy
<> 0) then
769 // has only vertical velocity
773 if (ceilingY
= Unknown
) then findCeiling(); // need to do this anyway
775 if (y
<= ceilingY
) then begin y
:= ceilingY
; stickToCeiling(); end; // oops, hit a ceiling
776 // environment didn't changed
783 floorJustTraced
:= (floorY
= Unknown
);
784 if floorJustTraced
then findFloor();
785 if (floorType
= TFloorType
.LiquidOut
) then env
:= TEnvType
.ELiquid
else env
:= TEnvType
.EAir
;
787 //e_LogWritefln('floorY=%s; newy=%s; dY=%s; floorType=%s', [floorY, y, dY, floorType]);
788 if (y
>= floorY
) then
793 //e_LogWritefln(' HIT FLOORY: floorY=%s; newy=%s; dY=%s; floorType=%s', [floorY, y, dY, floorType]);
795 TFloorType
.Wall
: // hit the ground
797 // check if our ground wasn't moved since the last scan
798 if not floorJustTraced
then
800 {$IF DEFINED(D2F_DEBUG_FALL_MPLAT)}
803 findFloor(true); // force trace
804 {$IF DEFINED(D2F_DEBUG_FALL_MPLAT)}
805 if (floorY
<> oldFloorY
) then
807 e_LogWritefln('force rescanning vpart at (%s,%s); oldFloorY=%s; floorY=%s', [x
, y
, oldFloorY
, floorY
]);
810 if (floorType
= TFloorType
.LiquidOut
) then env
:= TEnvType
.ELiquid
else env
:= TEnvType
.EAir
;
811 if (y
<> floorY
) then continue
;
813 // environment didn't changed
815 break
; // done with vertical movement
817 TFloorType
.LiquidIn
: // entering the liquid
819 // we're entered the liquid
820 env
:= TEnvType
.ELiquid
;
821 // rescan, so we'll know when we'll exit the liquid
822 findFloor(true); // force rescan
824 TFloorType
.LiquidOut
: // exiting the liquid
826 // we're exited the liquid
827 env
:= TEnvType
.EAir
;
828 // rescan, so we'll know when we'll enter something interesting
829 findFloor(true); // force rescan
830 if (floorType
= TFloorType
.Wall
) and (floorY
= y
) then
833 break
; // done with vertical movement
840 break
; // done with vertical movement
853 if (g_Map_PanelAtPoint(x
, y
, GridTagObstacle
) <> nil) then begin die(); exit
; end;
857 if (x
< g_Map_MinX
) or (y
< g_Map_MinY
) or (x
> g_Map_MaxX
) or (y
> g_Map_MaxY
) then begin die(); end;
862 // blood will dissolve in other liquids
863 if (particleType
= TPartType
.Blood
) then
865 if (env
= TEnvType
.ELiquid
) then
868 if (liveTime
<= 0) then begin die(); exit
; end;
869 ex
:= 255-trunc(255.0*time
/liveTime
);
870 if (ex
<= 10) then begin die(); exit
; end;
871 if (ex
> 250) then ex
:= 255;
877 // water will disappear in any liquid
878 if (env
= TEnvType
.ELiquid
) then begin die(); exit
; end;
881 if (liveTime
<= 0) then begin die(); exit
; end;
882 ex
:= 255-trunc(255.0*time
/liveTime
);
883 if (ex
<= 10) then begin die(); exit
; end;
884 if (ex
> 250) then ex
:= 255;
890 // ////////////////////////////////////////////////////////////////////////// //
891 procedure g_GFX_SparkVel (fX
, fY
: Integer; count
: Word; vx
, vy
: Integer; devX
, devY
: Byte); forward;
893 procedure g_GFX_Blood (fX
, fY
: Integer; count
: Word; vx
, vy
: Integer;
894 devX
, devY
: Word; cr
, cg
, cb
: Byte; kind
: Byte = BLOOD_NORMAL
);
896 function genColor (cbase
, crnd
: Integer; def
: Byte=0): Byte;
901 if (cbase
< 0) then result
:= 0
902 else if (cbase
> 255) then result
:= 255
903 else result
:= Byte(cbase
);
913 devX1
, devX2
, devY1
, devY2
: Integer;
918 if not gpart_dbg_enabled
then exit
;
920 if (kind
= BLOOD_SPARKS
) then
922 g_GFX_SparkVel(fX
, fY
, 2+Random(2), -vx
div 2, -vy
div 2, devX
, devY
);
926 l
:= Length(Particles
);
927 if (l
= 0) then exit
;
928 if (count
> l
) then count
:= l
;
935 for a
:= 1 to count
do
937 with Particles
[CurrentParticle
] do
939 x
:= fX
-devX1
+Random(devX2
);
940 y
:= fY
-devY1
+Random(devY2
);
942 // check for level bounds
943 if (x
< g_Map_MinX
) or (y
< g_Map_MinY
) or (x
> g_Map_MaxX
) or (y
> g_Map_MaxY
) then continue
;
945 // in what environment we are starting in?
946 pan
:= g_Map_PanelAtPoint(x
, y
, (GridTagObstacle
or GridTagLiquid
));
949 // either in a wall, or in a liquid
950 if ((pan
.tag
and GridTagObstacle
) <> 0) then continue
; // don't spawn in walls
951 env
:= TEnvType
.ELiquid
;
955 env
:= TEnvType
.EAir
;
958 velX
:= vx
+(Random
-Random
)*3;
959 velY
:= vy
+(Random
-Random
)*3;
963 if (velY
-4 < -4) then velY
:= -4 else velY
:= velY
-4;
966 accelX
:= -sign(velX
)*Random
/100;
969 crnd
:= 20*Random(6)-50;
971 red
:= genColor(cr
, CRnd
, 0);
972 green
:= genColor(cg
, CRnd
, 0);
973 blue
:= genColor(cb
, CRnd
, 0);
976 particleType
:= TPartType
.Blood
;
977 state
:= TPartState
.Normal
;
979 liveTime
:= 120+Random(40);
984 if (CurrentParticle
>= MaxParticles
-1) then CurrentParticle
:= 0 else CurrentParticle
+= 1;
989 procedure g_GFX_Water (fX
, fY
: Integer; count
: Word; fVelX
, fVelY
: Single; devX
, devY
, color
: Byte;
990 simple
: Boolean=false; cr
: Byte=0; cg
: Byte=0; cb
: Byte=0);
993 devX1
, devX2
, devY1
, devY2
: Integer;
997 if not gpart_dbg_enabled
then exit
;
999 l
:= Length(Particles
);
1000 if (l
= 0) then exit
;
1001 if (count
> l
) then count
:= l
;
1003 if (abs(fVelX
) < 3.0) then fVelX
:= 3.0-6.0*Random
;
1005 devX1
:= devX
div 2;
1007 devY1
:= devY
div 2;
1010 if (not simple
) and (color
> 3) then color
:= 0;
1012 for a
:= 1 to count
do
1014 with Particles
[CurrentParticle
] do
1018 x
:= fX
-devX1
+Random(devX2
);
1019 y
:= fY
-devY1
+Random(devY2
);
1021 if (abs(fVelX
) < 0.5) then velX
:= 1.0-2.0*Random
else velX
:= fVelX
*Random
;
1022 if (Random(10) < 7) then velX
:= -velX
;
1023 velY
:= fVelY
*Random
;
1038 // check for level bounds
1039 if (x
< g_Map_MinX
) or (y
< g_Map_MinY
) or (x
> g_Map_MaxX
) or (y
> g_Map_MaxY
) then continue
;
1041 // this hack will allow water spawned in water to fly out
1042 // it can happen when player fell from a huge height (see "DOOM2D.WAD:\MAP03", for example)
1043 if (fVelY
>= 0) then
1045 // in what environment we are starting in?
1046 pan
:= g_Map_PanelAtPoint(x
, y
, (GridTagObstacle
or GridTagLiquid
));
1050 pan
:= g_Map_PanelAtPoint(x
, y
, GridTagObstacle
);
1052 if (pan
<> nil) then continue
;
1053 env
:= TEnvType
.EAir
;
1059 red
:= 155+Random(9)*10;
1060 green
:= trunc(150*Random
);
1065 red
:= trunc(150*Random
);
1066 green
:= 175+Random(9)*10;
1071 red
:= trunc(200*Random
);
1073 blue
:= 175+Random(9)*10;
1075 4: // Ñâîé öâåò, ñâåòëåå
1077 red
:= 20+Random(19)*10;
1080 red
:= nmin(red
+cr
, 255);
1081 green
:= nmin(green
+cg
, 255);
1082 blue
:= nmin(blue
+cb
, 255);
1084 5: // Ñâîé öâåò, òåìÃåå
1086 red
:= 20+Random(19)*10;
1089 red
:= nmax(cr
-red
, 0);
1090 green
:= nmax(cg
-green
, 0);
1091 blue
:= nmax(cb
-blue
, 0);
1095 red
:= 90+random(12)*10;
1102 particleType
:= TPartType
.Water
;
1103 state
:= TPartState
.Normal
;
1105 liveTime
:= 60+Random(60);
1107 ceilingY
:= Unknown
;
1110 if (CurrentParticle
>= MaxParticles
-1) then CurrentParticle
:= 0 else CurrentParticle
+= 1;
1115 procedure g_GFX_SimpleWater (fX
, fY
: Integer; count
: Word; fVelX
, fVelY
: Single; defColor
, cr
, cg
, cb
: Byte);
1117 g_GFX_Water(fX
, fY
, count
, 0, 0, 0, 0, defColor
, true, cr
, cg
, cb
);
1121 // ////////////////////////////////////////////////////////////////////////// //
1122 procedure TParticle
.thinkerBubble ();
1133 if (y
<= ceilingY
) then begin die(); exit
; end;
1137 if (y
>= floorY
) then begin die(); exit
; end;
1139 if (y
< g_Map_MinY
) or (y
> g_Map_MaxY
) then begin die(); exit
; end;
1142 if (velY
> -4) then velY
+= accelY
;
1148 {.$DEFINE D2F_DEBUG_BUBBLES}
1149 procedure g_GFX_Bubbles (fX
, fY
: Integer; count
: Word; devX
, devY
: Byte);
1151 a
, liquidx
: Integer;
1152 devX1
, devX2
, devY1
, devY2
: Integer;
1154 {$IF DEFINED(D2F_DEBUG_BUBBLES)}
1159 if not gpart_dbg_enabled
then exit
;
1161 l
:= Length(Particles
);
1162 if (l
= 0) then exit
;
1163 if (count
> l
) then count
:= l
;
1165 devX1
:= devX
div 2;
1167 devY1
:= devY
div 2;
1170 for a
:= 1 to count
do
1172 with Particles
[CurrentParticle
] do
1174 x
:= fX
-devX1
+Random(devX2
);
1175 y
:= fY
-devY1
+Random(devY2
);
1177 // check for level bounds
1178 if (x
< g_Map_MinX
) or (y
< g_Map_MinY
) or (x
> g_Map_MaxX
) or (y
> g_Map_MaxY
) then continue
;
1181 // don't spawn bubbles outside of the liquid
1182 if not isLiquidAt(X, Y) {ByteBool(gCollideMap[Y, X] and MARK_LIQUID)} then
1186 // trace liquid, so we'll know where it ends; do it in 8px steps for speed
1187 // tracer will return `false` if we started outside of the liquid
1189 {$IF DEFINED(D2F_DEBUG_BUBBLES)}
1190 stt
:= getTimeMicro();
1191 ptr
:= mapGrid
.traceOrthoRayWhileIn(liquidx
, liquidTopY
, x
, y
, x
, 0, GridTagWater
or GridTagAcid1
or GridTagAcid2
);
1192 stt
:= getTimeMicro()-stt
;
1193 e_LogWritefln('traceOrthoRayWhileIn: time=%s (%s); liquidTopY=%s', [Integer(stt
), ptr
, liquidTopY
]);
1195 stt
:= getTimeMicro();
1196 nptr
:= g_Map_TraceLiquidNonPrecise(x
, y
, 0, -8, liquidx
, liquidTopY
);
1197 stt
:= getTimeMicro()-stt
;
1198 e_LogWritefln('g_Map_TraceLiquidNonPrecise: time=%s (%s); liquidTopY=%s', [Integer(stt
), nptr
, liquidTopY
]);
1199 if not nptr
then continue
;
1201 if not g_Map_TraceLiquidNonPrecise(x
, y
, 0, -8, liquidx
, ceilingY
) then continue
;
1202 if not g_Map_TraceLiquidNonPrecise(x
, y
, 0, +8, liquidx
, floorY
) then continue
;
1215 state
:= TPartState
.Normal
;
1216 particleType
:= TPartType
.Bubbles
;
1221 if (CurrentParticle
>= MaxParticles
-1) then CurrentParticle
:= 0 else CurrentParticle
+= 1;
1226 // ////////////////////////////////////////////////////////////////////////// //
1227 procedure TParticle
.thinkerSpark ();
1235 if not gpart_dbg_phys_enabled
then begin x
+= round(velX
); y
+= round(velY
); goto _done
; end;
1240 //writeln('spark0: pos=(', x, ',', y, '); delta=(', dx, ',', dy, '); state=', state, '; ceilingY=', ceilingY, '; floorY=', floorY);
1243 if (abs(velX
) < 0.1) and (abs(velY
) < 0.1) then
1252 // has some horizontal velocity
1253 pan
:= g_Map_traceToNearest(x
, y
, x
+dx
, y
+dy
, (GridTagObstacle
or GridTagLiquid
), @ex
, @ey
);
1254 if (x
<> ex
) then begin floorY
:= Unknown
; ceilingY
:= Unknown
; end; // dunno yet
1257 if (pan
<> nil) then
1259 if ((pan
.tag
and GridTagLiquid
) <> 0) then begin die(); exit
; end; // die in liquid
1260 // hit the wall; falling down vertically
1265 else if (dy
<> 0) then
1267 // has some vertical velocity
1271 if (ceilingY
= Unknown
) then findCeiling(); // need to do this anyway
1273 if (y
<= ceilingY
) then
1275 // oops, hit a ceiling
1278 accelY
:= abs(accelY
);
1280 // environment didn't changed
1285 if (floorY
= Unknown
) then findFloor(); // need to do this anyway
1287 if (y
>= floorY
) then
1289 // hit something except a floor?
1290 if (floorType
<> TFloorType
.Wall
) then begin die(); exit
; end; // yep: just die
1291 // otherwise, go to sleep
1294 // environment didn't changed
1300 if (x
< g_Map_MinX
) or (y
< g_Map_MinY
) or (x
> g_Map_MaxX
) or (y
> g_Map_MaxY
) then begin die(); end;
1302 if (velX
<> 0.0) then velX
+= accelX
;
1304 if (velY
<> 0.0) then
1306 if (accelY
< 10) then accelY
+= 0.08;
1310 //writeln('spark1: pos=(', x, ',', y, '); delta=(', velX:6:3, ',', velY:6:3, '); state=', state, '; ceilingY=', ceilingY, '; floorY=', floorY);
1316 // ////////////////////////////////////////////////////////////////////////// //
1317 procedure g_GFX_SparkVel (fX
, fY
: Integer; count
: Word; vx
, vy
: Integer; devX
, devY
: Byte);
1320 devX1
, devX2
, devY1
, devY2
: Integer;
1324 if not gpart_dbg_enabled
then exit
;
1326 l
:= Length(Particles
);
1327 if (l
= 0) then exit
;
1328 if (count
> l
) then count
:= l
;
1330 devX1
:= devX
div 2;
1332 devY1
:= devY
div 2;
1335 for a
:= 1 to count
do
1337 with Particles
[CurrentParticle
] do
1339 x
:= fX
-devX1
+Random(devX2
);
1340 y
:= fY
-devY1
+Random(devY2
);
1342 // check for level bounds
1343 if (x
< g_Map_MinX
) or (y
< g_Map_MinY
) or (x
> g_Map_MaxX
) or (y
> g_Map_MaxY
) then continue
;
1345 // in what environment we are starting in?
1346 pan
:= g_Map_PanelAtPoint(x
, y
, (GridTagObstacle
or GridTagLiquid
));
1347 if (pan
<> nil) then
1349 // either in a wall, or in a liquid
1350 //if ((pan.tag and GridTagObstacle) <> 0) then continue; // don't spawn in walls
1351 //env := TEnvType.ELiquid;
1356 env
:= TEnvType
.EAir
;
1359 velX
:= vx
+(Random
-Random
)*3;
1360 velY
:= vy
+(Random
-Random
)*3;
1364 if (velY
-4 < -4) then velY
:= -4 else velY
:= velY
-4;
1367 accelX
:= -sign(velX
)*Random
/100;
1371 green
:= 100+Random(155);
1375 particleType
:= TPartType
.Spark
;
1376 state
:= TPartState
.Normal
;
1378 liveTime
:= 30+Random(60);
1380 ceilingY
:= Unknown
;
1383 if (CurrentParticle
>= MaxParticles
-1) then CurrentParticle
:= 0 else CurrentParticle
+= 1;
1388 procedure g_GFX_Spark (fX
, fY
: Integer; count
: Word; angle
: SmallInt
; devX
, devY
: Byte);
1392 devX1
, devX2
, devY1
, devY2
: Integer;
1393 baseVelX
, baseVelY
: Single;
1397 if not gpart_dbg_enabled
then exit
;
1399 l
:= Length(Particles
);
1400 if (l
= 0) then exit
;
1401 if (count
> l
) then count
:= l
;
1405 devX1
:= devX
div 2;
1407 devY1
:= devY
div 2;
1410 b
:= DegToRad(angle
);
1412 baseVelY
:= 1.6*sin(b
);
1413 if (abs(baseVelX
) < 0.01) then baseVelX
:= 0.0;
1414 if (abs(baseVelY
) < 0.01) then baseVelY
:= 0.0;
1416 for a
:= 1 to count
do
1418 with Particles
[CurrentParticle
] do
1420 x
:= fX
-devX1
+Random(devX2
);
1421 y
:= fY
-devY1
+Random(devY2
);
1423 // check for level bounds
1424 if (x
< g_Map_MinX
) or (y
< g_Map_MinY
) or (x
> g_Map_MaxX
) or (y
> g_Map_MaxY
) then continue
;
1426 // in what environment we are starting in?
1427 pan
:= g_Map_PanelAtPoint(x
, y
, (GridTagObstacle
or GridTagLiquid
));
1428 if (pan
<> nil) then
1430 // either in a wall, or in a liquid
1431 //if ((pan.tag and GridTagObstacle) <> 0) then continue; // don't spawn in walls
1432 //env := TEnvType.ELiquid;
1437 env
:= TEnvType
.EAir
;
1440 velX
:= baseVelX
*Random
;
1441 velY
:= baseVelY
-Random
;
1446 green
:= 100+Random(155);
1450 particleType
:= TPartType
.Spark
;
1451 state
:= TPartState
.Normal
;
1453 liveTime
:= 30+Random(60);
1455 ceilingY
:= Unknown
;
1458 if (CurrentParticle
>= MaxParticles
-1) then CurrentParticle
:= 0 else CurrentParticle
+= 1;
1463 // ////////////////////////////////////////////////////////////////////////// //
1464 procedure g_GFX_SetMax (count
: Integer);
1468 if count
> 50000 then count
:= 50000;
1469 if (count
< 1) then count
:= 1;
1470 SetLength(Particles
, count
);
1471 for a
:= 0 to High(Particles
) do Particles
[a
].die();
1472 MaxParticles
:= count
;
1473 CurrentParticle
:= 0;
1477 function g_GFX_GetMax (): Integer;
1479 result
:= MaxParticles
;
1483 function FindOnceAnim (): DWORD
;
1487 if OnceAnims
<> nil then
1488 for i
:= 0 to High(OnceAnims
) do
1489 if OnceAnims
[i
].Animation
= nil then
1495 if OnceAnims
= nil then
1497 SetLength(OnceAnims
, 16);
1502 Result
:= High(OnceAnims
) + 1;
1503 SetLength(OnceAnims
, Length(OnceAnims
) + 16);
1508 procedure g_GFX_OnceAnim (x
, y
: Integer; Anim
: TAnimation
; AnimType
: Byte = 0);
1512 if not gpart_dbg_enabled
then exit
;
1514 if (Anim
= nil) then exit
;
1516 find_id
:= FindOnceAnim();
1518 OnceAnims
[find_id
].AnimType
:= AnimType
;
1519 OnceAnims
[find_id
].Animation
:= TAnimation
.Create(Anim
.FramesID
, Anim
.Loop
, Anim
.Speed
);
1520 OnceAnims
[find_id
].Animation
.Blending
:= Anim
.Blending
;
1521 OnceAnims
[find_id
].Animation
.alpha
:= Anim
.alpha
;
1522 OnceAnims
[find_id
].x
:= x
;
1523 OnceAnims
[find_id
].y
:= y
;
1527 // ////////////////////////////////////////////////////////////////////////// //
1528 procedure g_GFX_Init ();
1530 //g_Game_SetLoadingText(_lc[I_LOAD_COLLIDE_MAP]+' 1/6', 0, False);
1531 //SetLength(gCollideMap, gMapInfo.Height+1);
1532 //for a := 0 to High(gCollideMap) do SetLength(gCollideMap[a], gMapInfo.Width+1);
1535 gpart_dbg_enabled
:= false;
1540 procedure g_GFX_Free ();
1545 SetLength(Particles
, MaxParticles
);
1546 for a
:= 0 to High(Particles
) do Particles
[a
].die();
1547 CurrentParticle
:= 0;
1549 if (OnceAnims
<> nil) then
1551 for a
:= 0 to High(OnceAnims
) do OnceAnims
[a
].Animation
.Free();
1562 // ////////////////////////////////////////////////////////////////////////// //
1563 procedure g_GFX_Update ();
1569 if not gpart_dbg_enabled
then exit
;
1571 if (Particles
<> nil) then
1573 w
:= gMapInfo
.Width
;
1574 h
:= gMapInfo
.Height
;
1576 len
:= High(Particles
);
1578 for a
:= 0 to len
do
1580 if Particles
[a
].alive
then
1582 with Particles
[a
] do
1584 if (time
= liveTime
) then begin die(); continue
; end;
1585 if (x
+1 >= w
) or (y
+1 >= h
) or (x
<= 0) or (y
<= 0) then begin die(); end;
1590 end; // Particles <> nil
1595 if OnceAnims
<> nil then
1597 for a
:= 0 to High(OnceAnims
) do
1598 if OnceAnims
[a
].Animation
<> nil then
1600 case OnceAnims
[a
].AnimType
of
1603 if Random(3) = 0 then
1604 OnceAnims
[a
].x
:= OnceAnims
[a
].x
-1+Random(3);
1605 if Random(2) = 0 then
1606 OnceAnims
[a
].y
:= OnceAnims
[a
].y
-Random(2);
1610 if OnceAnims
[a
].Animation
.Played
then
1612 OnceAnims
[a
].Animation
.Free();
1613 OnceAnims
[a
].Animation
:= nil;
1616 OnceAnims
[a
].Animation
.Update();
1622 procedure g_GFX_Draw ();
1629 r
, g
, b
, a
: GLfloat
;
1636 if not gpart_dbg_enabled
then exit
;
1638 if (Particles
<> nil) then
1640 glDisable(GL_TEXTURE_2D
);
1641 if (g_dbg_scale
< 0.6) then glPointSize(1)
1642 else if (g_dbg_scale
> 1.3) then glPointSize(g_dbg_scale
+1)
1643 else glPointSize(2);
1644 glDisable(GL_POINT_SMOOTH
);
1647 glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
1651 SetLength(v
, Length(Particles
));
1652 for a
:= 0 to High(Particles
) do
1654 with Particles
[a
] do
1656 if alive
and (x
>= sX
) and (y
>= sY
) and (x
<= sX
+ sWidth
) and (sY
<= sY
+ sHeight
) then
1658 v
[count
].x
:= x
+ 0.37;
1659 v
[count
].y
:= y
+ 0.37;
1660 v
[count
].r
:= red
/ 255;
1661 v
[count
].g
:= green
/ 255;
1662 v
[count
].b
:= blue
/ 255;
1663 v
[count
].a
:= alpha
/ 255;
1669 glVertexPointer(2, GL_FLOAT
, SizeOf(Vertex
), @v
[0].x
);
1670 glColorPointer(4, GL_FLOAT
, SizeOf(Vertex
), @v
[0].r
);
1671 glEnableClientState(GL_VERTEX_ARRAY
);
1672 glEnableClientState(GL_COLOR_ARRAY
);
1673 glDisableClientState(GL_NORMAL_ARRAY
);
1674 glDisableClientState(GL_TEXTURE_COORD_ARRAY
);
1675 glDrawArrays(GL_POINTS
, 0, count
);
1679 len
:= High(Particles
);
1680 for a
:= 0 to len
do
1682 with Particles
[a
] do
1684 if not alive
then continue
;
1685 if (x
>= sX
) and (y
>= sY
) and (x
<= sX
+sWidth
) and (sY
<= sY
+sHeight
) then
1687 glColor4ub(red
, green
, blue
, alpha
);
1688 glVertex2f(x
+0.37, y
+0.37);
1696 glDisable(GL_BLEND
);
1699 if (OnceAnims
<> nil) then
1701 len
:= High(OnceAnims
);
1702 for a
:= 0 to len
do
1704 if (OnceAnims
[a
].Animation
<> nil) then
1706 with OnceAnims
[a
] do Animation
.Draw(x
, y
, TMirrorType
.None
);