DEADSOFTWARE

9c58759a071c0ace4bc29776649bf51d7c5998b3
[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_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 ();
68 var
69 gpart_dbg_enabled: Boolean = true;
70 gpart_dbg_phys_enabled: Boolean = true;
73 //WARNING: only for Holmes!
74 function awmIsSetHolmes (x, y: Integer): Boolean; inline;
76 type (* private state *)
77 TPartType = (Blood, Spark, Bubbles, Water);
78 TPartState = (Free, Normal, Stuck, Sleeping);
79 TFloorType = (Wall, LiquidIn, LiquidOut);
80 // Wall: floorY is just before floor
81 // LiquidIn: floorY is liquid *start* (i.e. just in a liquid)
82 // LiquidOut: floorY is liquid *end* (i.e. just out of a liquid)
83 TEnvType = (EAir, ELiquid, EWall); // where particle is now
85 // note: this MUST be record, so we can keep it in
86 // dynamic array and has sequential memory access pattern
87 PParticle = ^TParticle;
88 TParticle = record
89 x, y: Integer;
90 oldX, oldY: Integer;
91 velX, velY: Single;
92 accelX, accelY: Single;
93 state: TPartState;
94 particleType: TPartType;
95 red, green, blue: Byte;
96 alpha: Byte;
97 time, liveTime, waitTime: Word;
98 stickDX: Integer; // STATE_STICK: -1,1: stuck to a wall; 0: stuck to ceiling
99 justSticked: Boolean; // not used
100 floorY: Integer; // actually, floor-1; `Unknown`: unknown
101 floorType: TFloorType;
102 env: TEnvType; // where particle is now
103 ceilingY: Integer; // actually, ceiling+1; `Unknown`: unknown
104 wallEndY: Integer; // if we stuck to a wall, this is where wall ends
106 //k8: sorry, i have to emulate virtual methods this way, 'cause i haet `Object`
107 procedure thinkerBloodAndWater ();
108 procedure thinkerSpark ();
109 procedure thinkerBubble ();
111 procedure findFloor (force: Boolean=false); // this updates `floorY` if forced or Unknown
112 procedure findCeiling (force: Boolean=false); // this updates `ceilingY` if forced or Unknown
114 procedure freeze (); inline; // remove velocities and acceleration
115 procedure sleep (); inline; // switch to sleep mode
117 function checkAirStreams (): Boolean; // `true`: affected by air stream
119 function alive (): Boolean; inline;
120 procedure die (); inline;
121 procedure think (); inline;
122 end;
124 TOnceAnim = record
125 AnimType: Byte;
126 x, y: Integer;
127 oldX, oldY: Integer;
128 Animation: TAnimation;
129 end;
131 var (* private state *)
132 Particles: array of TParticle = nil;
133 OnceAnims: array of TOnceAnim = nil;
135 implementation
137 uses
138 g_map, g_panel, g_basic, Math, r_animations,
139 g_options, g_console, SysUtils, g_triggers, MAPDEF,
140 g_game, g_language, g_net, utils, xprofiler;
143 const
144 Unknown = Integer($7fffffff);
146 var
147 MaxParticles: Integer = 0;
148 CurrentParticle: Integer = 0;
149 // awakeMap has one bit for each map grid cell; on g_Mark,
150 // corresponding bits will be set, and in `think()` all particles
151 // in marked cells will be awaken
152 awakeMap: packed array of LongWord = nil;
153 awakeMapH: Integer = -1;
154 awakeMapW: Integer = -1;
155 awakeMinX, awakeMinY: Integer;
156 awakeDirty: Boolean = false;
157 {$IF DEFINED(D2F_DEBUG_PART_AWAKE)}
158 awakeMapHlm: packed array of LongWord = nil;
159 {$ENDIF}
162 // ////////////////////////////////////////////////////////////////////////// //
163 function awmIsSetHolmes (x, y: Integer): Boolean; inline;
164 begin
165 {$IF DEFINED(D2F_DEBUG_PART_AWAKE)}
166 if (Length(awakeMapHlm) = 0) then begin result := false; exit; end;
167 x := (x-awakeMinX) div mapGrid.tileSize;
168 y := (y-awakeMinY) div mapGrid.tileSize;
169 if (x >= 0) and (y >= 0) and (x div 32 < awakeMapW) and (y < awakeMapH) then
170 begin
171 if (y*awakeMapW+x div 32 < Length(awakeMapHlm)) then
172 begin
173 result := ((awakeMapHlm[y*awakeMapW+x div 32] and (LongWord(1) shl (x mod 32))) <> 0);
174 end
175 else
176 begin
177 result := false;
178 end;
179 end
180 else
181 begin
182 result := false;
183 end;
184 {$ELSE}
185 result := false;
186 {$ENDIF}
187 end;
190 // ////////////////////////////////////////////////////////////////////////// //
191 // HACK! using mapgrid
192 procedure awmClear (); inline;
193 begin
194 {$IF DEFINED(D2F_DEBUG_PART_AWAKE)}
195 if (Length(awakeMap) > 0) then
196 begin
197 if (Length(awakeMapHlm) <> Length(awakeMap)) then SetLength(awakeMapHlm, Length(awakeMap));
198 Move(awakeMap[0], awakeMapHlm[0], Length(awakeMap)*sizeof(awakeMap[0]));
199 end;
200 {$ENDIF}
201 if awakeDirty and (awakeMapW > 0) then
202 begin
203 FillDWord(awakeMap[0], Length(awakeMap), 0);
204 awakeDirty := false;
205 end;
206 end;
209 procedure awmSetup ();
210 begin
211 assert(mapGrid <> nil);
212 awakeMapW := (mapGrid.gridWidth+mapGrid.tileSize-1) div mapGrid.tileSize;
213 awakeMapW := (awakeMapW+31) div 32; // LongWord has 32 bits ;-)
214 awakeMapH := (mapGrid.gridHeight+mapGrid.tileSize-1) div mapGrid.tileSize;
215 awakeMinX := mapGrid.gridX0;
216 awakeMinY := mapGrid.gridY0;
217 SetLength(awakeMap, awakeMapW*awakeMapH);
218 {$IF DEFINED(D2F_DEBUG_PART_AWAKE)}
219 SetLength(awakeMapHlm, awakeMapW*awakeMapH);
220 FillDWord(awakeMapHlm[0], Length(awakeMapHlm), 0);
221 {$ENDIF}
222 //{$IF DEFINED(D2F_DEBUG)}
223 e_LogWritefln('particle awake map: %sx%s (for grid of size %sx%s)', [awakeMapW, awakeMapH, mapGrid.gridWidth, mapGrid.gridHeight]);
224 //{$ENDIF}
225 awakeDirty := true;
226 awmClear();
227 end;
230 function awmIsSet (x, y: Integer): Boolean; inline;
231 begin
232 x := (x-awakeMinX) div mapGrid.tileSize;
233 y := (y-awakeMinY) div mapGrid.tileSize;
234 if (x >= 0) and (y >= 0) and (x div 32 < awakeMapW) and (y < awakeMapH) then
235 begin
236 {$IF DEFINED(D2F_DEBUG)}
237 assert(y*awakeMapW+x div 32 < Length(awakeMap));
238 {$ENDIF}
239 result := ((awakeMap[y*awakeMapW+x div 32] and (LongWord(1) shl (x mod 32))) <> 0);
240 end
241 else
242 begin
243 result := false;
244 end;
245 end;
248 procedure awmSet (x, y: Integer); inline;
249 var
250 v: PLongWord;
251 begin
252 x := (x-awakeMinX) div mapGrid.tileSize;
253 y := (y-awakeMinY) div mapGrid.tileSize;
254 if (x >= 0) and (y >= 0) and (x div 32 < awakeMapW) and (y < awakeMapH) then
255 begin
256 {$IF DEFINED(D2F_DEBUG)}
257 assert(y*awakeMapW+x div 32 < Length(awakeMap));
258 {$ENDIF}
259 v := @awakeMap[y*awakeMapW+x div 32];
260 v^ := v^ or (LongWord(1) shl (x mod 32));
261 awakeDirty := true;
262 end;
263 end;
266 // ////////////////////////////////////////////////////////////////////////// //
267 // st: set mark
268 // t: mark type
269 // currently unused
270 procedure g_Mark (x, y, Width, Height: Integer; t: Byte; st: Boolean=true);
271 const Extrude = 1;
272 var
273 dx, dy, ex, ey: Integer;
274 v: PLongWord;
275 begin
276 if (not gpart_dbg_enabled) or (not gpart_dbg_phys_enabled) then exit;
277 if (awakeMapW < 1) or (awakeMapH < 1) then exit;
279 if (Width < 1) or (Height < 1) then exit;
281 // make some border, so we'll hit particles around the panel
282 ex := x+Width+Extrude-1-awakeMinX;
283 ey := y+Height+Extrude-1-awakeMinY;
284 x := (x-Extrude)-awakeMinX;
285 y := (y-Extrude)-awakeMinY;
287 x := x div mapGrid.tileSize;
288 y := y div mapGrid.tileSize;
289 ex := ex div mapGrid.tileSize;
290 ey := ey div mapGrid.tileSize;
292 // has something to do?
293 if (ex < 0) or (ey < 0) or (x >= awakeMapW*32) or (y >= awakeMapH) then exit;
294 if (x < 0) then x := 0;
295 if (y < 0) then y := 0;
296 if (ex >= awakeMapW*32) then ex := awakeMapW*32-1;
297 if (ey >= awakeMapH) then ey := awakeMapH;
299 awakeDirty := true;
300 for dy := y to ey do
301 begin
302 for dx := x to ex do
303 begin
304 {$IF DEFINED(D2F_DEBUG)}
305 assert((dx >= 0) and (dy >= 0) and (dx div 32 < awakeMapW) and (dy < awakeMapH));
306 assert(dy*awakeMapW+dx div 32 < Length(awakeMap));
307 {$ENDIF}
308 v := @awakeMap[dy*awakeMapW+dx div 32];
309 v^ := v^ or (LongWord(1) shl (dx mod 32));
310 end;
311 end;
312 end;
315 // ////////////////////////////////////////////////////////////////////////// //
316 function TParticle.alive (): Boolean; inline; begin result := (state <> TPartState.Free); end;
317 procedure TParticle.die (); inline; begin state := TPartState.Free; end;
319 // remove velocities and acceleration
320 procedure TParticle.freeze (); inline;
321 begin
322 // stop right there, you criminal scum!
323 velX := 0;
324 velY := 0;
325 accelX := 0;
326 accelY := 0;
327 end;
330 // `true`: affected by air stream
331 function TParticle.checkAirStreams (): Boolean;
332 var
333 pan: TPanel;
334 r: Integer;
335 begin
336 pan := g_Map_PanelAtPoint(x, y, GridTagLift);
337 result := (pan <> nil) and WordBool(pan.PanelType and (PANEL_LIFTUP or PANEL_LIFTDOWN or PANEL_LIFTLEFT or PANEL_LIFTRIGHT));
338 r := Random(3);
339 if result then
340 begin
341 case pan.LiftType of
342 LIFTTYPE_UP:
343 begin
344 if (velY > -1-r) then velY -= 0.8;
345 if (abs(velX) > 0.1) then velX -= velX/10.0;
346 velX += (Random-Random)*0.2;
347 accelY := 0.15;
348 end;
349 LIFTTYPE_DOWN:
350 begin
351 if (velY < 1+r) then velY += 0.8;
352 accelY := 0.15;
353 end;
354 LIFTTYPE_LEFT:
355 begin
356 if (velX > -8-r) then velX -= (8+r) div 2;
357 accelY := 0.15;
358 end;
359 LIFTTYPE_RIGHT:
360 begin
361 if (velX < 8+r) then velX += (8+r) div 2;
362 accelY := 0.15;
363 end;
364 else
365 result := false;
366 end;
367 // awake
368 if result and (state = TPartState.Sleeping) then state := TPartState.Normal;
369 end;
370 end;
373 // switch to sleep mode
374 procedure TParticle.sleep (); inline;
375 begin
376 if not checkAirStreams() then
377 begin
378 state := TPartState.Sleeping;
379 freeze();
380 end;
381 end;
384 procedure TParticle.findFloor (force: Boolean=false);
385 var
386 ex: Integer;
387 pan: TPanel;
388 begin
389 if (not force) and (floorY <> Unknown) then exit;
390 // stuck in the wall? rescan, 'cause it can be mplat
391 if (env = TEnvType.EWall) then
392 begin
393 pan := g_Map_PanelAtPoint(x, y, (GridTagObstacle or GridTagLiquid));
394 if (pan <> nil) then
395 begin
396 // either in a wall, or in a liquid
397 if ((pan.tag and GridTagObstacle) <> 0) then
398 begin
399 // we are in the wall, wtf?!
400 floorY := y;
401 env := TEnvType.EWall;
402 floorType := TFloorType.Wall;
403 state := TPartState.Sleeping; // anyway
404 exit;
405 end;
406 // we are in liquid, trace to liquid end
407 env := TEnvType.ELiquid;
408 end;
409 end;
410 // are we in a liquid?
411 if (env = TEnvType.ELiquid) then
412 begin
413 // trace out of the liquid
414 //env := TEnvType.ELiquid;
415 floorType := TFloorType.LiquidOut;
416 //e_LogWritefln('tracing out of a liquid; floorY=%s; y=%s', [floorY, y]);
417 mapGrid.traceOrthoRayWhileIn(ex, floorY, x, y, x, g_Map_MaxY, GridTagLiquid);
418 floorY += 1; // so `floorY` is just out of a liquid
419 //e_LogWritefln(' traced out of a liquid; floorY=%s; y=%s', [floorY, y]);
420 end
421 else
422 begin
423 // in the air
424 assert(env = TEnvType.EAir);
425 //env := TEnvType.EAir;
426 pan := g_Map_traceToNearest(x, y, x, g_Map_MaxY, (GridTagObstacle or GridTagLiquid), @ex, @floorY);
427 if (pan <> nil) then
428 begin
429 // wall or liquid
430 if ((pan.tag and GridTagObstacle) <> 0) then
431 begin
432 // wall
433 floorType := TFloorType.Wall;
434 end
435 else
436 begin
437 // liquid
438 floorType := TFloorType.LiquidIn; // entering liquid
439 floorY += 1; // so `floorY` is just in a liquid
440 end;
441 end
442 else
443 begin
444 // out of the level; assume wall, but it doesn't really matter
445 floorType := TFloorType.Wall;
446 floorY := g_Map_MaxY+2;
447 end;
448 end;
449 end;
452 procedure TParticle.findCeiling (force: Boolean=false);
453 var
454 ex: Integer;
455 begin
456 if (not force) and (ceilingY <> Unknown) then exit;
457 if (nil = g_Map_traceToNearest(x, y, x, g_Map_MinY, GridTagSolid, @ex, @ceilingY)) then
458 begin
459 ceilingY := g_Map_MinY-2;
460 end;
461 end;
464 procedure TParticle.think (); inline;
465 procedure awake ();
466 begin
467 if (state = TPartState.Stuck) then
468 begin
469 //writeln('awaking particle at (', x, ',', y, ')');
470 if (stickDX = 0) then
471 begin
472 state := TPartState.Normal; // stuck to a ceiling
473 end
474 else
475 begin
476 // stuck to a wall, check if wall is still there
477 if (wallEndY <> Unknown) then
478 begin
479 wallEndY := Unknown;
480 if (g_Map_PanelAtPoint(x+stickDX, y, GridTagObstacle) = nil) then
481 begin
482 // a wall was moved out, start falling
483 state := TPartState.Normal;
484 if (velY = 0) then velY := 0.1;
485 if (accelY = 0) then accelY := 0.5;
486 end;
487 end;
488 end;
489 end
490 else
491 begin
492 state := TPartState.Normal;
493 if (velY = 0) then velY := 0.1;
494 if (accelY = 0) then accelY := 0.5;
495 end;
496 floorY := Unknown;
497 ceilingY := Unknown;
498 end;
500 begin
501 oldx := x;
502 oldy := y;
503 // awake sleeping particle, if necessary
504 if awakeDirty then
505 begin
506 if awmIsSet(x, y) then awake();
508 case state of
509 TPartState.Sleeping, TPartState.Stuck:
510 if awmIsSet(x, y) then awake();
511 else
512 if (env = TEnvType.EWall) and awmIsSet(x, y) then awake();
513 end;
515 end;
516 case particleType of
517 TPartType.Blood, TPartType.Water: thinkerBloodAndWater();
518 TPartType.Spark: thinkerSpark();
519 TPartType.Bubbles: thinkerBubble();
520 end;
521 end;
524 // ////////////////////////////////////////////////////////////////////////// //
525 procedure TParticle.thinkerBloodAndWater ();
526 procedure stickToCeiling ();
527 begin
528 state := TPartState.Stuck;
529 stickDX := 0;
530 freeze();
531 ceilingY := y; // yep
532 end;
534 procedure stickToWall (dx: Integer);
535 var
536 ex: Integer;
537 begin
538 state := TPartState.Stuck;
539 if (dx > 0) then stickDX := 1 else stickDX := -1;
540 freeze();
541 // find next floor transition
542 findFloor();
543 // find `wallEndY`
544 mapGrid.traceOrthoRayWhileIn(ex, wallEndY, x+stickDX, y, x+stickDX, floorY+1, (GridTagWall or GridTagDoor or GridTagStep));
545 end;
547 procedure hitAFloor ();
548 begin
549 state := TPartState.Sleeping; // we aren't moving anymore
550 freeze();
551 floorY := y; // yep
552 floorType := TFloorType.Wall; // yep
553 end;
555 // `true`: didn't, get outa thinker
556 function drip (): Boolean;
557 begin
558 case particleType of
559 TPartType.Blood: result := (Random(200) = 100);
560 TPartType.Water: result := (Random(30) = 15);
561 else raise Exception.Create('internal error in particle engine: drip');
562 end;
563 if result then
564 begin
565 velY := 0.5;
566 accelY := 0.15;
567 // if we're falling from ceiling, switch to normal mode
568 if (state = TPartState.Stuck) and (stickDX = 0) then state := TPartState.Normal;
569 end;
570 end;
572 // switch to freefall mode
573 procedure freefall ();
574 begin
575 state := TPartState.Normal;
576 velY := 0.5;
577 accelY := 0.15;
578 end;
580 procedure applyGravity (inLiquid: Boolean);
581 begin
582 state := TPartState.Normal;
583 if inLiquid then
584 begin
585 velY := 0.5;
586 accelY := 0.15;
587 end
588 else
589 begin
590 velY := 0.8;
591 accelY := 0.5;
592 end;
593 end;
595 label
596 _done, _gravityagain, _stuckagain;
597 var
598 pan: TPanel;
599 dx, dy: SmallInt;
600 ex, ey: Integer;
601 checkEnv, inAir, inStep: Boolean;
602 floorJustTraced: Boolean;
603 {$IF DEFINED(D2F_DEBUG_FALL_MPLAT)}
604 oldFloorY: Integer;
605 {$ENDIF}
606 begin
607 if not gpart_dbg_phys_enabled then begin x += round(velX); y += round(velY); goto _done; end;
609 if gAdvBlood then
610 begin
611 // still check for air streams when sleeping (no)
612 if (state = TPartState.Sleeping) then begin {checkAirStreams();} goto _done; end; // so blood will dissolve
614 // process stuck particles
615 if (state = TPartState.Stuck) then
616 begin
617 // stuck to a ceiling?
618 if (stickDX = 0) then
619 begin
620 // yeah, stuck to a ceiling
621 if (ceilingY = Unknown) then findCeiling();
622 // dropped from a ceiling?
623 if (y > ceilingY) then
624 begin
625 // yep
626 velY := 0.5;
627 accelY := 0.15;
628 state := TPartState.Normal;
629 end
630 else
631 begin
632 // otherwise, try to drip
633 if drip() then goto _done;
634 end;
635 end
636 else
637 begin
638 // stuck to a wall
639 if (wallEndY = Unknown) then
640 begin
641 // this can happen if mplat was moved out; find new `wallEndY`
642 findFloor(true); // force trace, just in case
643 if (floorType = TFloorType.LiquidOut) then env := TEnvType.ELiquid else env := TEnvType.EAir;
644 mapGrid.traceOrthoRayWhileIn(ex, wallEndY, x+stickDX, y, x+stickDX, floorY+1, (GridTagWall or GridTagDoor or GridTagStep));
645 end;
646 _stuckagain:
647 // floor transition?
648 if (wallEndY <= floorY) and (y >= floorY) then
649 begin
650 y := floorY;
651 case floorType of
652 TFloorType.Wall: // hit the ground
653 begin
654 // check if our ground wasn't moved since the last scan
655 findFloor(true); // force trace
656 if (y = floorY) then
657 begin
658 sleep();
659 goto _done; // nothing to do anymore
660 end;
661 // otherwise, do it again
662 goto _stuckagain;
663 end;
664 TFloorType.LiquidIn: // entering the liquid
665 begin
666 // rescan, so we'll know when we'll exit the liquid
667 findFloor(true); // force rescan
668 end;
669 TFloorType.LiquidOut: // exiting the liquid
670 begin
671 // rescan, so we'll know when we'll enter something interesting
672 findFloor(true); // force rescan
673 if (floorType = TFloorType.Wall) and (floorY = y) then begin sleep(); goto _done; end;
674 end;
675 end;
676 end;
677 // wall transition?
678 if (floorY <= wallEndY) and (y >= wallEndY) then
679 begin
680 // just unstuck from the wall, switch to freefall mode
681 y := wallEndY;
682 freefall();
683 end
684 else
685 begin
686 // otherwise, try to drip
687 if drip() then goto _done;
688 end;
689 end;
690 // nope, process as usual
691 end;
693 // it is important to have it here
694 dx := round(velX);
695 dy := round(velY);
697 inAir := checkAirStreams();
699 // gravity, if not stuck
700 if (state <> TPartState.Stuck) and (abs(velX) < 0.1) and (abs(velY) < 0.1) then
701 begin
702 floorJustTraced := (floorY = Unknown);
703 if floorJustTraced then findFloor();
704 _gravityagain:
705 // floor transition?
706 if (y = floorY) then
707 begin
708 case floorType of
709 TFloorType.Wall: // hit the ground
710 begin
711 // check if our ground wasn't moved since the last scan
712 if not floorJustTraced then
713 begin
714 findFloor(true); // force trace
715 if (floorType = TFloorType.LiquidOut) then env := TEnvType.ELiquid else env := TEnvType.EAir;
716 if (y <> floorY) then goto _gravityagain;
717 end;
718 // otherwise, nothing to do
719 end;
720 TFloorType.LiquidIn: // entering the liquid
721 begin
722 // rescan, so we'll know when we'll exit the liquid
723 findFloor(true); // force rescan
724 applyGravity(true);
725 end;
726 TFloorType.LiquidOut: // exiting the liquid
727 begin
728 // rescan, so we'll know when we'll enter something interesting
729 findFloor(true); // force rescan
730 if (floorType <> TFloorType.Wall) or (floorY <> y) then applyGravity(floorType = TFloorType.LiquidIn);
731 end;
732 end;
733 end
734 else
735 begin
736 // looks like we're in the air
737 applyGravity(false);
738 end;
739 end;
741 // trace movement
742 if (dx <> 0) then
743 begin
744 // has some horizontal velocity
745 inStep := False;
746 pan := g_Map_traceToNearest(x, y, x+dx, y+dy, GridTagSolid, @ex, @ey);
747 if (pan = nil) and (dy >= 0) then
748 begin
749 // do not stuck inside step
750 if g_Map_traceToNearest(x, y, x, y, GridTagStep, nil, nil) = nil then
751 // check for step panel below
752 pan := g_Map_traceToNearest(x, y, x, y+dy, GridTagStep, nil, @ey);
753 inStep := pan <> nil;
754 if inStep then
755 begin
756 // stick to panel edges
757 if ex < pan.X then
758 ex := pan.X
759 else if ex > pan.X + pan.Width - 1 then
760 ex := pan.X + pan.Width - 1;
761 end;
762 end;
763 checkEnv := (x <> ex);
764 x := ex;
765 y := ey;
766 if checkEnv then
767 begin
768 // dunno yet
769 floorY := Unknown;
770 ceilingY := Unknown;
771 // check environment (air/liquid)
772 if (g_Map_PanelAtPoint(x, y, GridTagLiquid) <> nil) then env := TEnvType.ELiquid else env := TEnvType.EAir;
773 end;
774 if (pan <> nil) then
775 begin
776 if inStep then
777 stickToWall(dx)
778 else
779 begin
780 // we stuck
781 // the only case when we can have both ceiling and wall is corner; stick to wall in this case
782 // check if we stuck to a wall
783 if (dx < 0) then dx := -1 else dx := 1;
784 if (g_Map_PanelAtPoint(x+dx, y, GridTagSolid) <> nil) then
785 begin
786 // stuck to a wall
787 stickToWall(dx);
788 end
789 else
790 begin
791 // stuck to a ceiling
792 stickToCeiling();
793 end;
794 end;
795 end;
796 end
797 else if (dy <> 0) then
798 begin
799 // has only vertical velocity
800 if (dy < 0) then
801 begin
802 // flying up
803 if (ceilingY = Unknown) then findCeiling(); // need to do this anyway
804 y += dy;
805 if (y <= ceilingY) then begin y := ceilingY; stickToCeiling(); end; // oops, hit a ceiling
806 // environment didn't changed
807 end
808 else
809 begin
810 while (dy > 0) do
811 begin
812 // falling down
813 floorJustTraced := (floorY = Unknown);
814 if floorJustTraced then findFloor();
815 if (floorType = TFloorType.LiquidOut) then env := TEnvType.ELiquid else env := TEnvType.EAir;
816 y += dy;
817 //e_LogWritefln('floorY=%s; newy=%s; dY=%s; floorType=%s', [floorY, y, dY, floorType]);
818 if (y >= floorY) then
819 begin
820 // floor transition
821 dy := y-floorY;
822 y := floorY;
823 //e_LogWritefln(' HIT FLOORY: floorY=%s; newy=%s; dY=%s; floorType=%s', [floorY, y, dY, floorType]);
824 case floorType of
825 TFloorType.Wall: // hit the ground
826 begin
827 // check if our ground wasn't moved since the last scan
828 if not floorJustTraced then
829 begin
830 {$IF DEFINED(D2F_DEBUG_FALL_MPLAT)}
831 oldFloorY := floorY;
832 {$ENDIF}
833 findFloor(true); // force trace
834 {$IF DEFINED(D2F_DEBUG_FALL_MPLAT)}
835 if (floorY <> oldFloorY) then
836 begin
837 e_LogWritefln('force rescanning vpart at (%s,%s); oldFloorY=%s; floorY=%s', [x, y, oldFloorY, floorY]);
838 end;
839 {$ENDIF}
840 if (floorType = TFloorType.LiquidOut) then env := TEnvType.ELiquid else env := TEnvType.EAir;
841 if (y <> floorY) then continue;
842 end;
843 // environment didn't changed
844 if not inAir then hitAFloor();
845 break; // done with vertical movement
846 end;
847 TFloorType.LiquidIn: // entering the liquid
848 begin
849 // we're entered the liquid
850 env := TEnvType.ELiquid;
851 // rescan, so we'll know when we'll exit the liquid
852 findFloor(true); // force rescan
853 end;
854 TFloorType.LiquidOut: // exiting the liquid
855 begin
856 // we're exited the liquid
857 env := TEnvType.EAir;
858 // rescan, so we'll know when we'll enter something interesting
859 findFloor(true); // force rescan
860 if (floorType = TFloorType.Wall) and (floorY = y) then
861 begin
862 if not inAir then hitAFloor();
863 break; // done with vertical movement
864 end;
865 end;
866 end;
867 end
868 else
869 begin
870 break; // done with vertical movement
871 end;
872 end;
873 end;
874 end;
875 end // if gAdvBlood
876 else
877 begin
878 // simple blood
879 dx := round(velX);
880 dy := round(velY);
881 y += dy;
882 x += dx;
883 if (g_Map_PanelAtPoint(x, y, GridTagObstacle) <> nil) then begin die(); exit; end;
884 end;
886 _done:
887 if (x < g_Map_MinX) or (y < g_Map_MinY) or (x > g_Map_MaxX) or (y > g_Map_MaxY) then begin die(); end;
889 velX += accelX;
890 velY += accelY;
892 // blood will dissolve in other liquids
893 if (particleType = TPartType.Blood) then
894 begin
895 if (env = TEnvType.ELiquid) then
896 begin
897 if waitTime > 0 then
898 waitTime -= 1
899 else
900 time += 1;
901 if (liveTime <= 0) then begin die(); exit; end;
902 ex := 255-trunc(255.0*time/liveTime);
903 if (ex <= 10) then begin die(); exit; end;
904 if (ex > 250) then ex := 255;
905 alpha := Byte(ex);
906 end;
907 end
908 else
909 begin
910 // water will disappear in any liquid
911 if (env = TEnvType.ELiquid) then begin die(); exit; end;
912 if waitTime > 0 then
913 waitTime -= 1
914 else
915 time += 1;
916 // dry water
917 if (liveTime <= 0) then begin die(); exit; end;
918 ex := 255-trunc(255.0*time/liveTime);
919 if (ex <= 10) then begin die(); exit; end;
920 if (ex > 250) then ex := 255;
921 alpha := Byte(ex);
922 end;
923 end;
926 // ////////////////////////////////////////////////////////////////////////// //
927 procedure g_GFX_SparkVel (fX, fY: Integer; count: Word; vx, vy: Integer; devX, devY: Byte); forward;
929 procedure g_GFX_Blood (fX, fY: Integer; count: Word; vx, vy: Integer;
930 devX, devY: Word; cr, cg, cb: Byte; kind: Byte = BLOOD_NORMAL);
932 function genColor (cbase, crnd: Integer; def: Byte=0): Byte;
933 begin
934 if (cbase > 0) then
935 begin
936 cbase += crnd;
937 if (cbase < 0) then result := 0
938 else if (cbase > 255) then result := 255
939 else result := Byte(cbase);
940 end
941 else
942 begin
943 result := def;
944 end;
945 end;
947 var
948 a: Integer;
949 devX1, devX2, devY1, devY2: Integer;
950 l: Integer;
951 crnd: Integer;
952 pan: TPanel;
953 begin
954 if not gpart_dbg_enabled then exit;
956 if (kind = BLOOD_SPARKS) then
957 begin
958 g_GFX_SparkVel(fX, fY, 2+Random(2), -vx div 2, -vy div 2, devX, devY);
959 exit;
960 end
961 else if (kind = BLOOD_CSPARKS) OR (kind = BLOOD_COMBINE) then
962 begin
963 g_GFX_SparkVel(fX, fY, count, -vx div 2, -vy div 2, devX, devY);
964 if kind <> BLOOD_COMBINE then exit
965 end;
967 l := Length(Particles);
968 if (l = 0) then exit;
969 if (count > l) then count := l;
971 devX1 := devX div 2;
972 devX2 := devX+1;
973 devY1 := devY div 2;
974 devY2 := devY+1;
976 for a := 1 to count do
977 begin
978 with Particles[CurrentParticle] do
979 begin
980 x := fX-devX1+Random(devX2);
981 y := fY-devY1+Random(devY2);
982 oldx := x;
983 oldy := y;
985 // check for level bounds
986 if (x < g_Map_MinX) or (y < g_Map_MinY) or (x > g_Map_MaxX) or (y > g_Map_MaxY) then continue;
988 // in what environment we are starting in?
989 pan := g_Map_PanelAtPoint(x, y, (GridTagSolid or GridTagLiquid));
990 if (pan <> nil) then
991 begin
992 // either in a wall, or in a liquid
993 if ((pan.tag and GridTagSolid) <> 0) then continue; // don't spawn in walls
994 env := TEnvType.ELiquid;
995 end
996 else
997 begin
998 env := TEnvType.EAir;
999 end;
1001 velX := vx+(Random-Random)*3;
1002 velY := vy+(Random-Random)*3;
1004 if (velY > -4) then
1005 begin
1006 if (velY-4 < -4) then velY := -4 else velY := velY-4;
1007 end;
1009 accelX := -sign(velX)*Random/100;
1010 accelY := 0.8;
1012 crnd := 20*Random(6)-50;
1014 red := genColor(cr, CRnd, 0);
1015 green := genColor(cg, CRnd, 0);
1016 blue := genColor(cb, CRnd, 0);
1017 alpha := 255;
1019 particleType := TPartType.Blood;
1020 state := TPartState.Normal;
1021 time := 0;
1022 liveTime := 120+Random(40);
1023 waitTime := 20;
1024 floorY := Unknown;
1025 ceilingY := Unknown;
1026 end;
1028 if (CurrentParticle >= MaxParticles-1) then CurrentParticle := 0 else CurrentParticle += 1;
1029 end;
1030 end;
1033 procedure g_GFX_Water (fX, fY: Integer; count: Word; fVelX, fVelY: Single; devX, devY, color: Byte;
1034 simple: Boolean=false; cr: Byte=0; cg: Byte=0; cb: Byte=0);
1035 var
1036 a: Integer;
1037 devX1, devX2, devY1, devY2: Integer;
1038 l: Integer;
1039 pan: TPanel;
1040 begin
1041 if not gpart_dbg_enabled then exit;
1043 l := Length(Particles);
1044 if (l = 0) then exit;
1045 if (count > l) then count := l;
1047 if (abs(fVelX) < 3.0) then fVelX := 3.0-6.0*Random;
1049 devX1 := devX div 2;
1050 devX2 := devX+1;
1051 devY1 := devY div 2;
1052 devY2 := devY+1;
1054 if (not simple) and (color > 3) then color := 0;
1056 for a := 1 to count do
1057 begin
1058 with Particles[CurrentParticle] do
1059 begin
1060 if not simple then
1061 begin
1062 x := fX-devX1+Random(devX2);
1063 y := fY-devY1+Random(devY2);
1065 if (abs(fVelX) < 0.5) then velX := 1.0-2.0*Random else velX := fVelX*Random;
1066 if (Random(10) < 7) then velX := -velX;
1067 velY := fVelY*Random;
1068 accelX := 0.0;
1069 accelY := 0.8;
1070 end
1071 else
1072 begin
1073 x := fX;
1074 y := fY;
1076 velX := fVelX;
1077 velY := fVelY;
1078 accelX := 0.0;
1079 accelY := 0.8;
1080 end;
1082 oldx := x;
1083 oldy := y;
1085 // check for level bounds
1086 if (x < g_Map_MinX) or (y < g_Map_MinY) or (x > g_Map_MaxX) or (y > g_Map_MaxY) then continue;
1088 // this hack will allow water spawned in water to fly out
1089 // it can happen when player fell from a huge height (see "DOOM2D.WAD:\MAP03", for example)
1090 if (fVelY >= 0) then
1091 begin
1092 // in what environment we are starting in?
1093 pan := g_Map_PanelAtPoint(x, y, (GridTagObstacle or GridTagLiquid));
1094 end
1095 else
1096 begin
1097 pan := g_Map_PanelAtPoint(x, y, GridTagObstacle);
1098 end;
1099 if (pan <> nil) then continue;
1100 env := TEnvType.EAir;
1102 // color
1103 case color of
1104 1: // reddish
1105 begin
1106 red := 155+Random(9)*10;
1107 green := trunc(150*Random);
1108 blue := green;
1109 end;
1110 2: // greenish
1111 begin
1112 red := trunc(150*Random);
1113 green := 175+Random(9)*10;
1114 blue := red;
1115 end;
1116 3: // bluish
1117 begin
1118 red := trunc(200*Random);
1119 green := red;
1120 blue := 175+Random(9)*10;
1121 end;
1122 4: // Ñâîé öâåò, ñâåòëåå
1123 begin
1124 red := 20+Random(19)*10;
1125 green := red;
1126 blue := red;
1127 red := nmin(red+cr, 255);
1128 green := nmin(green+cg, 255);
1129 blue := nmin(blue+cb, 255);
1130 end;
1131 5: // Ñâîé öâåò, òåìíåå
1132 begin
1133 red := 20+Random(19)*10;
1134 green := red;
1135 blue := red;
1136 red := nmax(cr-red, 0);
1137 green := nmax(cg-green, 0);
1138 blue := nmax(cb-blue, 0);
1139 end;
1140 else // grayish
1141 begin
1142 red := 90+random(12)*10;
1143 green := red;
1144 blue := red;
1145 end;
1146 end;
1147 alpha := 255;
1149 particleType := TPartType.Water;
1150 state := TPartState.Normal;
1151 time := 0;
1152 liveTime := 60+Random(60);
1153 waitTime := 120;
1154 floorY := Unknown;
1155 ceilingY := Unknown;
1156 end;
1158 if (CurrentParticle >= MaxParticles-1) then CurrentParticle := 0 else CurrentParticle += 1;
1159 end;
1160 end;
1163 procedure g_GFX_SimpleWater (fX, fY: Integer; count: Word; fVelX, fVelY: Single; defColor, cr, cg, cb: Byte);
1164 begin
1165 g_GFX_Water(fX, fY, count, 0, 0, 0, 0, defColor, true, cr, cg, cb);
1166 end;
1169 // ////////////////////////////////////////////////////////////////////////// //
1170 procedure TParticle.thinkerBubble ();
1171 var
1172 dy: Integer;
1173 begin
1174 dy := round(velY);
1176 if (dy <> 0) then
1177 begin
1178 y += dy;
1179 if (dy < 0) then
1180 begin
1181 if (y <= ceilingY) then begin die(); exit; end;
1182 end
1183 else
1184 begin
1185 if (y >= floorY) then begin die(); exit; end;
1186 end;
1187 if (y < g_Map_MinY) or (y > g_Map_MaxY) then begin die(); exit; end;
1188 end;
1190 if (velY > -4) then velY += accelY;
1192 if waitTime > 0 then
1193 waitTime -= 1
1194 else
1195 time += 1;
1196 end;
1199 {.$DEFINE D2F_DEBUG_BUBBLES}
1200 procedure g_GFX_Bubbles (fX, fY: Integer; count: Word; devX, devY: Byte);
1201 var
1202 a, liquidx: Integer;
1203 devX1, devX2, devY1, devY2: Integer;
1204 l: Integer;
1205 {$IF DEFINED(D2F_DEBUG_BUBBLES)}
1206 stt: UInt64;
1207 nptr, ptr: Boolean;
1208 {$ENDIF}
1209 begin
1210 if not gpart_dbg_enabled then exit;
1212 l := Length(Particles);
1213 if (l = 0) then exit;
1214 if (count > l) then count := l;
1216 devX1 := devX div 2;
1217 devX2 := devX+1;
1218 devY1 := devY div 2;
1219 devY2 := devY+1;
1221 for a := 1 to count do
1222 begin
1223 with Particles[CurrentParticle] do
1224 begin
1225 x := fX-devX1+Random(devX2);
1226 y := fY-devY1+Random(devY2);
1227 oldx := x;
1228 oldy := y;
1230 // check for level bounds
1231 if (x < g_Map_MinX) or (y < g_Map_MinY) or (x > g_Map_MaxX) or (y > g_Map_MaxY) then continue;
1233 (*
1234 // don't spawn bubbles outside of the liquid
1235 if not isLiquidAt(X, Y) {ByteBool(gCollideMap[Y, X] and MARK_LIQUID)} then
1236 Continue;
1237 *)
1239 // trace liquid, so we'll know where it ends; do it in 8px steps for speed
1240 // tracer will return `false` if we started outside of the liquid
1242 {$IF DEFINED(D2F_DEBUG_BUBBLES)}
1243 stt := getTimeMicro();
1244 ptr := mapGrid.traceOrthoRayWhileIn(liquidx, liquidTopY, x, y, x, 0, GridTagWater or GridTagAcid1 or GridTagAcid2);
1245 stt := getTimeMicro()-stt;
1246 e_LogWritefln('traceOrthoRayWhileIn: time=%s (%s); liquidTopY=%s', [Integer(stt), ptr, liquidTopY]);
1247 //
1248 stt := getTimeMicro();
1249 nptr := g_Map_TraceLiquidNonPrecise(x, y, 0, -8, liquidx, liquidTopY);
1250 stt := getTimeMicro()-stt;
1251 e_LogWritefln('g_Map_TraceLiquidNonPrecise: time=%s (%s); liquidTopY=%s', [Integer(stt), nptr, liquidTopY]);
1252 if not nptr then continue;
1253 {$ELSE}
1254 if not g_Map_TraceLiquidNonPrecise(x, y, 0, -8, liquidx, ceilingY) then continue;
1255 if not g_Map_TraceLiquidNonPrecise(x, y, 0, +8, liquidx, floorY) then continue;
1256 {$ENDIF}
1258 velX := 0;
1259 velY := -1-Random;
1260 accelX := 0;
1261 accelY := velY/10;
1263 red := 255;
1264 green := 255;
1265 blue := 255;
1266 alpha := 255;
1268 state := TPartState.Normal;
1269 particleType := TPartType.Bubbles;
1270 time := 0;
1271 liveTime := 65535;
1272 waitTime := 0;
1273 end;
1275 if (CurrentParticle >= MaxParticles-1) then CurrentParticle := 0 else CurrentParticle += 1;
1276 end;
1277 end;
1280 // ////////////////////////////////////////////////////////////////////////// //
1281 procedure TParticle.thinkerSpark ();
1282 label
1283 _done;
1284 var
1285 dx, dy: SmallInt;
1286 pan: TPanel;
1287 ex, ey: Integer;
1288 begin
1289 if not gpart_dbg_phys_enabled then begin x += round(velX); y += round(velY); goto _done; end;
1291 dx := round(velX);
1292 dy := round(velY);
1294 //writeln('spark0: pos=(', x, ',', y, '); delta=(', dx, ',', dy, '); state=', state, '; ceilingY=', ceilingY, '; floorY=', floorY);
1296 // apply gravity
1297 if (abs(velX) < 0.1) and (abs(velY) < 0.1) then
1298 begin
1299 velY := 0.8;
1300 accelY := 0.5;
1301 end;
1303 // flying
1304 if (dx <> 0) then
1305 begin
1306 // has some horizontal velocity
1307 pan := g_Map_traceToNearest(x, y, x+dx, y+dy, (GridTagSolid or GridTagLiquid), @ex, @ey);
1308 if (x <> ex) then begin floorY := Unknown; ceilingY := Unknown; end; // dunno yet
1309 x := ex;
1310 y := ey;
1311 if (pan <> nil) then
1312 begin
1313 if ((pan.tag and GridTagLiquid) <> 0) then begin die(); exit; end; // die in liquid
1314 // hit the wall; falling down vertically
1315 velX := 0;
1316 accelX := 0;
1317 end;
1318 end
1319 else if (dy <> 0) then
1320 begin
1321 // has some vertical velocity
1322 if (dy < 0) then
1323 begin
1324 // flying up
1325 if (ceilingY = Unknown) then findCeiling(); // need to do this anyway
1326 y += dy;
1327 if (y <= ceilingY) then
1328 begin
1329 // oops, hit a ceiling
1330 y := ceilingY;
1331 velY := -velY;
1332 accelY := abs(accelY);
1333 end;
1334 // environment didn't changed
1335 end
1336 else
1337 begin
1338 // falling down
1339 if (floorY = Unknown) then findFloor(); // need to do this anyway
1340 y += dy;
1341 if (y >= floorY) then
1342 begin
1343 // hit something except a floor?
1344 if (floorType <> TFloorType.Wall) then begin die(); exit; end; // yep: just die
1345 // otherwise, go to sleep
1346 y := floorY;
1347 sleep();
1348 // environment didn't changed
1349 end;
1350 end;
1351 end;
1353 _done:
1354 if (x < g_Map_MinX) or (y < g_Map_MinY) or (x > g_Map_MaxX) or (y > g_Map_MaxY) then begin die(); end;
1356 if (velX <> 0.0) then velX += accelX;
1358 if (velY <> 0.0) then
1359 begin
1360 if (accelY < 10) then accelY += 0.08;
1361 velY += accelY;
1362 end;
1364 //writeln('spark1: pos=(', x, ',', y, '); delta=(', velX:6:3, ',', velY:6:3, '); state=', state, '; ceilingY=', ceilingY, '; floorY=', floorY);
1366 if waitTime > 0 then
1367 waitTime -= 1
1368 else
1369 time += 1;
1370 end;
1373 // ////////////////////////////////////////////////////////////////////////// //
1374 procedure g_GFX_SparkVel (fX, fY: Integer; count: Word; vx, vy: Integer; devX, devY: Byte);
1375 var
1376 a: Integer;
1377 devX1, devX2, devY1, devY2: Integer;
1378 l: Integer;
1379 pan: TPanel;
1380 begin
1381 if not gpart_dbg_enabled then exit;
1383 l := Length(Particles);
1384 if (l = 0) then exit;
1385 if (count > l) then count := l;
1387 devX1 := devX div 2;
1388 devX2 := devX+1;
1389 devY1 := devY div 2;
1390 devY2 := devY+1;
1392 for a := 1 to count do
1393 begin
1394 with Particles[CurrentParticle] do
1395 begin
1396 x := fX-devX1+Random(devX2);
1397 y := fY-devY1+Random(devY2);
1398 oldx := x;
1399 oldy := y;
1401 // check for level bounds
1402 if (x < g_Map_MinX) or (y < g_Map_MinY) or (x > g_Map_MaxX) or (y > g_Map_MaxY) then continue;
1404 // in what environment we are starting in?
1405 pan := g_Map_PanelAtPoint(x, y, (GridTagSolid or GridTagLiquid));
1406 if (pan <> nil) then
1407 begin
1408 // either in a wall, or in a liquid
1409 //if ((pan.tag and GridTagSolid) <> 0) then continue; // don't spawn in walls
1410 //env := TEnvType.ELiquid;
1411 continue;
1412 end
1413 else
1414 begin
1415 env := TEnvType.EAir;
1416 end;
1418 velX := vx+(Random-Random)*3;
1419 velY := vy+(Random-Random)*3;
1421 if (velY > -4) then
1422 begin
1423 if (velY-4 < -4) then velY := -4 else velY := velY-4;
1424 end;
1426 accelX := -sign(velX)*Random/100;
1427 accelY := 0.8;
1429 red := 255;
1430 green := 100+Random(155);
1431 blue := 64;
1432 alpha := 255;
1434 particleType := TPartType.Spark;
1435 state := TPartState.Normal;
1436 time := 0;
1437 liveTime := 30+Random(60);
1438 waitTime := 0;
1439 floorY := Unknown;
1440 ceilingY := Unknown;
1441 end;
1443 if (CurrentParticle >= MaxParticles-1) then CurrentParticle := 0 else CurrentParticle += 1;
1444 end;
1445 end;
1448 procedure g_GFX_Spark (fX, fY: Integer; count: Word; angle: SmallInt; devX, devY: Byte);
1449 var
1450 a: Integer;
1451 b: Single;
1452 devX1, devX2, devY1, devY2: Integer;
1453 baseVelX, baseVelY: Single;
1454 l: Integer;
1455 pan: TPanel;
1456 begin
1457 if not gpart_dbg_enabled then exit;
1459 l := Length(Particles);
1460 if (l = 0) then exit;
1461 if (count > l) then count := l;
1463 angle := 360-angle;
1465 devX1 := devX div 2;
1466 devX2 := devX+1;
1467 devY1 := devY div 2;
1468 devY2 := devY+1;
1470 b := DegToRad(angle);
1471 baseVelX := cos(b);
1472 baseVelY := 1.6*sin(b);
1473 if (abs(baseVelX) < 0.01) then baseVelX := 0.0;
1474 if (abs(baseVelY) < 0.01) then baseVelY := 0.0;
1476 for a := 1 to count do
1477 begin
1478 with Particles[CurrentParticle] do
1479 begin
1480 x := fX-devX1+Random(devX2);
1481 y := fY-devY1+Random(devY2);
1482 oldx := x;
1483 oldy := y;
1485 // check for level bounds
1486 if (x < g_Map_MinX) or (y < g_Map_MinY) or (x > g_Map_MaxX) or (y > g_Map_MaxY) then continue;
1488 // in what environment we are starting in?
1489 pan := g_Map_PanelAtPoint(x, y, (GridTagSolid or GridTagLiquid));
1490 if (pan <> nil) then
1491 begin
1492 // either in a wall, or in a liquid
1493 //if ((pan.tag and GridTagSolid) <> 0) then continue; // don't spawn in walls
1494 //env := TEnvType.ELiquid;
1495 continue;
1496 end
1497 else
1498 begin
1499 env := TEnvType.EAir;
1500 end;
1502 velX := baseVelX*Random;
1503 velY := baseVelY-Random;
1504 accelX := velX/3.0;
1505 accelY := velY/5.0;
1507 red := 255;
1508 green := 100+Random(155);
1509 blue := 64;
1510 alpha := 255;
1512 particleType := TPartType.Spark;
1513 state := TPartState.Normal;
1514 time := 0;
1515 liveTime := 30+Random(60);
1516 waitTime := 0;
1517 floorY := Unknown;
1518 ceilingY := Unknown;
1519 end;
1521 if (CurrentParticle >= MaxParticles-1) then CurrentParticle := 0 else CurrentParticle += 1;
1522 end;
1523 end;
1526 // ////////////////////////////////////////////////////////////////////////// //
1527 procedure g_GFX_SetMax (count: Integer);
1528 var
1529 a: Integer;
1530 begin
1531 if count > 50000 then count := 50000;
1532 if (count < 1) then count := 1;
1533 SetLength(Particles, count);
1534 for a := 0 to High(Particles) do Particles[a].die();
1535 MaxParticles := count;
1536 CurrentParticle := 0;
1537 end;
1540 function g_GFX_GetMax (): Integer;
1541 begin
1542 result := MaxParticles;
1543 end;
1546 function FindOnceAnim (): DWORD;
1547 var
1548 i: Integer;
1549 begin
1550 if OnceAnims <> nil then
1551 for i := 0 to High(OnceAnims) do
1552 if OnceAnims[i].Animation = nil then
1553 begin
1554 Result := i;
1555 Exit;
1556 end;
1558 if OnceAnims = nil then
1559 begin
1560 SetLength(OnceAnims, 16);
1561 Result := 0;
1562 end
1563 else
1564 begin
1565 Result := High(OnceAnims) + 1;
1566 SetLength(OnceAnims, Length(OnceAnims) + 16);
1567 end;
1568 end;
1571 procedure g_GFX_OnceAnim (x, y: Integer; Anim: TAnimation; AnimType: Byte = 0);
1572 var
1573 find_id: DWORD;
1574 begin
1575 if not gpart_dbg_enabled then exit;
1577 if (Anim = nil) then exit;
1579 find_id := FindOnceAnim();
1581 OnceAnims[find_id].AnimType := AnimType;
1582 OnceAnims[find_id].Animation := TAnimation.Create(Anim.FramesID, Anim.Loop, Anim.Speed);
1583 OnceAnims[find_id].Animation.Blending := Anim.Blending;
1584 OnceAnims[find_id].Animation.alpha := Anim.alpha;
1585 OnceAnims[find_id].x := x;
1586 OnceAnims[find_id].y := y;
1587 end;
1590 // ////////////////////////////////////////////////////////////////////////// //
1591 procedure g_GFX_Init ();
1592 begin
1593 //g_Game_SetLoadingText(_lc[I_LOAD_COLLIDE_MAP]+' 1/6', 0, False);
1594 //SetLength(gCollideMap, gMapInfo.Height+1);
1595 //for a := 0 to High(gCollideMap) do SetLength(gCollideMap[a], gMapInfo.Width+1);
1596 awmSetup();
1597 {$IFDEF HEADLESS}
1598 gpart_dbg_enabled := false;
1599 {$ENDIF}
1600 end;
1603 procedure g_GFX_Free ();
1604 var
1605 a: Integer;
1606 begin
1607 Particles := nil;
1608 SetLength(Particles, MaxParticles);
1609 for a := 0 to High(Particles) do Particles[a].die();
1610 CurrentParticle := 0;
1612 if (OnceAnims <> nil) then
1613 begin
1614 for a := 0 to High(OnceAnims) do OnceAnims[a].Animation.Free();
1615 OnceAnims := nil;
1616 end;
1618 awakeMap := nil;
1619 // why not?
1620 awakeMapH := -1;
1621 awakeMapW := -1;
1622 end;
1625 // ////////////////////////////////////////////////////////////////////////// //
1626 procedure g_GFX_Update ();
1627 var
1628 a: Integer;
1629 w, h: Integer;
1630 len: Integer;
1631 begin
1632 if not gpart_dbg_enabled then exit;
1634 if (Particles <> nil) then
1635 begin
1636 w := gMapInfo.Width;
1637 h := gMapInfo.Height;
1639 len := High(Particles);
1641 for a := 0 to len do
1642 begin
1643 if Particles[a].alive then
1644 begin
1645 with Particles[a] do
1646 begin
1647 if (time = liveTime) then begin die(); continue; end;
1648 if (x+1 >= w) or (y+1 >= h) or (x <= 0) or (y <= 0) then begin die(); end;
1649 think();
1650 end; // with
1651 end; // if
1652 end; // for
1653 end; // Particles <> nil
1655 // clear awake map
1656 awmClear();
1658 if OnceAnims <> nil then
1659 begin
1660 for a := 0 to High(OnceAnims) do
1661 if OnceAnims[a].Animation <> nil then
1662 begin
1663 OnceAnims[a].oldx := OnceAnims[a].x;
1664 OnceAnims[a].oldy := OnceAnims[a].y;
1666 case OnceAnims[a].AnimType of
1667 ONCEANIM_SMOKE:
1668 begin
1669 if Random(3) = 0 then
1670 OnceAnims[a].x := OnceAnims[a].x-1+Random(3);
1671 if Random(2) = 0 then
1672 OnceAnims[a].y := OnceAnims[a].y-Random(2);
1673 end;
1674 end;
1676 if OnceAnims[a].Animation.Played then
1677 begin
1678 OnceAnims[a].Animation.Free();
1679 OnceAnims[a].Animation := nil;
1680 end
1681 else
1682 OnceAnims[a].Animation.Update();
1683 end;
1684 end;
1685 end;
1687 end.