DEADSOFTWARE

particle cosmetix for mplats
[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 procedure awake ();
362 begin
363 state := TPartState.Normal;
364 floorY := Unknown;
365 ceilingY := Unknown;
366 if (velY = 0) then velY := 0.1;
367 if (accelY = 0) then accelY := 0.5;
368 end;
370 begin
371 // awake sleeping particle, if necessary
372 if awakeDirty then
373 begin
374 case state of
375 TPartState.Sleeping, TPartState.Stuck:
376 if awmIsSet(x, y) then awake();
377 else
378 if (env = TEnvType.EWall) and awmIsSet(x, y) then awake();
379 end;
380 end;
381 case particleType of
382 TPartType.Blood, TPartType.Water: thinkerBloodAndWater();
383 TPartType.Spark: thinkerSpark();
384 TPartType.Bubbles: thinkerBubble();
385 end;
386 end;
389 // ////////////////////////////////////////////////////////////////////////// //
390 procedure TParticle.thinkerBloodAndWater ();
391 procedure stickToCeiling ();
392 begin
393 state := TPartState.Stuck;
394 stickDX := 0;
395 freeze();
396 ceilingY := y; // yep
397 end;
399 procedure stickToWall (dx: Integer);
400 var
401 ex: Integer;
402 begin
403 state := TPartState.Stuck;
404 if (dX > 0) then stickDX := 1 else stickDX := -1;
405 freeze();
406 // find next floor transition
407 findFloor();
408 // find `wallEndY`
409 mapGrid.traceOrthoRayWhileIn(ex, wallEndY, x+stickDX, y, x+stickDX, floorY+1, (GridTagWall or GridTagDoor or GridTagStep));
410 end;
412 procedure hitAFloor ();
413 begin
414 state := TPartState.Sleeping; // we aren't moving anymore
415 freeze();
416 floorY := y; // yep
417 floorType := TFloorType.Wall; // yep
418 end;
420 // `true`: didn't, get outa thinker
421 function drip (): Boolean;
422 begin
423 case particleType of
424 TPartType.Blood: result := (Random(200) = 100);
425 TPartType.Water: result := (Random(30) = 15);
426 else raise Exception.Create('internal error in particle engine: drip');
427 end;
428 if result then begin velY := 0.5; accelY := 0.15; end;
429 end;
431 // switch to freefall mode
432 procedure freefall ();
433 begin
434 state := TPartState.Normal;
435 velY := 0.5;
436 accelY := 0.15;
437 end;
439 procedure applyGravity (inLiquid: Boolean);
440 begin
441 state := TPartState.Normal;
442 if (inLiquid) then
443 begin
444 velY := 0.5;
445 accelY := 0.15;
446 end
447 else
448 begin
449 velY := 0.8;
450 accelY := 0.5;
451 end;
452 end;
454 label
455 _done;
456 var
457 pan: TPanel;
458 dX, dY: SmallInt;
459 ex, ey: Integer;
460 checkEnv: Boolean;
461 begin
462 if not gpart_dbg_phys_enabled then goto _done;
464 if gAdvBlood then
465 begin
466 // still check for air streams when sleeping (no)
467 if (state = TPartState.Sleeping) then begin {checkAirStreams();} goto _done; end; // so blood will dissolve
469 // process stuck particles
470 if (state = TPartState.Stuck) then
471 begin
472 // stuck to a ceiling?
473 if (stickDX = 0) then
474 begin
475 // yeah, stuck to a ceiling
476 assert(ceilingY <> Unknown);
477 // dropped from a ceiling?
478 if (y > ceilingY) then
479 begin
480 // yep
481 velY := 0.5;
482 accelY := 0.15;
483 state := TPartState.Normal;
484 end
485 else
486 begin
487 // otherwise, try to drip
488 if drip() then goto _done;
489 end;
490 end
491 else
492 begin
493 // stuck to a wall
494 assert(wallEndY <> Unknown);
495 // floor transition?
496 if (wallEndY <= floorY) and (y >= floorY) then
497 begin
498 y := floorY;
499 case floorType of
500 TFloorType.Wall: // hit the ground
501 begin
502 sleep();
503 goto _done; // nothing to do anymore
504 end;
505 TFloorType.LiquidIn: // entering the liquid
506 begin
507 // rescan, so we'll know when we'll exit the liquid
508 findFloor(true); // force rescan
509 end;
510 TFloorType.LiquidOut: // exiting the liquid
511 begin
512 // rescan, so we'll know when we'll enter something interesting
513 findFloor(true); // force rescan
514 if (floorType = TFloorType.Wall) and (floorY = y) then begin sleep(); goto _done; end;
515 end;
516 end;
517 end;
518 // wall transition?
519 if (floorY <= wallEndY) and (y >= wallEndY) then
520 begin
521 // just unstuck from the wall, switch to freefall mode
522 y := wallEndY;
523 freefall();
524 end
525 else
526 begin
527 // otherwise, try to drip
528 if drip() then goto _done;
529 end;
530 end;
531 // nope, process as usual
532 end;
534 // it is important to have it here
535 dX := round(velX);
536 dY := round(velY);
538 if (state = TPartState.Normal) then checkAirStreams();
540 // gravity, if not stuck
541 if (state <> TPartState.Stuck) and (abs(velX) < 0.1) and (abs(velY) < 0.1) then
542 begin
543 if (floorY = Unknown) then findFloor();
544 // floor transition?
545 if (y = floorY) then
546 begin
547 case floorType of
548 TFloorType.Wall: // hit the ground
549 begin
550 // nothing to do
551 end;
552 TFloorType.LiquidIn: // entering the liquid
553 begin
554 // rescan, so we'll know when we'll exit the liquid
555 findFloor(true); // force rescan
556 applyGravity(true);
557 end;
558 TFloorType.LiquidOut: // exiting the liquid
559 begin
560 // rescan, so we'll know when we'll enter something interesting
561 findFloor(true); // force rescan
562 if (floorType <> TFloorType.Wall) or (floorY <> y) then applyGravity(floorType = TFloorType.LiquidIn);
563 end;
564 end;
565 end
566 else
567 begin
568 // looks like we're in the air
569 applyGravity(false);
570 end;
571 end;
573 // trace movement
574 if (dX <> 0) then
575 begin
576 // has some horizontal velocity
577 pan := g_Map_traceToNearest(x, y, x+dX, y+dY, GridTagObstacle, @ex, @ey);
578 checkEnv := (x <> ex);
579 x := ex;
580 y := ey;
581 if checkEnv then
582 begin
583 // dunno yet
584 floorY := Unknown;
585 ceilingY := Unknown;
586 // check environment (air/liquid)
587 if (g_Map_PanelAtPoint(x, y, GridTagLiquid) <> nil) then env := TEnvType.ELiquid else env := TEnvType.EAir;
588 end;
589 if (pan <> nil) then
590 begin
591 // we stuck
592 // the only case when we can have both ceiling and wall is corner; stick to wall in this case
593 // check if we stuck to a wall
594 if (dX < 0) then dX := -1 else dX := 1;
595 if (g_Map_PanelAtPoint(x+dX, y, GridTagObstacle) <> nil) then
596 begin
597 // stuck to a wall
598 stickToWall(dX);
599 end
600 else
601 begin
602 // stuck to a ceiling
603 stickToCeiling();
604 end;
605 end;
606 end
607 else if (dY <> 0) then
608 begin
609 // has only vertical velocity
610 if (dY < 0) then
611 begin
612 // flying up
613 if (ceilingY = Unknown) then findCeiling(); // need to do this anyway
614 y += dY;
615 if (y <= ceilingY) then begin y := ceilingY; stickToCeiling(); end; // oops, hit a ceiling
616 // environment didn't changed
617 end
618 else
619 begin
620 while (dY > 0) do
621 begin
622 // falling down
623 if (floorY = Unknown) then findFloor(); // need to do this anyway
624 if (floorType = TFloorType.LiquidOut) then env := TEnvType.ELiquid else env := TEnvType.EAir;
625 y += dY;
626 //e_LogWritefln('floorY=%s; newy=%s; dY=%s; floorType=%s', [floorY, y, dY, floorType]);
627 if (y >= floorY) then
628 begin
629 // floor transition
630 dY := y-floorY;
631 y := floorY;
632 //e_LogWritefln(' HIT FLOORY: floorY=%s; newy=%s; dY=%s; floorType=%s', [floorY, y, dY, floorType]);
633 case floorType of
634 TFloorType.Wall: // hit the ground
635 begin
636 // environment didn't changed
637 hitAFloor();
638 break; // done with vertical movement
639 end;
640 TFloorType.LiquidIn: // entering the liquid
641 begin
642 // we're entered the liquid
643 env := TEnvType.ELiquid;
644 // rescan, so we'll know when we'll exit the liquid
645 findFloor(true); // force rescan
646 end;
647 TFloorType.LiquidOut: // exiting the liquid
648 begin
649 // we're exited the liquid
650 env := TEnvType.EAir;
651 // rescan, so we'll know when we'll enter something interesting
652 findFloor(true); // force rescan
653 if (floorType = TFloorType.Wall) and (floorY = y) then
654 begin
655 hitAFloor();
656 break; // done with vertical movement
657 end;
658 end;
659 end;
660 end
661 else
662 begin
663 break; // done with vertical movement
664 end;
665 end;
666 end;
667 end;
668 end // if gAdvBlood
669 else
670 begin
671 // simple blood
672 dX := round(velX);
673 dY := round(velY);
674 y += dY;
675 x += dX;
676 if (g_Map_PanelAtPoint(x, y, GridTagObstacle) <> nil) then begin die(); exit; end;
677 end;
679 _done:
680 if (x < g_Map_MinX) or (y < g_Map_MinY) or (x > g_Map_MaxX) or (y > g_Map_MaxY) then begin die(); end;
682 velX += accelX;
683 velY += accelY;
685 // blood will dissolve in other liquids
686 if (particleType = TPartType.Blood) then
687 begin
688 if (env = TEnvType.ELiquid) then
689 begin
690 time += 1;
691 if (liveTime <= 0) then begin die(); exit; end;
692 ex := 255-trunc(255.0*time/liveTime);
693 if (ex <= 10) then begin die(); exit; end;
694 if (ex > 250) then ex := 255;
695 alpha := Byte(ex);
696 end;
697 end
698 else
699 begin
700 // water will disappear in any liquid
701 if (env = TEnvType.ELiquid) then begin die(); exit; end;
702 time += 1;
703 // dry water
704 if (liveTime <= 0) then begin die(); exit; end;
705 ex := 255-trunc(255.0*time/liveTime);
706 if (ex <= 10) then begin die(); exit; end;
707 if (ex > 250) then ex := 255;
708 alpha := Byte(ex);
709 end;
710 end;
713 // ////////////////////////////////////////////////////////////////////////// //
714 procedure g_GFX_SparkVel (fX, fY: Integer; count: Word; vx, vy: Integer; devX, devY: Byte); forward;
716 procedure g_GFX_Blood (fX, fY: Integer; count: Word; vx, vy: Integer;
717 devX, devY: Word; cr, cg, cb: Byte; kind: Byte = BLOOD_NORMAL);
719 function genColor (cbase, crnd: Integer; def: Byte=0): Byte;
720 begin
721 if (cbase > 0) then
722 begin
723 cbase += crnd;
724 if (cbase < 0) then result := 0
725 else if (cbase > 255) then result := 255
726 else result := Byte(cbase);
727 end
728 else
729 begin
730 result := def;
731 end;
732 end;
734 var
735 a: Integer;
736 devX1, devX2, devY1, devY2: Integer;
737 l: Integer;
738 crnd: Integer;
739 pan: TPanel;
740 begin
741 if not gpart_dbg_enabled then exit;
743 if (kind = BLOOD_SPARKS) then
744 begin
745 g_GFX_SparkVel(fX, fY, 2+Random(2), -vx div 2, -vy div 2, devX, devY);
746 exit;
747 end;
749 l := Length(Particles);
750 if (l = 0) then exit;
751 if (count > l) then count := l;
753 devX1 := devX div 2;
754 devX2 := devX+1;
755 devY1 := devY div 2;
756 devY2 := devY+1;
758 for a := 1 to count do
759 begin
760 with Particles[CurrentParticle] do
761 begin
762 x := fX-devX1+Random(devX2);
763 y := fY-devY1+Random(devY2);
765 // check for level bounds
766 if (x < g_Map_MinX) or (y < g_Map_MinY) or (x > g_Map_MaxX) or (y > g_Map_MaxY) then continue;
768 // in what environment we are starting in?
769 pan := g_Map_PanelAtPoint(x, y, (GridTagObstacle or GridTagLiquid));
770 if (pan <> nil) then
771 begin
772 // either in a wall, or in a liquid
773 if ((pan.tag and GridTagObstacle) <> 0) then continue; // don't spawn in walls
774 env := TEnvType.ELiquid;
775 end
776 else
777 begin
778 env := TEnvType.EAir;
779 end;
781 velX := vx+(Random-Random)*3;
782 velY := vy+(Random-Random)*3;
784 if (velY > -4) then
785 begin
786 if (velY-4 < -4) then velY := -4 else velY := velY-4;
787 end;
789 accelX := -sign(velX)*Random/100;
790 accelY := 0.8;
792 crnd := 20*Random(6)-50;
794 red := genColor(cr, CRnd, 0);
795 green := genColor(cg, CRnd, 0);
796 blue := genColor(cb, CRnd, 0);
797 alpha := 255;
799 particleType := TPartType.Blood;
800 state := TPartState.Normal;
801 time := 0;
802 liveTime := 120+Random(40);
803 floorY := Unknown;
804 ceilingY := Unknown;
805 end;
807 if (CurrentParticle >= MaxParticles-1) then CurrentParticle := 0 else CurrentParticle += 1;
808 end;
809 end;
812 procedure g_GFX_Water (fX, fY: Integer; count: Word; fVelX, fVelY: Single; devX, devY, color: Byte;
813 simple: Boolean=false; cr: Byte=0; cg: Byte=0; cb: Byte=0);
814 var
815 a: Integer;
816 devX1, devX2, devY1, devY2: Integer;
817 l: Integer;
818 pan: TPanel;
819 begin
820 if not gpart_dbg_enabled then exit;
822 l := Length(Particles);
823 if (l = 0) then exit;
824 if (count > l) then count := l;
826 if (abs(fVelX) < 3.0) then fVelX := 3.0-6.0*Random;
828 devX1 := devX div 2;
829 devX2 := devX+1;
830 devY1 := devY div 2;
831 devY2 := devY+1;
833 if (not simple) and (color > 3) then color := 0;
835 for a := 1 to count do
836 begin
837 with Particles[CurrentParticle] do
838 begin
839 if not simple then
840 begin
841 x := fX-devX1+Random(devX2);
842 y := fY-devY1+Random(devY2);
844 if (abs(fVelX) < 0.5) then velX := 1.0-2.0*Random else velX := fVelX*Random;
845 if (Random(10) < 7) then velX := -velX;
846 velY := fVelY*Random;
847 accelX := 0.0;
848 accelY := 0.8;
849 end
850 else
851 begin
852 x := fX;
853 y := fY;
855 velX := fVelX;
856 velY := fVelY;
857 accelX := 0.0;
858 accelY := 0.8;
859 end;
861 // check for level bounds
862 if (x < g_Map_MinX) or (y < g_Map_MinY) or (x > g_Map_MaxX) or (y > g_Map_MaxY) then continue;
864 // this hack will allow water spawned in water to fly out
865 // it can happen when player fell from a huge height (see "DOOM2D.WAD:\MAP03", for example)
866 if (fVelY >= 0) then
867 begin
868 // in what environment we are starting in?
869 pan := g_Map_PanelAtPoint(x, y, (GridTagObstacle or GridTagLiquid));
870 end
871 else
872 begin
873 pan := g_Map_PanelAtPoint(x, y, GridTagObstacle);
874 end;
875 if (pan <> nil) then continue;
876 env := TEnvType.EAir;
878 // color
879 case color of
880 1: // reddish
881 begin
882 red := 155+Random(9)*10;
883 green := trunc(150*Random);
884 blue := green;
885 end;
886 2: // greenish
887 begin
888 red := trunc(150*Random);
889 green := 175+Random(9)*10;
890 blue := red;
891 end;
892 3: // bluish
893 begin
894 red := trunc(200*Random);
895 green := red;
896 blue := 175+Random(9)*10;
897 end;
898 4: // Ñâîé öâåò, ñâåòëåå
899 begin
900 red := 20+Random(19)*10;
901 green := red;
902 blue := red;
903 red := nmin(red+cr, 255);
904 green := nmin(green+cg, 255);
905 blue := nmin(blue+cb, 255);
906 end;
907 5: // Ñâîé öâåò, òåìíåå
908 begin
909 red := 20+Random(19)*10;
910 green := red;
911 blue := red;
912 red := nmax(cr-red, 0);
913 green := nmax(cg-green, 0);
914 blue := nmax(cb-blue, 0);
915 end;
916 else // grayish
917 begin
918 red := 90+random(12)*10;
919 green := red;
920 blue := red;
921 end;
922 end;
923 alpha := 255;
925 particleType := TPartType.Water;
926 state := TPartState.Normal;
927 time := 0;
928 liveTime := 60+Random(60);
929 floorY := Unknown;
930 ceilingY := Unknown;
931 end;
933 if (CurrentParticle >= MaxParticles-1) then CurrentParticle := 0 else CurrentParticle += 1;
934 end;
935 end;
938 procedure g_GFX_SimpleWater (fX, fY: Integer; count: Word; fVelX, fVelY: Single; defColor, cr, cg, cb: Byte);
939 begin
940 g_GFX_Water(fX, fY, count, 0, 0, 0, 0, defColor, true, cr, cg, cb);
941 end;
944 // ////////////////////////////////////////////////////////////////////////// //
945 procedure TParticle.thinkerBubble ();
946 var
947 dY: Integer;
948 begin
949 dY := round(velY);
951 if (dY <> 0) then
952 begin
953 y += dY;
954 if (dY < 0) then
955 begin
956 if (y <= ceilingY) then begin die(); exit; end;
957 end
958 else
959 begin
960 if (y >= floorY) then begin die(); exit; end;
961 end;
962 if (y < g_Map_MinY) or (y > g_Map_MaxY) then begin die(); exit; end;
963 end;
965 if (velY > -4) then velY += accelY;
967 time += 1;
968 end;
971 {.$DEFINE D2F_DEBUG_BUBBLES}
972 procedure g_GFX_Bubbles (fX, fY: Integer; count: Word; devX, devY: Byte);
973 var
974 a, liquidx: Integer;
975 devX1, devX2, devY1, devY2: Integer;
976 l: Integer;
977 {$IF DEFINED(D2F_DEBUG_BUBBLES)}
978 stt: UInt64;
979 nptr, ptr: Boolean;
980 {$ENDIF}
981 begin
982 if not gpart_dbg_enabled then exit;
984 l := Length(Particles);
985 if (l = 0) then exit;
986 if (count > l) then count := l;
988 devX1 := devX div 2;
989 devX2 := devX+1;
990 devY1 := devY div 2;
991 devY2 := devY+1;
993 for a := 1 to count do
994 begin
995 with Particles[CurrentParticle] do
996 begin
997 x := fX-devX1+Random(devX2);
998 y := fY-devY1+Random(devY2);
1000 // check for level bounds
1001 if (x < g_Map_MinX) or (y < g_Map_MinY) or (x > g_Map_MaxX) or (y > g_Map_MaxY) then continue;
1003 (*
1004 // don't spawn bubbles outside of the liquid
1005 if not isLiquidAt(X, Y) {ByteBool(gCollideMap[Y, X] and MARK_LIQUID)} then
1006 Continue;
1007 *)
1009 // trace liquid, so we'll know where it ends; do it in 8px steps for speed
1010 // tracer will return `false` if we started outside of the liquid
1012 {$IF DEFINED(D2F_DEBUG_BUBBLES)}
1013 stt := curTimeMicro();
1014 ptr := mapGrid.traceOrthoRayWhileIn(liquidx, liquidTopY, x, y, x, 0, GridTagWater or GridTagAcid1 or GridTagAcid2);
1015 stt := curTimeMicro()-stt;
1016 e_LogWritefln('traceOrthoRayWhileIn: time=%s (%s); liquidTopY=%s', [Integer(stt), ptr, liquidTopY]);
1017 //
1018 stt := curTimeMicro();
1019 nptr := g_Map_TraceLiquidNonPrecise(x, y, 0, -8, liquidx, liquidTopY);
1020 stt := curTimeMicro()-stt;
1021 e_LogWritefln('g_Map_TraceLiquidNonPrecise: time=%s (%s); liquidTopY=%s', [Integer(stt), nptr, liquidTopY]);
1022 if not nptr then continue;
1023 {$ELSE}
1024 if not g_Map_TraceLiquidNonPrecise(x, y, 0, -8, liquidx, ceilingY) then continue;
1025 if not g_Map_TraceLiquidNonPrecise(x, y, 0, +8, liquidx, floorY) then continue;
1026 {$ENDIF}
1028 velX := 0;
1029 velY := -1-Random;
1030 accelX := 0;
1031 accelY := velY/10;
1033 red := 255;
1034 green := 255;
1035 blue := 255;
1036 alpha := 255;
1038 state := TPartState.Normal;
1039 particleType := TPartType.Bubbles;
1040 time := 0;
1041 liveTime := 65535;
1042 end;
1044 if (CurrentParticle >= MaxParticles-1) then CurrentParticle := 0 else CurrentParticle += 1;
1045 end;
1046 end;
1049 // ////////////////////////////////////////////////////////////////////////// //
1050 procedure TParticle.thinkerSpark ();
1051 label
1052 _done;
1053 var
1054 dX, dY: SmallInt;
1055 pan: TPanel;
1056 ex, ey: Integer;
1057 begin
1058 if not gpart_dbg_phys_enabled then goto _done;
1060 dX := round(velX);
1061 dY := round(velY);
1063 // apply gravity
1064 if (abs(velX) < 0.1) and (abs(velY) < 0.1) then
1065 begin
1066 velY := 0.8;
1067 accelY := 0.5;
1068 end;
1070 // flying
1071 if (dX <> 0) then
1072 begin
1073 // has some horizontal velocity
1074 pan := g_Map_traceToNearest(x, y, x+dX, y+dY, (GridTagObstacle or GridTagLiquid), @ex, @ey);
1075 if (x <> ex) then begin floorY := Unknown; ceilingY := Unknown; end; // dunno yet
1076 x := ex;
1077 y := ey;
1078 if (pan <> nil) then
1079 begin
1080 if ((pan.tag and GridTagLiquid) <> 0) then begin die(); exit; end; // die in liquid
1081 // hit the wall; falling down vertically
1082 velX := 0;
1083 accelX := 0;
1084 end;
1085 end
1086 else if (dY <> 0) then
1087 begin
1088 // has some vertical velocity
1089 if (dY < 0) then
1090 begin
1091 // flying up
1092 if (ceilingY = Unknown) then findCeiling(); // need to do this anyway
1093 y += dY;
1094 if (y <= ceilingY) then
1095 begin
1096 // oops, hit a ceiling
1097 y := ceilingY;
1098 velY := -velY;
1099 accelY := abs(accelY);
1100 end;
1101 // environment didn't changed
1102 end
1103 else
1104 begin
1105 // falling down
1106 if (floorY = Unknown) then findFloor(); // need to do this anyway
1107 y += dY;
1108 if (y >= floorY) then
1109 begin
1110 // hit something except a floor?
1111 if (floorType <> TFloorType.Wall) then begin die(); exit; end; // yep: just die
1112 // otherwise, go to sleep
1113 y := floorY;
1114 sleep();
1115 // environment didn't changed
1116 end;
1117 end;
1118 end;
1120 _done:
1121 if (x < g_Map_MinX) or (y < g_Map_MinY) or (x > g_Map_MaxX) or (y > g_Map_MaxY) then begin die(); end;
1123 if (velX <> 0.0) then velX += accelX;
1125 if (velY <> 0.0) then
1126 begin
1127 if (accelY < 10) then accelY += 0.08;
1128 velY += accelY;
1129 end;
1131 time += 1;
1132 end;
1135 // ////////////////////////////////////////////////////////////////////////// //
1136 procedure g_GFX_SparkVel (fX, fY: Integer; count: Word; vx, vy: Integer; devX, devY: Byte);
1137 var
1138 a: Integer;
1139 devX1, devX2, devY1, devY2: Integer;
1140 l: Integer;
1141 pan: TPanel;
1142 begin
1143 if not gpart_dbg_enabled then exit;
1145 l := Length(Particles);
1146 if (l = 0) then exit;
1147 if (count > l) then count := l;
1149 devX1 := devX div 2;
1150 devX2 := devX+1;
1151 devY1 := devY div 2;
1152 devY2 := devY+1;
1154 for a := 1 to count do
1155 begin
1156 with Particles[CurrentParticle] do
1157 begin
1158 x := fX-devX1+Random(devX2);
1159 y := fY-devY1+Random(devY2);
1161 // check for level bounds
1162 if (x < g_Map_MinX) or (y < g_Map_MinY) or (x > g_Map_MaxX) or (y > g_Map_MaxY) then continue;
1164 // in what environment we are starting in?
1165 pan := g_Map_PanelAtPoint(x, y, (GridTagObstacle or GridTagLiquid));
1166 if (pan <> nil) then
1167 begin
1168 // either in a wall, or in a liquid
1169 //if ((pan.tag and GridTagObstacle) <> 0) then continue; // don't spawn in walls
1170 //env := TEnvType.ELiquid;
1171 continue;
1172 end
1173 else
1174 begin
1175 env := TEnvType.EAir;
1176 end;
1178 velX := vx+(Random-Random)*3;
1179 velY := vy+(Random-Random)*3;
1181 if (velY > -4) then
1182 begin
1183 if (velY-4 < -4) then velY := -4 else velY := velY-4;
1184 end;
1186 accelX := -sign(velX)*Random/100;
1187 accelY := 0.8;
1189 red := 255;
1190 green := 100+Random(155);
1191 blue := 64;
1192 alpha := 255;
1194 particleType := TPartType.Spark;
1195 state := TPartState.Normal;
1196 time := 0;
1197 liveTime := 30+Random(60);
1198 floorY := Unknown;
1199 ceilingY := Unknown;
1200 end;
1202 if (CurrentParticle >= MaxParticles-1) then CurrentParticle := 0 else CurrentParticle += 1;
1203 end;
1204 end;
1207 procedure g_GFX_Spark (fX, fY: Integer; count: Word; angle: SmallInt; devX, devY: Byte);
1208 var
1209 a: Integer;
1210 b: Single;
1211 devX1, devX2, devY1, devY2: Integer;
1212 baseVelX, baseVelY: Single;
1213 l: Integer;
1214 pan: TPanel;
1215 begin
1216 if not gpart_dbg_enabled then exit;
1218 l := Length(Particles);
1219 if (l = 0) then exit;
1220 if (count > l) then count := l;
1222 angle := 360-angle;
1224 devX1 := devX div 2;
1225 devX2 := devX+1;
1226 devY1 := devY div 2;
1227 devY2 := devY+1;
1229 b := DegToRad(angle);
1230 baseVelX := cos(b);
1231 baseVelY := 1.6*sin(b);
1232 if (abs(baseVelX) < 0.01) then baseVelX := 0.0;
1233 if (abs(baseVelY) < 0.01) then baseVelY := 0.0;
1235 for a := 1 to count do
1236 begin
1237 with Particles[CurrentParticle] do
1238 begin
1239 x := fX-devX1+Random(devX2);
1240 y := fY-devY1+Random(devY2);
1242 // check for level bounds
1243 if (x < g_Map_MinX) or (y < g_Map_MinY) or (x > g_Map_MaxX) or (y > g_Map_MaxY) then continue;
1245 // in what environment we are starting in?
1246 pan := g_Map_PanelAtPoint(x, y, (GridTagObstacle or GridTagLiquid));
1247 if (pan <> nil) then
1248 begin
1249 // either in a wall, or in a liquid
1250 //if ((pan.tag and GridTagObstacle) <> 0) then continue; // don't spawn in walls
1251 //env := TEnvType.ELiquid;
1252 continue;
1253 end
1254 else
1255 begin
1256 env := TEnvType.EAir;
1257 end;
1259 velX := baseVelX*Random;
1260 velY := baseVelY-Random;
1261 accelX := velX/3.0;
1262 accelY := velY/5.0;
1264 red := 255;
1265 green := 100+Random(155);
1266 blue := 64;
1267 alpha := 255;
1269 particleType := TPartType.Spark;
1270 state := TPartState.Normal;
1271 time := 0;
1272 liveTime := 30+Random(60);
1273 floorY := Unknown;
1274 ceilingY := Unknown;
1275 end;
1277 if (CurrentParticle >= MaxParticles-1) then CurrentParticle := 0 else CurrentParticle += 1;
1278 end;
1279 end;
1282 // ////////////////////////////////////////////////////////////////////////// //
1283 procedure g_GFX_SetMax (count: Integer);
1284 var
1285 a: Integer;
1286 begin
1287 if count > 50000 then count := 50000;
1288 if (count < 1) then count := 1;
1289 SetLength(Particles, count);
1290 for a := 0 to High(Particles) do Particles[a].die();
1291 MaxParticles := count;
1292 CurrentParticle := 0;
1293 end;
1296 function g_GFX_GetMax (): Integer;
1297 begin
1298 result := MaxParticles;
1299 end;
1302 function FindOnceAnim (): DWORD;
1303 var
1304 i: Integer;
1305 begin
1306 if OnceAnims <> nil then
1307 for i := 0 to High(OnceAnims) do
1308 if OnceAnims[i].Animation = nil then
1309 begin
1310 Result := i;
1311 Exit;
1312 end;
1314 if OnceAnims = nil then
1315 begin
1316 SetLength(OnceAnims, 16);
1317 Result := 0;
1318 end
1319 else
1320 begin
1321 Result := High(OnceAnims) + 1;
1322 SetLength(OnceAnims, Length(OnceAnims) + 16);
1323 end;
1324 end;
1327 procedure g_GFX_OnceAnim (x, y: Integer; Anim: TAnimation; AnimType: Byte = 0);
1328 var
1329 find_id: DWORD;
1330 begin
1331 if not gpart_dbg_enabled then exit;
1333 if (Anim = nil) then exit;
1335 find_id := FindOnceAnim();
1337 OnceAnims[find_id].AnimType := AnimType;
1338 OnceAnims[find_id].Animation := TAnimation.Create(Anim.FramesID, Anim.Loop, Anim.Speed);
1339 OnceAnims[find_id].Animation.Blending := Anim.Blending;
1340 OnceAnims[find_id].Animation.alpha := Anim.alpha;
1341 OnceAnims[find_id].x := x;
1342 OnceAnims[find_id].y := y;
1343 end;
1346 // ////////////////////////////////////////////////////////////////////////// //
1347 // st: set mark
1348 // t: mark type
1349 // currently unused
1350 procedure g_Mark (x, y, Width, Height: Integer; t: Byte; st: Boolean=true);
1351 var
1352 cx, ex, ey: Integer;
1353 ts: Integer;
1354 begin
1355 if not gpart_dbg_enabled then exit;
1357 if (Width < 1) or (Height < 1) then exit;
1358 // make some border, so we'll hit particles lying around the panel
1359 x -= 1; Width += 2;
1360 y -= 1; Height += 2;
1361 ex := x+Width;
1362 ey := y+Height;
1363 ts := mapGrid.tileSize;
1364 while (y < ey) do
1365 begin
1366 cx := x;
1367 while (cx < ex) do
1368 begin
1369 awmSet(cx, y);
1370 Inc(cx, ts);
1371 end;
1372 Inc(y, ts);
1373 end;
1374 end;
1377 // ////////////////////////////////////////////////////////////////////////// //
1378 procedure g_GFX_Init ();
1379 begin
1380 //g_Game_SetLoadingText(_lc[I_LOAD_COLLIDE_MAP]+' 1/6', 0, False);
1381 //SetLength(gCollideMap, gMapInfo.Height+1);
1382 //for a := 0 to High(gCollideMap) do SetLength(gCollideMap[a], gMapInfo.Width+1);
1383 awmSetup();
1384 {$IFDEF HEADLESS}
1385 gpart_dbg_enabled := false;
1386 {$ENDIF}
1387 end;
1390 procedure g_GFX_Free ();
1391 var
1392 a: Integer;
1393 begin
1394 Particles := nil;
1395 SetLength(Particles, MaxParticles);
1396 for a := 0 to High(Particles) do Particles[a].die();
1397 CurrentParticle := 0;
1399 if (OnceAnims <> nil) then
1400 begin
1401 for a := 0 to High(OnceAnims) do OnceAnims[a].Animation.Free();
1402 OnceAnims := nil;
1403 end;
1405 awakeMap := nil;
1406 // why not?
1407 awakeMapH := -1;
1408 awakeMapW := -1;
1409 end;
1412 // ////////////////////////////////////////////////////////////////////////// //
1413 procedure g_GFX_Update ();
1414 var
1415 a: Integer;
1416 w, h: Integer;
1417 len: Integer;
1418 begin
1419 if not gpart_dbg_enabled then exit;
1421 if (Particles <> nil) then
1422 begin
1423 w := gMapInfo.Width;
1424 h := gMapInfo.Height;
1426 len := High(Particles);
1428 for a := 0 to len do
1429 begin
1430 if Particles[a].alive then
1431 begin
1432 with Particles[a] do
1433 begin
1434 if (time = liveTime) then begin die(); continue; end;
1435 if (x+1 >= w) or (y+1 >= h) or (x <= 0) or (y <= 0) then begin die(); end;
1436 think();
1437 end; // with
1438 end; // if
1439 end; // for
1440 end; // Particles <> nil
1442 // clear awake map
1443 awmClear();
1445 if OnceAnims <> nil then
1446 begin
1447 for a := 0 to High(OnceAnims) do
1448 if OnceAnims[a].Animation <> nil then
1449 begin
1450 case OnceAnims[a].AnimType of
1451 ONCEANIM_SMOKE:
1452 begin
1453 if Random(3) = 0 then
1454 OnceAnims[a].x := OnceAnims[a].x-1+Random(3);
1455 if Random(2) = 0 then
1456 OnceAnims[a].y := OnceAnims[a].y-Random(2);
1457 end;
1458 end;
1460 if OnceAnims[a].Animation.Played then
1461 begin
1462 OnceAnims[a].Animation.Free();
1463 OnceAnims[a].Animation := nil;
1464 end
1465 else
1466 OnceAnims[a].Animation.Update();
1467 end;
1468 end;
1469 end;
1472 procedure g_GFX_Draw ();
1473 var
1474 a, len: Integer;
1475 begin
1476 if not gpart_dbg_enabled then exit;
1478 if (Particles <> nil) then
1479 begin
1480 glDisable(GL_TEXTURE_2D);
1481 glPointSize(2);
1483 glEnable(GL_BLEND);
1484 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1486 glBegin(GL_POINTS);
1488 len := High(Particles);
1489 for a := 0 to len do
1490 begin
1491 with Particles[a] do
1492 begin
1493 if alive and (x >= sX) and (y >= sY) and (x <= sX+sWidth) and (sY <= sY+sHeight) then
1494 begin
1495 glColor4ub(red, green, blue, alpha);
1496 glVertex2f(x+0.37, y+0.37);
1497 end;
1498 end;
1499 end;
1501 glEnd();
1503 glDisable(GL_BLEND);
1504 end;
1506 if (OnceAnims <> nil) then
1507 begin
1508 len := High(OnceAnims);
1509 for a := 0 to len do
1510 begin
1511 if (OnceAnims[a].Animation <> nil) then
1512 begin
1513 with OnceAnims[a] do Animation.Draw(x, y, M_NONE);
1514 end;
1515 end;
1516 end;
1517 end;
1520 end.