DEADSOFTWARE

particles: water cosmetix
[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 // this hack will allow water spawned in water to fly out
861 // it can happen when player fell from a huge height (see "DOOM2D.WAD:\MAP03", for example)
862 if (fVelY >= 0) then
863 begin
864 // in what environment we are starting in?
865 pan := g_Map_PanelAtPoint(x, y, (GridTagObstacle or GridTagLiquid));
866 end
867 else
868 begin
869 pan := g_Map_PanelAtPoint(x, y, GridTagObstacle);
870 end;
871 env := TEnvType.EAir;
873 // color
874 case color of
875 1: // reddish
876 begin
877 red := 155+Random(9)*10;
878 green := trunc(150*Random);
879 blue := green;
880 end;
881 2: // greenish
882 begin
883 red := trunc(150*Random);
884 green := 175+Random(9)*10;
885 blue := red;
886 end;
887 3: // bluish
888 begin
889 red := trunc(200*Random);
890 green := red;
891 blue := 175+Random(9)*10;
892 end;
893 4: // Ñâîé öâåò, ñâåòëåå
894 begin
895 red := 20+Random(19)*10;
896 green := red;
897 blue := red;
898 red := nmin(red+cr, 255);
899 green := nmin(green+cg, 255);
900 blue := nmin(blue+cb, 255);
901 end;
902 5: // Ñâîé öâåò, òåìíåå
903 begin
904 red := 20+Random(19)*10;
905 green := red;
906 blue := red;
907 red := nmax(cr-red, 0);
908 green := nmax(cg-green, 0);
909 blue := nmax(cb-blue, 0);
910 end;
911 else // grayish
912 begin
913 red := 90+random(12)*10;
914 green := red;
915 blue := red;
916 end;
917 end;
918 alpha := 255;
920 particleType := TPartType.Water;
921 state := TPartState.Normal;
922 time := 0;
923 liveTime := 60+Random(60);
924 floorY := Unknown;
925 ceilingY := Unknown;
926 end;
928 if (CurrentParticle >= MaxParticles-1) then CurrentParticle := 0 else CurrentParticle += 1;
929 end;
930 end;
933 procedure g_GFX_SimpleWater (fX, fY: Integer; count: Word; fVelX, fVelY: Single; defColor, cr, cg, cb: Byte);
934 begin
935 g_GFX_Water(fX, fY, count, 0, 0, 0, 0, defColor, true, cr, cg, cb);
936 end;
939 // ////////////////////////////////////////////////////////////////////////// //
940 procedure TParticle.thinkerBubble ();
941 var
942 dY: Integer;
943 begin
944 dY := round(velY);
946 if (dY <> 0) then
947 begin
948 y += dY;
949 if (dY < 0) then
950 begin
951 if (y <= ceilingY) then begin die(); exit; end;
952 end
953 else
954 begin
955 if (y >= floorY) then begin die(); exit; end;
956 end;
957 if (y < g_Map_MinY) or (y > g_Map_MaxY) then begin die(); exit; end;
958 end;
960 if (velY > -4) then velY += accelY;
962 time += 1;
963 end;
966 {.$DEFINE D2F_DEBUG_BUBBLES}
967 procedure g_GFX_Bubbles (fX, fY: Integer; count: Word; devX, devY: Byte);
968 var
969 a, liquidx: Integer;
970 devX1, devX2, devY1, devY2: Integer;
971 l: Integer;
972 {$IF DEFINED(D2F_DEBUG_BUBBLES)}
973 stt: UInt64;
974 nptr, ptr: Boolean;
975 {$ENDIF}
976 begin
977 if not gpart_dbg_enabled then exit;
979 l := Length(Particles);
980 if (l = 0) then exit;
981 if (count > l) then count := l;
983 devX1 := devX div 2;
984 devX2 := devX+1;
985 devY1 := devY div 2;
986 devY2 := devY+1;
988 for a := 1 to count do
989 begin
990 with Particles[CurrentParticle] do
991 begin
992 x := fX-devX1+Random(devX2);
993 y := fY-devY1+Random(devY2);
995 // check for level bounds
996 if (x < g_Map_MinX) or (y < g_Map_MinY) or (x > g_Map_MaxX) or (y > g_Map_MaxY) then continue;
998 (*
999 // don't spawn bubbles outside of the liquid
1000 if not isLiquidAt(X, Y) {ByteBool(gCollideMap[Y, X] and MARK_LIQUID)} then
1001 Continue;
1002 *)
1004 // trace liquid, so we'll know where it ends; do it in 8px steps for speed
1005 // tracer will return `false` if we started outside of the liquid
1007 {$IF DEFINED(D2F_DEBUG_BUBBLES)}
1008 stt := curTimeMicro();
1009 ptr := mapGrid.traceOrthoRayWhileIn(liquidx, liquidTopY, x, y, x, 0, GridTagWater or GridTagAcid1 or GridTagAcid2);
1010 stt := curTimeMicro()-stt;
1011 e_LogWritefln('traceOrthoRayWhileIn: time=%s (%s); liquidTopY=%s', [Integer(stt), ptr, liquidTopY]);
1012 //
1013 stt := curTimeMicro();
1014 nptr := g_Map_TraceLiquidNonPrecise(x, y, 0, -8, liquidx, liquidTopY);
1015 stt := curTimeMicro()-stt;
1016 e_LogWritefln('g_Map_TraceLiquidNonPrecise: time=%s (%s); liquidTopY=%s', [Integer(stt), nptr, liquidTopY]);
1017 if not nptr then continue;
1018 {$ELSE}
1019 if not g_Map_TraceLiquidNonPrecise(x, y, 0, -8, liquidx, ceilingY) then continue;
1020 if not g_Map_TraceLiquidNonPrecise(x, y, 0, +8, liquidx, floorY) then continue;
1021 {$ENDIF}
1023 velX := 0;
1024 velY := -1-Random;
1025 accelX := 0;
1026 accelY := velY/10;
1028 red := 255;
1029 green := 255;
1030 blue := 255;
1031 alpha := 255;
1033 state := TPartState.Normal;
1034 particleType := TPartType.Bubbles;
1035 time := 0;
1036 liveTime := 65535;
1037 end;
1039 if (CurrentParticle >= MaxParticles-1) then CurrentParticle := 0 else CurrentParticle += 1;
1040 end;
1041 end;
1044 // ////////////////////////////////////////////////////////////////////////// //
1045 procedure TParticle.thinkerSpark ();
1046 label
1047 _done;
1048 var
1049 dX, dY: SmallInt;
1050 pan: TPanel;
1051 ex, ey: Integer;
1052 begin
1053 if not gpart_dbg_phys_enabled then goto _done;
1055 dX := round(velX);
1056 dY := round(velY);
1058 // apply gravity
1059 if (abs(velX) < 0.1) and (abs(velY) < 0.1) then
1060 begin
1061 velY := 0.8;
1062 accelY := 0.5;
1063 end;
1065 // flying
1066 if (dX <> 0) then
1067 begin
1068 // has some horizontal velocity
1069 pan := g_Map_traceToNearest(x, y, x+dX, y+dY, (GridTagObstacle or GridTagLiquid), @ex, @ey);
1070 if (x <> ex) then begin floorY := Unknown; ceilingY := Unknown; end; // dunno yet
1071 x := ex;
1072 y := ey;
1073 if (pan <> nil) then
1074 begin
1075 if ((pan.tag and GridTagLiquid) <> 0) then begin die(); exit; end; // die in liquid
1076 // hit the wall; falling down vertically
1077 velX := 0;
1078 accelX := 0;
1079 end;
1080 end
1081 else if (dY <> 0) then
1082 begin
1083 // has some vertical velocity
1084 if (dY < 0) then
1085 begin
1086 // flying up
1087 if (ceilingY = Unknown) then findCeiling(); // need to do this anyway
1088 y += dY;
1089 if (y <= ceilingY) then
1090 begin
1091 // oops, hit a ceiling
1092 y := ceilingY;
1093 velY := -velY;
1094 accelY := abs(accelY);
1095 end;
1096 // environment didn't changed
1097 end
1098 else
1099 begin
1100 // falling down
1101 if (floorY = Unknown) then findFloor(); // need to do this anyway
1102 y += dY;
1103 if (y >= floorY) then
1104 begin
1105 // hit something except a floor?
1106 if (floorType <> TFloorType.Wall) then begin die(); exit; end; // yep: just die
1107 // otherwise, go to sleep
1108 y := floorY;
1109 sleep();
1110 // environment didn't changed
1111 end;
1112 end;
1113 end;
1115 _done:
1116 if (x < g_Map_MinX) or (y < g_Map_MinY) or (x > g_Map_MaxX) or (y > g_Map_MaxY) then begin die(); end;
1118 if (velX <> 0.0) then velX += accelX;
1120 if (velY <> 0.0) then
1121 begin
1122 if (accelY < 10) then accelY += 0.08;
1123 velY += accelY;
1124 end;
1126 time += 1;
1127 end;
1130 // ////////////////////////////////////////////////////////////////////////// //
1131 procedure g_GFX_SparkVel (fX, fY: Integer; count: Word; vx, vy: Integer; devX, devY: Byte);
1132 var
1133 a: Integer;
1134 devX1, devX2, devY1, devY2: Integer;
1135 l: Integer;
1136 pan: TPanel;
1137 begin
1138 if not gpart_dbg_enabled then exit;
1140 l := Length(Particles);
1141 if (l = 0) then exit;
1142 if (count > l) then count := l;
1144 devX1 := devX div 2;
1145 devX2 := devX+1;
1146 devY1 := devY div 2;
1147 devY2 := devY+1;
1149 for a := 1 to count do
1150 begin
1151 with Particles[CurrentParticle] do
1152 begin
1153 x := fX-devX1+Random(devX2);
1154 y := fY-devY1+Random(devY2);
1156 // check for level bounds
1157 if (x < g_Map_MinX) or (y < g_Map_MinY) or (x > g_Map_MaxX) or (y > g_Map_MaxY) then continue;
1159 // in what environment we are starting in?
1160 pan := g_Map_PanelAtPoint(x, y, (GridTagObstacle or GridTagLiquid));
1161 if (pan <> nil) then
1162 begin
1163 // either in a wall, or in a liquid
1164 //if ((pan.tag and GridTagObstacle) <> 0) then continue; // don't spawn in walls
1165 //env := TEnvType.ELiquid;
1166 continue;
1167 end
1168 else
1169 begin
1170 env := TEnvType.EAir;
1171 end;
1173 velX := vx+(Random-Random)*3;
1174 velY := vy+(Random-Random)*3;
1176 if (velY > -4) then
1177 begin
1178 if (velY-4 < -4) then velY := -4 else velY := velY-4;
1179 end;
1181 accelX := -sign(velX)*Random/100;
1182 accelY := 0.8;
1184 red := 255;
1185 green := 100+Random(155);
1186 blue := 64;
1187 alpha := 255;
1189 particleType := TPartType.Spark;
1190 state := TPartState.Normal;
1191 time := 0;
1192 liveTime := 30+Random(60);
1193 floorY := Unknown;
1194 ceilingY := Unknown;
1195 end;
1197 if (CurrentParticle >= MaxParticles-1) then CurrentParticle := 0 else CurrentParticle += 1;
1198 end;
1199 end;
1202 procedure g_GFX_Spark (fX, fY: Integer; count: Word; angle: SmallInt; devX, devY: Byte);
1203 var
1204 a: Integer;
1205 b: Single;
1206 devX1, devX2, devY1, devY2: Integer;
1207 baseVelX, baseVelY: Single;
1208 l: Integer;
1209 pan: TPanel;
1210 begin
1211 if not gpart_dbg_enabled then exit;
1213 l := Length(Particles);
1214 if (l = 0) then exit;
1215 if (count > l) then count := l;
1217 angle := 360-angle;
1219 devX1 := devX div 2;
1220 devX2 := devX+1;
1221 devY1 := devY div 2;
1222 devY2 := devY+1;
1224 b := DegToRad(angle);
1225 baseVelX := cos(b);
1226 baseVelY := 1.6*sin(b);
1227 if (abs(baseVelX) < 0.01) then baseVelX := 0.0;
1228 if (abs(baseVelY) < 0.01) then baseVelY := 0.0;
1230 for a := 1 to count do
1231 begin
1232 with Particles[CurrentParticle] do
1233 begin
1234 x := fX-devX1+Random(devX2);
1235 y := fY-devY1+Random(devY2);
1237 // check for level bounds
1238 if (x < g_Map_MinX) or (y < g_Map_MinY) or (x > g_Map_MaxX) or (y > g_Map_MaxY) then continue;
1240 // in what environment we are starting in?
1241 pan := g_Map_PanelAtPoint(x, y, (GridTagObstacle or GridTagLiquid));
1242 if (pan <> nil) then
1243 begin
1244 // either in a wall, or in a liquid
1245 //if ((pan.tag and GridTagObstacle) <> 0) then continue; // don't spawn in walls
1246 //env := TEnvType.ELiquid;
1247 continue;
1248 end
1249 else
1250 begin
1251 env := TEnvType.EAir;
1252 end;
1254 velX := baseVelX*Random;
1255 velY := baseVelY-Random;
1256 accelX := velX/3.0;
1257 accelY := velY/5.0;
1259 red := 255;
1260 green := 100+Random(155);
1261 blue := 64;
1262 alpha := 255;
1264 particleType := TPartType.Spark;
1265 state := TPartState.Normal;
1266 time := 0;
1267 liveTime := 30+Random(60);
1268 floorY := Unknown;
1269 ceilingY := Unknown;
1270 end;
1272 if (CurrentParticle >= MaxParticles-1) then CurrentParticle := 0 else CurrentParticle += 1;
1273 end;
1274 end;
1277 // ////////////////////////////////////////////////////////////////////////// //
1278 procedure g_GFX_SetMax (count: Integer);
1279 var
1280 a: Integer;
1281 begin
1282 if count > 50000 then count := 50000;
1283 if (count < 1) then count := 1;
1284 SetLength(Particles, count);
1285 for a := 0 to High(Particles) do Particles[a].die();
1286 MaxParticles := count;
1287 CurrentParticle := 0;
1288 end;
1291 function g_GFX_GetMax (): Integer;
1292 begin
1293 result := MaxParticles;
1294 end;
1297 function FindOnceAnim (): DWORD;
1298 var
1299 i: Integer;
1300 begin
1301 if OnceAnims <> nil then
1302 for i := 0 to High(OnceAnims) do
1303 if OnceAnims[i].Animation = nil then
1304 begin
1305 Result := i;
1306 Exit;
1307 end;
1309 if OnceAnims = nil then
1310 begin
1311 SetLength(OnceAnims, 16);
1312 Result := 0;
1313 end
1314 else
1315 begin
1316 Result := High(OnceAnims) + 1;
1317 SetLength(OnceAnims, Length(OnceAnims) + 16);
1318 end;
1319 end;
1322 procedure g_GFX_OnceAnim (x, y: Integer; Anim: TAnimation; AnimType: Byte = 0);
1323 var
1324 find_id: DWORD;
1325 begin
1326 if not gpart_dbg_enabled then exit;
1328 if (Anim = nil) then exit;
1330 find_id := FindOnceAnim();
1332 OnceAnims[find_id].AnimType := AnimType;
1333 OnceAnims[find_id].Animation := TAnimation.Create(Anim.FramesID, Anim.Loop, Anim.Speed);
1334 OnceAnims[find_id].Animation.Blending := Anim.Blending;
1335 OnceAnims[find_id].Animation.alpha := Anim.alpha;
1336 OnceAnims[find_id].x := x;
1337 OnceAnims[find_id].y := y;
1338 end;
1341 // ////////////////////////////////////////////////////////////////////////// //
1342 // st: set mark
1343 // t: mark type
1344 // currently unused
1345 procedure g_Mark (x, y, Width, Height: Integer; t: Byte; st: Boolean=true);
1346 var
1347 cx, ex, ey: Integer;
1348 ts: Integer;
1349 begin
1350 if not gpart_dbg_enabled then exit;
1352 if (Width < 1) or (Height < 1) then exit;
1353 // make some border, so we'll hit particles lying around the panel
1354 x -= 1; Width += 2;
1355 y -= 1; Height += 2;
1356 ex := x+Width;
1357 ey := y+Height;
1358 ts := mapGrid.tileSize;
1359 while (y < ey) do
1360 begin
1361 cx := x;
1362 while (cx < ex) do
1363 begin
1364 awmSet(cx, y);
1365 Inc(cx, ts);
1366 end;
1367 Inc(y, ts);
1368 end;
1369 end;
1372 // ////////////////////////////////////////////////////////////////////////// //
1373 procedure g_GFX_Init ();
1374 begin
1375 //g_Game_SetLoadingText(_lc[I_LOAD_COLLIDE_MAP]+' 1/6', 0, False);
1376 //SetLength(gCollideMap, gMapInfo.Height+1);
1377 //for a := 0 to High(gCollideMap) do SetLength(gCollideMap[a], gMapInfo.Width+1);
1378 awmSetup();
1379 {$IFDEF HEADLESS}
1380 gpart_dbg_enabled := false;
1381 {$ENDIF}
1382 end;
1385 procedure g_GFX_Free ();
1386 var
1387 a: Integer;
1388 begin
1389 Particles := nil;
1390 SetLength(Particles, MaxParticles);
1391 for a := 0 to High(Particles) do Particles[a].die();
1392 CurrentParticle := 0;
1394 if (OnceAnims <> nil) then
1395 begin
1396 for a := 0 to High(OnceAnims) do OnceAnims[a].Animation.Free();
1397 OnceAnims := nil;
1398 end;
1400 awakeMap := nil;
1401 // why not?
1402 awakeMapH := -1;
1403 awakeMapW := -1;
1404 end;
1407 // ////////////////////////////////////////////////////////////////////////// //
1408 procedure g_GFX_Update ();
1409 var
1410 a: Integer;
1411 w, h: Integer;
1412 len: Integer;
1413 begin
1414 if not gpart_dbg_enabled then exit;
1416 if (Particles <> nil) then
1417 begin
1418 w := gMapInfo.Width;
1419 h := gMapInfo.Height;
1421 len := High(Particles);
1423 for a := 0 to len do
1424 begin
1425 if Particles[a].alive then
1426 begin
1427 with Particles[a] do
1428 begin
1429 if (time = liveTime) then begin die(); continue; end;
1430 if (x+1 >= w) or (y+1 >= h) or (x <= 0) or (y <= 0) then begin die(); end;
1431 think();
1432 end; // with
1433 end; // if
1434 end; // for
1435 end; // Particles <> nil
1437 // clear awake map
1438 awmClear();
1440 if OnceAnims <> nil then
1441 begin
1442 for a := 0 to High(OnceAnims) do
1443 if OnceAnims[a].Animation <> nil then
1444 begin
1445 case OnceAnims[a].AnimType of
1446 ONCEANIM_SMOKE:
1447 begin
1448 if Random(3) = 0 then
1449 OnceAnims[a].x := OnceAnims[a].x-1+Random(3);
1450 if Random(2) = 0 then
1451 OnceAnims[a].y := OnceAnims[a].y-Random(2);
1452 end;
1453 end;
1455 if OnceAnims[a].Animation.Played then
1456 begin
1457 OnceAnims[a].Animation.Free();
1458 OnceAnims[a].Animation := nil;
1459 end
1460 else
1461 OnceAnims[a].Animation.Update();
1462 end;
1463 end;
1464 end;
1467 procedure g_GFX_Draw ();
1468 var
1469 a, len: Integer;
1470 begin
1471 if not gpart_dbg_enabled then exit;
1473 if (Particles <> nil) then
1474 begin
1475 glDisable(GL_TEXTURE_2D);
1476 glPointSize(2);
1478 glEnable(GL_BLEND);
1479 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1481 glBegin(GL_POINTS);
1483 len := High(Particles);
1484 for a := 0 to len do
1485 begin
1486 with Particles[a] do
1487 begin
1488 if alive and (x >= sX) and (y >= sY) and (x <= sX+sWidth) and (sY <= sY+sHeight) then
1489 begin
1490 glColor4ub(red, green, blue, alpha);
1491 glVertex2f(x+0.37, y+0.37);
1492 end;
1493 end;
1494 end;
1496 glEnd();
1498 glDisable(GL_BLEND);
1499 end;
1501 if (OnceAnims <> nil) then
1502 begin
1503 len := High(OnceAnims);
1504 for a := 0 to len do
1505 begin
1506 if (OnceAnims[a].Animation <> nil) then
1507 begin
1508 with OnceAnims[a] do Animation.Draw(x, y, M_NONE);
1509 end;
1510 end;
1511 end;
1512 end;
1515 end.