DEADSOFTWARE

more particles code; still somewhat buggy with mplats, tho, but i'll take care of...
[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 begin
451 if not gpart_dbg_phys_enabled then goto _done;
453 if gAdvBlood then
454 begin
455 // still check for air streams when sleeping
456 if (state = TPartState.Sleeping) then begin checkAirStreams(); goto _done; end; // so blood will dissolve
458 // process stuck particles
459 if (state = TPartState.Stuck) then
460 begin
461 // stuck to a ceiling?
462 if (stickDX = 0) then
463 begin
464 // yeah, stuck to a ceiling
465 assert(ceilingY <> Unknown);
466 // dropped from a ceiling?
467 if (y > ceilingY) then
468 begin
469 // yep
470 velY := 0.5;
471 accelY := 0.15;
472 state := TPartState.Normal;
473 end
474 else
475 begin
476 // otherwise, try to drip
477 if drip() then goto _done;
478 end;
479 end
480 else
481 begin
482 // stuck to a wall
483 assert(wallEndY <> Unknown);
484 // floor transition?
485 if (wallEndY <= floorY) and (y >= floorY) then
486 begin
487 y := floorY;
488 case floorType of
489 TFloorType.Wall: // hit the ground
490 begin
491 sleep();
492 goto _done; // nothing to do anymore
493 end;
494 TFloorType.LiquidIn: // entering the liquid
495 begin
496 // rescan, so we'll know when we'll exit the liquid
497 findFloor(true); // force rescan
498 end;
499 TFloorType.LiquidOut: // exiting the liquid
500 begin
501 // rescan, so we'll know when we'll enter something interesting
502 findFloor(true); // force rescan
503 if (floorType = TFloorType.Wall) and (floorY = y) then begin sleep(); goto _done; end;
504 end;
505 end;
506 end;
507 // wall transition?
508 if (floorY <= wallEndY) and (y >= wallEndY) then
509 begin
510 // just unstuck from the wall, switch to freefall mode
511 y := wallEndY;
512 freefall();
513 end
514 else
515 begin
516 // otherwise, try to drip
517 if drip() then goto _done;
518 end;
519 end;
520 // nope, process as usual
521 end;
523 // it is important to have it here
524 dX := round(velX);
525 dY := round(velY);
527 // gravity, if not stuck
528 if (state <> TPartState.Stuck) and (abs(velX) < 0.1) and (abs(velY) < 0.1) then
529 begin
530 if (floorY = Unknown) then findFloor();
531 // floor transition?
532 if (y = floorY) then
533 begin
534 case floorType of
535 TFloorType.Wall: // hit the ground
536 begin
537 // nothing to do
538 end;
539 TFloorType.LiquidIn: // entering the liquid
540 begin
541 // rescan, so we'll know when we'll exit the liquid
542 findFloor(true); // force rescan
543 applyGravity(true);
544 end;
545 TFloorType.LiquidOut: // exiting the liquid
546 begin
547 // rescan, so we'll know when we'll enter something interesting
548 findFloor(true); // force rescan
549 if (floorType <> TFloorType.Wall) or (floorY <> y) then applyGravity(floorType = TFloorType.LiquidIn);
550 end;
551 end;
552 end
553 else
554 begin
555 // looks like we're in the air
556 applyGravity(false);
557 end;
558 end;
560 // trace movement
561 if (dX <> 0) then
562 begin
563 // has some horizontal velocity
564 pan := g_Map_traceToNearest(x, y, x+dX, y+dY, GridTagObstacle, @ex, @ey);
565 if (x <> ex) then begin floorY := Unknown; ceilingY := Unknown; end; // dunno yet
566 x := ex;
567 y := ey;
568 if (pan <> nil) then
569 begin
570 // we stuck
571 // the only case when we can have both ceiling and wall is corner; stick to wall in this case
572 // check environment (air/liquid)
573 if (g_Map_PanelAtPoint(x, y, GridTagLiquid) <> nil) then env := TEnvType.ELiquid else env := TEnvType.EAir;
574 // check if we stuck to a wall
575 if (dX < 0) then dX := -1 else dX := 1;
576 if (g_Map_PanelAtPoint(x+dX, y, GridTagObstacle) <> nil) then
577 begin
578 // stuck to a wall
579 stickToWall(dX);
580 end
581 else
582 begin
583 // stuck to a ceiling
584 stickToCeiling();
585 end;
586 end;
587 end
588 else if (dY <> 0) then
589 begin
590 // has only vertical velocity
591 if (dY < 0) then
592 begin
593 // flying up
594 if (ceilingY = Unknown) then findCeiling(); // need to do this anyway
595 y += dY;
596 if (y <= ceilingY) then begin y := ceilingY; stickToCeiling(); end; // oops, hit a ceiling
597 // environment didn't changed
598 end
599 else
600 begin
601 while (dY > 0) do
602 begin
603 // falling down
604 if (floorY = Unknown) then findFloor(); // need to do this anyway
605 y += dY;
606 //e_LogWritefln('floorY=%s; newy=%s; dY=%s; floorType=%s', [floorY, y, dY, floorType]);
607 if (y >= floorY) then
608 begin
609 // floor transition
610 dY := y-floorY;
611 y := floorY;
612 //e_LogWritefln(' HIT FLOORY: floorY=%s; newy=%s; dY=%s; floorType=%s', [floorY, y, dY, floorType]);
613 case floorType of
614 TFloorType.Wall: // hit the ground
615 begin
616 // environment didn't changed
617 hitAFloor();
618 break; // done with vertical movement
619 end;
620 TFloorType.LiquidIn: // entering the liquid
621 begin
622 // we're entered the liquid
623 env := TEnvType.ELiquid;
624 // rescan, so we'll know when we'll exit the liquid
625 findFloor(true); // force rescan
626 end;
627 TFloorType.LiquidOut: // exiting the liquid
628 begin
629 // we're exited the liquid
630 env := TEnvType.EAir;
631 // rescan, so we'll know when we'll enter something interesting
632 findFloor(true); // force rescan
633 if (floorType = TFloorType.Wall) and (floorY = y) then
634 begin
635 hitAFloor();
636 break; // done with vertical movement
637 end;
638 end;
639 end;
640 end
641 else
642 begin
643 break; // done with vertical movement
644 end;
645 end;
646 end;
647 end;
648 end // if gAdvBlood
649 else
650 begin
651 // simple blood
652 dX := round(velX);
653 dY := round(velY);
654 y += dY;
655 x += dX;
656 if (g_Map_PanelAtPoint(x, y, GridTagObstacle) <> nil) then begin die(); exit; end;
657 end;
659 _done:
660 if (x < g_Map_MinX) or (y < g_Map_MinY) or (x > g_Map_MaxX) or (y > g_Map_MaxY) then begin die(); end;
662 velX += accelX;
663 velY += accelY;
665 // blood will dissolve in other liquids
666 if (particleType = TPartType.Blood) then
667 begin
668 if (env = TEnvType.ELiquid) then
669 begin
670 time += 1;
671 if (liveTime <= 0) then begin die(); exit; end;
672 ex := 255-trunc(255.0*time/liveTime);
673 if (ex >= 250) then begin die(); exit; end;
674 if (ex < 0) then ex := 0;
675 alpha := Byte(ex);
676 end;
677 end
678 else
679 begin
680 // water will disappear in water (?)
681 if (env = TEnvType.ELiquid) then die();
682 time += 1;
683 end;
684 end;
687 // ////////////////////////////////////////////////////////////////////////// //
688 procedure g_GFX_SparkVel (fX, fY: Integer; count: Word; vx, vy: Integer; devX, devY: Byte); forward;
690 procedure g_GFX_Blood (fX, fY: Integer; count: Word; vx, vy: Integer;
691 devX, devY: Word; cr, cg, cb: Byte; kind: Byte = BLOOD_NORMAL);
693 function genColor (cbase, crnd: Integer; def: Byte=0): Byte;
694 begin
695 if (cbase > 0) then
696 begin
697 cbase += crnd;
698 if (cbase < 0) then result := 0
699 else if (cbase > 255) then result := 255
700 else result := Byte(cbase);
701 end
702 else
703 begin
704 result := def;
705 end;
706 end;
708 var
709 a: Integer;
710 devX1, devX2, devY1, devY2: Integer;
711 l: Integer;
712 crnd: Integer;
713 pan: TPanel;
714 begin
715 if not gpart_dbg_enabled then exit;
717 if (kind = BLOOD_SPARKS) then
718 begin
719 g_GFX_SparkVel(fX, fY, 2+Random(2), -vx div 2, -vy div 2, devX, devY);
720 exit;
721 end;
723 l := Length(Particles);
724 if (l = 0) then exit;
725 if (count > l) then count := l;
727 devX1 := devX div 2;
728 devX2 := devX+1;
729 devY1 := devY div 2;
730 devY2 := devY+1;
732 for a := 1 to count do
733 begin
734 with Particles[CurrentParticle] do
735 begin
736 x := fX-devX1+Random(devX2);
737 y := fY-devY1+Random(devY2);
739 // check for level bounds
740 if (x < g_Map_MinX) or (y < g_Map_MinY) or (x > g_Map_MaxX) or (y > g_Map_MaxY) then continue;
742 // in what environment we are starting in?
743 pan := g_Map_PanelAtPoint(x, y, (GridTagObstacle or GridTagLiquid));
744 if (pan <> nil) then
745 begin
746 // either in a wall, or in a liquid
747 if ((pan.tag and GridTagObstacle) <> 0) then continue; // don't spawn in walls
748 env := TEnvType.ELiquid;
749 end
750 else
751 begin
752 env := TEnvType.EAir;
753 end;
755 velX := vx+(Random-Random)*3;
756 velY := vy+(Random-Random)*3;
758 if (velY > -4) then
759 begin
760 if (velY-4 < -4) then velY := -4 else velY := velY-4;
761 end;
763 accelX := -sign(velX)*Random/100;
764 accelY := 0.8;
766 crnd := 20*Random(6)-50;
768 red := genColor(cr, CRnd, 0);
769 green := genColor(cg, CRnd, 0);
770 blue := genColor(cb, CRnd, 0);
771 alpha := 255;
773 particleType := TPartType.Blood;
774 state := TPartState.Normal;
775 time := 0;
776 liveTime := 120+Random(40);
777 floorY := Unknown;
778 ceilingY := Unknown;
779 end;
781 if (CurrentParticle >= MaxParticles-1) then CurrentParticle := 0 else CurrentParticle += 1;
782 end;
783 end;
786 procedure g_GFX_Water (fX, fY: Integer; count: Word; fVelX, fVelY: Single; devX, devY, color: Byte;
787 simple: Boolean=false; cr: Byte=0; cg: Byte=0; cb: Byte=0);
788 var
789 a: Integer;
790 devX1, devX2, devY1, devY2: Integer;
791 l: Integer;
792 pan: TPanel;
793 begin
794 if not gpart_dbg_enabled then exit;
796 l := Length(Particles);
797 if (l = 0) then exit;
798 if (count > l) then count := l;
800 if (abs(fVelX) < 3.0) then fVelX := 3.0-6.0*Random;
802 devX1 := devX div 2;
803 devX2 := devX+1;
804 devY1 := devY div 2;
805 devY2 := devY+1;
807 if (not simple) and (color > 3) then color := 0;
809 for a := 1 to count do
810 begin
811 with Particles[CurrentParticle] do
812 begin
813 if not simple then
814 begin
815 x := fX-devX1+Random(devX2);
816 y := fY-devY1+Random(devY2);
818 if (abs(fVelX) < 0.5) then velX := 1.0-2.0*Random else velX := fVelX*Random;
819 if (Random(10) < 7) then velX := -velX;
820 velY := fVelY*Random;
821 accelX := 0.0;
822 accelY := 0.8;
823 end
824 else
825 begin
826 x := fX;
827 y := fY;
829 velX := fVelX;
830 velY := fVelY;
831 accelX := 0.0;
832 accelY := 0.8;
833 end;
835 // check for level bounds
836 if (x < g_Map_MinX) or (y < g_Map_MinY) or (x > g_Map_MaxX) or (y > g_Map_MaxY) then continue;
838 // in what environment we are starting in?
839 pan := g_Map_PanelAtPoint(x, y, (GridTagObstacle or GridTagLiquid));
840 if (pan <> nil) then
841 begin
842 // either in a wall, or in a liquid
843 //if ((pan.tag and GridTagObstacle) <> 0) then continue; // don't spawn in walls
844 //env := TEnvType.ELiquid;
845 continue;
846 end
847 else
848 begin
849 env := TEnvType.EAir;
850 end;
852 // color
853 case color of
854 1: // reddish
855 begin
856 red := 155+Random(9)*10;
857 green := trunc(150*Random);
858 blue := green;
859 end;
860 2: // greenish
861 begin
862 red := trunc(150*Random);
863 green := 175+Random(9)*10;
864 blue := red;
865 end;
866 3: // bluish
867 begin
868 red := trunc(200*Random);
869 green := red;
870 blue := 175+Random(9)*10;
871 end;
872 4: // Ñâîé öâåò, ñâåòëåå
873 begin
874 red := 20+Random(19)*10;
875 green := red;
876 blue := red;
877 red := nmin(red+cr, 255);
878 green := nmin(green+cg, 255);
879 blue := nmin(blue+cb, 255);
880 end;
881 5: // Ñâîé öâåò, òåìíåå
882 begin
883 red := 20+Random(19)*10;
884 green := red;
885 blue := red;
886 red := nmax(cr-red, 0);
887 green := nmax(cg-green, 0);
888 blue := nmax(cb-blue, 0);
889 end;
890 else // grayish
891 begin
892 red := 90+random(12)*10;
893 green := red;
894 blue := red;
895 end;
896 end;
897 alpha := 255;
899 particleType := TPartType.Water;
900 state := TPartState.Normal;
901 time := 0;
902 liveTime := 60+Random(60);
903 floorY := Unknown;
904 ceilingY := Unknown;
905 end;
907 if (CurrentParticle >= MaxParticles-1) then CurrentParticle := 0 else CurrentParticle += 1;
908 end;
909 end;
912 procedure g_GFX_SimpleWater (fX, fY: Integer; count: Word; fVelX, fVelY: Single; defColor, cr, cg, cb: Byte);
913 begin
914 g_GFX_Water(fX, fY, count, 0, 0, 0, 0, defColor, true, cr, cg, cb);
915 end;
918 // ////////////////////////////////////////////////////////////////////////// //
919 procedure TParticle.thinkerBubble ();
920 var
921 dY: Integer;
922 begin
923 dY := round(velY);
925 if (dY <> 0) then
926 begin
927 y += dY;
928 if (dY < 0) then
929 begin
930 if (y <= ceilingY) then begin die(); exit; end;
931 end
932 else
933 begin
934 if (y >= floorY) then begin die(); exit; end;
935 end;
936 if (y < g_Map_MinY) or (y > g_Map_MaxY) then begin die(); exit; end;
937 end;
939 if (velY > -4) then velY += accelY;
941 time += 1;
942 end;
945 {.$DEFINE D2F_DEBUG_BUBBLES}
946 procedure g_GFX_Bubbles (fX, fY: Integer; count: Word; devX, devY: Byte);
947 var
948 a, liquidx: Integer;
949 devX1, devX2, devY1, devY2: Integer;
950 l: Integer;
951 {$IF DEFINED(D2F_DEBUG_BUBBLES)}
952 stt: UInt64;
953 nptr, ptr: Boolean;
954 {$ENDIF}
955 begin
956 if not gpart_dbg_enabled then exit;
958 l := Length(Particles);
959 if (l = 0) then exit;
960 if (count > l) then count := l;
962 devX1 := devX div 2;
963 devX2 := devX+1;
964 devY1 := devY div 2;
965 devY2 := devY+1;
967 for a := 1 to count do
968 begin
969 with Particles[CurrentParticle] do
970 begin
971 x := fX-devX1+Random(devX2);
972 y := fY-devY1+Random(devY2);
974 // check for level bounds
975 if (x < g_Map_MinX) or (y < g_Map_MinY) or (x > g_Map_MaxX) or (y > g_Map_MaxY) then continue;
977 (*
978 // don't spawn bubbles outside of the liquid
979 if not isLiquidAt(X, Y) {ByteBool(gCollideMap[Y, X] and MARK_LIQUID)} then
980 Continue;
981 *)
983 // trace liquid, so we'll know where it ends; do it in 8px steps for speed
984 // tracer will return `false` if we started outside of the liquid
986 {$IF DEFINED(D2F_DEBUG_BUBBLES)}
987 stt := curTimeMicro();
988 ptr := mapGrid.traceOrthoRayWhileIn(liquidx, liquidTopY, x, y, x, 0, GridTagWater or GridTagAcid1 or GridTagAcid2);
989 stt := curTimeMicro()-stt;
990 e_LogWritefln('traceOrthoRayWhileIn: time=%s (%s); liquidTopY=%s', [Integer(stt), ptr, liquidTopY]);
991 //
992 stt := curTimeMicro();
993 nptr := g_Map_TraceLiquidNonPrecise(x, y, 0, -8, liquidx, liquidTopY);
994 stt := curTimeMicro()-stt;
995 e_LogWritefln('g_Map_TraceLiquidNonPrecise: time=%s (%s); liquidTopY=%s', [Integer(stt), nptr, liquidTopY]);
996 if not nptr then continue;
997 {$ELSE}
998 if not g_Map_TraceLiquidNonPrecise(x, y, 0, -8, liquidx, ceilingY) then continue;
999 if not g_Map_TraceLiquidNonPrecise(x, y, 0, +8, liquidx, floorY) then continue;
1000 {$ENDIF}
1002 velX := 0;
1003 velY := -1-Random;
1004 accelX := 0;
1005 accelY := velY/10;
1007 red := 255;
1008 green := 255;
1009 blue := 255;
1010 alpha := 255;
1012 state := TPartState.Normal;
1013 particleType := TPartType.Bubbles;
1014 time := 0;
1015 liveTime := 65535;
1016 end;
1018 if (CurrentParticle >= MaxParticles-1) then CurrentParticle := 0 else CurrentParticle += 1;
1019 end;
1020 end;
1023 // ////////////////////////////////////////////////////////////////////////// //
1024 procedure TParticle.thinkerSpark ();
1025 label
1026 _done;
1027 var
1028 dX, dY: SmallInt;
1029 pan: TPanel;
1030 ex, ey: Integer;
1031 begin
1032 if not gpart_dbg_phys_enabled then goto _done;
1034 dX := round(velX);
1035 dY := round(velY);
1037 // apply gravity
1038 if (abs(velX) < 0.1) and (abs(velY) < 0.1) then
1039 begin
1040 velY := 0.8;
1041 accelY := 0.5;
1042 end;
1044 // flying
1045 if (dX <> 0) then
1046 begin
1047 // has some horizontal velocity
1048 pan := g_Map_traceToNearest(x, y, x+dX, y+dY, GridTagObstacle, @ex, @ey);
1049 if (x <> ex) then begin floorY := Unknown; ceilingY := Unknown; end; // dunno yet
1050 x := ex;
1051 y := ey;
1052 if (pan <> nil) then
1053 begin
1054 // hit the wall; falling down vertically
1055 velX := 0;
1056 accelX := 0;
1057 end;
1058 end
1059 else if (dY <> 0) then
1060 begin
1061 // has some vertical velocity
1062 if (dY < 0) then
1063 begin
1064 // flying up
1065 if (ceilingY = Unknown) then findCeiling(); // need to do this anyway
1066 y += dY;
1067 if (y <= ceilingY) then
1068 begin
1069 // oops, hit a ceiling
1070 y := ceilingY;
1071 velY := -velY;
1072 accelY := abs(accelY);
1073 end;
1074 // environment didn't changed
1075 end
1076 else
1077 begin
1078 // falling down
1079 if (floorY = Unknown) then findFloor(); // need to do this anyway
1080 y += dY;
1081 if (y >= floorY) then
1082 begin
1083 // hit something except a floor?
1084 if (floorType <> TFloorType.Wall) then begin die(); exit; end; // yep: just die
1085 // otherwise, go to sleep
1086 y := floorY;
1087 sleep();
1088 // environment didn't changed
1089 end;
1090 end;
1091 end;
1093 _done:
1094 if (x < g_Map_MinX) or (y < g_Map_MinY) or (x > g_Map_MaxX) or (y > g_Map_MaxY) then begin die(); end;
1096 if (velX <> 0.0) then velX += accelX;
1098 if (velY <> 0.0) then
1099 begin
1100 if (accelY < 10) then accelY += 0.08;
1101 velY += accelY;
1102 end;
1104 time += 1;
1105 end;
1108 // ////////////////////////////////////////////////////////////////////////// //
1109 procedure g_GFX_SparkVel (fX, fY: Integer; count: Word; vx, vy: Integer; devX, devY: Byte);
1110 var
1111 a: Integer;
1112 devX1, devX2, devY1, devY2: Integer;
1113 l: Integer;
1114 pan: TPanel;
1115 begin
1116 if not gpart_dbg_enabled then exit;
1118 l := Length(Particles);
1119 if (l = 0) then exit;
1120 if (count > l) then count := l;
1122 devX1 := devX div 2;
1123 devX2 := devX+1;
1124 devY1 := devY div 2;
1125 devY2 := devY+1;
1127 for a := 1 to count do
1128 begin
1129 with Particles[CurrentParticle] do
1130 begin
1131 x := fX-devX1+Random(devX2);
1132 y := fY-devY1+Random(devY2);
1134 // check for level bounds
1135 if (x < g_Map_MinX) or (y < g_Map_MinY) or (x > g_Map_MaxX) or (y > g_Map_MaxY) then continue;
1137 // in what environment we are starting in?
1138 pan := g_Map_PanelAtPoint(x, y, (GridTagObstacle or GridTagLiquid));
1139 if (pan <> nil) then
1140 begin
1141 // either in a wall, or in a liquid
1142 //if ((pan.tag and GridTagObstacle) <> 0) then continue; // don't spawn in walls
1143 //env := TEnvType.ELiquid;
1144 continue;
1145 end
1146 else
1147 begin
1148 env := TEnvType.EAir;
1149 end;
1151 velX := vx+(Random-Random)*3;
1152 velY := vy+(Random-Random)*3;
1154 if (velY > -4) then
1155 begin
1156 if (velY-4 < -4) then velY := -4 else velY := velY-4;
1157 end;
1159 accelX := -sign(velX)*Random/100;
1160 accelY := 0.8;
1162 red := 255;
1163 green := 100+Random(155);
1164 blue := 64;
1165 alpha := 255;
1167 particleType := TPartType.Spark;
1168 state := TPartState.Normal;
1169 time := 0;
1170 liveTime := 30+Random(60);
1171 floorY := Unknown;
1172 ceilingY := Unknown;
1173 end;
1175 if (CurrentParticle >= MaxParticles-1) then CurrentParticle := 0 else CurrentParticle += 1;
1176 end;
1177 end;
1180 procedure g_GFX_Spark (fX, fY: Integer; count: Word; angle: SmallInt; devX, devY: Byte);
1181 var
1182 a: Integer;
1183 b: Single;
1184 devX1, devX2, devY1, devY2: Integer;
1185 baseVelX, baseVelY: Single;
1186 l: Integer;
1187 pan: TPanel;
1188 begin
1189 if not gpart_dbg_enabled then exit;
1191 l := Length(Particles);
1192 if (l = 0) then exit;
1193 if (count > l) then count := l;
1195 angle := 360-angle;
1197 devX1 := devX div 2;
1198 devX2 := devX+1;
1199 devY1 := devY div 2;
1200 devY2 := devY+1;
1202 b := DegToRad(angle);
1203 baseVelX := cos(b);
1204 baseVelY := 1.6*sin(b);
1205 if (abs(baseVelX) < 0.01) then baseVelX := 0.0;
1206 if (abs(baseVelY) < 0.01) then baseVelY := 0.0;
1208 for a := 1 to count do
1209 begin
1210 with Particles[CurrentParticle] do
1211 begin
1212 x := fX-devX1+Random(devX2);
1213 y := fY-devY1+Random(devY2);
1215 // check for level bounds
1216 if (x < g_Map_MinX) or (y < g_Map_MinY) or (x > g_Map_MaxX) or (y > g_Map_MaxY) then continue;
1218 // in what environment we are starting in?
1219 pan := g_Map_PanelAtPoint(x, y, (GridTagObstacle or GridTagLiquid));
1220 if (pan <> nil) then
1221 begin
1222 // either in a wall, or in a liquid
1223 //if ((pan.tag and GridTagObstacle) <> 0) then continue; // don't spawn in walls
1224 //env := TEnvType.ELiquid;
1225 continue;
1226 end
1227 else
1228 begin
1229 env := TEnvType.EAir;
1230 end;
1232 velX := baseVelX*Random;
1233 velY := baseVelY-Random;
1234 accelX := velX/3.0;
1235 accelY := velY/5.0;
1237 red := 255;
1238 green := 100+Random(155);
1239 blue := 64;
1240 alpha := 255;
1242 particleType := TPartType.Spark;
1243 state := TPartState.Normal;
1244 time := 0;
1245 liveTime := 30+Random(60);
1246 floorY := Unknown;
1247 ceilingY := Unknown;
1248 end;
1250 if (CurrentParticle >= MaxParticles-1) then CurrentParticle := 0 else CurrentParticle += 1;
1251 end;
1252 end;
1255 // ////////////////////////////////////////////////////////////////////////// //
1256 procedure g_GFX_SetMax (count: Integer);
1257 var
1258 a: Integer;
1259 begin
1260 if count > 50000 then count := 50000;
1261 if (count < 1) then count := 1;
1262 SetLength(Particles, count);
1263 for a := 0 to High(Particles) do Particles[a].die();
1264 MaxParticles := count;
1265 CurrentParticle := 0;
1266 end;
1269 function g_GFX_GetMax (): Integer;
1270 begin
1271 result := MaxParticles;
1272 end;
1275 function FindOnceAnim (): DWORD;
1276 var
1277 i: Integer;
1278 begin
1279 if OnceAnims <> nil then
1280 for i := 0 to High(OnceAnims) do
1281 if OnceAnims[i].Animation = nil then
1282 begin
1283 Result := i;
1284 Exit;
1285 end;
1287 if OnceAnims = nil then
1288 begin
1289 SetLength(OnceAnims, 16);
1290 Result := 0;
1291 end
1292 else
1293 begin
1294 Result := High(OnceAnims) + 1;
1295 SetLength(OnceAnims, Length(OnceAnims) + 16);
1296 end;
1297 end;
1300 procedure g_GFX_OnceAnim (x, y: Integer; Anim: TAnimation; AnimType: Byte = 0);
1301 var
1302 find_id: DWORD;
1303 begin
1304 if not gpart_dbg_enabled then exit;
1306 if (Anim = nil) then exit;
1308 find_id := FindOnceAnim();
1310 OnceAnims[find_id].AnimType := AnimType;
1311 OnceAnims[find_id].Animation := TAnimation.Create(Anim.FramesID, Anim.Loop, Anim.Speed);
1312 OnceAnims[find_id].Animation.Blending := Anim.Blending;
1313 OnceAnims[find_id].Animation.alpha := Anim.alpha;
1314 OnceAnims[find_id].x := x;
1315 OnceAnims[find_id].y := y;
1316 end;
1319 // ////////////////////////////////////////////////////////////////////////// //
1320 // st: set mark
1321 // t: mark type
1322 // currently unused
1323 procedure g_Mark (x, y, Width, Height: Integer; t: Byte; st: Boolean=true);
1324 var
1325 cx, ex, ey: Integer;
1326 ts: Integer;
1327 begin
1328 if not gpart_dbg_enabled then exit;
1330 if (Width < 1) or (Height < 1) then exit;
1331 // make some border, so we'll hit particles lying around the panel
1332 x -= 1; Width += 2;
1333 y -= 1; Height += 2;
1334 ex := x+Width;
1335 ey := y+Height;
1336 ts := mapGrid.tileSize;
1337 while (y < ey) do
1338 begin
1339 cx := x;
1340 while (cx < ex) do
1341 begin
1342 awmSet(cx, y);
1343 Inc(cx, ts);
1344 end;
1345 Inc(y, ts);
1346 end;
1347 end;
1350 // ////////////////////////////////////////////////////////////////////////// //
1351 procedure g_GFX_Init ();
1352 begin
1353 //g_Game_SetLoadingText(_lc[I_LOAD_COLLIDE_MAP]+' 1/6', 0, False);
1354 //SetLength(gCollideMap, gMapInfo.Height+1);
1355 //for a := 0 to High(gCollideMap) do SetLength(gCollideMap[a], gMapInfo.Width+1);
1356 awmSetup();
1357 {$IFDEF HEADLESS}
1358 gpart_dbg_enabled := false;
1359 {$ENDIF}
1360 end;
1363 procedure g_GFX_Free ();
1364 var
1365 a: Integer;
1366 begin
1367 Particles := nil;
1368 SetLength(Particles, MaxParticles);
1369 for a := 0 to High(Particles) do Particles[a].die();
1370 CurrentParticle := 0;
1372 if (OnceAnims <> nil) then
1373 begin
1374 for a := 0 to High(OnceAnims) do OnceAnims[a].Animation.Free();
1375 OnceAnims := nil;
1376 end;
1378 awakeMap := nil;
1379 // why not?
1380 awakeMapH := -1;
1381 awakeMapW := -1;
1382 end;
1385 // ////////////////////////////////////////////////////////////////////////// //
1386 procedure g_GFX_Update ();
1387 var
1388 a: Integer;
1389 w, h: Integer;
1390 len: Integer;
1391 begin
1392 if not gpart_dbg_enabled then exit;
1394 if (Particles <> nil) then
1395 begin
1396 w := gMapInfo.Width;
1397 h := gMapInfo.Height;
1399 len := High(Particles);
1401 for a := 0 to len do
1402 begin
1403 if Particles[a].alive then
1404 begin
1405 with Particles[a] do
1406 begin
1407 if (time = liveTime) then begin die(); continue; end;
1408 if (x+1 >= w) or (y+1 >= h) or (x <= 0) or (y <= 0) then begin die(); end;
1409 think();
1410 end; // with
1411 end; // if
1412 end; // for
1413 end; // Particles <> nil
1415 // clear awake map
1416 awmClear();
1418 if OnceAnims <> nil then
1419 begin
1420 for a := 0 to High(OnceAnims) do
1421 if OnceAnims[a].Animation <> nil then
1422 begin
1423 case OnceAnims[a].AnimType of
1424 ONCEANIM_SMOKE:
1425 begin
1426 if Random(3) = 0 then
1427 OnceAnims[a].x := OnceAnims[a].x-1+Random(3);
1428 if Random(2) = 0 then
1429 OnceAnims[a].y := OnceAnims[a].y-Random(2);
1430 end;
1431 end;
1433 if OnceAnims[a].Animation.Played then
1434 begin
1435 OnceAnims[a].Animation.Free();
1436 OnceAnims[a].Animation := nil;
1437 end
1438 else
1439 OnceAnims[a].Animation.Update();
1440 end;
1441 end;
1442 end;
1445 procedure g_GFX_Draw ();
1446 var
1447 a, len: Integer;
1448 begin
1449 if not gpart_dbg_enabled then exit;
1451 if (Particles <> nil) then
1452 begin
1453 glDisable(GL_TEXTURE_2D);
1454 glPointSize(2);
1456 glEnable(GL_BLEND);
1457 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1459 glBegin(GL_POINTS);
1461 len := High(Particles);
1462 for a := 0 to len do
1463 begin
1464 with Particles[a] do
1465 begin
1466 if alive and (x >= sX) and (y >= sY) and (x <= sX+sWidth) and (sY <= sY+sHeight) then
1467 begin
1468 glColor4ub(red, green, blue, alpha);
1469 glVertex2i(x, y);
1470 end;
1471 end;
1472 end;
1474 glEnd();
1476 glDisable(GL_BLEND);
1477 end;
1479 if (OnceAnims <> nil) then
1480 begin
1481 len := High(OnceAnims);
1482 for a := 0 to len do
1483 begin
1484 if (OnceAnims[a].Animation <> nil) then
1485 begin
1486 with OnceAnims[a] do Animation.Draw(x, y, M_NONE);
1487 end;
1488 end;
1489 end;
1490 end;
1493 end.