DEADSOFTWARE

particles: fixed "in liquid" check bug
[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 alive (): Boolean; inline;
125 procedure die (); inline;
126 procedure think (); inline;
127 end;
129 TOnceAnim = record
130 AnimType: Byte;
131 x, y: Integer;
132 Animation: TAnimation;
133 end;
136 var
137 Particles: array of TParticle = nil;
138 OnceAnims: array of TOnceAnim = nil;
139 MaxParticles: Integer = 0;
140 CurrentParticle: Integer = 0;
141 // awakeMap has one bit for each map grid cell; on g_Mark,
142 // corresponding bits will be set, and in `think()` all particles
143 // in marked cells will be awaken
144 awakeMap: packed array of LongWord = nil;
145 awakeMapH: Integer = -1;
146 awakeMapW: Integer = -1;
147 awakeMinX, awakeMinY: Integer;
148 awakeDirty: Boolean = false;
151 // ////////////////////////////////////////////////////////////////////////// //
152 // HACK! using mapgrid
153 procedure awmClear (); inline;
154 begin
155 if awakeDirty and (awakeMapW > 0) then
156 begin
157 FillDWord(awakeMap[0], Length(awakeMap), 0);
158 awakeDirty := false;
159 end;
160 end;
163 procedure awmSetup ();
164 begin
165 assert(mapGrid <> nil);
166 awakeMapW := (mapGrid.gridWidth+mapGrid.tileSize-1) div mapGrid.tileSize;
167 awakeMapW := (awakeMapW+31) div 32; // LongWord has 32 bits ;-)
168 awakeMapH := (mapGrid.gridHeight+mapGrid.tileSize-1) div mapGrid.tileSize;
169 awakeMinX := mapGrid.gridX0;
170 awakeMinY := mapGrid.gridY0;
171 SetLength(awakeMap, awakeMapW*awakeMapH);
172 {$IF DEFINED(D2F_DEBUG)}
173 e_LogWritefln('particle awake map: %sx%s (for grid of size %sx%s)', [awakeMapW, awakeMapH, mapGrid.gridWidth, mapGrid.gridHeight]);
174 {$ENDIF}
175 awakeDirty := true;
176 awmClear();
177 end;
180 function awmIsSet (x, y: Integer): Boolean; inline;
181 begin
182 x := (x-awakeMinX) div mapGrid.tileSize;
183 y := (y-awakeMinY) div mapGrid.tileSize;
184 if (x >= 0) and (y >= 0) and (x div 32 < awakeMapW) and (y < awakeMapH) then
185 begin
186 {$IF DEFINED(D2F_DEBUG)}
187 assert(y*awakeMapW+x div 32 < Length(awakeMap));
188 {$ENDIF}
189 result := ((awakeMap[y*awakeMapW+x div 32] and (LongWord(1) shl (x mod 32))) <> 0);
190 end
191 else
192 begin
193 result := false;
194 end;
195 end;
198 procedure awmSet (x, y: Integer); inline;
199 var
200 v: PLongWord;
201 begin
202 x := (x-awakeMinX) div mapGrid.tileSize;
203 y := (y-awakeMinY) div mapGrid.tileSize;
204 if (x >= 0) and (y >= 0) and (x div 32 < awakeMapW) and (y < awakeMapH) then
205 begin
206 {$IF DEFINED(D2F_DEBUG)}
207 assert(y*awakeMapW+x div 32 < Length(awakeMap));
208 {$ENDIF}
209 v := @awakeMap[y*awakeMapW+x div 32];
210 v^ := v^ or (LongWord(1) shl (x mod 32));
211 awakeDirty := true;
212 end;
213 end;
216 // ////////////////////////////////////////////////////////////////////////// //
217 function TParticle.alive (): Boolean; inline; begin result := (state <> TPartState.Free); end;
218 procedure TParticle.die (); inline; begin state := TPartState.Free; end;
220 // remove velocities and acceleration
221 procedure TParticle.freeze (); inline;
222 begin
223 // stop right there, you criminal scum!
224 velX := 0;
225 velY := 0;
226 accelX := 0;
227 accelY := 0;
228 end;
231 // switch to sleep mode
232 procedure TParticle.sleep (); inline;
233 begin
234 state := TPartState.Sleeping;
235 freeze();
236 end;
239 procedure TParticle.findFloor (force: Boolean=false);
240 var
241 ex: Integer;
242 pan: TPanel;
243 begin
244 if (not force) and (floorY <> Unknown) then exit;
245 // stuck in the wall? rescan, 'cause it can be mplat
246 if (env = TEnvType.EWall) then
247 begin
248 pan := g_Map_PanelAtPoint(x, y, (GridTagObstacle or GridTagLiquid));
249 if (pan <> nil) then
250 begin
251 // either in a wall, or in a liquid
252 if ((pan.tag and GridTagObstacle) <> 0) then
253 begin
254 // we are in the wall, wtf?!
255 floorY := y;
256 env := TEnvType.EWall;
257 floorType := TFloorType.Wall;
258 state := TPartState.Sleeping; // anyway
259 exit;
260 end;
261 // we are in liquid, trace to liquid end
262 env := TEnvType.ELiquid;
263 end;
264 end;
265 // are we in a liquid?
266 if (env = TEnvType.ELiquid) then
267 begin
268 // trace out of the liquid
269 //env := TEnvType.ELiquid;
270 floorType := TFloorType.LiquidOut;
271 //e_LogWritefln('tracing out of a liquid; floorY=%s; y=%s', [floorY, y]);
272 mapGrid.traceOrthoRayWhileIn(ex, floorY, x, y, x, g_Map_MaxY, GridTagLiquid);
273 floorY += 1; // so `floorY` is just out of a liquid
274 //e_LogWritefln(' traced out of a liquid; floorY=%s; y=%s', [floorY, y]);
275 end
276 else
277 begin
278 // in the air
279 assert(env = TEnvType.EAir);
280 //env := TEnvType.EAir;
281 pan := g_Map_traceToNearest(x, y, x, g_Map_MaxY, (GridTagObstacle or GridTagLiquid), @ex, @floorY);
282 if (pan <> nil) then
283 begin
284 // wall or liquid
285 if ((pan.tag and GridTagObstacle) <> 0) then
286 begin
287 // wall
288 floorType := TFloorType.Wall;
289 end
290 else
291 begin
292 // liquid
293 floorType := TFloorType.LiquidIn; // entering liquid
294 floorY += 1; // so `floorY` is just in a liquid
295 end;
296 end
297 else
298 begin
299 // out of the level; assume wall, but it doesn't really matter
300 floorType := TFloorType.Wall;
301 floorY := g_Map_MaxY+2;
302 end;
303 end;
304 end;
307 procedure TParticle.findCeiling (force: Boolean=false);
308 var
309 ex: Integer;
310 begin
311 if (not force) and (ceilingY <> Unknown) then exit;
312 if (nil = g_Map_traceToNearest(x, y, x, g_Map_MinY, GridTagObstacle, @ex, @ceilingY)) then
313 begin
314 ceilingY := g_Map_MinY-2;
315 end;
316 end;
319 procedure TParticle.think (); inline;
320 begin
321 // awake sleeping particle, if necessary
322 if awakeDirty then
323 begin
324 case state of
325 TPartState.Sleeping, TPartState.Stuck:
326 if awmIsSet(x, y) then
327 begin
328 state := TPartState.Normal;
329 floorY := Unknown;
330 ceilingY := Unknown;
331 if (velY = 0) then velY := 0.1;
332 if (accelY = 0) then accelY := 0.5;
333 end;
334 end;
335 end;
336 case particleType of
337 TPartType.Blood, TPartType.Water: thinkerBloodAndWater();
338 TPartType.Spark: thinkerSpark();
339 TPartType.Bubbles: thinkerBubble();
340 end;
341 end;
344 // ////////////////////////////////////////////////////////////////////////// //
345 procedure TParticle.thinkerBloodAndWater ();
346 procedure stickToCeiling ();
347 begin
348 state := TPartState.Stuck;
349 stickDX := 0;
350 freeze();
351 ceilingY := y; // yep
352 end;
354 procedure stickToWall (dx: Integer);
355 var
356 ex: Integer;
357 begin
358 state := TPartState.Stuck;
359 if (dX > 0) then stickDX := 1 else stickDX := -1;
360 freeze();
361 // find next floor transition
362 findFloor();
363 // find `wallEndY`
364 mapGrid.traceOrthoRayWhileIn(ex, wallEndY, x+stickDX, y, x+stickDX, floorY+1, (GridTagWall or GridTagDoor or GridTagStep));
365 end;
367 procedure hitAFloor ();
368 begin
369 state := TPartState.Sleeping; // we aren't moving anymore
370 freeze();
371 floorY := y; // yep
372 floorType := TFloorType.Wall; // yep
373 end;
375 // `true`: didn't, get outa thinker
376 function drip (): Boolean;
377 begin
378 case particleType of
379 TPartType.Blood: result := (Random(200) = 100);
380 TPartType.Water: result := (Random(30) = 15);
381 else raise Exception.Create('internal error in particle engine: drip');
382 end;
383 if result then begin velY := 0.5; accelY := 0.15; end;
384 end;
386 // `true`: affected by air stream
387 function checkAirStreams (): Boolean;
388 var
389 pan: TPanel;
390 begin
391 pan := g_Map_PanelAtPoint(x, y, GridTagLift);
392 result := (pan <> nil);
393 if result then
394 begin
395 if ((pan.PanelType and PANEL_LIFTUP) <> 0) then
396 begin
397 if (velY > -4-Random(3)) then velY -= 0.8;
398 if (abs(velX) > 0.1) then velX -= velX/10.0;
399 velX += (Random-Random)*0.2;
400 accelY := 0.15;
401 end
402 else if ((pan.PanelType and PANEL_LIFTLEFT) <> 0) then
403 begin
404 if (velX > -8-Random(3)) then velX -= 0.8;
405 accelY := 0.15;
406 end
407 else if ((pan.PanelType and PANEL_LIFTRIGHT) <> 0) then
408 begin
409 if (velX < 8+Random(3)) then velX += 0.8;
410 accelY := 0.15;
411 end
412 else
413 begin
414 result := false;
415 end;
416 // awake
417 if result and (state = TPartState.Sleeping) then state := TPartState.Normal;
418 end;
419 end;
421 // switch to freefall mode
422 procedure freefall ();
423 begin
424 state := TPartState.Normal;
425 velY := 0.5;
426 accelY := 0.15;
427 end;
429 procedure applyGravity (inLiquid: Boolean);
430 begin
431 state := TPartState.Normal;
432 if (inLiquid) then
433 begin
434 velY := 0.5;
435 accelY := 0.15;
436 end
437 else
438 begin
439 velY := 0.8;
440 accelY := 0.5;
441 end;
442 end;
444 label
445 _done;
446 var
447 pan: TPanel;
448 dX, dY: SmallInt;
449 ex, ey: Integer;
450 checkEnv: Boolean;
451 begin
452 if not gpart_dbg_phys_enabled then goto _done;
454 if gAdvBlood then
455 begin
456 // still check for air streams when sleeping
457 if (state = TPartState.Sleeping) then begin checkAirStreams(); goto _done; end; // so blood will dissolve
459 // process stuck particles
460 if (state = TPartState.Stuck) then
461 begin
462 // stuck to a ceiling?
463 if (stickDX = 0) then
464 begin
465 // yeah, stuck to a ceiling
466 assert(ceilingY <> Unknown);
467 // dropped from a ceiling?
468 if (y > ceilingY) then
469 begin
470 // yep
471 velY := 0.5;
472 accelY := 0.15;
473 state := TPartState.Normal;
474 end
475 else
476 begin
477 // otherwise, try to drip
478 if drip() then goto _done;
479 end;
480 end
481 else
482 begin
483 // stuck to a wall
484 assert(wallEndY <> Unknown);
485 // floor transition?
486 if (wallEndY <= floorY) and (y >= floorY) then
487 begin
488 y := floorY;
489 case floorType of
490 TFloorType.Wall: // hit the ground
491 begin
492 sleep();
493 goto _done; // nothing to do anymore
494 end;
495 TFloorType.LiquidIn: // entering the liquid
496 begin
497 // rescan, so we'll know when we'll exit the liquid
498 findFloor(true); // force rescan
499 end;
500 TFloorType.LiquidOut: // exiting the liquid
501 begin
502 // rescan, so we'll know when we'll enter something interesting
503 findFloor(true); // force rescan
504 if (floorType = TFloorType.Wall) and (floorY = y) then begin sleep(); goto _done; end;
505 end;
506 end;
507 end;
508 // wall transition?
509 if (floorY <= wallEndY) and (y >= wallEndY) then
510 begin
511 // just unstuck from the wall, switch to freefall mode
512 y := wallEndY;
513 freefall();
514 end
515 else
516 begin
517 // otherwise, try to drip
518 if drip() then goto _done;
519 end;
520 end;
521 // nope, process as usual
522 end;
524 // it is important to have it here
525 dX := round(velX);
526 dY := round(velY);
528 // gravity, if not stuck
529 if (state <> TPartState.Stuck) and (abs(velX) < 0.1) and (abs(velY) < 0.1) then
530 begin
531 if (floorY = Unknown) then findFloor();
532 // floor transition?
533 if (y = floorY) then
534 begin
535 case floorType of
536 TFloorType.Wall: // hit the ground
537 begin
538 // nothing to do
539 end;
540 TFloorType.LiquidIn: // entering the liquid
541 begin
542 // rescan, so we'll know when we'll exit the liquid
543 findFloor(true); // force rescan
544 applyGravity(true);
545 end;
546 TFloorType.LiquidOut: // exiting the liquid
547 begin
548 // rescan, so we'll know when we'll enter something interesting
549 findFloor(true); // force rescan
550 if (floorType <> TFloorType.Wall) or (floorY <> y) then applyGravity(floorType = TFloorType.LiquidIn);
551 end;
552 end;
553 end
554 else
555 begin
556 // looks like we're in the air
557 applyGravity(false);
558 end;
559 end;
561 // trace movement
562 if (dX <> 0) then
563 begin
564 // has some horizontal velocity
565 pan := g_Map_traceToNearest(x, y, x+dX, y+dY, GridTagObstacle, @ex, @ey);
566 checkEnv := (x <> ex);
567 x := ex;
568 y := ey;
569 if checkEnv then
570 begin
571 // dunno yet
572 floorY := Unknown;
573 ceilingY := Unknown;
574 // check environment (air/liquid)
575 if (g_Map_PanelAtPoint(x, y, GridTagLiquid) <> nil) then env := TEnvType.ELiquid else env := TEnvType.EAir;
576 end;
577 if (pan <> nil) then
578 begin
579 // we stuck
580 // the only case when we can have both ceiling and wall is corner; stick to wall in this case
581 // check if we stuck to a wall
582 if (dX < 0) then dX := -1 else dX := 1;
583 if (g_Map_PanelAtPoint(x+dX, y, GridTagObstacle) <> nil) then
584 begin
585 // stuck to a wall
586 stickToWall(dX);
587 end
588 else
589 begin
590 // stuck to a ceiling
591 stickToCeiling();
592 end;
593 end;
594 end
595 else if (dY <> 0) then
596 begin
597 // has only vertical velocity
598 if (dY < 0) then
599 begin
600 // flying up
601 if (ceilingY = Unknown) then findCeiling(); // need to do this anyway
602 y += dY;
603 if (y <= ceilingY) then begin y := ceilingY; stickToCeiling(); end; // oops, hit a ceiling
604 // environment didn't changed
605 end
606 else
607 begin
608 while (dY > 0) do
609 begin
610 // falling down
611 if (floorY = Unknown) then findFloor(); // need to do this anyway
612 if (floorType = TFloorType.LiquidOut) then env := TEnvType.ELiquid else env := TEnvType.EAir;
613 y += dY;
614 //e_LogWritefln('floorY=%s; newy=%s; dY=%s; floorType=%s', [floorY, y, dY, floorType]);
615 if (y >= floorY) then
616 begin
617 // floor transition
618 dY := y-floorY;
619 y := floorY;
620 //e_LogWritefln(' HIT FLOORY: floorY=%s; newy=%s; dY=%s; floorType=%s', [floorY, y, dY, floorType]);
621 case floorType of
622 TFloorType.Wall: // hit the ground
623 begin
624 // environment didn't changed
625 hitAFloor();
626 break; // done with vertical movement
627 end;
628 TFloorType.LiquidIn: // entering the liquid
629 begin
630 // we're entered the liquid
631 env := TEnvType.ELiquid;
632 // rescan, so we'll know when we'll exit the liquid
633 findFloor(true); // force rescan
634 end;
635 TFloorType.LiquidOut: // exiting the liquid
636 begin
637 // we're exited the liquid
638 env := TEnvType.EAir;
639 // rescan, so we'll know when we'll enter something interesting
640 findFloor(true); // force rescan
641 if (floorType = TFloorType.Wall) and (floorY = y) then
642 begin
643 hitAFloor();
644 break; // done with vertical movement
645 end;
646 end;
647 end;
648 end
649 else
650 begin
651 break; // done with vertical movement
652 end;
653 end;
654 end;
655 end;
656 end // if gAdvBlood
657 else
658 begin
659 // simple blood
660 dX := round(velX);
661 dY := round(velY);
662 y += dY;
663 x += dX;
664 if (g_Map_PanelAtPoint(x, y, GridTagObstacle) <> nil) then begin die(); exit; end;
665 end;
667 _done:
668 if (x < g_Map_MinX) or (y < g_Map_MinY) or (x > g_Map_MaxX) or (y > g_Map_MaxY) then begin die(); end;
670 velX += accelX;
671 velY += accelY;
673 // blood will dissolve in other liquids
674 if (particleType = TPartType.Blood) then
675 begin
676 if (env = TEnvType.ELiquid) then
677 begin
678 time += 1;
679 if (liveTime <= 0) then begin die(); exit; end;
680 ex := 255-trunc(255.0*time/liveTime);
681 if (ex <= 10) then begin die(); exit; end;
682 if (ex > 250) then ex := 255;
683 alpha := Byte(ex);
684 end;
685 end
686 else
687 begin
688 // water will disappear in any liquid
689 if (env = TEnvType.ELiquid) then begin die(); exit; end;
690 time += 1;
691 // dry water
692 if (liveTime <= 0) then begin die(); exit; end;
693 ex := 255-trunc(255.0*time/liveTime);
694 if (ex <= 10) then begin die(); exit; end;
695 if (ex > 250) then ex := 255;
696 alpha := Byte(ex);
697 end;
698 end;
701 // ////////////////////////////////////////////////////////////////////////// //
702 procedure g_GFX_SparkVel (fX, fY: Integer; count: Word; vx, vy: Integer; devX, devY: Byte); forward;
704 procedure g_GFX_Blood (fX, fY: Integer; count: Word; vx, vy: Integer;
705 devX, devY: Word; cr, cg, cb: Byte; kind: Byte = BLOOD_NORMAL);
707 function genColor (cbase, crnd: Integer; def: Byte=0): Byte;
708 begin
709 if (cbase > 0) then
710 begin
711 cbase += crnd;
712 if (cbase < 0) then result := 0
713 else if (cbase > 255) then result := 255
714 else result := Byte(cbase);
715 end
716 else
717 begin
718 result := def;
719 end;
720 end;
722 var
723 a: Integer;
724 devX1, devX2, devY1, devY2: Integer;
725 l: Integer;
726 crnd: Integer;
727 pan: TPanel;
728 begin
729 if not gpart_dbg_enabled then exit;
731 if (kind = BLOOD_SPARKS) then
732 begin
733 g_GFX_SparkVel(fX, fY, 2+Random(2), -vx div 2, -vy div 2, devX, devY);
734 exit;
735 end;
737 l := Length(Particles);
738 if (l = 0) then exit;
739 if (count > l) then count := l;
741 devX1 := devX div 2;
742 devX2 := devX+1;
743 devY1 := devY div 2;
744 devY2 := devY+1;
746 for a := 1 to count do
747 begin
748 with Particles[CurrentParticle] do
749 begin
750 x := fX-devX1+Random(devX2);
751 y := fY-devY1+Random(devY2);
753 // check for level bounds
754 if (x < g_Map_MinX) or (y < g_Map_MinY) or (x > g_Map_MaxX) or (y > g_Map_MaxY) then continue;
756 // in what environment we are starting in?
757 pan := g_Map_PanelAtPoint(x, y, (GridTagObstacle or GridTagLiquid));
758 if (pan <> nil) then
759 begin
760 // either in a wall, or in a liquid
761 if ((pan.tag and GridTagObstacle) <> 0) then continue; // don't spawn in walls
762 env := TEnvType.ELiquid;
763 end
764 else
765 begin
766 env := TEnvType.EAir;
767 end;
769 velX := vx+(Random-Random)*3;
770 velY := vy+(Random-Random)*3;
772 if (velY > -4) then
773 begin
774 if (velY-4 < -4) then velY := -4 else velY := velY-4;
775 end;
777 accelX := -sign(velX)*Random/100;
778 accelY := 0.8;
780 crnd := 20*Random(6)-50;
782 red := genColor(cr, CRnd, 0);
783 green := genColor(cg, CRnd, 0);
784 blue := genColor(cb, CRnd, 0);
785 alpha := 255;
787 particleType := TPartType.Blood;
788 state := TPartState.Normal;
789 time := 0;
790 liveTime := 120+Random(40);
791 floorY := Unknown;
792 ceilingY := Unknown;
793 end;
795 if (CurrentParticle >= MaxParticles-1) then CurrentParticle := 0 else CurrentParticle += 1;
796 end;
797 end;
800 procedure g_GFX_Water (fX, fY: Integer; count: Word; fVelX, fVelY: Single; devX, devY, color: Byte;
801 simple: Boolean=false; cr: Byte=0; cg: Byte=0; cb: Byte=0);
802 var
803 a: Integer;
804 devX1, devX2, devY1, devY2: Integer;
805 l: Integer;
806 pan: TPanel;
807 begin
808 if not gpart_dbg_enabled then exit;
810 l := Length(Particles);
811 if (l = 0) then exit;
812 if (count > l) then count := l;
814 if (abs(fVelX) < 3.0) then fVelX := 3.0-6.0*Random;
816 devX1 := devX div 2;
817 devX2 := devX+1;
818 devY1 := devY div 2;
819 devY2 := devY+1;
821 if (not simple) and (color > 3) then color := 0;
823 for a := 1 to count do
824 begin
825 with Particles[CurrentParticle] do
826 begin
827 if not simple then
828 begin
829 x := fX-devX1+Random(devX2);
830 y := fY-devY1+Random(devY2);
832 if (abs(fVelX) < 0.5) then velX := 1.0-2.0*Random else velX := fVelX*Random;
833 if (Random(10) < 7) then velX := -velX;
834 velY := fVelY*Random;
835 accelX := 0.0;
836 accelY := 0.8;
837 end
838 else
839 begin
840 x := fX;
841 y := fY;
843 velX := fVelX;
844 velY := fVelY;
845 accelX := 0.0;
846 accelY := 0.8;
847 end;
849 // check for level bounds
850 if (x < g_Map_MinX) or (y < g_Map_MinY) or (x > g_Map_MaxX) or (y > g_Map_MaxY) then continue;
852 // in what environment we are starting in?
853 pan := g_Map_PanelAtPoint(x, y, (GridTagObstacle or GridTagLiquid));
854 if (pan <> nil) then
855 begin
856 // either in a wall, or in a liquid
857 //if ((pan.tag and GridTagObstacle) <> 0) then continue; // don't spawn in walls
858 //env := TEnvType.ELiquid;
859 continue;
860 end
861 else
862 begin
863 env := TEnvType.EAir;
864 end;
866 // color
867 case color of
868 1: // reddish
869 begin
870 red := 155+Random(9)*10;
871 green := trunc(150*Random);
872 blue := green;
873 end;
874 2: // greenish
875 begin
876 red := trunc(150*Random);
877 green := 175+Random(9)*10;
878 blue := red;
879 end;
880 3: // bluish
881 begin
882 red := trunc(200*Random);
883 green := red;
884 blue := 175+Random(9)*10;
885 end;
886 4: // Ñâîé öâåò, ñâåòëåå
887 begin
888 red := 20+Random(19)*10;
889 green := red;
890 blue := red;
891 red := nmin(red+cr, 255);
892 green := nmin(green+cg, 255);
893 blue := nmin(blue+cb, 255);
894 end;
895 5: // Ñâîé öâåò, òåìíåå
896 begin
897 red := 20+Random(19)*10;
898 green := red;
899 blue := red;
900 red := nmax(cr-red, 0);
901 green := nmax(cg-green, 0);
902 blue := nmax(cb-blue, 0);
903 end;
904 else // grayish
905 begin
906 red := 90+random(12)*10;
907 green := red;
908 blue := red;
909 end;
910 end;
911 alpha := 255;
913 particleType := TPartType.Water;
914 state := TPartState.Normal;
915 time := 0;
916 liveTime := 60+Random(60);
917 floorY := Unknown;
918 ceilingY := Unknown;
919 end;
921 if (CurrentParticle >= MaxParticles-1) then CurrentParticle := 0 else CurrentParticle += 1;
922 end;
923 end;
926 procedure g_GFX_SimpleWater (fX, fY: Integer; count: Word; fVelX, fVelY: Single; defColor, cr, cg, cb: Byte);
927 begin
928 g_GFX_Water(fX, fY, count, 0, 0, 0, 0, defColor, true, cr, cg, cb);
929 end;
932 // ////////////////////////////////////////////////////////////////////////// //
933 procedure TParticle.thinkerBubble ();
934 var
935 dY: Integer;
936 begin
937 dY := round(velY);
939 if (dY <> 0) then
940 begin
941 y += dY;
942 if (dY < 0) then
943 begin
944 if (y <= ceilingY) then begin die(); exit; end;
945 end
946 else
947 begin
948 if (y >= floorY) then begin die(); exit; end;
949 end;
950 if (y < g_Map_MinY) or (y > g_Map_MaxY) then begin die(); exit; end;
951 end;
953 if (velY > -4) then velY += accelY;
955 time += 1;
956 end;
959 {.$DEFINE D2F_DEBUG_BUBBLES}
960 procedure g_GFX_Bubbles (fX, fY: Integer; count: Word; devX, devY: Byte);
961 var
962 a, liquidx: Integer;
963 devX1, devX2, devY1, devY2: Integer;
964 l: Integer;
965 {$IF DEFINED(D2F_DEBUG_BUBBLES)}
966 stt: UInt64;
967 nptr, ptr: Boolean;
968 {$ENDIF}
969 begin
970 if not gpart_dbg_enabled then exit;
972 l := Length(Particles);
973 if (l = 0) then exit;
974 if (count > l) then count := l;
976 devX1 := devX div 2;
977 devX2 := devX+1;
978 devY1 := devY div 2;
979 devY2 := devY+1;
981 for a := 1 to count do
982 begin
983 with Particles[CurrentParticle] do
984 begin
985 x := fX-devX1+Random(devX2);
986 y := fY-devY1+Random(devY2);
988 // check for level bounds
989 if (x < g_Map_MinX) or (y < g_Map_MinY) or (x > g_Map_MaxX) or (y > g_Map_MaxY) then continue;
991 (*
992 // don't spawn bubbles outside of the liquid
993 if not isLiquidAt(X, Y) {ByteBool(gCollideMap[Y, X] and MARK_LIQUID)} then
994 Continue;
995 *)
997 // trace liquid, so we'll know where it ends; do it in 8px steps for speed
998 // tracer will return `false` if we started outside of the liquid
1000 {$IF DEFINED(D2F_DEBUG_BUBBLES)}
1001 stt := curTimeMicro();
1002 ptr := mapGrid.traceOrthoRayWhileIn(liquidx, liquidTopY, x, y, x, 0, GridTagWater or GridTagAcid1 or GridTagAcid2);
1003 stt := curTimeMicro()-stt;
1004 e_LogWritefln('traceOrthoRayWhileIn: time=%s (%s); liquidTopY=%s', [Integer(stt), ptr, liquidTopY]);
1005 //
1006 stt := curTimeMicro();
1007 nptr := g_Map_TraceLiquidNonPrecise(x, y, 0, -8, liquidx, liquidTopY);
1008 stt := curTimeMicro()-stt;
1009 e_LogWritefln('g_Map_TraceLiquidNonPrecise: time=%s (%s); liquidTopY=%s', [Integer(stt), nptr, liquidTopY]);
1010 if not nptr then continue;
1011 {$ELSE}
1012 if not g_Map_TraceLiquidNonPrecise(x, y, 0, -8, liquidx, ceilingY) then continue;
1013 if not g_Map_TraceLiquidNonPrecise(x, y, 0, +8, liquidx, floorY) then continue;
1014 {$ENDIF}
1016 velX := 0;
1017 velY := -1-Random;
1018 accelX := 0;
1019 accelY := velY/10;
1021 red := 255;
1022 green := 255;
1023 blue := 255;
1024 alpha := 255;
1026 state := TPartState.Normal;
1027 particleType := TPartType.Bubbles;
1028 time := 0;
1029 liveTime := 65535;
1030 end;
1032 if (CurrentParticle >= MaxParticles-1) then CurrentParticle := 0 else CurrentParticle += 1;
1033 end;
1034 end;
1037 // ////////////////////////////////////////////////////////////////////////// //
1038 procedure TParticle.thinkerSpark ();
1039 label
1040 _done;
1041 var
1042 dX, dY: SmallInt;
1043 pan: TPanel;
1044 ex, ey: Integer;
1045 begin
1046 if not gpart_dbg_phys_enabled then goto _done;
1048 dX := round(velX);
1049 dY := round(velY);
1051 // apply gravity
1052 if (abs(velX) < 0.1) and (abs(velY) < 0.1) then
1053 begin
1054 velY := 0.8;
1055 accelY := 0.5;
1056 end;
1058 // flying
1059 if (dX <> 0) then
1060 begin
1061 // has some horizontal velocity
1062 pan := g_Map_traceToNearest(x, y, x+dX, y+dY, GridTagObstacle, @ex, @ey);
1063 if (x <> ex) then begin floorY := Unknown; ceilingY := Unknown; end; // dunno yet
1064 x := ex;
1065 y := ey;
1066 if (pan <> nil) then
1067 begin
1068 // hit the wall; falling down vertically
1069 velX := 0;
1070 accelX := 0;
1071 end;
1072 end
1073 else if (dY <> 0) then
1074 begin
1075 // has some vertical velocity
1076 if (dY < 0) then
1077 begin
1078 // flying up
1079 if (ceilingY = Unknown) then findCeiling(); // need to do this anyway
1080 y += dY;
1081 if (y <= ceilingY) then
1082 begin
1083 // oops, hit a ceiling
1084 y := ceilingY;
1085 velY := -velY;
1086 accelY := abs(accelY);
1087 end;
1088 // environment didn't changed
1089 end
1090 else
1091 begin
1092 // falling down
1093 if (floorY = Unknown) then findFloor(); // need to do this anyway
1094 y += dY;
1095 if (y >= floorY) then
1096 begin
1097 // hit something except a floor?
1098 if (floorType <> TFloorType.Wall) then begin die(); exit; end; // yep: just die
1099 // otherwise, go to sleep
1100 y := floorY;
1101 sleep();
1102 // environment didn't changed
1103 end;
1104 end;
1105 end;
1107 _done:
1108 if (x < g_Map_MinX) or (y < g_Map_MinY) or (x > g_Map_MaxX) or (y > g_Map_MaxY) then begin die(); end;
1110 if (velX <> 0.0) then velX += accelX;
1112 if (velY <> 0.0) then
1113 begin
1114 if (accelY < 10) then accelY += 0.08;
1115 velY += accelY;
1116 end;
1118 time += 1;
1119 end;
1122 // ////////////////////////////////////////////////////////////////////////// //
1123 procedure g_GFX_SparkVel (fX, fY: Integer; count: Word; vx, vy: Integer; devX, devY: Byte);
1124 var
1125 a: Integer;
1126 devX1, devX2, devY1, devY2: Integer;
1127 l: Integer;
1128 pan: TPanel;
1129 begin
1130 if not gpart_dbg_enabled then exit;
1132 l := Length(Particles);
1133 if (l = 0) then exit;
1134 if (count > l) then count := l;
1136 devX1 := devX div 2;
1137 devX2 := devX+1;
1138 devY1 := devY div 2;
1139 devY2 := devY+1;
1141 for a := 1 to count do
1142 begin
1143 with Particles[CurrentParticle] do
1144 begin
1145 x := fX-devX1+Random(devX2);
1146 y := fY-devY1+Random(devY2);
1148 // check for level bounds
1149 if (x < g_Map_MinX) or (y < g_Map_MinY) or (x > g_Map_MaxX) or (y > g_Map_MaxY) then continue;
1151 // in what environment we are starting in?
1152 pan := g_Map_PanelAtPoint(x, y, (GridTagObstacle or GridTagLiquid));
1153 if (pan <> nil) then
1154 begin
1155 // either in a wall, or in a liquid
1156 //if ((pan.tag and GridTagObstacle) <> 0) then continue; // don't spawn in walls
1157 //env := TEnvType.ELiquid;
1158 continue;
1159 end
1160 else
1161 begin
1162 env := TEnvType.EAir;
1163 end;
1165 velX := vx+(Random-Random)*3;
1166 velY := vy+(Random-Random)*3;
1168 if (velY > -4) then
1169 begin
1170 if (velY-4 < -4) then velY := -4 else velY := velY-4;
1171 end;
1173 accelX := -sign(velX)*Random/100;
1174 accelY := 0.8;
1176 red := 255;
1177 green := 100+Random(155);
1178 blue := 64;
1179 alpha := 255;
1181 particleType := TPartType.Spark;
1182 state := TPartState.Normal;
1183 time := 0;
1184 liveTime := 30+Random(60);
1185 floorY := Unknown;
1186 ceilingY := Unknown;
1187 end;
1189 if (CurrentParticle >= MaxParticles-1) then CurrentParticle := 0 else CurrentParticle += 1;
1190 end;
1191 end;
1194 procedure g_GFX_Spark (fX, fY: Integer; count: Word; angle: SmallInt; devX, devY: Byte);
1195 var
1196 a: Integer;
1197 b: Single;
1198 devX1, devX2, devY1, devY2: Integer;
1199 baseVelX, baseVelY: Single;
1200 l: Integer;
1201 pan: TPanel;
1202 begin
1203 if not gpart_dbg_enabled then exit;
1205 l := Length(Particles);
1206 if (l = 0) then exit;
1207 if (count > l) then count := l;
1209 angle := 360-angle;
1211 devX1 := devX div 2;
1212 devX2 := devX+1;
1213 devY1 := devY div 2;
1214 devY2 := devY+1;
1216 b := DegToRad(angle);
1217 baseVelX := cos(b);
1218 baseVelY := 1.6*sin(b);
1219 if (abs(baseVelX) < 0.01) then baseVelX := 0.0;
1220 if (abs(baseVelY) < 0.01) then baseVelY := 0.0;
1222 for a := 1 to count do
1223 begin
1224 with Particles[CurrentParticle] do
1225 begin
1226 x := fX-devX1+Random(devX2);
1227 y := fY-devY1+Random(devY2);
1229 // check for level bounds
1230 if (x < g_Map_MinX) or (y < g_Map_MinY) or (x > g_Map_MaxX) or (y > g_Map_MaxY) then continue;
1232 // in what environment we are starting in?
1233 pan := g_Map_PanelAtPoint(x, y, (GridTagObstacle or GridTagLiquid));
1234 if (pan <> nil) then
1235 begin
1236 // either in a wall, or in a liquid
1237 //if ((pan.tag and GridTagObstacle) <> 0) then continue; // don't spawn in walls
1238 //env := TEnvType.ELiquid;
1239 continue;
1240 end
1241 else
1242 begin
1243 env := TEnvType.EAir;
1244 end;
1246 velX := baseVelX*Random;
1247 velY := baseVelY-Random;
1248 accelX := velX/3.0;
1249 accelY := velY/5.0;
1251 red := 255;
1252 green := 100+Random(155);
1253 blue := 64;
1254 alpha := 255;
1256 particleType := TPartType.Spark;
1257 state := TPartState.Normal;
1258 time := 0;
1259 liveTime := 30+Random(60);
1260 floorY := Unknown;
1261 ceilingY := Unknown;
1262 end;
1264 if (CurrentParticle >= MaxParticles-1) then CurrentParticle := 0 else CurrentParticle += 1;
1265 end;
1266 end;
1269 // ////////////////////////////////////////////////////////////////////////// //
1270 procedure g_GFX_SetMax (count: Integer);
1271 var
1272 a: Integer;
1273 begin
1274 if count > 50000 then count := 50000;
1275 if (count < 1) then count := 1;
1276 SetLength(Particles, count);
1277 for a := 0 to High(Particles) do Particles[a].die();
1278 MaxParticles := count;
1279 CurrentParticle := 0;
1280 end;
1283 function g_GFX_GetMax (): Integer;
1284 begin
1285 result := MaxParticles;
1286 end;
1289 function FindOnceAnim (): DWORD;
1290 var
1291 i: Integer;
1292 begin
1293 if OnceAnims <> nil then
1294 for i := 0 to High(OnceAnims) do
1295 if OnceAnims[i].Animation = nil then
1296 begin
1297 Result := i;
1298 Exit;
1299 end;
1301 if OnceAnims = nil then
1302 begin
1303 SetLength(OnceAnims, 16);
1304 Result := 0;
1305 end
1306 else
1307 begin
1308 Result := High(OnceAnims) + 1;
1309 SetLength(OnceAnims, Length(OnceAnims) + 16);
1310 end;
1311 end;
1314 procedure g_GFX_OnceAnim (x, y: Integer; Anim: TAnimation; AnimType: Byte = 0);
1315 var
1316 find_id: DWORD;
1317 begin
1318 if not gpart_dbg_enabled then exit;
1320 if (Anim = nil) then exit;
1322 find_id := FindOnceAnim();
1324 OnceAnims[find_id].AnimType := AnimType;
1325 OnceAnims[find_id].Animation := TAnimation.Create(Anim.FramesID, Anim.Loop, Anim.Speed);
1326 OnceAnims[find_id].Animation.Blending := Anim.Blending;
1327 OnceAnims[find_id].Animation.alpha := Anim.alpha;
1328 OnceAnims[find_id].x := x;
1329 OnceAnims[find_id].y := y;
1330 end;
1333 // ////////////////////////////////////////////////////////////////////////// //
1334 // st: set mark
1335 // t: mark type
1336 // currently unused
1337 procedure g_Mark (x, y, Width, Height: Integer; t: Byte; st: Boolean=true);
1338 var
1339 cx, ex, ey: Integer;
1340 ts: Integer;
1341 begin
1342 if not gpart_dbg_enabled then exit;
1344 if (Width < 1) or (Height < 1) then exit;
1345 // make some border, so we'll hit particles lying around the panel
1346 x -= 1; Width += 2;
1347 y -= 1; Height += 2;
1348 ex := x+Width;
1349 ey := y+Height;
1350 ts := mapGrid.tileSize;
1351 while (y < ey) do
1352 begin
1353 cx := x;
1354 while (cx < ex) do
1355 begin
1356 awmSet(cx, y);
1357 Inc(cx, ts);
1358 end;
1359 Inc(y, ts);
1360 end;
1361 end;
1364 // ////////////////////////////////////////////////////////////////////////// //
1365 procedure g_GFX_Init ();
1366 begin
1367 //g_Game_SetLoadingText(_lc[I_LOAD_COLLIDE_MAP]+' 1/6', 0, False);
1368 //SetLength(gCollideMap, gMapInfo.Height+1);
1369 //for a := 0 to High(gCollideMap) do SetLength(gCollideMap[a], gMapInfo.Width+1);
1370 awmSetup();
1371 {$IFDEF HEADLESS}
1372 gpart_dbg_enabled := false;
1373 {$ENDIF}
1374 end;
1377 procedure g_GFX_Free ();
1378 var
1379 a: Integer;
1380 begin
1381 Particles := nil;
1382 SetLength(Particles, MaxParticles);
1383 for a := 0 to High(Particles) do Particles[a].die();
1384 CurrentParticle := 0;
1386 if (OnceAnims <> nil) then
1387 begin
1388 for a := 0 to High(OnceAnims) do OnceAnims[a].Animation.Free();
1389 OnceAnims := nil;
1390 end;
1392 awakeMap := nil;
1393 // why not?
1394 awakeMapH := -1;
1395 awakeMapW := -1;
1396 end;
1399 // ////////////////////////////////////////////////////////////////////////// //
1400 procedure g_GFX_Update ();
1401 var
1402 a: Integer;
1403 w, h: Integer;
1404 len: Integer;
1405 begin
1406 if not gpart_dbg_enabled then exit;
1408 if (Particles <> nil) then
1409 begin
1410 w := gMapInfo.Width;
1411 h := gMapInfo.Height;
1413 len := High(Particles);
1415 for a := 0 to len do
1416 begin
1417 if Particles[a].alive then
1418 begin
1419 with Particles[a] do
1420 begin
1421 if (time = liveTime) then begin die(); continue; end;
1422 if (x+1 >= w) or (y+1 >= h) or (x <= 0) or (y <= 0) then begin die(); end;
1423 think();
1424 end; // with
1425 end; // if
1426 end; // for
1427 end; // Particles <> nil
1429 // clear awake map
1430 awmClear();
1432 if OnceAnims <> nil then
1433 begin
1434 for a := 0 to High(OnceAnims) do
1435 if OnceAnims[a].Animation <> nil then
1436 begin
1437 case OnceAnims[a].AnimType of
1438 ONCEANIM_SMOKE:
1439 begin
1440 if Random(3) = 0 then
1441 OnceAnims[a].x := OnceAnims[a].x-1+Random(3);
1442 if Random(2) = 0 then
1443 OnceAnims[a].y := OnceAnims[a].y-Random(2);
1444 end;
1445 end;
1447 if OnceAnims[a].Animation.Played then
1448 begin
1449 OnceAnims[a].Animation.Free();
1450 OnceAnims[a].Animation := nil;
1451 end
1452 else
1453 OnceAnims[a].Animation.Update();
1454 end;
1455 end;
1456 end;
1459 procedure g_GFX_Draw ();
1460 var
1461 a, len: Integer;
1462 begin
1463 if not gpart_dbg_enabled then exit;
1465 if (Particles <> nil) then
1466 begin
1467 glDisable(GL_TEXTURE_2D);
1468 glPointSize(2);
1470 glEnable(GL_BLEND);
1471 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1473 glBegin(GL_POINTS);
1475 len := High(Particles);
1476 for a := 0 to len do
1477 begin
1478 with Particles[a] do
1479 begin
1480 if alive and (x >= sX) and (y >= sY) and (x <= sX+sWidth) and (sY <= sY+sHeight) then
1481 begin
1482 glColor4ub(red, green, blue, alpha);
1483 glVertex2f(x+0.37, y+0.37);
1484 end;
1485 end;
1486 end;
1488 glEnd();
1490 glDisable(GL_BLEND);
1491 end;
1493 if (OnceAnims <> nil) then
1494 begin
1495 len := High(OnceAnims);
1496 for a := 0 to len do
1497 begin
1498 if (OnceAnims[a].Animation <> nil) then
1499 begin
1500 with OnceAnims[a] do Animation.Draw(x, y, M_NONE);
1501 end;
1502 end;
1503 end;
1504 end;
1507 end.