DEADSOFTWARE

game: remove unneded render imports
[d2df-sdl.git] / src / game / g_gfx.pas
1 (* Copyright (C) Doom 2D: Forever Developers
2 *
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.
6 *
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.
11 *
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/>.
14 *)
15 {$INCLUDE ../shared/a_modes.inc}
16 {.$DEFINE D2F_DEBUG_FALL_MPLAT}
17 {/$DEFINE D2F_DEBUG_PART_AWAKE}
18 unit g_gfx;
20 interface
22 uses
23 e_log, g_textures;
25 const
26 BLOOD_NORMAL = 0;
27 BLOOD_SPARKS = 1;
28 BLOOD_CSPARKS = 2;
29 BLOOD_COMBINE = 3;
31 ONCEANIM_NONE = 0;
32 ONCEANIM_SMOKE = 1;
34 MARK_FREE = 0;
35 MARK_WALL = 1;
36 MARK_WATER = 2;
37 MARK_ACID = 4;
38 MARK_LIFTDOWN = 8;
39 MARK_LIFTUP = 16;
40 MARK_DOOR = 32;
41 MARK_LIFTLEFT = 64;
42 MARK_LIFTRIGHT = 128;
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_Mark (x, y, Width, Height: Integer; t: Byte; st: Boolean=true);
64 procedure g_GFX_Update ();
66 var
67 gpart_dbg_enabled: Boolean = true;
68 gpart_dbg_phys_enabled: Boolean = true;
71 //WARNING: only for Holmes!
72 function awmIsSetHolmes (x, y: Integer): Boolean; inline;
74 type (* private state *)
75 TPartType = (Blood, Spark, Bubbles, Water);
76 TPartState = (Free, Normal, Stuck, Sleeping);
77 TFloorType = (Wall, LiquidIn, LiquidOut);
78 // Wall: floorY is just before floor
79 // LiquidIn: floorY is liquid *start* (i.e. just in a liquid)
80 // LiquidOut: floorY is liquid *end* (i.e. just out of a liquid)
81 TEnvType = (EAir, ELiquid, EWall); // where particle is now
83 // note: this MUST be record, so we can keep it in
84 // dynamic array and has sequential memory access pattern
85 PParticle = ^TParticle;
86 TParticle = record
87 x, y: Integer;
88 oldX, oldY: Integer;
89 velX, velY: Single;
90 accelX, accelY: Single;
91 state: TPartState;
92 particleType: TPartType;
93 red, green, blue: Byte;
94 alpha: Byte;
95 time, liveTime, waitTime: Word;
96 stickDX: Integer; // STATE_STICK: -1,1: stuck to a wall; 0: stuck to ceiling
97 justSticked: Boolean; // not used
98 floorY: Integer; // actually, floor-1; `Unknown`: unknown
99 floorType: TFloorType;
100 env: TEnvType; // where particle is now
101 ceilingY: Integer; // actually, ceiling+1; `Unknown`: unknown
102 wallEndY: Integer; // if we stuck to a wall, this is where wall ends
104 //k8: sorry, i have to emulate virtual methods this way, 'cause i haet `Object`
105 procedure thinkerBloodAndWater ();
106 procedure thinkerSpark ();
107 procedure thinkerBubble ();
109 procedure findFloor (force: Boolean=false); // this updates `floorY` if forced or Unknown
110 procedure findCeiling (force: Boolean=false); // this updates `ceilingY` if forced or Unknown
112 procedure freeze (); inline; // remove velocities and acceleration
113 procedure sleep (); inline; // switch to sleep mode
115 function checkAirStreams (): Boolean; // `true`: affected by air stream
117 function alive (): Boolean; inline;
118 procedure die (); inline;
119 procedure think (); inline;
120 end;
122 var (* private state *)
123 Particles: array of TParticle = nil;
125 implementation
127 uses
128 g_map, g_panel, g_basic, Math,
129 g_options, g_console, SysUtils, g_triggers, MAPDEF,
130 g_game, g_language, g_net, utils, xprofiler;
133 const
134 Unknown = Integer($7fffffff);
136 var
137 MaxParticles: Integer = 0;
138 CurrentParticle: Integer = 0;
139 // awakeMap has one bit for each map grid cell; on g_Mark,
140 // corresponding bits will be set, and in `think()` all particles
141 // in marked cells will be awaken
142 awakeMap: packed array of LongWord = nil;
143 awakeMapH: Integer = -1;
144 awakeMapW: Integer = -1;
145 awakeMinX, awakeMinY: Integer;
146 awakeDirty: Boolean = false;
147 {$IF DEFINED(D2F_DEBUG_PART_AWAKE)}
148 awakeMapHlm: packed array of LongWord = nil;
149 {$ENDIF}
152 // ////////////////////////////////////////////////////////////////////////// //
153 function awmIsSetHolmes (x, y: Integer): Boolean; inline;
154 begin
155 {$IF DEFINED(D2F_DEBUG_PART_AWAKE)}
156 if (Length(awakeMapHlm) = 0) then begin result := false; exit; end;
157 x := (x-awakeMinX) div mapGrid.tileSize;
158 y := (y-awakeMinY) div mapGrid.tileSize;
159 if (x >= 0) and (y >= 0) and (x div 32 < awakeMapW) and (y < awakeMapH) then
160 begin
161 if (y*awakeMapW+x div 32 < Length(awakeMapHlm)) then
162 begin
163 result := ((awakeMapHlm[y*awakeMapW+x div 32] and (LongWord(1) shl (x mod 32))) <> 0);
164 end
165 else
166 begin
167 result := false;
168 end;
169 end
170 else
171 begin
172 result := false;
173 end;
174 {$ELSE}
175 result := false;
176 {$ENDIF}
177 end;
180 // ////////////////////////////////////////////////////////////////////////// //
181 // HACK! using mapgrid
182 procedure awmClear (); inline;
183 begin
184 {$IF DEFINED(D2F_DEBUG_PART_AWAKE)}
185 if (Length(awakeMap) > 0) then
186 begin
187 if (Length(awakeMapHlm) <> Length(awakeMap)) then SetLength(awakeMapHlm, Length(awakeMap));
188 Move(awakeMap[0], awakeMapHlm[0], Length(awakeMap)*sizeof(awakeMap[0]));
189 end;
190 {$ENDIF}
191 if awakeDirty and (awakeMapW > 0) then
192 begin
193 FillDWord(awakeMap[0], Length(awakeMap), 0);
194 awakeDirty := false;
195 end;
196 end;
199 procedure awmSetup ();
200 begin
201 assert(mapGrid <> nil);
202 awakeMapW := (mapGrid.gridWidth+mapGrid.tileSize-1) div mapGrid.tileSize;
203 awakeMapW := (awakeMapW+31) div 32; // LongWord has 32 bits ;-)
204 awakeMapH := (mapGrid.gridHeight+mapGrid.tileSize-1) div mapGrid.tileSize;
205 awakeMinX := mapGrid.gridX0;
206 awakeMinY := mapGrid.gridY0;
207 SetLength(awakeMap, awakeMapW*awakeMapH);
208 {$IF DEFINED(D2F_DEBUG_PART_AWAKE)}
209 SetLength(awakeMapHlm, awakeMapW*awakeMapH);
210 FillDWord(awakeMapHlm[0], Length(awakeMapHlm), 0);
211 {$ENDIF}
212 //{$IF DEFINED(D2F_DEBUG)}
213 e_LogWritefln('particle awake map: %sx%s (for grid of size %sx%s)', [awakeMapW, awakeMapH, mapGrid.gridWidth, mapGrid.gridHeight]);
214 //{$ENDIF}
215 awakeDirty := true;
216 awmClear();
217 end;
220 function awmIsSet (x, y: Integer): Boolean; inline;
221 begin
222 x := (x-awakeMinX) div mapGrid.tileSize;
223 y := (y-awakeMinY) div mapGrid.tileSize;
224 if (x >= 0) and (y >= 0) and (x div 32 < awakeMapW) and (y < awakeMapH) then
225 begin
226 {$IF DEFINED(D2F_DEBUG)}
227 assert(y*awakeMapW+x div 32 < Length(awakeMap));
228 {$ENDIF}
229 result := ((awakeMap[y*awakeMapW+x div 32] and (LongWord(1) shl (x mod 32))) <> 0);
230 end
231 else
232 begin
233 result := false;
234 end;
235 end;
238 procedure awmSet (x, y: Integer); inline;
239 var
240 v: PLongWord;
241 begin
242 x := (x-awakeMinX) div mapGrid.tileSize;
243 y := (y-awakeMinY) div mapGrid.tileSize;
244 if (x >= 0) and (y >= 0) and (x div 32 < awakeMapW) and (y < awakeMapH) then
245 begin
246 {$IF DEFINED(D2F_DEBUG)}
247 assert(y*awakeMapW+x div 32 < Length(awakeMap));
248 {$ENDIF}
249 v := @awakeMap[y*awakeMapW+x div 32];
250 v^ := v^ or (LongWord(1) shl (x mod 32));
251 awakeDirty := true;
252 end;
253 end;
256 // ////////////////////////////////////////////////////////////////////////// //
257 // st: set mark
258 // t: mark type
259 // currently unused
260 procedure g_Mark (x, y, Width, Height: Integer; t: Byte; st: Boolean=true);
261 const Extrude = 1;
262 var
263 dx, dy, ex, ey: Integer;
264 v: PLongWord;
265 begin
266 if (not gpart_dbg_enabled) or (not gpart_dbg_phys_enabled) then exit;
267 if (awakeMapW < 1) or (awakeMapH < 1) then exit;
269 if (Width < 1) or (Height < 1) then exit;
271 // make some border, so we'll hit particles around the panel
272 ex := x+Width+Extrude-1-awakeMinX;
273 ey := y+Height+Extrude-1-awakeMinY;
274 x := (x-Extrude)-awakeMinX;
275 y := (y-Extrude)-awakeMinY;
277 x := x div mapGrid.tileSize;
278 y := y div mapGrid.tileSize;
279 ex := ex div mapGrid.tileSize;
280 ey := ey div mapGrid.tileSize;
282 // has something to do?
283 if (ex < 0) or (ey < 0) or (x >= awakeMapW*32) or (y >= awakeMapH) then exit;
284 if (x < 0) then x := 0;
285 if (y < 0) then y := 0;
286 if (ex >= awakeMapW*32) then ex := awakeMapW*32-1;
287 if (ey >= awakeMapH) then ey := awakeMapH;
289 awakeDirty := true;
290 for dy := y to ey do
291 begin
292 for dx := x to ex do
293 begin
294 {$IF DEFINED(D2F_DEBUG)}
295 assert((dx >= 0) and (dy >= 0) and (dx div 32 < awakeMapW) and (dy < awakeMapH));
296 assert(dy*awakeMapW+dx div 32 < Length(awakeMap));
297 {$ENDIF}
298 v := @awakeMap[dy*awakeMapW+dx div 32];
299 v^ := v^ or (LongWord(1) shl (dx mod 32));
300 end;
301 end;
302 end;
305 // ////////////////////////////////////////////////////////////////////////// //
306 function TParticle.alive (): Boolean; inline; begin result := (state <> TPartState.Free); end;
307 procedure TParticle.die (); inline; begin state := TPartState.Free; end;
309 // remove velocities and acceleration
310 procedure TParticle.freeze (); inline;
311 begin
312 // stop right there, you criminal scum!
313 velX := 0;
314 velY := 0;
315 accelX := 0;
316 accelY := 0;
317 end;
320 // `true`: affected by air stream
321 function TParticle.checkAirStreams (): Boolean;
322 var
323 pan: TPanel;
324 r: Integer;
325 begin
326 pan := g_Map_PanelAtPoint(x, y, GridTagLift);
327 result := (pan <> nil) and WordBool(pan.PanelType and (PANEL_LIFTUP or PANEL_LIFTDOWN or PANEL_LIFTLEFT or PANEL_LIFTRIGHT));
328 r := Random(3);
329 if result then
330 begin
331 case pan.LiftType of
332 LIFTTYPE_UP:
333 begin
334 if (velY > -1-r) then velY -= 0.8;
335 if (abs(velX) > 0.1) then velX -= velX/10.0;
336 velX += (Random-Random)*0.2;
337 accelY := 0.15;
338 end;
339 LIFTTYPE_DOWN:
340 begin
341 if (velY < 1+r) then velY += 0.8;
342 accelY := 0.15;
343 end;
344 LIFTTYPE_LEFT:
345 begin
346 if (velX > -8-r) then velX -= (8+r) div 2;
347 accelY := 0.15;
348 end;
349 LIFTTYPE_RIGHT:
350 begin
351 if (velX < 8+r) then velX += (8+r) div 2;
352 accelY := 0.15;
353 end;
354 else
355 result := false;
356 end;
357 // awake
358 if result and (state = TPartState.Sleeping) then state := TPartState.Normal;
359 end;
360 end;
363 // switch to sleep mode
364 procedure TParticle.sleep (); inline;
365 begin
366 if not checkAirStreams() then
367 begin
368 state := TPartState.Sleeping;
369 freeze();
370 end;
371 end;
374 procedure TParticle.findFloor (force: Boolean=false);
375 var
376 ex: Integer;
377 pan: TPanel;
378 begin
379 if (not force) and (floorY <> Unknown) then exit;
380 // stuck in the wall? rescan, 'cause it can be mplat
381 if (env = TEnvType.EWall) then
382 begin
383 pan := g_Map_PanelAtPoint(x, y, (GridTagObstacle or GridTagLiquid));
384 if (pan <> nil) then
385 begin
386 // either in a wall, or in a liquid
387 if ((pan.tag and GridTagObstacle) <> 0) then
388 begin
389 // we are in the wall, wtf?!
390 floorY := y;
391 env := TEnvType.EWall;
392 floorType := TFloorType.Wall;
393 state := TPartState.Sleeping; // anyway
394 exit;
395 end;
396 // we are in liquid, trace to liquid end
397 env := TEnvType.ELiquid;
398 end;
399 end;
400 // are we in a liquid?
401 if (env = TEnvType.ELiquid) then
402 begin
403 // trace out of the liquid
404 //env := TEnvType.ELiquid;
405 floorType := TFloorType.LiquidOut;
406 //e_LogWritefln('tracing out of a liquid; floorY=%s; y=%s', [floorY, y]);
407 mapGrid.traceOrthoRayWhileIn(ex, floorY, x, y, x, g_Map_MaxY, GridTagLiquid);
408 floorY += 1; // so `floorY` is just out of a liquid
409 //e_LogWritefln(' traced out of a liquid; floorY=%s; y=%s', [floorY, y]);
410 end
411 else
412 begin
413 // in the air
414 assert(env = TEnvType.EAir);
415 //env := TEnvType.EAir;
416 pan := g_Map_traceToNearest(x, y, x, g_Map_MaxY, (GridTagObstacle or GridTagLiquid), @ex, @floorY);
417 if (pan <> nil) then
418 begin
419 // wall or liquid
420 if ((pan.tag and GridTagObstacle) <> 0) then
421 begin
422 // wall
423 floorType := TFloorType.Wall;
424 end
425 else
426 begin
427 // liquid
428 floorType := TFloorType.LiquidIn; // entering liquid
429 floorY += 1; // so `floorY` is just in a liquid
430 end;
431 end
432 else
433 begin
434 // out of the level; assume wall, but it doesn't really matter
435 floorType := TFloorType.Wall;
436 floorY := g_Map_MaxY+2;
437 end;
438 end;
439 end;
442 procedure TParticle.findCeiling (force: Boolean=false);
443 var
444 ex: Integer;
445 begin
446 if (not force) and (ceilingY <> Unknown) then exit;
447 if (nil = g_Map_traceToNearest(x, y, x, g_Map_MinY, GridTagSolid, @ex, @ceilingY)) then
448 begin
449 ceilingY := g_Map_MinY-2;
450 end;
451 end;
454 procedure TParticle.think (); inline;
455 procedure awake ();
456 begin
457 if (state = TPartState.Stuck) then
458 begin
459 //writeln('awaking particle at (', x, ',', y, ')');
460 if (stickDX = 0) then
461 begin
462 state := TPartState.Normal; // stuck to a ceiling
463 end
464 else
465 begin
466 // stuck to a wall, check if wall is still there
467 if (wallEndY <> Unknown) then
468 begin
469 wallEndY := Unknown;
470 if (g_Map_PanelAtPoint(x+stickDX, y, GridTagObstacle) = nil) then
471 begin
472 // a wall was moved out, start falling
473 state := TPartState.Normal;
474 if (velY = 0) then velY := 0.1;
475 if (accelY = 0) then accelY := 0.5;
476 end;
477 end;
478 end;
479 end
480 else
481 begin
482 state := TPartState.Normal;
483 if (velY = 0) then velY := 0.1;
484 if (accelY = 0) then accelY := 0.5;
485 end;
486 floorY := Unknown;
487 ceilingY := Unknown;
488 end;
490 begin
491 oldx := x;
492 oldy := y;
493 // awake sleeping particle, if necessary
494 if awakeDirty then
495 begin
496 if awmIsSet(x, y) then awake();
498 case state of
499 TPartState.Sleeping, TPartState.Stuck:
500 if awmIsSet(x, y) then awake();
501 else
502 if (env = TEnvType.EWall) and awmIsSet(x, y) then awake();
503 end;
505 end;
506 case particleType of
507 TPartType.Blood, TPartType.Water: thinkerBloodAndWater();
508 TPartType.Spark: thinkerSpark();
509 TPartType.Bubbles: thinkerBubble();
510 end;
511 end;
514 // ////////////////////////////////////////////////////////////////////////// //
515 procedure TParticle.thinkerBloodAndWater ();
516 procedure stickToCeiling ();
517 begin
518 state := TPartState.Stuck;
519 stickDX := 0;
520 freeze();
521 ceilingY := y; // yep
522 end;
524 procedure stickToWall (dx: Integer);
525 var
526 ex: Integer;
527 begin
528 state := TPartState.Stuck;
529 if (dx > 0) then stickDX := 1 else stickDX := -1;
530 freeze();
531 // find next floor transition
532 findFloor();
533 // find `wallEndY`
534 mapGrid.traceOrthoRayWhileIn(ex, wallEndY, x+stickDX, y, x+stickDX, floorY+1, (GridTagWall or GridTagDoor or GridTagStep));
535 end;
537 procedure hitAFloor ();
538 begin
539 state := TPartState.Sleeping; // we aren't moving anymore
540 freeze();
541 floorY := y; // yep
542 floorType := TFloorType.Wall; // yep
543 end;
545 // `true`: didn't, get outa thinker
546 function drip (): Boolean;
547 begin
548 case particleType of
549 TPartType.Blood: result := (Random(200) = 100);
550 TPartType.Water: result := (Random(30) = 15);
551 else raise Exception.Create('internal error in particle engine: drip');
552 end;
553 if result then
554 begin
555 velY := 0.5;
556 accelY := 0.15;
557 // if we're falling from ceiling, switch to normal mode
558 if (state = TPartState.Stuck) and (stickDX = 0) then state := TPartState.Normal;
559 end;
560 end;
562 // switch to freefall mode
563 procedure freefall ();
564 begin
565 state := TPartState.Normal;
566 velY := 0.5;
567 accelY := 0.15;
568 end;
570 procedure applyGravity (inLiquid: Boolean);
571 begin
572 state := TPartState.Normal;
573 if inLiquid then
574 begin
575 velY := 0.5;
576 accelY := 0.15;
577 end
578 else
579 begin
580 velY := 0.8;
581 accelY := 0.5;
582 end;
583 end;
585 label
586 _done, _gravityagain, _stuckagain;
587 var
588 pan: TPanel;
589 dx, dy: SmallInt;
590 ex, ey: Integer;
591 checkEnv, inAir, inStep: Boolean;
592 floorJustTraced: Boolean;
593 {$IF DEFINED(D2F_DEBUG_FALL_MPLAT)}
594 oldFloorY: Integer;
595 {$ENDIF}
596 begin
597 if not gpart_dbg_phys_enabled then begin x += round(velX); y += round(velY); goto _done; end;
599 if gAdvBlood then
600 begin
601 // still check for air streams when sleeping (no)
602 if (state = TPartState.Sleeping) then begin {checkAirStreams();} goto _done; end; // so blood will dissolve
604 // process stuck particles
605 if (state = TPartState.Stuck) then
606 begin
607 // stuck to a ceiling?
608 if (stickDX = 0) then
609 begin
610 // yeah, stuck to a ceiling
611 if (ceilingY = Unknown) then findCeiling();
612 // dropped from a ceiling?
613 if (y > ceilingY) then
614 begin
615 // yep
616 velY := 0.5;
617 accelY := 0.15;
618 state := TPartState.Normal;
619 end
620 else
621 begin
622 // otherwise, try to drip
623 if drip() then goto _done;
624 end;
625 end
626 else
627 begin
628 // stuck to a wall
629 if (wallEndY = Unknown) then
630 begin
631 // this can happen if mplat was moved out; find new `wallEndY`
632 findFloor(true); // force trace, just in case
633 if (floorType = TFloorType.LiquidOut) then env := TEnvType.ELiquid else env := TEnvType.EAir;
634 mapGrid.traceOrthoRayWhileIn(ex, wallEndY, x+stickDX, y, x+stickDX, floorY+1, (GridTagWall or GridTagDoor or GridTagStep));
635 end;
636 _stuckagain:
637 // floor transition?
638 if (wallEndY <= floorY) and (y >= floorY) then
639 begin
640 y := floorY;
641 case floorType of
642 TFloorType.Wall: // hit the ground
643 begin
644 // check if our ground wasn't moved since the last scan
645 findFloor(true); // force trace
646 if (y = floorY) then
647 begin
648 sleep();
649 goto _done; // nothing to do anymore
650 end;
651 // otherwise, do it again
652 goto _stuckagain;
653 end;
654 TFloorType.LiquidIn: // entering the liquid
655 begin
656 // rescan, so we'll know when we'll exit the liquid
657 findFloor(true); // force rescan
658 end;
659 TFloorType.LiquidOut: // exiting the liquid
660 begin
661 // rescan, so we'll know when we'll enter something interesting
662 findFloor(true); // force rescan
663 if (floorType = TFloorType.Wall) and (floorY = y) then begin sleep(); goto _done; end;
664 end;
665 end;
666 end;
667 // wall transition?
668 if (floorY <= wallEndY) and (y >= wallEndY) then
669 begin
670 // just unstuck from the wall, switch to freefall mode
671 y := wallEndY;
672 freefall();
673 end
674 else
675 begin
676 // otherwise, try to drip
677 if drip() then goto _done;
678 end;
679 end;
680 // nope, process as usual
681 end;
683 // it is important to have it here
684 dx := round(velX);
685 dy := round(velY);
687 inAir := checkAirStreams();
689 // gravity, if not stuck
690 if (state <> TPartState.Stuck) and (abs(velX) < 0.1) and (abs(velY) < 0.1) then
691 begin
692 floorJustTraced := (floorY = Unknown);
693 if floorJustTraced then findFloor();
694 _gravityagain:
695 // floor transition?
696 if (y = floorY) then
697 begin
698 case floorType of
699 TFloorType.Wall: // hit the ground
700 begin
701 // check if our ground wasn't moved since the last scan
702 if not floorJustTraced then
703 begin
704 findFloor(true); // force trace
705 if (floorType = TFloorType.LiquidOut) then env := TEnvType.ELiquid else env := TEnvType.EAir;
706 if (y <> floorY) then goto _gravityagain;
707 end;
708 // otherwise, nothing to do
709 end;
710 TFloorType.LiquidIn: // entering the liquid
711 begin
712 // rescan, so we'll know when we'll exit the liquid
713 findFloor(true); // force rescan
714 applyGravity(true);
715 end;
716 TFloorType.LiquidOut: // exiting the liquid
717 begin
718 // rescan, so we'll know when we'll enter something interesting
719 findFloor(true); // force rescan
720 if (floorType <> TFloorType.Wall) or (floorY <> y) then applyGravity(floorType = TFloorType.LiquidIn);
721 end;
722 end;
723 end
724 else
725 begin
726 // looks like we're in the air
727 applyGravity(false);
728 end;
729 end;
731 // trace movement
732 if (dx <> 0) then
733 begin
734 // has some horizontal velocity
735 inStep := False;
736 pan := g_Map_traceToNearest(x, y, x+dx, y+dy, GridTagSolid, @ex, @ey);
737 if (pan = nil) and (dy >= 0) then
738 begin
739 // do not stuck inside step
740 if g_Map_traceToNearest(x, y, x, y, GridTagStep, nil, nil) = nil then
741 // check for step panel below
742 pan := g_Map_traceToNearest(x, y, x, y+dy, GridTagStep, nil, @ey);
743 inStep := pan <> nil;
744 if inStep then
745 begin
746 // stick to panel edges
747 if ex < pan.X then
748 ex := pan.X
749 else if ex > pan.X + pan.Width - 1 then
750 ex := pan.X + pan.Width - 1;
751 end;
752 end;
753 checkEnv := (x <> ex);
754 x := ex;
755 y := ey;
756 if checkEnv then
757 begin
758 // dunno yet
759 floorY := Unknown;
760 ceilingY := Unknown;
761 // check environment (air/liquid)
762 if (g_Map_PanelAtPoint(x, y, GridTagLiquid) <> nil) then env := TEnvType.ELiquid else env := TEnvType.EAir;
763 end;
764 if (pan <> nil) then
765 begin
766 if inStep then
767 stickToWall(dx)
768 else
769 begin
770 // we stuck
771 // the only case when we can have both ceiling and wall is corner; stick to wall in this case
772 // check if we stuck to a wall
773 if (dx < 0) then dx := -1 else dx := 1;
774 if (g_Map_PanelAtPoint(x+dx, y, GridTagSolid) <> nil) then
775 begin
776 // stuck to a wall
777 stickToWall(dx);
778 end
779 else
780 begin
781 // stuck to a ceiling
782 stickToCeiling();
783 end;
784 end;
785 end;
786 end
787 else if (dy <> 0) then
788 begin
789 // has only vertical velocity
790 if (dy < 0) then
791 begin
792 // flying up
793 if (ceilingY = Unknown) then findCeiling(); // need to do this anyway
794 y += dy;
795 if (y <= ceilingY) then begin y := ceilingY; stickToCeiling(); end; // oops, hit a ceiling
796 // environment didn't changed
797 end
798 else
799 begin
800 while (dy > 0) do
801 begin
802 // falling down
803 floorJustTraced := (floorY = Unknown);
804 if floorJustTraced then findFloor();
805 if (floorType = TFloorType.LiquidOut) then env := TEnvType.ELiquid else env := TEnvType.EAir;
806 y += dy;
807 //e_LogWritefln('floorY=%s; newy=%s; dY=%s; floorType=%s', [floorY, y, dY, floorType]);
808 if (y >= floorY) then
809 begin
810 // floor transition
811 dy := y-floorY;
812 y := floorY;
813 //e_LogWritefln(' HIT FLOORY: floorY=%s; newy=%s; dY=%s; floorType=%s', [floorY, y, dY, floorType]);
814 case floorType of
815 TFloorType.Wall: // hit the ground
816 begin
817 // check if our ground wasn't moved since the last scan
818 if not floorJustTraced then
819 begin
820 {$IF DEFINED(D2F_DEBUG_FALL_MPLAT)}
821 oldFloorY := floorY;
822 {$ENDIF}
823 findFloor(true); // force trace
824 {$IF DEFINED(D2F_DEBUG_FALL_MPLAT)}
825 if (floorY <> oldFloorY) then
826 begin
827 e_LogWritefln('force rescanning vpart at (%s,%s); oldFloorY=%s; floorY=%s', [x, y, oldFloorY, floorY]);
828 end;
829 {$ENDIF}
830 if (floorType = TFloorType.LiquidOut) then env := TEnvType.ELiquid else env := TEnvType.EAir;
831 if (y <> floorY) then continue;
832 end;
833 // environment didn't changed
834 if not inAir then hitAFloor();
835 break; // done with vertical movement
836 end;
837 TFloorType.LiquidIn: // entering the liquid
838 begin
839 // we're entered the liquid
840 env := TEnvType.ELiquid;
841 // rescan, so we'll know when we'll exit the liquid
842 findFloor(true); // force rescan
843 end;
844 TFloorType.LiquidOut: // exiting the liquid
845 begin
846 // we're exited the liquid
847 env := TEnvType.EAir;
848 // rescan, so we'll know when we'll enter something interesting
849 findFloor(true); // force rescan
850 if (floorType = TFloorType.Wall) and (floorY = y) then
851 begin
852 if not inAir then hitAFloor();
853 break; // done with vertical movement
854 end;
855 end;
856 end;
857 end
858 else
859 begin
860 break; // done with vertical movement
861 end;
862 end;
863 end;
864 end;
865 end // if gAdvBlood
866 else
867 begin
868 // simple blood
869 dx := round(velX);
870 dy := round(velY);
871 y += dy;
872 x += dx;
873 if (g_Map_PanelAtPoint(x, y, GridTagObstacle) <> nil) then begin die(); exit; end;
874 end;
876 _done:
877 if (x < g_Map_MinX) or (y < g_Map_MinY) or (x > g_Map_MaxX) or (y > g_Map_MaxY) then begin die(); end;
879 velX += accelX;
880 velY += accelY;
882 // blood will dissolve in other liquids
883 if (particleType = TPartType.Blood) then
884 begin
885 if (env = TEnvType.ELiquid) then
886 begin
887 if waitTime > 0 then
888 waitTime -= 1
889 else
890 time += 1;
891 if (liveTime <= 0) then begin die(); exit; end;
892 ex := 255-trunc(255.0*time/liveTime);
893 if (ex <= 10) then begin die(); exit; end;
894 if (ex > 250) then ex := 255;
895 alpha := Byte(ex);
896 end;
897 end
898 else
899 begin
900 // water will disappear in any liquid
901 if (env = TEnvType.ELiquid) then begin die(); exit; end;
902 if waitTime > 0 then
903 waitTime -= 1
904 else
905 time += 1;
906 // dry water
907 if (liveTime <= 0) then begin die(); exit; end;
908 ex := 255-trunc(255.0*time/liveTime);
909 if (ex <= 10) then begin die(); exit; end;
910 if (ex > 250) then ex := 255;
911 alpha := Byte(ex);
912 end;
913 end;
916 // ////////////////////////////////////////////////////////////////////////// //
917 procedure g_GFX_SparkVel (fX, fY: Integer; count: Word; vx, vy: Integer; devX, devY: Byte); forward;
919 procedure g_GFX_Blood (fX, fY: Integer; count: Word; vx, vy: Integer;
920 devX, devY: Word; cr, cg, cb: Byte; kind: Byte = BLOOD_NORMAL);
922 function genColor (cbase, crnd: Integer; def: Byte=0): Byte;
923 begin
924 if (cbase > 0) then
925 begin
926 cbase += crnd;
927 if (cbase < 0) then result := 0
928 else if (cbase > 255) then result := 255
929 else result := Byte(cbase);
930 end
931 else
932 begin
933 result := def;
934 end;
935 end;
937 var
938 a: Integer;
939 devX1, devX2, devY1, devY2: Integer;
940 l: Integer;
941 crnd: Integer;
942 pan: TPanel;
943 begin
944 if not gpart_dbg_enabled then exit;
946 if (kind = BLOOD_SPARKS) then
947 begin
948 g_GFX_SparkVel(fX, fY, 2+Random(2), -vx div 2, -vy div 2, devX, devY);
949 exit;
950 end
951 else if (kind = BLOOD_CSPARKS) OR (kind = BLOOD_COMBINE) then
952 begin
953 g_GFX_SparkVel(fX, fY, count, -vx div 2, -vy div 2, devX, devY);
954 if kind <> BLOOD_COMBINE then exit
955 end;
957 l := Length(Particles);
958 if (l = 0) then exit;
959 if (count > l) then count := l;
961 devX1 := devX div 2;
962 devX2 := devX+1;
963 devY1 := devY div 2;
964 devY2 := devY+1;
966 for a := 1 to count do
967 begin
968 with Particles[CurrentParticle] do
969 begin
970 x := fX-devX1+Random(devX2);
971 y := fY-devY1+Random(devY2);
972 oldx := x;
973 oldy := y;
975 // check for level bounds
976 if (x < g_Map_MinX) or (y < g_Map_MinY) or (x > g_Map_MaxX) or (y > g_Map_MaxY) then continue;
978 // in what environment we are starting in?
979 pan := g_Map_PanelAtPoint(x, y, (GridTagSolid or GridTagLiquid));
980 if (pan <> nil) then
981 begin
982 // either in a wall, or in a liquid
983 if ((pan.tag and GridTagSolid) <> 0) then continue; // don't spawn in walls
984 env := TEnvType.ELiquid;
985 end
986 else
987 begin
988 env := TEnvType.EAir;
989 end;
991 velX := vx+(Random-Random)*3;
992 velY := vy+(Random-Random)*3;
994 if (velY > -4) then
995 begin
996 if (velY-4 < -4) then velY := -4 else velY := velY-4;
997 end;
999 accelX := -sign(velX)*Random/100;
1000 accelY := 0.8;
1002 crnd := 20*Random(6)-50;
1004 red := genColor(cr, CRnd, 0);
1005 green := genColor(cg, CRnd, 0);
1006 blue := genColor(cb, CRnd, 0);
1007 alpha := 255;
1009 particleType := TPartType.Blood;
1010 state := TPartState.Normal;
1011 time := 0;
1012 liveTime := 120+Random(40);
1013 waitTime := 20;
1014 floorY := Unknown;
1015 ceilingY := Unknown;
1016 end;
1018 if (CurrentParticle >= MaxParticles-1) then CurrentParticle := 0 else CurrentParticle += 1;
1019 end;
1020 end;
1023 procedure g_GFX_Water (fX, fY: Integer; count: Word; fVelX, fVelY: Single; devX, devY, color: Byte;
1024 simple: Boolean=false; cr: Byte=0; cg: Byte=0; cb: Byte=0);
1025 var
1026 a: Integer;
1027 devX1, devX2, devY1, devY2: Integer;
1028 l: Integer;
1029 pan: TPanel;
1030 begin
1031 if not gpart_dbg_enabled then exit;
1033 l := Length(Particles);
1034 if (l = 0) then exit;
1035 if (count > l) then count := l;
1037 if (abs(fVelX) < 3.0) then fVelX := 3.0-6.0*Random;
1039 devX1 := devX div 2;
1040 devX2 := devX+1;
1041 devY1 := devY div 2;
1042 devY2 := devY+1;
1044 if (not simple) and (color > 3) then color := 0;
1046 for a := 1 to count do
1047 begin
1048 with Particles[CurrentParticle] do
1049 begin
1050 if not simple then
1051 begin
1052 x := fX-devX1+Random(devX2);
1053 y := fY-devY1+Random(devY2);
1055 if (abs(fVelX) < 0.5) then velX := 1.0-2.0*Random else velX := fVelX*Random;
1056 if (Random(10) < 7) then velX := -velX;
1057 velY := fVelY*Random;
1058 accelX := 0.0;
1059 accelY := 0.8;
1060 end
1061 else
1062 begin
1063 x := fX;
1064 y := fY;
1066 velX := fVelX;
1067 velY := fVelY;
1068 accelX := 0.0;
1069 accelY := 0.8;
1070 end;
1072 oldx := x;
1073 oldy := y;
1075 // check for level bounds
1076 if (x < g_Map_MinX) or (y < g_Map_MinY) or (x > g_Map_MaxX) or (y > g_Map_MaxY) then continue;
1078 // this hack will allow water spawned in water to fly out
1079 // it can happen when player fell from a huge height (see "DOOM2D.WAD:\MAP03", for example)
1080 if (fVelY >= 0) then
1081 begin
1082 // in what environment we are starting in?
1083 pan := g_Map_PanelAtPoint(x, y, (GridTagObstacle or GridTagLiquid));
1084 end
1085 else
1086 begin
1087 pan := g_Map_PanelAtPoint(x, y, GridTagObstacle);
1088 end;
1089 if (pan <> nil) then continue;
1090 env := TEnvType.EAir;
1092 // color
1093 case color of
1094 1: // reddish
1095 begin
1096 red := 155+Random(9)*10;
1097 green := trunc(150*Random);
1098 blue := green;
1099 end;
1100 2: // greenish
1101 begin
1102 red := trunc(150*Random);
1103 green := 175+Random(9)*10;
1104 blue := red;
1105 end;
1106 3: // bluish
1107 begin
1108 red := trunc(200*Random);
1109 green := red;
1110 blue := 175+Random(9)*10;
1111 end;
1112 4: // Ñâîé öâåò, ñâåòëåå
1113 begin
1114 red := 20+Random(19)*10;
1115 green := red;
1116 blue := red;
1117 red := nmin(red+cr, 255);
1118 green := nmin(green+cg, 255);
1119 blue := nmin(blue+cb, 255);
1120 end;
1121 5: // Ñâîé öâåò, òåìíåå
1122 begin
1123 red := 20+Random(19)*10;
1124 green := red;
1125 blue := red;
1126 red := nmax(cr-red, 0);
1127 green := nmax(cg-green, 0);
1128 blue := nmax(cb-blue, 0);
1129 end;
1130 else // grayish
1131 begin
1132 red := 90+random(12)*10;
1133 green := red;
1134 blue := red;
1135 end;
1136 end;
1137 alpha := 255;
1139 particleType := TPartType.Water;
1140 state := TPartState.Normal;
1141 time := 0;
1142 liveTime := 60+Random(60);
1143 waitTime := 120;
1144 floorY := Unknown;
1145 ceilingY := Unknown;
1146 end;
1148 if (CurrentParticle >= MaxParticles-1) then CurrentParticle := 0 else CurrentParticle += 1;
1149 end;
1150 end;
1153 procedure g_GFX_SimpleWater (fX, fY: Integer; count: Word; fVelX, fVelY: Single; defColor, cr, cg, cb: Byte);
1154 begin
1155 g_GFX_Water(fX, fY, count, 0, 0, 0, 0, defColor, true, cr, cg, cb);
1156 end;
1159 // ////////////////////////////////////////////////////////////////////////// //
1160 procedure TParticle.thinkerBubble ();
1161 var
1162 dy: Integer;
1163 begin
1164 dy := round(velY);
1166 if (dy <> 0) then
1167 begin
1168 y += dy;
1169 if (dy < 0) then
1170 begin
1171 if (y <= ceilingY) then begin die(); exit; end;
1172 end
1173 else
1174 begin
1175 if (y >= floorY) then begin die(); exit; end;
1176 end;
1177 if (y < g_Map_MinY) or (y > g_Map_MaxY) then begin die(); exit; end;
1178 end;
1180 if (velY > -4) then velY += accelY;
1182 if waitTime > 0 then
1183 waitTime -= 1
1184 else
1185 time += 1;
1186 end;
1189 {.$DEFINE D2F_DEBUG_BUBBLES}
1190 procedure g_GFX_Bubbles (fX, fY: Integer; count: Word; devX, devY: Byte);
1191 var
1192 a, liquidx: Integer;
1193 devX1, devX2, devY1, devY2: Integer;
1194 l: Integer;
1195 {$IF DEFINED(D2F_DEBUG_BUBBLES)}
1196 stt: UInt64;
1197 nptr, ptr: Boolean;
1198 {$ENDIF}
1199 begin
1200 if not gpart_dbg_enabled then exit;
1202 l := Length(Particles);
1203 if (l = 0) then exit;
1204 if (count > l) then count := l;
1206 devX1 := devX div 2;
1207 devX2 := devX+1;
1208 devY1 := devY div 2;
1209 devY2 := devY+1;
1211 for a := 1 to count do
1212 begin
1213 with Particles[CurrentParticle] do
1214 begin
1215 x := fX-devX1+Random(devX2);
1216 y := fY-devY1+Random(devY2);
1217 oldx := x;
1218 oldy := y;
1220 // check for level bounds
1221 if (x < g_Map_MinX) or (y < g_Map_MinY) or (x > g_Map_MaxX) or (y > g_Map_MaxY) then continue;
1223 (*
1224 // don't spawn bubbles outside of the liquid
1225 if not isLiquidAt(X, Y) {ByteBool(gCollideMap[Y, X] and MARK_LIQUID)} then
1226 Continue;
1227 *)
1229 // trace liquid, so we'll know where it ends; do it in 8px steps for speed
1230 // tracer will return `false` if we started outside of the liquid
1232 {$IF DEFINED(D2F_DEBUG_BUBBLES)}
1233 stt := getTimeMicro();
1234 ptr := mapGrid.traceOrthoRayWhileIn(liquidx, liquidTopY, x, y, x, 0, GridTagWater or GridTagAcid1 or GridTagAcid2);
1235 stt := getTimeMicro()-stt;
1236 e_LogWritefln('traceOrthoRayWhileIn: time=%s (%s); liquidTopY=%s', [Integer(stt), ptr, liquidTopY]);
1237 //
1238 stt := getTimeMicro();
1239 nptr := g_Map_TraceLiquidNonPrecise(x, y, 0, -8, liquidx, liquidTopY);
1240 stt := getTimeMicro()-stt;
1241 e_LogWritefln('g_Map_TraceLiquidNonPrecise: time=%s (%s); liquidTopY=%s', [Integer(stt), nptr, liquidTopY]);
1242 if not nptr then continue;
1243 {$ELSE}
1244 if not g_Map_TraceLiquidNonPrecise(x, y, 0, -8, liquidx, ceilingY) then continue;
1245 if not g_Map_TraceLiquidNonPrecise(x, y, 0, +8, liquidx, floorY) then continue;
1246 {$ENDIF}
1248 velX := 0;
1249 velY := -1-Random;
1250 accelX := 0;
1251 accelY := velY/10;
1253 red := 255;
1254 green := 255;
1255 blue := 255;
1256 alpha := 255;
1258 state := TPartState.Normal;
1259 particleType := TPartType.Bubbles;
1260 time := 0;
1261 liveTime := 65535;
1262 waitTime := 0;
1263 end;
1265 if (CurrentParticle >= MaxParticles-1) then CurrentParticle := 0 else CurrentParticle += 1;
1266 end;
1267 end;
1270 // ////////////////////////////////////////////////////////////////////////// //
1271 procedure TParticle.thinkerSpark ();
1272 label
1273 _done;
1274 var
1275 dx, dy: SmallInt;
1276 pan: TPanel;
1277 ex, ey: Integer;
1278 begin
1279 if not gpart_dbg_phys_enabled then begin x += round(velX); y += round(velY); goto _done; end;
1281 dx := round(velX);
1282 dy := round(velY);
1284 //writeln('spark0: pos=(', x, ',', y, '); delta=(', dx, ',', dy, '); state=', state, '; ceilingY=', ceilingY, '; floorY=', floorY);
1286 // apply gravity
1287 if (abs(velX) < 0.1) and (abs(velY) < 0.1) then
1288 begin
1289 velY := 0.8;
1290 accelY := 0.5;
1291 end;
1293 // flying
1294 if (dx <> 0) then
1295 begin
1296 // has some horizontal velocity
1297 pan := g_Map_traceToNearest(x, y, x+dx, y+dy, (GridTagSolid or GridTagLiquid), @ex, @ey);
1298 if (x <> ex) then begin floorY := Unknown; ceilingY := Unknown; end; // dunno yet
1299 x := ex;
1300 y := ey;
1301 if (pan <> nil) then
1302 begin
1303 if ((pan.tag and GridTagLiquid) <> 0) then begin die(); exit; end; // die in liquid
1304 // hit the wall; falling down vertically
1305 velX := 0;
1306 accelX := 0;
1307 end;
1308 end
1309 else if (dy <> 0) then
1310 begin
1311 // has some vertical velocity
1312 if (dy < 0) then
1313 begin
1314 // flying up
1315 if (ceilingY = Unknown) then findCeiling(); // need to do this anyway
1316 y += dy;
1317 if (y <= ceilingY) then
1318 begin
1319 // oops, hit a ceiling
1320 y := ceilingY;
1321 velY := -velY;
1322 accelY := abs(accelY);
1323 end;
1324 // environment didn't changed
1325 end
1326 else
1327 begin
1328 // falling down
1329 if (floorY = Unknown) then findFloor(); // need to do this anyway
1330 y += dy;
1331 if (y >= floorY) then
1332 begin
1333 // hit something except a floor?
1334 if (floorType <> TFloorType.Wall) then begin die(); exit; end; // yep: just die
1335 // otherwise, go to sleep
1336 y := floorY;
1337 sleep();
1338 // environment didn't changed
1339 end;
1340 end;
1341 end;
1343 _done:
1344 if (x < g_Map_MinX) or (y < g_Map_MinY) or (x > g_Map_MaxX) or (y > g_Map_MaxY) then begin die(); end;
1346 if (velX <> 0.0) then velX += accelX;
1348 if (velY <> 0.0) then
1349 begin
1350 if (accelY < 10) then accelY += 0.08;
1351 velY += accelY;
1352 end;
1354 //writeln('spark1: pos=(', x, ',', y, '); delta=(', velX:6:3, ',', velY:6:3, '); state=', state, '; ceilingY=', ceilingY, '; floorY=', floorY);
1356 if waitTime > 0 then
1357 waitTime -= 1
1358 else
1359 time += 1;
1360 end;
1363 // ////////////////////////////////////////////////////////////////////////// //
1364 procedure g_GFX_SparkVel (fX, fY: Integer; count: Word; vx, vy: Integer; devX, devY: Byte);
1365 var
1366 a: Integer;
1367 devX1, devX2, devY1, devY2: Integer;
1368 l: Integer;
1369 pan: TPanel;
1370 begin
1371 if not gpart_dbg_enabled then exit;
1373 l := Length(Particles);
1374 if (l = 0) then exit;
1375 if (count > l) then count := l;
1377 devX1 := devX div 2;
1378 devX2 := devX+1;
1379 devY1 := devY div 2;
1380 devY2 := devY+1;
1382 for a := 1 to count do
1383 begin
1384 with Particles[CurrentParticle] do
1385 begin
1386 x := fX-devX1+Random(devX2);
1387 y := fY-devY1+Random(devY2);
1388 oldx := x;
1389 oldy := y;
1391 // check for level bounds
1392 if (x < g_Map_MinX) or (y < g_Map_MinY) or (x > g_Map_MaxX) or (y > g_Map_MaxY) then continue;
1394 // in what environment we are starting in?
1395 pan := g_Map_PanelAtPoint(x, y, (GridTagSolid or GridTagLiquid));
1396 if (pan <> nil) then
1397 begin
1398 // either in a wall, or in a liquid
1399 //if ((pan.tag and GridTagSolid) <> 0) then continue; // don't spawn in walls
1400 //env := TEnvType.ELiquid;
1401 continue;
1402 end
1403 else
1404 begin
1405 env := TEnvType.EAir;
1406 end;
1408 velX := vx+(Random-Random)*3;
1409 velY := vy+(Random-Random)*3;
1411 if (velY > -4) then
1412 begin
1413 if (velY-4 < -4) then velY := -4 else velY := velY-4;
1414 end;
1416 accelX := -sign(velX)*Random/100;
1417 accelY := 0.8;
1419 red := 255;
1420 green := 100+Random(155);
1421 blue := 64;
1422 alpha := 255;
1424 particleType := TPartType.Spark;
1425 state := TPartState.Normal;
1426 time := 0;
1427 liveTime := 30+Random(60);
1428 waitTime := 0;
1429 floorY := Unknown;
1430 ceilingY := Unknown;
1431 end;
1433 if (CurrentParticle >= MaxParticles-1) then CurrentParticle := 0 else CurrentParticle += 1;
1434 end;
1435 end;
1438 procedure g_GFX_Spark (fX, fY: Integer; count: Word; angle: SmallInt; devX, devY: Byte);
1439 var
1440 a: Integer;
1441 b: Single;
1442 devX1, devX2, devY1, devY2: Integer;
1443 baseVelX, baseVelY: Single;
1444 l: Integer;
1445 pan: TPanel;
1446 begin
1447 if not gpart_dbg_enabled then exit;
1449 l := Length(Particles);
1450 if (l = 0) then exit;
1451 if (count > l) then count := l;
1453 angle := 360-angle;
1455 devX1 := devX div 2;
1456 devX2 := devX+1;
1457 devY1 := devY div 2;
1458 devY2 := devY+1;
1460 b := DegToRad(angle);
1461 baseVelX := cos(b);
1462 baseVelY := 1.6*sin(b);
1463 if (abs(baseVelX) < 0.01) then baseVelX := 0.0;
1464 if (abs(baseVelY) < 0.01) then baseVelY := 0.0;
1466 for a := 1 to count do
1467 begin
1468 with Particles[CurrentParticle] do
1469 begin
1470 x := fX-devX1+Random(devX2);
1471 y := fY-devY1+Random(devY2);
1472 oldx := x;
1473 oldy := y;
1475 // check for level bounds
1476 if (x < g_Map_MinX) or (y < g_Map_MinY) or (x > g_Map_MaxX) or (y > g_Map_MaxY) then continue;
1478 // in what environment we are starting in?
1479 pan := g_Map_PanelAtPoint(x, y, (GridTagSolid or GridTagLiquid));
1480 if (pan <> nil) then
1481 begin
1482 // either in a wall, or in a liquid
1483 //if ((pan.tag and GridTagSolid) <> 0) then continue; // don't spawn in walls
1484 //env := TEnvType.ELiquid;
1485 continue;
1486 end
1487 else
1488 begin
1489 env := TEnvType.EAir;
1490 end;
1492 velX := baseVelX*Random;
1493 velY := baseVelY-Random;
1494 accelX := velX/3.0;
1495 accelY := velY/5.0;
1497 red := 255;
1498 green := 100+Random(155);
1499 blue := 64;
1500 alpha := 255;
1502 particleType := TPartType.Spark;
1503 state := TPartState.Normal;
1504 time := 0;
1505 liveTime := 30+Random(60);
1506 waitTime := 0;
1507 floorY := Unknown;
1508 ceilingY := Unknown;
1509 end;
1511 if (CurrentParticle >= MaxParticles-1) then CurrentParticle := 0 else CurrentParticle += 1;
1512 end;
1513 end;
1516 // ////////////////////////////////////////////////////////////////////////// //
1517 procedure g_GFX_SetMax (count: Integer);
1518 var
1519 a: Integer;
1520 begin
1521 if count > 50000 then count := 50000;
1522 if (count < 1) then count := 1;
1523 SetLength(Particles, count);
1524 for a := 0 to High(Particles) do Particles[a].die();
1525 MaxParticles := count;
1526 CurrentParticle := 0;
1527 end;
1530 function g_GFX_GetMax (): Integer;
1531 begin
1532 result := MaxParticles;
1533 end;
1535 // ////////////////////////////////////////////////////////////////////////// //
1536 procedure g_GFX_Init ();
1537 begin
1538 //g_Game_SetLoadingText(_lc[I_LOAD_COLLIDE_MAP]+' 1/6', 0, False);
1539 //SetLength(gCollideMap, gMapInfo.Height+1);
1540 //for a := 0 to High(gCollideMap) do SetLength(gCollideMap[a], gMapInfo.Width+1);
1541 awmSetup();
1542 {$IFDEF HEADLESS}
1543 gpart_dbg_enabled := false;
1544 {$ENDIF}
1545 end;
1548 procedure g_GFX_Free ();
1549 var
1550 a: Integer;
1551 begin
1552 Particles := nil;
1553 SetLength(Particles, MaxParticles);
1554 for a := 0 to High(Particles) do Particles[a].die();
1555 CurrentParticle := 0;
1557 awakeMap := nil;
1558 // why not?
1559 awakeMapH := -1;
1560 awakeMapW := -1;
1561 end;
1564 // ////////////////////////////////////////////////////////////////////////// //
1565 procedure g_GFX_Update ();
1566 var
1567 a: Integer;
1568 w, h: Integer;
1569 len: Integer;
1570 begin
1571 if not gpart_dbg_enabled then exit;
1573 if (Particles <> nil) then
1574 begin
1575 w := gMapInfo.Width;
1576 h := gMapInfo.Height;
1578 len := High(Particles);
1580 for a := 0 to len do
1581 begin
1582 if Particles[a].alive then
1583 begin
1584 with Particles[a] do
1585 begin
1586 if (time = liveTime) then begin die(); continue; end;
1587 if (x+1 >= w) or (y+1 >= h) or (x <= 0) or (y <= 0) then begin die(); end;
1588 think();
1589 end; // with
1590 end; // if
1591 end; // for
1592 end; // Particles <> nil
1594 // clear awake map
1595 awmClear();
1596 end;
1598 end.