DEADSOFTWARE

particles: still emit water if player fell into water from a very big height; air...
[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, either version 3 of the License, or
6 * (at your option) any later version.
7 *
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.
12 *
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/>.
15 *)
16 {$INCLUDE ../shared/a_modes.inc}
17 unit g_gfx;
19 interface
21 uses
22 e_log, g_textures;
24 const
25 BLOOD_NORMAL = 0;
26 BLOOD_SPARKS = 1;
28 ONCEANIM_NONE = 0;
29 ONCEANIM_SMOKE = 1;
31 MARK_FREE = 0;
32 MARK_WALL = 1;
33 MARK_WATER = 2;
34 MARK_ACID = 4;
35 MARK_LIFTDOWN = 8;
36 MARK_LIFTUP = 16;
37 MARK_DOOR = 32;
38 MARK_LIFTLEFT = 64;
39 MARK_LIFTRIGHT = 128;
40 MARK_BLOCKED = MARK_WALL or MARK_DOOR;
41 MARK_LIQUID = MARK_WATER or MARK_ACID;
42 MARK_LIFT = MARK_LIFTDOWN or MARK_LIFTUP or MARK_LIFTLEFT or MARK_LIFTRIGHT;
45 procedure g_GFX_Init ();
46 procedure g_GFX_Free ();
48 procedure g_GFX_Blood (fX, fY: Integer; count: Word; vx, vy: Integer;
49 devX, devY: Word; cr, cg, cb: Byte; kind: Byte=BLOOD_NORMAL);
50 procedure g_GFX_Spark (fX, fY: Integer; count: Word; angle: SmallInt; devX, devY: Byte);
51 procedure g_GFX_Water (fX, fY: Integer; count: Word; fVelX, fVelY: Single; devX, devY, color: Byte;
52 simple: Boolean=false; cr: Byte=0; cg: Byte=0; cb: Byte=0);
53 procedure g_GFX_SimpleWater (fX, fY: Integer; count: Word; fVelX, fVelY: Single; defColor, cr, cg, cb: Byte);
54 procedure g_GFX_Bubbles (fX, fY: Integer; count: Word; devX, devY: Byte);
56 procedure g_GFX_SetMax (count: Integer);
57 function g_GFX_GetMax (): Integer;
59 procedure g_GFX_OnceAnim (X, Y: Integer; Anim: TAnimation; AnimType: Byte = 0);
61 procedure g_Mark (x, y, Width, Height: Integer; t: Byte; st: Boolean=true);
63 procedure g_GFX_Update ();
64 procedure g_GFX_Draw ();
67 var
68 gpart_dbg_enabled: Boolean = true;
69 gpart_dbg_phys_enabled: Boolean = true;
72 implementation
74 uses
75 g_map, g_panel, g_basic, Math, e_graphics, GL, GLExt,
76 g_options, g_console, SysUtils, g_triggers, MAPDEF,
77 g_game, g_language, g_net, utils, xprofiler;
80 const
81 Unknown = Integer($7fffffff);
84 type
85 TPartType = (Blood, Spark, Bubbles, Water);
86 TPartState = (Free, Normal, Stuck, Sleeping);
87 TFloorType = (Wall, LiquidIn, LiquidOut);
88 // Wall: floorY is just before floor
89 // LiquidIn: floorY is liquid *start* (i.e. just in a liquid)
90 // LiquidOut: floorY is liquid *end* (i.e. just out of a liquid)
91 TEnvType = (EAir, ELiquid, EWall); // where particle is now
93 // note: this MUST be record, so we can keep it in
94 // dynamic array and has sequential memory access pattern
95 PParticle = ^TParticle;
96 TParticle = record
97 x, y: Integer;
98 velX, velY: Single;
99 accelX, accelY: Single;
100 state: TPartState;
101 particleType: TPartType;
102 red, green, blue: Byte;
103 alpha: Byte;
104 time, liveTime: Word;
105 stickDX: Integer; // STATE_STICK: -1,1: stuck to a wall; 0: stuck to ceiling
106 justSticked: Boolean; // not used
107 floorY: Integer; // actually, floor-1; `Unknown`: unknown
108 floorType: TFloorType;
109 env: TEnvType; // where particle is now
110 ceilingY: Integer; // actually, ceiling+1; `Unknown`: unknown
111 wallEndY: Integer; // if we stuck to a wall, this is where wall ends
113 //k8: sorry, i have to emulate virtual methods this way, 'cause i haet `Object`
114 procedure thinkerBloodAndWater ();
115 procedure thinkerSpark ();
116 procedure thinkerBubble ();
118 procedure findFloor (force: Boolean=false); // this updates `floorY` if forced or Unknown
119 procedure findCeiling (force: Boolean=false); // this updates `ceilingY` if forced or Unknown
121 procedure freeze (); inline; // remove velocities and acceleration
122 procedure sleep (); inline; // switch to sleep mode
124 function checkAirStreams (): Boolean; // `true`: affected by air stream
126 function alive (): Boolean; inline;
127 procedure die (); inline;
128 procedure think (); inline;
129 end;
131 TOnceAnim = record
132 AnimType: Byte;
133 x, y: Integer;
134 Animation: TAnimation;
135 end;
138 var
139 Particles: array of TParticle = nil;
140 OnceAnims: array of TOnceAnim = nil;
141 MaxParticles: Integer = 0;
142 CurrentParticle: Integer = 0;
143 // awakeMap has one bit for each map grid cell; on g_Mark,
144 // corresponding bits will be set, and in `think()` all particles
145 // in marked cells will be awaken
146 awakeMap: packed array of LongWord = nil;
147 awakeMapH: Integer = -1;
148 awakeMapW: Integer = -1;
149 awakeMinX, awakeMinY: Integer;
150 awakeDirty: Boolean = false;
153 // ////////////////////////////////////////////////////////////////////////// //
154 // HACK! using mapgrid
155 procedure awmClear (); inline;
156 begin
157 if awakeDirty and (awakeMapW > 0) then
158 begin
159 FillDWord(awakeMap[0], Length(awakeMap), 0);
160 awakeDirty := false;
161 end;
162 end;
165 procedure awmSetup ();
166 begin
167 assert(mapGrid <> nil);
168 awakeMapW := (mapGrid.gridWidth+mapGrid.tileSize-1) div mapGrid.tileSize;
169 awakeMapW := (awakeMapW+31) div 32; // LongWord has 32 bits ;-)
170 awakeMapH := (mapGrid.gridHeight+mapGrid.tileSize-1) div mapGrid.tileSize;
171 awakeMinX := mapGrid.gridX0;
172 awakeMinY := mapGrid.gridY0;
173 SetLength(awakeMap, awakeMapW*awakeMapH);
174 {$IF DEFINED(D2F_DEBUG)}
175 e_LogWritefln('particle awake map: %sx%s (for grid of size %sx%s)', [awakeMapW, awakeMapH, mapGrid.gridWidth, mapGrid.gridHeight]);
176 {$ENDIF}
177 awakeDirty := true;
178 awmClear();
179 end;
182 function awmIsSet (x, y: Integer): Boolean; inline;
183 begin
184 x := (x-awakeMinX) div mapGrid.tileSize;
185 y := (y-awakeMinY) div mapGrid.tileSize;
186 if (x >= 0) and (y >= 0) and (x div 32 < awakeMapW) and (y < awakeMapH) then
187 begin
188 {$IF DEFINED(D2F_DEBUG)}
189 assert(y*awakeMapW+x div 32 < Length(awakeMap));
190 {$ENDIF}
191 result := ((awakeMap[y*awakeMapW+x div 32] and (LongWord(1) shl (x mod 32))) <> 0);
192 end
193 else
194 begin
195 result := false;
196 end;
197 end;
200 procedure awmSet (x, y: Integer); inline;
201 var
202 v: PLongWord;
203 begin
204 x := (x-awakeMinX) div mapGrid.tileSize;
205 y := (y-awakeMinY) div mapGrid.tileSize;
206 if (x >= 0) and (y >= 0) and (x div 32 < awakeMapW) and (y < awakeMapH) then
207 begin
208 {$IF DEFINED(D2F_DEBUG)}
209 assert(y*awakeMapW+x div 32 < Length(awakeMap));
210 {$ENDIF}
211 v := @awakeMap[y*awakeMapW+x div 32];
212 v^ := v^ or (LongWord(1) shl (x mod 32));
213 awakeDirty := true;
214 end;
215 end;
218 // ////////////////////////////////////////////////////////////////////////// //
219 function TParticle.alive (): Boolean; inline; begin result := (state <> TPartState.Free); end;
220 procedure TParticle.die (); inline; begin state := TPartState.Free; end;
222 // remove velocities and acceleration
223 procedure TParticle.freeze (); inline;
224 begin
225 // stop right there, you criminal scum!
226 velX := 0;
227 velY := 0;
228 accelX := 0;
229 accelY := 0;
230 end;
233 // `true`: affected by air stream
234 function TParticle.checkAirStreams (): Boolean;
235 var
236 pan: TPanel;
237 begin
238 pan := g_Map_PanelAtPoint(x, y, GridTagLift);
239 result := (pan <> nil);
240 if result then
241 begin
242 if ((pan.PanelType and PANEL_LIFTUP) <> 0) then
243 begin
244 if (velY > -4-Random(3)) then velY -= 0.8;
245 if (abs(velX) > 0.1) then velX -= velX/10.0;
246 velX += (Random-Random)*0.2;
247 accelY := 0.15;
248 end
249 else if ((pan.PanelType and PANEL_LIFTLEFT) <> 0) then
250 begin
251 if (velX > -8-Random(3)) then velX -= 0.8;
252 accelY := 0.15;
253 end
254 else if ((pan.PanelType and PANEL_LIFTRIGHT) <> 0) then
255 begin
256 if (velX < 8+Random(3)) then velX += 0.8;
257 accelY := 0.15;
258 end
259 else
260 begin
261 result := false;
262 end;
263 // awake
264 if result and (state = TPartState.Sleeping) then state := TPartState.Normal;
265 end;
266 end;
269 // switch to sleep mode
270 procedure TParticle.sleep (); inline;
271 begin
272 if not checkAirStreams() then
273 begin
274 state := TPartState.Sleeping;
275 freeze();
276 end;
277 end;
280 procedure TParticle.findFloor (force: Boolean=false);
281 var
282 ex: Integer;
283 pan: TPanel;
284 begin
285 if (not force) and (floorY <> Unknown) then exit;
286 // stuck in the wall? rescan, 'cause it can be mplat
287 if (env = TEnvType.EWall) then
288 begin
289 pan := g_Map_PanelAtPoint(x, y, (GridTagObstacle or GridTagLiquid));
290 if (pan <> nil) then
291 begin
292 // either in a wall, or in a liquid
293 if ((pan.tag and GridTagObstacle) <> 0) then
294 begin
295 // we are in the wall, wtf?!
296 floorY := y;
297 env := TEnvType.EWall;
298 floorType := TFloorType.Wall;
299 state := TPartState.Sleeping; // anyway
300 exit;
301 end;
302 // we are in liquid, trace to liquid end
303 env := TEnvType.ELiquid;
304 end;
305 end;
306 // are we in a liquid?
307 if (env = TEnvType.ELiquid) then
308 begin
309 // trace out of the liquid
310 //env := TEnvType.ELiquid;
311 floorType := TFloorType.LiquidOut;
312 //e_LogWritefln('tracing out of a liquid; floorY=%s; y=%s', [floorY, y]);
313 mapGrid.traceOrthoRayWhileIn(ex, floorY, x, y, x, g_Map_MaxY, GridTagLiquid);
314 floorY += 1; // so `floorY` is just out of a liquid
315 //e_LogWritefln(' traced out of a liquid; floorY=%s; y=%s', [floorY, y]);
316 end
317 else
318 begin
319 // in the air
320 assert(env = TEnvType.EAir);
321 //env := TEnvType.EAir;
322 pan := g_Map_traceToNearest(x, y, x, g_Map_MaxY, (GridTagObstacle or GridTagLiquid), @ex, @floorY);
323 if (pan <> nil) then
324 begin
325 // wall or liquid
326 if ((pan.tag and GridTagObstacle) <> 0) then
327 begin
328 // wall
329 floorType := TFloorType.Wall;
330 end
331 else
332 begin
333 // liquid
334 floorType := TFloorType.LiquidIn; // entering liquid
335 floorY += 1; // so `floorY` is just in a liquid
336 end;
337 end
338 else
339 begin
340 // out of the level; assume wall, but it doesn't really matter
341 floorType := TFloorType.Wall;
342 floorY := g_Map_MaxY+2;
343 end;
344 end;
345 end;
348 procedure TParticle.findCeiling (force: Boolean=false);
349 var
350 ex: Integer;
351 begin
352 if (not force) and (ceilingY <> Unknown) then exit;
353 if (nil = g_Map_traceToNearest(x, y, x, g_Map_MinY, GridTagObstacle, @ex, @ceilingY)) then
354 begin
355 ceilingY := g_Map_MinY-2;
356 end;
357 end;
360 procedure TParticle.think (); inline;
361 begin
362 // awake sleeping particle, if necessary
363 if awakeDirty then
364 begin
365 case state of
366 TPartState.Sleeping, TPartState.Stuck:
367 if awmIsSet(x, y) then
368 begin
369 state := TPartState.Normal;
370 floorY := Unknown;
371 ceilingY := Unknown;
372 if (velY = 0) then velY := 0.1;
373 if (accelY = 0) then accelY := 0.5;
374 end;
375 end;
376 end;
377 case particleType of
378 TPartType.Blood, TPartType.Water: thinkerBloodAndWater();
379 TPartType.Spark: thinkerSpark();
380 TPartType.Bubbles: thinkerBubble();
381 end;
382 end;
385 // ////////////////////////////////////////////////////////////////////////// //
386 procedure TParticle.thinkerBloodAndWater ();
387 procedure stickToCeiling ();
388 begin
389 state := TPartState.Stuck;
390 stickDX := 0;
391 freeze();
392 ceilingY := y; // yep
393 end;
395 procedure stickToWall (dx: Integer);
396 var
397 ex: Integer;
398 begin
399 state := TPartState.Stuck;
400 if (dX > 0) then stickDX := 1 else stickDX := -1;
401 freeze();
402 // find next floor transition
403 findFloor();
404 // find `wallEndY`
405 mapGrid.traceOrthoRayWhileIn(ex, wallEndY, x+stickDX, y, x+stickDX, floorY+1, (GridTagWall or GridTagDoor or GridTagStep));
406 end;
408 procedure hitAFloor ();
409 begin
410 state := TPartState.Sleeping; // we aren't moving anymore
411 freeze();
412 floorY := y; // yep
413 floorType := TFloorType.Wall; // yep
414 end;
416 // `true`: didn't, get outa thinker
417 function drip (): Boolean;
418 begin
419 case particleType of
420 TPartType.Blood: result := (Random(200) = 100);
421 TPartType.Water: result := (Random(30) = 15);
422 else raise Exception.Create('internal error in particle engine: drip');
423 end;
424 if result then begin velY := 0.5; accelY := 0.15; end;
425 end;
427 // switch to freefall mode
428 procedure freefall ();
429 begin
430 state := TPartState.Normal;
431 velY := 0.5;
432 accelY := 0.15;
433 end;
435 procedure applyGravity (inLiquid: Boolean);
436 begin
437 state := TPartState.Normal;
438 if (inLiquid) then
439 begin
440 velY := 0.5;
441 accelY := 0.15;
442 end
443 else
444 begin
445 velY := 0.8;
446 accelY := 0.5;
447 end;
448 end;
450 label
451 _done;
452 var
453 pan: TPanel;
454 dX, dY: SmallInt;
455 ex, ey: Integer;
456 checkEnv: Boolean;
457 begin
458 if not gpart_dbg_phys_enabled then goto _done;
460 if gAdvBlood then
461 begin
462 // still check for air streams when sleeping (no)
463 if (state = TPartState.Sleeping) then begin {checkAirStreams();} goto _done; end; // so blood will dissolve
465 // process stuck particles
466 if (state = TPartState.Stuck) then
467 begin
468 // stuck to a ceiling?
469 if (stickDX = 0) then
470 begin
471 // yeah, stuck to a ceiling
472 assert(ceilingY <> Unknown);
473 // dropped from a ceiling?
474 if (y > ceilingY) then
475 begin
476 // yep
477 velY := 0.5;
478 accelY := 0.15;
479 state := TPartState.Normal;
480 end
481 else
482 begin
483 // otherwise, try to drip
484 if drip() then goto _done;
485 end;
486 end
487 else
488 begin
489 // stuck to a wall
490 assert(wallEndY <> Unknown);
491 // floor transition?
492 if (wallEndY <= floorY) and (y >= floorY) then
493 begin
494 y := floorY;
495 case floorType of
496 TFloorType.Wall: // hit the ground
497 begin
498 sleep();
499 goto _done; // nothing to do anymore
500 end;
501 TFloorType.LiquidIn: // entering the liquid
502 begin
503 // rescan, so we'll know when we'll exit the liquid
504 findFloor(true); // force rescan
505 end;
506 TFloorType.LiquidOut: // exiting the liquid
507 begin
508 // rescan, so we'll know when we'll enter something interesting
509 findFloor(true); // force rescan
510 if (floorType = TFloorType.Wall) and (floorY = y) then begin sleep(); goto _done; end;
511 end;
512 end;
513 end;
514 // wall transition?
515 if (floorY <= wallEndY) and (y >= wallEndY) then
516 begin
517 // just unstuck from the wall, switch to freefall mode
518 y := wallEndY;
519 freefall();
520 end
521 else
522 begin
523 // otherwise, try to drip
524 if drip() then goto _done;
525 end;
526 end;
527 // nope, process as usual
528 end;
530 // it is important to have it here
531 dX := round(velX);
532 dY := round(velY);
534 if (state = TPartState.Normal) then checkAirStreams();
536 // gravity, if not stuck
537 if (state <> TPartState.Stuck) and (abs(velX) < 0.1) and (abs(velY) < 0.1) then
538 begin
539 if (floorY = Unknown) then findFloor();
540 // floor transition?
541 if (y = floorY) then
542 begin
543 case floorType of
544 TFloorType.Wall: // hit the ground
545 begin
546 // nothing to do
547 end;
548 TFloorType.LiquidIn: // entering the liquid
549 begin
550 // rescan, so we'll know when we'll exit the liquid
551 findFloor(true); // force rescan
552 applyGravity(true);
553 end;
554 TFloorType.LiquidOut: // exiting the liquid
555 begin
556 // rescan, so we'll know when we'll enter something interesting
557 findFloor(true); // force rescan
558 if (floorType <> TFloorType.Wall) or (floorY <> y) then applyGravity(floorType = TFloorType.LiquidIn);
559 end;
560 end;
561 end
562 else
563 begin
564 // looks like we're in the air
565 applyGravity(false);
566 end;
567 end;
569 // trace movement
570 if (dX <> 0) then
571 begin
572 // has some horizontal velocity
573 pan := g_Map_traceToNearest(x, y, x+dX, y+dY, GridTagObstacle, @ex, @ey);
574 checkEnv := (x <> ex);
575 x := ex;
576 y := ey;
577 if checkEnv then
578 begin
579 // dunno yet
580 floorY := Unknown;
581 ceilingY := Unknown;
582 // check environment (air/liquid)
583 if (g_Map_PanelAtPoint(x, y, GridTagLiquid) <> nil) then env := TEnvType.ELiquid else env := TEnvType.EAir;
584 end;
585 if (pan <> nil) then
586 begin
587 // we stuck
588 // the only case when we can have both ceiling and wall is corner; stick to wall in this case
589 // check if we stuck to a wall
590 if (dX < 0) then dX := -1 else dX := 1;
591 if (g_Map_PanelAtPoint(x+dX, y, GridTagObstacle) <> nil) then
592 begin
593 // stuck to a wall
594 stickToWall(dX);
595 end
596 else
597 begin
598 // stuck to a ceiling
599 stickToCeiling();
600 end;
601 end;
602 end
603 else if (dY <> 0) then
604 begin
605 // has only vertical velocity
606 if (dY < 0) then
607 begin
608 // flying up
609 if (ceilingY = Unknown) then findCeiling(); // need to do this anyway
610 y += dY;
611 if (y <= ceilingY) then begin y := ceilingY; stickToCeiling(); end; // oops, hit a ceiling
612 // environment didn't changed
613 end
614 else
615 begin
616 while (dY > 0) do
617 begin
618 // falling down
619 if (floorY = Unknown) then findFloor(); // need to do this anyway
620 if (floorType = TFloorType.LiquidOut) then env := TEnvType.ELiquid else env := TEnvType.EAir;
621 y += dY;
622 //e_LogWritefln('floorY=%s; newy=%s; dY=%s; floorType=%s', [floorY, y, dY, floorType]);
623 if (y >= floorY) then
624 begin
625 // floor transition
626 dY := y-floorY;
627 y := floorY;
628 //e_LogWritefln(' HIT FLOORY: floorY=%s; newy=%s; dY=%s; floorType=%s', [floorY, y, dY, floorType]);
629 case floorType of
630 TFloorType.Wall: // hit the ground
631 begin
632 // environment didn't changed
633 hitAFloor();
634 break; // done with vertical movement
635 end;
636 TFloorType.LiquidIn: // entering the liquid
637 begin
638 // we're entered the liquid
639 env := TEnvType.ELiquid;
640 // rescan, so we'll know when we'll exit the liquid
641 findFloor(true); // force rescan
642 end;
643 TFloorType.LiquidOut: // exiting the liquid
644 begin
645 // we're exited the liquid
646 env := TEnvType.EAir;
647 // rescan, so we'll know when we'll enter something interesting
648 findFloor(true); // force rescan
649 if (floorType = TFloorType.Wall) and (floorY = y) then
650 begin
651 hitAFloor();
652 break; // done with vertical movement
653 end;
654 end;
655 end;
656 end
657 else
658 begin
659 break; // done with vertical movement
660 end;
661 end;
662 end;
663 end;
664 end // if gAdvBlood
665 else
666 begin
667 // simple blood
668 dX := round(velX);
669 dY := round(velY);
670 y += dY;
671 x += dX;
672 if (g_Map_PanelAtPoint(x, y, GridTagObstacle) <> nil) then begin die(); exit; end;
673 end;
675 _done:
676 if (x < g_Map_MinX) or (y < g_Map_MinY) or (x > g_Map_MaxX) or (y > g_Map_MaxY) then begin die(); end;
678 velX += accelX;
679 velY += accelY;
681 // blood will dissolve in other liquids
682 if (particleType = TPartType.Blood) then
683 begin
684 if (env = TEnvType.ELiquid) then
685 begin
686 time += 1;
687 if (liveTime <= 0) then begin die(); exit; end;
688 ex := 255-trunc(255.0*time/liveTime);
689 if (ex <= 10) then begin die(); exit; end;
690 if (ex > 250) then ex := 255;
691 alpha := Byte(ex);
692 end;
693 end
694 else
695 begin
696 // water will disappear in any liquid
697 if (env = TEnvType.ELiquid) then begin die(); exit; end;
698 time += 1;
699 // dry water
700 if (liveTime <= 0) then begin die(); exit; end;
701 ex := 255-trunc(255.0*time/liveTime);
702 if (ex <= 10) then begin die(); exit; end;
703 if (ex > 250) then ex := 255;
704 alpha := Byte(ex);
705 end;
706 end;
709 // ////////////////////////////////////////////////////////////////////////// //
710 procedure g_GFX_SparkVel (fX, fY: Integer; count: Word; vx, vy: Integer; devX, devY: Byte); forward;
712 procedure g_GFX_Blood (fX, fY: Integer; count: Word; vx, vy: Integer;
713 devX, devY: Word; cr, cg, cb: Byte; kind: Byte = BLOOD_NORMAL);
715 function genColor (cbase, crnd: Integer; def: Byte=0): Byte;
716 begin
717 if (cbase > 0) then
718 begin
719 cbase += crnd;
720 if (cbase < 0) then result := 0
721 else if (cbase > 255) then result := 255
722 else result := Byte(cbase);
723 end
724 else
725 begin
726 result := def;
727 end;
728 end;
730 var
731 a: Integer;
732 devX1, devX2, devY1, devY2: Integer;
733 l: Integer;
734 crnd: Integer;
735 pan: TPanel;
736 begin
737 if not gpart_dbg_enabled then exit;
739 if (kind = BLOOD_SPARKS) then
740 begin
741 g_GFX_SparkVel(fX, fY, 2+Random(2), -vx div 2, -vy div 2, devX, devY);
742 exit;
743 end;
745 l := Length(Particles);
746 if (l = 0) then exit;
747 if (count > l) then count := l;
749 devX1 := devX div 2;
750 devX2 := devX+1;
751 devY1 := devY div 2;
752 devY2 := devY+1;
754 for a := 1 to count do
755 begin
756 with Particles[CurrentParticle] do
757 begin
758 x := fX-devX1+Random(devX2);
759 y := fY-devY1+Random(devY2);
761 // check for level bounds
762 if (x < g_Map_MinX) or (y < g_Map_MinY) or (x > g_Map_MaxX) or (y > g_Map_MaxY) then continue;
764 // in what environment we are starting in?
765 pan := g_Map_PanelAtPoint(x, y, (GridTagObstacle or GridTagLiquid));
766 if (pan <> nil) then
767 begin
768 // either in a wall, or in a liquid
769 if ((pan.tag and GridTagObstacle) <> 0) then continue; // don't spawn in walls
770 env := TEnvType.ELiquid;
771 end
772 else
773 begin
774 env := TEnvType.EAir;
775 end;
777 velX := vx+(Random-Random)*3;
778 velY := vy+(Random-Random)*3;
780 if (velY > -4) then
781 begin
782 if (velY-4 < -4) then velY := -4 else velY := velY-4;
783 end;
785 accelX := -sign(velX)*Random/100;
786 accelY := 0.8;
788 crnd := 20*Random(6)-50;
790 red := genColor(cr, CRnd, 0);
791 green := genColor(cg, CRnd, 0);
792 blue := genColor(cb, CRnd, 0);
793 alpha := 255;
795 particleType := TPartType.Blood;
796 state := TPartState.Normal;
797 time := 0;
798 liveTime := 120+Random(40);
799 floorY := Unknown;
800 ceilingY := Unknown;
801 end;
803 if (CurrentParticle >= MaxParticles-1) then CurrentParticle := 0 else CurrentParticle += 1;
804 end;
805 end;
808 procedure g_GFX_Water (fX, fY: Integer; count: Word; fVelX, fVelY: Single; devX, devY, color: Byte;
809 simple: Boolean=false; cr: Byte=0; cg: Byte=0; cb: Byte=0);
810 var
811 a: Integer;
812 devX1, devX2, devY1, devY2: Integer;
813 l: Integer;
814 pan: TPanel;
815 begin
816 if not gpart_dbg_enabled then exit;
818 l := Length(Particles);
819 if (l = 0) then exit;
820 if (count > l) then count := l;
822 if (abs(fVelX) < 3.0) then fVelX := 3.0-6.0*Random;
824 devX1 := devX div 2;
825 devX2 := devX+1;
826 devY1 := devY div 2;
827 devY2 := devY+1;
829 if (not simple) and (color > 3) then color := 0;
831 for a := 1 to count do
832 begin
833 with Particles[CurrentParticle] do
834 begin
835 if not simple then
836 begin
837 x := fX-devX1+Random(devX2);
838 y := fY-devY1+Random(devY2);
840 if (abs(fVelX) < 0.5) then velX := 1.0-2.0*Random else velX := fVelX*Random;
841 if (Random(10) < 7) then velX := -velX;
842 velY := fVelY*Random;
843 accelX := 0.0;
844 accelY := 0.8;
845 end
846 else
847 begin
848 x := fX;
849 y := fY;
851 velX := fVelX;
852 velY := fVelY;
853 accelX := 0.0;
854 accelY := 0.8;
855 end;
857 // check for level bounds
858 if (x < g_Map_MinX) or (y < g_Map_MinY) or (x > g_Map_MaxX) or (y > g_Map_MaxY) then continue;
860 // in what environment we are starting in?
861 pan := g_Map_PanelAtPoint(x, y, (GridTagObstacle or GridTagLiquid));
862 if (pan <> nil) then
863 begin
864 // either in a wall, or in a liquid
865 //if ((pan.tag and GridTagObstacle) <> 0) then continue; // don't spawn in walls
866 //env := TEnvType.ELiquid;
867 //continue;
868 env := TEnvType.EAir;
869 end
870 else
871 begin
872 env := TEnvType.EAir;
873 end;
875 // color
876 case color of
877 1: // reddish
878 begin
879 red := 155+Random(9)*10;
880 green := trunc(150*Random);
881 blue := green;
882 end;
883 2: // greenish
884 begin
885 red := trunc(150*Random);
886 green := 175+Random(9)*10;
887 blue := red;
888 end;
889 3: // bluish
890 begin
891 red := trunc(200*Random);
892 green := red;
893 blue := 175+Random(9)*10;
894 end;
895 4: // Ñâîé öâåò, ñâåòëåå
896 begin
897 red := 20+Random(19)*10;
898 green := red;
899 blue := red;
900 red := nmin(red+cr, 255);
901 green := nmin(green+cg, 255);
902 blue := nmin(blue+cb, 255);
903 end;
904 5: // Ñâîé öâåò, òåìíåå
905 begin
906 red := 20+Random(19)*10;
907 green := red;
908 blue := red;
909 red := nmax(cr-red, 0);
910 green := nmax(cg-green, 0);
911 blue := nmax(cb-blue, 0);
912 end;
913 else // grayish
914 begin
915 red := 90+random(12)*10;
916 green := red;
917 blue := red;
918 end;
919 end;
920 alpha := 255;
922 particleType := TPartType.Water;
923 state := TPartState.Normal;
924 time := 0;
925 liveTime := 60+Random(60);
926 floorY := Unknown;
927 ceilingY := Unknown;
928 end;
930 if (CurrentParticle >= MaxParticles-1) then CurrentParticle := 0 else CurrentParticle += 1;
931 end;
932 end;
935 procedure g_GFX_SimpleWater (fX, fY: Integer; count: Word; fVelX, fVelY: Single; defColor, cr, cg, cb: Byte);
936 begin
937 g_GFX_Water(fX, fY, count, 0, 0, 0, 0, defColor, true, cr, cg, cb);
938 end;
941 // ////////////////////////////////////////////////////////////////////////// //
942 procedure TParticle.thinkerBubble ();
943 var
944 dY: Integer;
945 begin
946 dY := round(velY);
948 if (dY <> 0) then
949 begin
950 y += dY;
951 if (dY < 0) then
952 begin
953 if (y <= ceilingY) then begin die(); exit; end;
954 end
955 else
956 begin
957 if (y >= floorY) then begin die(); exit; end;
958 end;
959 if (y < g_Map_MinY) or (y > g_Map_MaxY) then begin die(); exit; end;
960 end;
962 if (velY > -4) then velY += accelY;
964 time += 1;
965 end;
968 {.$DEFINE D2F_DEBUG_BUBBLES}
969 procedure g_GFX_Bubbles (fX, fY: Integer; count: Word; devX, devY: Byte);
970 var
971 a, liquidx: Integer;
972 devX1, devX2, devY1, devY2: Integer;
973 l: Integer;
974 {$IF DEFINED(D2F_DEBUG_BUBBLES)}
975 stt: UInt64;
976 nptr, ptr: Boolean;
977 {$ENDIF}
978 begin
979 if not gpart_dbg_enabled then exit;
981 l := Length(Particles);
982 if (l = 0) then exit;
983 if (count > l) then count := l;
985 devX1 := devX div 2;
986 devX2 := devX+1;
987 devY1 := devY div 2;
988 devY2 := devY+1;
990 for a := 1 to count do
991 begin
992 with Particles[CurrentParticle] do
993 begin
994 x := fX-devX1+Random(devX2);
995 y := fY-devY1+Random(devY2);
997 // check for level bounds
998 if (x < g_Map_MinX) or (y < g_Map_MinY) or (x > g_Map_MaxX) or (y > g_Map_MaxY) then continue;
1000 (*
1001 // don't spawn bubbles outside of the liquid
1002 if not isLiquidAt(X, Y) {ByteBool(gCollideMap[Y, X] and MARK_LIQUID)} then
1003 Continue;
1004 *)
1006 // trace liquid, so we'll know where it ends; do it in 8px steps for speed
1007 // tracer will return `false` if we started outside of the liquid
1009 {$IF DEFINED(D2F_DEBUG_BUBBLES)}
1010 stt := curTimeMicro();
1011 ptr := mapGrid.traceOrthoRayWhileIn(liquidx, liquidTopY, x, y, x, 0, GridTagWater or GridTagAcid1 or GridTagAcid2);
1012 stt := curTimeMicro()-stt;
1013 e_LogWritefln('traceOrthoRayWhileIn: time=%s (%s); liquidTopY=%s', [Integer(stt), ptr, liquidTopY]);
1014 //
1015 stt := curTimeMicro();
1016 nptr := g_Map_TraceLiquidNonPrecise(x, y, 0, -8, liquidx, liquidTopY);
1017 stt := curTimeMicro()-stt;
1018 e_LogWritefln('g_Map_TraceLiquidNonPrecise: time=%s (%s); liquidTopY=%s', [Integer(stt), nptr, liquidTopY]);
1019 if not nptr then continue;
1020 {$ELSE}
1021 if not g_Map_TraceLiquidNonPrecise(x, y, 0, -8, liquidx, ceilingY) then continue;
1022 if not g_Map_TraceLiquidNonPrecise(x, y, 0, +8, liquidx, floorY) then continue;
1023 {$ENDIF}
1025 velX := 0;
1026 velY := -1-Random;
1027 accelX := 0;
1028 accelY := velY/10;
1030 red := 255;
1031 green := 255;
1032 blue := 255;
1033 alpha := 255;
1035 state := TPartState.Normal;
1036 particleType := TPartType.Bubbles;
1037 time := 0;
1038 liveTime := 65535;
1039 end;
1041 if (CurrentParticle >= MaxParticles-1) then CurrentParticle := 0 else CurrentParticle += 1;
1042 end;
1043 end;
1046 // ////////////////////////////////////////////////////////////////////////// //
1047 procedure TParticle.thinkerSpark ();
1048 label
1049 _done;
1050 var
1051 dX, dY: SmallInt;
1052 pan: TPanel;
1053 ex, ey: Integer;
1054 begin
1055 if not gpart_dbg_phys_enabled then goto _done;
1057 dX := round(velX);
1058 dY := round(velY);
1060 // apply gravity
1061 if (abs(velX) < 0.1) and (abs(velY) < 0.1) then
1062 begin
1063 velY := 0.8;
1064 accelY := 0.5;
1065 end;
1067 // flying
1068 if (dX <> 0) then
1069 begin
1070 // has some horizontal velocity
1071 pan := g_Map_traceToNearest(x, y, x+dX, y+dY, (GridTagObstacle or GridTagLiquid), @ex, @ey);
1072 if (x <> ex) then begin floorY := Unknown; ceilingY := Unknown; end; // dunno yet
1073 x := ex;
1074 y := ey;
1075 if (pan <> nil) then
1076 begin
1077 if ((pan.tag and GridTagLiquid) <> 0) then begin die(); exit; end; // die in liquid
1078 // hit the wall; falling down vertically
1079 velX := 0;
1080 accelX := 0;
1081 end;
1082 end
1083 else if (dY <> 0) then
1084 begin
1085 // has some vertical velocity
1086 if (dY < 0) then
1087 begin
1088 // flying up
1089 if (ceilingY = Unknown) then findCeiling(); // need to do this anyway
1090 y += dY;
1091 if (y <= ceilingY) then
1092 begin
1093 // oops, hit a ceiling
1094 y := ceilingY;
1095 velY := -velY;
1096 accelY := abs(accelY);
1097 end;
1098 // environment didn't changed
1099 end
1100 else
1101 begin
1102 // falling down
1103 if (floorY = Unknown) then findFloor(); // need to do this anyway
1104 y += dY;
1105 if (y >= floorY) then
1106 begin
1107 // hit something except a floor?
1108 if (floorType <> TFloorType.Wall) then begin die(); exit; end; // yep: just die
1109 // otherwise, go to sleep
1110 y := floorY;
1111 sleep();
1112 // environment didn't changed
1113 end;
1114 end;
1115 end;
1117 _done:
1118 if (x < g_Map_MinX) or (y < g_Map_MinY) or (x > g_Map_MaxX) or (y > g_Map_MaxY) then begin die(); end;
1120 if (velX <> 0.0) then velX += accelX;
1122 if (velY <> 0.0) then
1123 begin
1124 if (accelY < 10) then accelY += 0.08;
1125 velY += accelY;
1126 end;
1128 time += 1;
1129 end;
1132 // ////////////////////////////////////////////////////////////////////////// //
1133 procedure g_GFX_SparkVel (fX, fY: Integer; count: Word; vx, vy: Integer; devX, devY: Byte);
1134 var
1135 a: Integer;
1136 devX1, devX2, devY1, devY2: Integer;
1137 l: Integer;
1138 pan: TPanel;
1139 begin
1140 if not gpart_dbg_enabled then exit;
1142 l := Length(Particles);
1143 if (l = 0) then exit;
1144 if (count > l) then count := l;
1146 devX1 := devX div 2;
1147 devX2 := devX+1;
1148 devY1 := devY div 2;
1149 devY2 := devY+1;
1151 for a := 1 to count do
1152 begin
1153 with Particles[CurrentParticle] do
1154 begin
1155 x := fX-devX1+Random(devX2);
1156 y := fY-devY1+Random(devY2);
1158 // check for level bounds
1159 if (x < g_Map_MinX) or (y < g_Map_MinY) or (x > g_Map_MaxX) or (y > g_Map_MaxY) then continue;
1161 // in what environment we are starting in?
1162 pan := g_Map_PanelAtPoint(x, y, (GridTagObstacle or GridTagLiquid));
1163 if (pan <> nil) then
1164 begin
1165 // either in a wall, or in a liquid
1166 //if ((pan.tag and GridTagObstacle) <> 0) then continue; // don't spawn in walls
1167 //env := TEnvType.ELiquid;
1168 continue;
1169 end
1170 else
1171 begin
1172 env := TEnvType.EAir;
1173 end;
1175 velX := vx+(Random-Random)*3;
1176 velY := vy+(Random-Random)*3;
1178 if (velY > -4) then
1179 begin
1180 if (velY-4 < -4) then velY := -4 else velY := velY-4;
1181 end;
1183 accelX := -sign(velX)*Random/100;
1184 accelY := 0.8;
1186 red := 255;
1187 green := 100+Random(155);
1188 blue := 64;
1189 alpha := 255;
1191 particleType := TPartType.Spark;
1192 state := TPartState.Normal;
1193 time := 0;
1194 liveTime := 30+Random(60);
1195 floorY := Unknown;
1196 ceilingY := Unknown;
1197 end;
1199 if (CurrentParticle >= MaxParticles-1) then CurrentParticle := 0 else CurrentParticle += 1;
1200 end;
1201 end;
1204 procedure g_GFX_Spark (fX, fY: Integer; count: Word; angle: SmallInt; devX, devY: Byte);
1205 var
1206 a: Integer;
1207 b: Single;
1208 devX1, devX2, devY1, devY2: Integer;
1209 baseVelX, baseVelY: Single;
1210 l: Integer;
1211 pan: TPanel;
1212 begin
1213 if not gpart_dbg_enabled then exit;
1215 l := Length(Particles);
1216 if (l = 0) then exit;
1217 if (count > l) then count := l;
1219 angle := 360-angle;
1221 devX1 := devX div 2;
1222 devX2 := devX+1;
1223 devY1 := devY div 2;
1224 devY2 := devY+1;
1226 b := DegToRad(angle);
1227 baseVelX := cos(b);
1228 baseVelY := 1.6*sin(b);
1229 if (abs(baseVelX) < 0.01) then baseVelX := 0.0;
1230 if (abs(baseVelY) < 0.01) then baseVelY := 0.0;
1232 for a := 1 to count do
1233 begin
1234 with Particles[CurrentParticle] do
1235 begin
1236 x := fX-devX1+Random(devX2);
1237 y := fY-devY1+Random(devY2);
1239 // check for level bounds
1240 if (x < g_Map_MinX) or (y < g_Map_MinY) or (x > g_Map_MaxX) or (y > g_Map_MaxY) then continue;
1242 // in what environment we are starting in?
1243 pan := g_Map_PanelAtPoint(x, y, (GridTagObstacle or GridTagLiquid));
1244 if (pan <> nil) then
1245 begin
1246 // either in a wall, or in a liquid
1247 //if ((pan.tag and GridTagObstacle) <> 0) then continue; // don't spawn in walls
1248 //env := TEnvType.ELiquid;
1249 continue;
1250 end
1251 else
1252 begin
1253 env := TEnvType.EAir;
1254 end;
1256 velX := baseVelX*Random;
1257 velY := baseVelY-Random;
1258 accelX := velX/3.0;
1259 accelY := velY/5.0;
1261 red := 255;
1262 green := 100+Random(155);
1263 blue := 64;
1264 alpha := 255;
1266 particleType := TPartType.Spark;
1267 state := TPartState.Normal;
1268 time := 0;
1269 liveTime := 30+Random(60);
1270 floorY := Unknown;
1271 ceilingY := Unknown;
1272 end;
1274 if (CurrentParticle >= MaxParticles-1) then CurrentParticle := 0 else CurrentParticle += 1;
1275 end;
1276 end;
1279 // ////////////////////////////////////////////////////////////////////////// //
1280 procedure g_GFX_SetMax (count: Integer);
1281 var
1282 a: Integer;
1283 begin
1284 if count > 50000 then count := 50000;
1285 if (count < 1) then count := 1;
1286 SetLength(Particles, count);
1287 for a := 0 to High(Particles) do Particles[a].die();
1288 MaxParticles := count;
1289 CurrentParticle := 0;
1290 end;
1293 function g_GFX_GetMax (): Integer;
1294 begin
1295 result := MaxParticles;
1296 end;
1299 function FindOnceAnim (): DWORD;
1300 var
1301 i: Integer;
1302 begin
1303 if OnceAnims <> nil then
1304 for i := 0 to High(OnceAnims) do
1305 if OnceAnims[i].Animation = nil then
1306 begin
1307 Result := i;
1308 Exit;
1309 end;
1311 if OnceAnims = nil then
1312 begin
1313 SetLength(OnceAnims, 16);
1314 Result := 0;
1315 end
1316 else
1317 begin
1318 Result := High(OnceAnims) + 1;
1319 SetLength(OnceAnims, Length(OnceAnims) + 16);
1320 end;
1321 end;
1324 procedure g_GFX_OnceAnim (x, y: Integer; Anim: TAnimation; AnimType: Byte = 0);
1325 var
1326 find_id: DWORD;
1327 begin
1328 if not gpart_dbg_enabled then exit;
1330 if (Anim = nil) then exit;
1332 find_id := FindOnceAnim();
1334 OnceAnims[find_id].AnimType := AnimType;
1335 OnceAnims[find_id].Animation := TAnimation.Create(Anim.FramesID, Anim.Loop, Anim.Speed);
1336 OnceAnims[find_id].Animation.Blending := Anim.Blending;
1337 OnceAnims[find_id].Animation.alpha := Anim.alpha;
1338 OnceAnims[find_id].x := x;
1339 OnceAnims[find_id].y := y;
1340 end;
1343 // ////////////////////////////////////////////////////////////////////////// //
1344 // st: set mark
1345 // t: mark type
1346 // currently unused
1347 procedure g_Mark (x, y, Width, Height: Integer; t: Byte; st: Boolean=true);
1348 var
1349 cx, ex, ey: Integer;
1350 ts: Integer;
1351 begin
1352 if not gpart_dbg_enabled then exit;
1354 if (Width < 1) or (Height < 1) then exit;
1355 // make some border, so we'll hit particles lying around the panel
1356 x -= 1; Width += 2;
1357 y -= 1; Height += 2;
1358 ex := x+Width;
1359 ey := y+Height;
1360 ts := mapGrid.tileSize;
1361 while (y < ey) do
1362 begin
1363 cx := x;
1364 while (cx < ex) do
1365 begin
1366 awmSet(cx, y);
1367 Inc(cx, ts);
1368 end;
1369 Inc(y, ts);
1370 end;
1371 end;
1374 // ////////////////////////////////////////////////////////////////////////// //
1375 procedure g_GFX_Init ();
1376 begin
1377 //g_Game_SetLoadingText(_lc[I_LOAD_COLLIDE_MAP]+' 1/6', 0, False);
1378 //SetLength(gCollideMap, gMapInfo.Height+1);
1379 //for a := 0 to High(gCollideMap) do SetLength(gCollideMap[a], gMapInfo.Width+1);
1380 awmSetup();
1381 {$IFDEF HEADLESS}
1382 gpart_dbg_enabled := false;
1383 {$ENDIF}
1384 end;
1387 procedure g_GFX_Free ();
1388 var
1389 a: Integer;
1390 begin
1391 Particles := nil;
1392 SetLength(Particles, MaxParticles);
1393 for a := 0 to High(Particles) do Particles[a].die();
1394 CurrentParticle := 0;
1396 if (OnceAnims <> nil) then
1397 begin
1398 for a := 0 to High(OnceAnims) do OnceAnims[a].Animation.Free();
1399 OnceAnims := nil;
1400 end;
1402 awakeMap := nil;
1403 // why not?
1404 awakeMapH := -1;
1405 awakeMapW := -1;
1406 end;
1409 // ////////////////////////////////////////////////////////////////////////// //
1410 procedure g_GFX_Update ();
1411 var
1412 a: Integer;
1413 w, h: Integer;
1414 len: Integer;
1415 begin
1416 if not gpart_dbg_enabled then exit;
1418 if (Particles <> nil) then
1419 begin
1420 w := gMapInfo.Width;
1421 h := gMapInfo.Height;
1423 len := High(Particles);
1425 for a := 0 to len do
1426 begin
1427 if Particles[a].alive then
1428 begin
1429 with Particles[a] do
1430 begin
1431 if (time = liveTime) then begin die(); continue; end;
1432 if (x+1 >= w) or (y+1 >= h) or (x <= 0) or (y <= 0) then begin die(); end;
1433 think();
1434 end; // with
1435 end; // if
1436 end; // for
1437 end; // Particles <> nil
1439 // clear awake map
1440 awmClear();
1442 if OnceAnims <> nil then
1443 begin
1444 for a := 0 to High(OnceAnims) do
1445 if OnceAnims[a].Animation <> nil then
1446 begin
1447 case OnceAnims[a].AnimType of
1448 ONCEANIM_SMOKE:
1449 begin
1450 if Random(3) = 0 then
1451 OnceAnims[a].x := OnceAnims[a].x-1+Random(3);
1452 if Random(2) = 0 then
1453 OnceAnims[a].y := OnceAnims[a].y-Random(2);
1454 end;
1455 end;
1457 if OnceAnims[a].Animation.Played then
1458 begin
1459 OnceAnims[a].Animation.Free();
1460 OnceAnims[a].Animation := nil;
1461 end
1462 else
1463 OnceAnims[a].Animation.Update();
1464 end;
1465 end;
1466 end;
1469 procedure g_GFX_Draw ();
1470 var
1471 a, len: Integer;
1472 begin
1473 if not gpart_dbg_enabled then exit;
1475 if (Particles <> nil) then
1476 begin
1477 glDisable(GL_TEXTURE_2D);
1478 glPointSize(2);
1480 glEnable(GL_BLEND);
1481 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1483 glBegin(GL_POINTS);
1485 len := High(Particles);
1486 for a := 0 to len do
1487 begin
1488 with Particles[a] do
1489 begin
1490 if alive and (x >= sX) and (y >= sY) and (x <= sX+sWidth) and (sY <= sY+sHeight) then
1491 begin
1492 glColor4ub(red, green, blue, alpha);
1493 glVertex2f(x+0.37, y+0.37);
1494 end;
1495 end;
1496 end;
1498 glEnd();
1500 glDisable(GL_BLEND);
1501 end;
1503 if (OnceAnims <> nil) then
1504 begin
1505 len := High(OnceAnims);
1506 for a := 0 to len do
1507 begin
1508 if (OnceAnims[a].Animation <> nil) then
1509 begin
1510 with OnceAnims[a] do Animation.Draw(x, y, M_NONE);
1511 end;
1512 end;
1513 end;
1514 end;
1517 end.