DEADSOFTWARE

always search for new state slots
[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, version 3 of the License ONLY.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 *
12 * You should have received a copy of the GNU General Public License
13 * along with this program. If not, see <http://www.gnu.org/licenses/>.
14 *)
15 {$INCLUDE ../shared/a_modes.inc}
16 {.$DEFINE D2F_DEBUG_FALL_MPLAT}
17 {/$DEFINE D2F_DEBUG_PART_AWAKE}
18 unit g_gfx;
20 interface
22 uses
23 e_log, g_textures;
25 const
26 BLOOD_NORMAL = 0;
27 BLOOD_SPARKS = 1;
28 BLOOD_CSPARKS = 2;
29 BLOOD_COMBINE = 3;
31 ONCEANIM_NONE = 0;
32 ONCEANIM_SMOKE = 1;
34 MARK_FREE = 0;
35 MARK_WALL = 1;
36 MARK_WATER = 2;
37 MARK_ACID = 4;
38 MARK_LIFTDOWN = 8;
39 MARK_LIFTUP = 16;
40 MARK_DOOR = 32;
41 MARK_LIFTLEFT = 64;
42 MARK_LIFTRIGHT = 128;
43 MARK_BLOCKED = MARK_WALL or MARK_DOOR;
44 MARK_LIQUID = MARK_WATER or MARK_ACID;
45 MARK_LIFT = MARK_LIFTDOWN or MARK_LIFTUP or MARK_LIFTLEFT or MARK_LIFTRIGHT;
48 procedure g_GFX_Init ();
49 procedure g_GFX_Free ();
51 procedure g_GFX_Blood (fX, fY: Integer; count: Word; vx, vy: Integer;
52 devX, devY: Word; cr, cg, cb: Byte; kind: Byte=BLOOD_NORMAL);
53 procedure g_GFX_Spark (fX, fY: Integer; count: Word; angle: SmallInt; devX, devY: Byte);
54 procedure g_GFX_Water (fX, fY: Integer; count: Word; fVelX, fVelY: Single; devX, devY, color: Byte;
55 simple: Boolean=false; cr: Byte=0; cg: Byte=0; cb: Byte=0);
56 procedure g_GFX_SimpleWater (fX, fY: Integer; count: Word; fVelX, fVelY: Single; defColor, cr, cg, cb: Byte);
57 procedure g_GFX_Bubbles (fX, fY: Integer; count: Word; devX, devY: Byte);
59 procedure g_GFX_SetMax (count: Integer);
60 function g_GFX_GetMax (): Integer;
62 procedure g_GFX_OnceAnim (X, Y: Integer; Anim: TAnimation; AnimType: Byte = 0);
64 procedure g_Mark (x, y, Width, Height: Integer; t: Byte; st: Boolean=true);
66 procedure g_GFX_Update ();
67 procedure g_GFX_Draw ();
70 var
71 gpart_dbg_enabled: Boolean = true;
72 gpart_dbg_phys_enabled: Boolean = true;
75 //WARNING: only for Holmes!
76 function awmIsSetHolmes (x, y: Integer): Boolean; inline;
79 implementation
81 uses
82 {$INCLUDE ../nogl/noGLuses.inc}
83 g_map, g_panel, g_basic, Math, e_graphics,
84 g_options, g_console, SysUtils, g_triggers, MAPDEF,
85 g_game, g_language, g_net, utils, xprofiler;
88 const
89 Unknown = Integer($7fffffff);
92 type
93 TPartType = (Blood, Spark, Bubbles, Water);
94 TPartState = (Free, Normal, Stuck, Sleeping);
95 TFloorType = (Wall, LiquidIn, LiquidOut);
96 // Wall: floorY is just before floor
97 // LiquidIn: floorY is liquid *start* (i.e. just in a liquid)
98 // LiquidOut: floorY is liquid *end* (i.e. just out of a liquid)
99 TEnvType = (EAir, ELiquid, EWall); // where particle is now
101 // note: this MUST be record, so we can keep it in
102 // dynamic array and has sequential memory access pattern
103 PParticle = ^TParticle;
104 TParticle = record
105 x, y: Integer;
106 velX, velY: Single;
107 accelX, accelY: Single;
108 state: TPartState;
109 particleType: TPartType;
110 red, green, blue: Byte;
111 alpha: Byte;
112 time, liveTime, waitTime: Word;
113 stickDX: Integer; // STATE_STICK: -1,1: stuck to a wall; 0: stuck to ceiling
114 justSticked: Boolean; // not used
115 floorY: Integer; // actually, floor-1; `Unknown`: unknown
116 floorType: TFloorType;
117 env: TEnvType; // where particle is now
118 ceilingY: Integer; // actually, ceiling+1; `Unknown`: unknown
119 wallEndY: Integer; // if we stuck to a wall, this is where wall ends
121 //k8: sorry, i have to emulate virtual methods this way, 'cause i haet `Object`
122 procedure thinkerBloodAndWater ();
123 procedure thinkerSpark ();
124 procedure thinkerBubble ();
126 procedure findFloor (force: Boolean=false); // this updates `floorY` if forced or Unknown
127 procedure findCeiling (force: Boolean=false); // this updates `ceilingY` if forced or Unknown
129 procedure freeze (); inline; // remove velocities and acceleration
130 procedure sleep (); inline; // switch to sleep mode
132 function checkAirStreams (): Boolean; // `true`: affected by air stream
134 function alive (): Boolean; inline;
135 procedure die (); inline;
136 procedure think (); inline;
137 end;
139 TOnceAnim = record
140 AnimType: Byte;
141 x, y: Integer;
142 Animation: TAnimation;
143 end;
146 var
147 Particles: array of TParticle = nil;
148 OnceAnims: array of TOnceAnim = nil;
149 MaxParticles: Integer = 0;
150 CurrentParticle: Integer = 0;
151 // awakeMap has one bit for each map grid cell; on g_Mark,
152 // corresponding bits will be set, and in `think()` all particles
153 // in marked cells will be awaken
154 awakeMap: packed array of LongWord = nil;
155 awakeMapH: Integer = -1;
156 awakeMapW: Integer = -1;
157 awakeMinX, awakeMinY: Integer;
158 awakeDirty: Boolean = false;
159 {$IF DEFINED(D2F_DEBUG_PART_AWAKE)}
160 awakeMapHlm: packed array of LongWord = nil;
161 {$ENDIF}
164 // ////////////////////////////////////////////////////////////////////////// //
165 function awmIsSetHolmes (x, y: Integer): Boolean; inline;
166 begin
167 {$IF DEFINED(D2F_DEBUG_PART_AWAKE)}
168 if (Length(awakeMapHlm) = 0) then begin result := false; exit; end;
169 x := (x-awakeMinX) div mapGrid.tileSize;
170 y := (y-awakeMinY) div mapGrid.tileSize;
171 if (x >= 0) and (y >= 0) and (x div 32 < awakeMapW) and (y < awakeMapH) then
172 begin
173 if (y*awakeMapW+x div 32 < Length(awakeMapHlm)) then
174 begin
175 result := ((awakeMapHlm[y*awakeMapW+x div 32] and (LongWord(1) shl (x mod 32))) <> 0);
176 end
177 else
178 begin
179 result := false;
180 end;
181 end
182 else
183 begin
184 result := false;
185 end;
186 {$ELSE}
187 result := false;
188 {$ENDIF}
189 end;
192 // ////////////////////////////////////////////////////////////////////////// //
193 // HACK! using mapgrid
194 procedure awmClear (); inline;
195 begin
196 {$IF DEFINED(D2F_DEBUG_PART_AWAKE)}
197 if (Length(awakeMap) > 0) then
198 begin
199 if (Length(awakeMapHlm) <> Length(awakeMap)) then SetLength(awakeMapHlm, Length(awakeMap));
200 Move(awakeMap[0], awakeMapHlm[0], Length(awakeMap)*sizeof(awakeMap[0]));
201 end;
202 {$ENDIF}
203 if awakeDirty and (awakeMapW > 0) then
204 begin
205 FillDWord(awakeMap[0], Length(awakeMap), 0);
206 awakeDirty := false;
207 end;
208 end;
211 procedure awmSetup ();
212 begin
213 assert(mapGrid <> nil);
214 awakeMapW := (mapGrid.gridWidth+mapGrid.tileSize-1) div mapGrid.tileSize;
215 awakeMapW := (awakeMapW+31) div 32; // LongWord has 32 bits ;-)
216 awakeMapH := (mapGrid.gridHeight+mapGrid.tileSize-1) div mapGrid.tileSize;
217 awakeMinX := mapGrid.gridX0;
218 awakeMinY := mapGrid.gridY0;
219 SetLength(awakeMap, awakeMapW*awakeMapH);
220 {$IF DEFINED(D2F_DEBUG_PART_AWAKE)}
221 SetLength(awakeMapHlm, awakeMapW*awakeMapH);
222 FillDWord(awakeMapHlm[0], Length(awakeMapHlm), 0);
223 {$ENDIF}
224 //{$IF DEFINED(D2F_DEBUG)}
225 e_LogWritefln('particle awake map: %sx%s (for grid of size %sx%s)', [awakeMapW, awakeMapH, mapGrid.gridWidth, mapGrid.gridHeight]);
226 //{$ENDIF}
227 awakeDirty := true;
228 awmClear();
229 end;
232 function awmIsSet (x, y: Integer): Boolean; inline;
233 begin
234 x := (x-awakeMinX) div mapGrid.tileSize;
235 y := (y-awakeMinY) div mapGrid.tileSize;
236 if (x >= 0) and (y >= 0) and (x div 32 < awakeMapW) and (y < awakeMapH) then
237 begin
238 {$IF DEFINED(D2F_DEBUG)}
239 assert(y*awakeMapW+x div 32 < Length(awakeMap));
240 {$ENDIF}
241 result := ((awakeMap[y*awakeMapW+x div 32] and (LongWord(1) shl (x mod 32))) <> 0);
242 end
243 else
244 begin
245 result := false;
246 end;
247 end;
250 procedure awmSet (x, y: Integer); inline;
251 var
252 v: PLongWord;
253 begin
254 x := (x-awakeMinX) div mapGrid.tileSize;
255 y := (y-awakeMinY) div mapGrid.tileSize;
256 if (x >= 0) and (y >= 0) and (x div 32 < awakeMapW) and (y < awakeMapH) then
257 begin
258 {$IF DEFINED(D2F_DEBUG)}
259 assert(y*awakeMapW+x div 32 < Length(awakeMap));
260 {$ENDIF}
261 v := @awakeMap[y*awakeMapW+x div 32];
262 v^ := v^ or (LongWord(1) shl (x mod 32));
263 awakeDirty := true;
264 end;
265 end;
268 // ////////////////////////////////////////////////////////////////////////// //
269 // st: set mark
270 // t: mark type
271 // currently unused
272 procedure g_Mark (x, y, Width, Height: Integer; t: Byte; st: Boolean=true);
273 const Extrude = 1;
274 var
275 dx, dy, ex, ey: Integer;
276 v: PLongWord;
277 begin
278 if (not gpart_dbg_enabled) or (not gpart_dbg_phys_enabled) then exit;
279 if (awakeMapW < 1) or (awakeMapH < 1) then exit;
281 if (Width < 1) or (Height < 1) then exit;
283 // make some border, so we'll hit particles around the panel
284 ex := x+Width+Extrude-1-awakeMinX;
285 ey := y+Height+Extrude-1-awakeMinY;
286 x := (x-Extrude)-awakeMinX;
287 y := (y-Extrude)-awakeMinY;
289 x := x div mapGrid.tileSize;
290 y := y div mapGrid.tileSize;
291 ex := ex div mapGrid.tileSize;
292 ey := ey div mapGrid.tileSize;
294 // has something to do?
295 if (ex < 0) or (ey < 0) or (x >= awakeMapW*32) or (y >= awakeMapH) then exit;
296 if (x < 0) then x := 0;
297 if (y < 0) then y := 0;
298 if (ex >= awakeMapW*32) then ex := awakeMapW*32-1;
299 if (ey >= awakeMapH) then ey := awakeMapH;
301 awakeDirty := true;
302 for dy := y to ey do
303 begin
304 for dx := x to ex do
305 begin
306 {$IF DEFINED(D2F_DEBUG)}
307 assert((dx >= 0) and (dy >= 0) and (dx div 32 < awakeMapW) and (dy < awakeMapH));
308 assert(dy*awakeMapW+dx div 32 < Length(awakeMap));
309 {$ENDIF}
310 v := @awakeMap[dy*awakeMapW+dx div 32];
311 v^ := v^ or (LongWord(1) shl (dx mod 32));
312 end;
313 end;
314 end;
317 // ////////////////////////////////////////////////////////////////////////// //
318 function TParticle.alive (): Boolean; inline; begin result := (state <> TPartState.Free); end;
319 procedure TParticle.die (); inline; begin state := TPartState.Free; end;
321 // remove velocities and acceleration
322 procedure TParticle.freeze (); inline;
323 begin
324 // stop right there, you criminal scum!
325 velX := 0;
326 velY := 0;
327 accelX := 0;
328 accelY := 0;
329 end;
332 // `true`: affected by air stream
333 function TParticle.checkAirStreams (): Boolean;
334 var
335 pan: TPanel;
336 r: Integer;
337 begin
338 pan := g_Map_PanelAtPoint(x, y, GridTagLift);
339 result := (pan <> nil) and WordBool(pan.PanelType and (PANEL_LIFTUP or PANEL_LIFTDOWN or PANEL_LIFTLEFT or PANEL_LIFTRIGHT));
340 r := Random(3);
341 if result then
342 begin
343 case pan.LiftType of
344 LIFTTYPE_UP:
345 begin
346 if (velY > -1-r) then velY -= 0.8;
347 if (abs(velX) > 0.1) then velX -= velX/10.0;
348 velX += (Random-Random)*0.2;
349 accelY := 0.15;
350 end;
351 LIFTTYPE_DOWN:
352 begin
353 if (velY < 1+r) then velY += 0.8;
354 accelY := 0.15;
355 end;
356 LIFTTYPE_LEFT:
357 begin
358 if (velX > -8-r) then velX -= (8+r) div 2;
359 accelY := 0.15;
360 end;
361 LIFTTYPE_RIGHT:
362 begin
363 if (velX < 8+r) then velX += (8+r) div 2;
364 accelY := 0.15;
365 end;
366 else
367 result := false;
368 end;
369 // awake
370 if result and (state = TPartState.Sleeping) then state := TPartState.Normal;
371 end;
372 end;
375 // switch to sleep mode
376 procedure TParticle.sleep (); inline;
377 begin
378 if not checkAirStreams() then
379 begin
380 state := TPartState.Sleeping;
381 freeze();
382 end;
383 end;
386 procedure TParticle.findFloor (force: Boolean=false);
387 var
388 ex: Integer;
389 pan: TPanel;
390 begin
391 if (not force) and (floorY <> Unknown) then exit;
392 // stuck in the wall? rescan, 'cause it can be mplat
393 if (env = TEnvType.EWall) then
394 begin
395 pan := g_Map_PanelAtPoint(x, y, (GridTagObstacle or GridTagLiquid));
396 if (pan <> nil) then
397 begin
398 // either in a wall, or in a liquid
399 if ((pan.tag and GridTagObstacle) <> 0) then
400 begin
401 // we are in the wall, wtf?!
402 floorY := y;
403 env := TEnvType.EWall;
404 floorType := TFloorType.Wall;
405 state := TPartState.Sleeping; // anyway
406 exit;
407 end;
408 // we are in liquid, trace to liquid end
409 env := TEnvType.ELiquid;
410 end;
411 end;
412 // are we in a liquid?
413 if (env = TEnvType.ELiquid) then
414 begin
415 // trace out of the liquid
416 //env := TEnvType.ELiquid;
417 floorType := TFloorType.LiquidOut;
418 //e_LogWritefln('tracing out of a liquid; floorY=%s; y=%s', [floorY, y]);
419 mapGrid.traceOrthoRayWhileIn(ex, floorY, x, y, x, g_Map_MaxY, GridTagLiquid);
420 floorY += 1; // so `floorY` is just out of a liquid
421 //e_LogWritefln(' traced out of a liquid; floorY=%s; y=%s', [floorY, y]);
422 end
423 else
424 begin
425 // in the air
426 assert(env = TEnvType.EAir);
427 //env := TEnvType.EAir;
428 pan := g_Map_traceToNearest(x, y, x, g_Map_MaxY, (GridTagObstacle or GridTagLiquid), @ex, @floorY);
429 if (pan <> nil) then
430 begin
431 // wall or liquid
432 if ((pan.tag and GridTagObstacle) <> 0) then
433 begin
434 // wall
435 floorType := TFloorType.Wall;
436 end
437 else
438 begin
439 // liquid
440 floorType := TFloorType.LiquidIn; // entering liquid
441 floorY += 1; // so `floorY` is just in a liquid
442 end;
443 end
444 else
445 begin
446 // out of the level; assume wall, but it doesn't really matter
447 floorType := TFloorType.Wall;
448 floorY := g_Map_MaxY+2;
449 end;
450 end;
451 end;
454 procedure TParticle.findCeiling (force: Boolean=false);
455 var
456 ex: Integer;
457 begin
458 if (not force) and (ceilingY <> Unknown) then exit;
459 if (nil = g_Map_traceToNearest(x, y, x, g_Map_MinY, GridTagSolid, @ex, @ceilingY)) then
460 begin
461 ceilingY := g_Map_MinY-2;
462 end;
463 end;
466 procedure TParticle.think (); inline;
467 procedure awake ();
468 begin
469 if (state = TPartState.Stuck) then
470 begin
471 //writeln('awaking particle at (', x, ',', y, ')');
472 if (stickDX = 0) then
473 begin
474 state := TPartState.Normal; // stuck to a ceiling
475 end
476 else
477 begin
478 // stuck to a wall, check if wall is still there
479 if (wallEndY <> Unknown) then
480 begin
481 wallEndY := Unknown;
482 if (g_Map_PanelAtPoint(x+stickDX, y, GridTagObstacle) = nil) then
483 begin
484 // a wall was moved out, start falling
485 state := TPartState.Normal;
486 if (velY = 0) then velY := 0.1;
487 if (accelY = 0) then accelY := 0.5;
488 end;
489 end;
490 end;
491 end
492 else
493 begin
494 state := TPartState.Normal;
495 if (velY = 0) then velY := 0.1;
496 if (accelY = 0) then accelY := 0.5;
497 end;
498 floorY := Unknown;
499 ceilingY := Unknown;
500 end;
502 begin
503 // awake sleeping particle, if necessary
504 if awakeDirty then
505 begin
506 if awmIsSet(x, y) then awake();
508 case state of
509 TPartState.Sleeping, TPartState.Stuck:
510 if awmIsSet(x, y) then awake();
511 else
512 if (env = TEnvType.EWall) and awmIsSet(x, y) then awake();
513 end;
515 end;
516 case particleType of
517 TPartType.Blood, TPartType.Water: thinkerBloodAndWater();
518 TPartType.Spark: thinkerSpark();
519 TPartType.Bubbles: thinkerBubble();
520 end;
521 end;
524 // ////////////////////////////////////////////////////////////////////////// //
525 procedure TParticle.thinkerBloodAndWater ();
526 procedure stickToCeiling ();
527 begin
528 state := TPartState.Stuck;
529 stickDX := 0;
530 freeze();
531 ceilingY := y; // yep
532 end;
534 procedure stickToWall (dx: Integer);
535 var
536 ex: Integer;
537 begin
538 state := TPartState.Stuck;
539 if (dx > 0) then stickDX := 1 else stickDX := -1;
540 freeze();
541 // find next floor transition
542 findFloor();
543 // find `wallEndY`
544 mapGrid.traceOrthoRayWhileIn(ex, wallEndY, x+stickDX, y, x+stickDX, floorY+1, (GridTagWall or GridTagDoor or GridTagStep));
545 end;
547 procedure hitAFloor ();
548 begin
549 state := TPartState.Sleeping; // we aren't moving anymore
550 freeze();
551 floorY := y; // yep
552 floorType := TFloorType.Wall; // yep
553 end;
555 // `true`: didn't, get outa thinker
556 function drip (): Boolean;
557 begin
558 case particleType of
559 TPartType.Blood: result := (Random(200) = 100);
560 TPartType.Water: result := (Random(30) = 15);
561 else raise Exception.Create('internal error in particle engine: drip');
562 end;
563 if result then
564 begin
565 velY := 0.5;
566 accelY := 0.15;
567 // if we're falling from ceiling, switch to normal mode
568 if (state = TPartState.Stuck) and (stickDX = 0) then state := TPartState.Normal;
569 end;
570 end;
572 // switch to freefall mode
573 procedure freefall ();
574 begin
575 state := TPartState.Normal;
576 velY := 0.5;
577 accelY := 0.15;
578 end;
580 procedure applyGravity (inLiquid: Boolean);
581 begin
582 state := TPartState.Normal;
583 if inLiquid then
584 begin
585 velY := 0.5;
586 accelY := 0.15;
587 end
588 else
589 begin
590 velY := 0.8;
591 accelY := 0.5;
592 end;
593 end;
595 label
596 _done, _gravityagain, _stuckagain;
597 var
598 pan: TPanel;
599 dx, dy: SmallInt;
600 ex, ey: Integer;
601 checkEnv, inAir, inStep: Boolean;
602 floorJustTraced: Boolean;
603 {$IF DEFINED(D2F_DEBUG_FALL_MPLAT)}
604 oldFloorY: Integer;
605 {$ENDIF}
606 begin
607 if not gpart_dbg_phys_enabled then begin x += round(velX); y += round(velY); goto _done; end;
609 if gAdvBlood then
610 begin
611 // still check for air streams when sleeping (no)
612 if (state = TPartState.Sleeping) then begin {checkAirStreams();} goto _done; end; // so blood will dissolve
614 // process stuck particles
615 if (state = TPartState.Stuck) then
616 begin
617 // stuck to a ceiling?
618 if (stickDX = 0) then
619 begin
620 // yeah, stuck to a ceiling
621 if (ceilingY = Unknown) then findCeiling();
622 // dropped from a ceiling?
623 if (y > ceilingY) then
624 begin
625 // yep
626 velY := 0.5;
627 accelY := 0.15;
628 state := TPartState.Normal;
629 end
630 else
631 begin
632 // otherwise, try to drip
633 if drip() then goto _done;
634 end;
635 end
636 else
637 begin
638 // stuck to a wall
639 if (wallEndY = Unknown) then
640 begin
641 // this can happen if mplat was moved out; find new `wallEndY`
642 findFloor(true); // force trace, just in case
643 if (floorType = TFloorType.LiquidOut) then env := TEnvType.ELiquid else env := TEnvType.EAir;
644 mapGrid.traceOrthoRayWhileIn(ex, wallEndY, x+stickDX, y, x+stickDX, floorY+1, (GridTagWall or GridTagDoor or GridTagStep));
645 end;
646 _stuckagain:
647 // floor transition?
648 if (wallEndY <= floorY) and (y >= floorY) then
649 begin
650 y := floorY;
651 case floorType of
652 TFloorType.Wall: // hit the ground
653 begin
654 // check if our ground wasn't moved since the last scan
655 findFloor(true); // force trace
656 if (y = floorY) then
657 begin
658 sleep();
659 goto _done; // nothing to do anymore
660 end;
661 // otherwise, do it again
662 goto _stuckagain;
663 end;
664 TFloorType.LiquidIn: // entering the liquid
665 begin
666 // rescan, so we'll know when we'll exit the liquid
667 findFloor(true); // force rescan
668 end;
669 TFloorType.LiquidOut: // exiting the liquid
670 begin
671 // rescan, so we'll know when we'll enter something interesting
672 findFloor(true); // force rescan
673 if (floorType = TFloorType.Wall) and (floorY = y) then begin sleep(); goto _done; end;
674 end;
675 end;
676 end;
677 // wall transition?
678 if (floorY <= wallEndY) and (y >= wallEndY) then
679 begin
680 // just unstuck from the wall, switch to freefall mode
681 y := wallEndY;
682 freefall();
683 end
684 else
685 begin
686 // otherwise, try to drip
687 if drip() then goto _done;
688 end;
689 end;
690 // nope, process as usual
691 end;
693 // it is important to have it here
694 dx := round(velX);
695 dy := round(velY);
697 inAir := checkAirStreams();
699 // gravity, if not stuck
700 if (state <> TPartState.Stuck) and (abs(velX) < 0.1) and (abs(velY) < 0.1) then
701 begin
702 floorJustTraced := (floorY = Unknown);
703 if floorJustTraced then findFloor();
704 _gravityagain:
705 // floor transition?
706 if (y = floorY) then
707 begin
708 case floorType of
709 TFloorType.Wall: // hit the ground
710 begin
711 // check if our ground wasn't moved since the last scan
712 if not floorJustTraced then
713 begin
714 findFloor(true); // force trace
715 if (floorType = TFloorType.LiquidOut) then env := TEnvType.ELiquid else env := TEnvType.EAir;
716 if (y <> floorY) then goto _gravityagain;
717 end;
718 // otherwise, nothing to do
719 end;
720 TFloorType.LiquidIn: // entering the liquid
721 begin
722 // rescan, so we'll know when we'll exit the liquid
723 findFloor(true); // force rescan
724 applyGravity(true);
725 end;
726 TFloorType.LiquidOut: // exiting the liquid
727 begin
728 // rescan, so we'll know when we'll enter something interesting
729 findFloor(true); // force rescan
730 if (floorType <> TFloorType.Wall) or (floorY <> y) then applyGravity(floorType = TFloorType.LiquidIn);
731 end;
732 end;
733 end
734 else
735 begin
736 // looks like we're in the air
737 applyGravity(false);
738 end;
739 end;
741 // trace movement
742 if (dx <> 0) then
743 begin
744 // has some horizontal velocity
745 inStep := False;
746 pan := g_Map_traceToNearest(x, y, x+dx, y+dy, GridTagSolid, @ex, @ey);
747 if (pan = nil) and (dy >= 0) then
748 begin
749 // do not stuck inside step
750 if g_Map_traceToNearest(x, y, x, y, GridTagStep, nil, nil) = nil then
751 // check for step panel below
752 pan := g_Map_traceToNearest(x, y, x, y+dy, GridTagStep, nil, @ey);
753 inStep := pan <> nil;
754 if inStep then
755 begin
756 // stick to panel edges
757 if ex < pan.X then
758 ex := pan.X
759 else if ex > pan.X + pan.Width - 1 then
760 ex := pan.X + pan.Width - 1;
761 end;
762 end;
763 checkEnv := (x <> ex);
764 x := ex;
765 y := ey;
766 if checkEnv then
767 begin
768 // dunno yet
769 floorY := Unknown;
770 ceilingY := Unknown;
771 // check environment (air/liquid)
772 if (g_Map_PanelAtPoint(x, y, GridTagLiquid) <> nil) then env := TEnvType.ELiquid else env := TEnvType.EAir;
773 end;
774 if (pan <> nil) then
775 begin
776 if inStep then
777 stickToWall(dx)
778 else
779 begin
780 // we stuck
781 // the only case when we can have both ceiling and wall is corner; stick to wall in this case
782 // check if we stuck to a wall
783 if (dx < 0) then dx := -1 else dx := 1;
784 if (g_Map_PanelAtPoint(x+dx, y, GridTagSolid) <> nil) then
785 begin
786 // stuck to a wall
787 stickToWall(dx);
788 end
789 else
790 begin
791 // stuck to a ceiling
792 stickToCeiling();
793 end;
794 end;
795 end;
796 end
797 else if (dy <> 0) then
798 begin
799 // has only vertical velocity
800 if (dy < 0) then
801 begin
802 // flying up
803 if (ceilingY = Unknown) then findCeiling(); // need to do this anyway
804 y += dy;
805 if (y <= ceilingY) then begin y := ceilingY; stickToCeiling(); end; // oops, hit a ceiling
806 // environment didn't changed
807 end
808 else
809 begin
810 while (dy > 0) do
811 begin
812 // falling down
813 floorJustTraced := (floorY = Unknown);
814 if floorJustTraced then findFloor();
815 if (floorType = TFloorType.LiquidOut) then env := TEnvType.ELiquid else env := TEnvType.EAir;
816 y += dy;
817 //e_LogWritefln('floorY=%s; newy=%s; dY=%s; floorType=%s', [floorY, y, dY, floorType]);
818 if (y >= floorY) then
819 begin
820 // floor transition
821 dy := y-floorY;
822 y := floorY;
823 //e_LogWritefln(' HIT FLOORY: floorY=%s; newy=%s; dY=%s; floorType=%s', [floorY, y, dY, floorType]);
824 case floorType of
825 TFloorType.Wall: // hit the ground
826 begin
827 // check if our ground wasn't moved since the last scan
828 if not floorJustTraced then
829 begin
830 {$IF DEFINED(D2F_DEBUG_FALL_MPLAT)}
831 oldFloorY := floorY;
832 {$ENDIF}
833 findFloor(true); // force trace
834 {$IF DEFINED(D2F_DEBUG_FALL_MPLAT)}
835 if (floorY <> oldFloorY) then
836 begin
837 e_LogWritefln('force rescanning vpart at (%s,%s); oldFloorY=%s; floorY=%s', [x, y, oldFloorY, floorY]);
838 end;
839 {$ENDIF}
840 if (floorType = TFloorType.LiquidOut) then env := TEnvType.ELiquid else env := TEnvType.EAir;
841 if (y <> floorY) then continue;
842 end;
843 // environment didn't changed
844 if not inAir then hitAFloor();
845 break; // done with vertical movement
846 end;
847 TFloorType.LiquidIn: // entering the liquid
848 begin
849 // we're entered the liquid
850 env := TEnvType.ELiquid;
851 // rescan, so we'll know when we'll exit the liquid
852 findFloor(true); // force rescan
853 end;
854 TFloorType.LiquidOut: // exiting the liquid
855 begin
856 // we're exited the liquid
857 env := TEnvType.EAir;
858 // rescan, so we'll know when we'll enter something interesting
859 findFloor(true); // force rescan
860 if (floorType = TFloorType.Wall) and (floorY = y) then
861 begin
862 if not inAir then hitAFloor();
863 break; // done with vertical movement
864 end;
865 end;
866 end;
867 end
868 else
869 begin
870 break; // done with vertical movement
871 end;
872 end;
873 end;
874 end;
875 end // if gAdvBlood
876 else
877 begin
878 // simple blood
879 dx := round(velX);
880 dy := round(velY);
881 y += dy;
882 x += dx;
883 if (g_Map_PanelAtPoint(x, y, GridTagObstacle) <> nil) then begin die(); exit; end;
884 end;
886 _done:
887 if (x < g_Map_MinX) or (y < g_Map_MinY) or (x > g_Map_MaxX) or (y > g_Map_MaxY) then begin die(); end;
889 velX += accelX;
890 velY += accelY;
892 // blood will dissolve in other liquids
893 if (particleType = TPartType.Blood) then
894 begin
895 if (env = TEnvType.ELiquid) then
896 begin
897 if waitTime > 0 then
898 waitTime -= 1
899 else
900 time += 1;
901 if (liveTime <= 0) then begin die(); exit; end;
902 ex := 255-trunc(255.0*time/liveTime);
903 if (ex <= 10) then begin die(); exit; end;
904 if (ex > 250) then ex := 255;
905 alpha := Byte(ex);
906 end;
907 end
908 else
909 begin
910 // water will disappear in any liquid
911 if (env = TEnvType.ELiquid) then begin die(); exit; end;
912 if waitTime > 0 then
913 waitTime -= 1
914 else
915 time += 1;
916 // dry water
917 if (liveTime <= 0) then begin die(); exit; end;
918 ex := 255-trunc(255.0*time/liveTime);
919 if (ex <= 10) then begin die(); exit; end;
920 if (ex > 250) then ex := 255;
921 alpha := Byte(ex);
922 end;
923 end;
926 // ////////////////////////////////////////////////////////////////////////// //
927 procedure g_GFX_SparkVel (fX, fY: Integer; count: Word; vx, vy: Integer; devX, devY: Byte); forward;
929 procedure g_GFX_Blood (fX, fY: Integer; count: Word; vx, vy: Integer;
930 devX, devY: Word; cr, cg, cb: Byte; kind: Byte = BLOOD_NORMAL);
932 function genColor (cbase, crnd: Integer; def: Byte=0): Byte;
933 begin
934 if (cbase > 0) then
935 begin
936 cbase += crnd;
937 if (cbase < 0) then result := 0
938 else if (cbase > 255) then result := 255
939 else result := Byte(cbase);
940 end
941 else
942 begin
943 result := def;
944 end;
945 end;
947 var
948 a: Integer;
949 devX1, devX2, devY1, devY2: Integer;
950 l: Integer;
951 crnd: Integer;
952 pan: TPanel;
953 begin
954 if not gpart_dbg_enabled then exit;
956 if (kind = BLOOD_SPARKS) then
957 begin
958 g_GFX_SparkVel(fX, fY, 2+Random(2), -vx div 2, -vy div 2, devX, devY);
959 exit;
960 end
961 else if (kind = BLOOD_CSPARKS) OR (kind = BLOOD_COMBINE) then
962 begin
963 g_GFX_SparkVel(fX, fY, count, -vx div 2, -vy div 2, devX, devY);
964 if kind <> BLOOD_COMBINE then exit
965 end;
967 l := Length(Particles);
968 if (l = 0) then exit;
969 if (count > l) then count := l;
971 devX1 := devX div 2;
972 devX2 := devX+1;
973 devY1 := devY div 2;
974 devY2 := devY+1;
976 for a := 1 to count do
977 begin
978 with Particles[CurrentParticle] do
979 begin
980 x := fX-devX1+Random(devX2);
981 y := fY-devY1+Random(devY2);
983 // check for level bounds
984 if (x < g_Map_MinX) or (y < g_Map_MinY) or (x > g_Map_MaxX) or (y > g_Map_MaxY) then continue;
986 // in what environment we are starting in?
987 pan := g_Map_PanelAtPoint(x, y, (GridTagSolid or GridTagLiquid));
988 if (pan <> nil) then
989 begin
990 // either in a wall, or in a liquid
991 if ((pan.tag and GridTagSolid) <> 0) then continue; // don't spawn in walls
992 env := TEnvType.ELiquid;
993 end
994 else
995 begin
996 env := TEnvType.EAir;
997 end;
999 velX := vx+(Random-Random)*3;
1000 velY := vy+(Random-Random)*3;
1002 if (velY > -4) then
1003 begin
1004 if (velY-4 < -4) then velY := -4 else velY := velY-4;
1005 end;
1007 accelX := -sign(velX)*Random/100;
1008 accelY := 0.8;
1010 crnd := 20*Random(6)-50;
1012 red := genColor(cr, CRnd, 0);
1013 green := genColor(cg, CRnd, 0);
1014 blue := genColor(cb, CRnd, 0);
1015 alpha := 255;
1017 particleType := TPartType.Blood;
1018 state := TPartState.Normal;
1019 time := 0;
1020 liveTime := 120+Random(40);
1021 waitTime := 20;
1022 floorY := Unknown;
1023 ceilingY := Unknown;
1024 end;
1026 if (CurrentParticle >= MaxParticles-1) then CurrentParticle := 0 else CurrentParticle += 1;
1027 end;
1028 end;
1031 procedure g_GFX_Water (fX, fY: Integer; count: Word; fVelX, fVelY: Single; devX, devY, color: Byte;
1032 simple: Boolean=false; cr: Byte=0; cg: Byte=0; cb: Byte=0);
1033 var
1034 a: Integer;
1035 devX1, devX2, devY1, devY2: Integer;
1036 l: Integer;
1037 pan: TPanel;
1038 begin
1039 if not gpart_dbg_enabled then exit;
1041 l := Length(Particles);
1042 if (l = 0) then exit;
1043 if (count > l) then count := l;
1045 if (abs(fVelX) < 3.0) then fVelX := 3.0-6.0*Random;
1047 devX1 := devX div 2;
1048 devX2 := devX+1;
1049 devY1 := devY div 2;
1050 devY2 := devY+1;
1052 if (not simple) and (color > 3) then color := 0;
1054 for a := 1 to count do
1055 begin
1056 with Particles[CurrentParticle] do
1057 begin
1058 if not simple then
1059 begin
1060 x := fX-devX1+Random(devX2);
1061 y := fY-devY1+Random(devY2);
1063 if (abs(fVelX) < 0.5) then velX := 1.0-2.0*Random else velX := fVelX*Random;
1064 if (Random(10) < 7) then velX := -velX;
1065 velY := fVelY*Random;
1066 accelX := 0.0;
1067 accelY := 0.8;
1068 end
1069 else
1070 begin
1071 x := fX;
1072 y := fY;
1074 velX := fVelX;
1075 velY := fVelY;
1076 accelX := 0.0;
1077 accelY := 0.8;
1078 end;
1080 // check for level bounds
1081 if (x < g_Map_MinX) or (y < g_Map_MinY) or (x > g_Map_MaxX) or (y > g_Map_MaxY) then continue;
1083 // this hack will allow water spawned in water to fly out
1084 // it can happen when player fell from a huge height (see "DOOM2D.WAD:\MAP03", for example)
1085 if (fVelY >= 0) then
1086 begin
1087 // in what environment we are starting in?
1088 pan := g_Map_PanelAtPoint(x, y, (GridTagObstacle or GridTagLiquid));
1089 end
1090 else
1091 begin
1092 pan := g_Map_PanelAtPoint(x, y, GridTagObstacle);
1093 end;
1094 if (pan <> nil) then continue;
1095 env := TEnvType.EAir;
1097 // color
1098 case color of
1099 1: // reddish
1100 begin
1101 red := 155+Random(9)*10;
1102 green := trunc(150*Random);
1103 blue := green;
1104 end;
1105 2: // greenish
1106 begin
1107 red := trunc(150*Random);
1108 green := 175+Random(9)*10;
1109 blue := red;
1110 end;
1111 3: // bluish
1112 begin
1113 red := trunc(200*Random);
1114 green := red;
1115 blue := 175+Random(9)*10;
1116 end;
1117 4: // Ñâîé öâåò, ñâåòëåå
1118 begin
1119 red := 20+Random(19)*10;
1120 green := red;
1121 blue := red;
1122 red := nmin(red+cr, 255);
1123 green := nmin(green+cg, 255);
1124 blue := nmin(blue+cb, 255);
1125 end;
1126 5: // Ñâîé öâåò, òåìíåå
1127 begin
1128 red := 20+Random(19)*10;
1129 green := red;
1130 blue := red;
1131 red := nmax(cr-red, 0);
1132 green := nmax(cg-green, 0);
1133 blue := nmax(cb-blue, 0);
1134 end;
1135 else // grayish
1136 begin
1137 red := 90+random(12)*10;
1138 green := red;
1139 blue := red;
1140 end;
1141 end;
1142 alpha := 255;
1144 particleType := TPartType.Water;
1145 state := TPartState.Normal;
1146 time := 0;
1147 liveTime := 60+Random(60);
1148 waitTime := 120;
1149 floorY := Unknown;
1150 ceilingY := Unknown;
1151 end;
1153 if (CurrentParticle >= MaxParticles-1) then CurrentParticle := 0 else CurrentParticle += 1;
1154 end;
1155 end;
1158 procedure g_GFX_SimpleWater (fX, fY: Integer; count: Word; fVelX, fVelY: Single; defColor, cr, cg, cb: Byte);
1159 begin
1160 g_GFX_Water(fX, fY, count, 0, 0, 0, 0, defColor, true, cr, cg, cb);
1161 end;
1164 // ////////////////////////////////////////////////////////////////////////// //
1165 procedure TParticle.thinkerBubble ();
1166 var
1167 dy: Integer;
1168 begin
1169 dy := round(velY);
1171 if (dy <> 0) then
1172 begin
1173 y += dy;
1174 if (dy < 0) then
1175 begin
1176 if (y <= ceilingY) then begin die(); exit; end;
1177 end
1178 else
1179 begin
1180 if (y >= floorY) then begin die(); exit; end;
1181 end;
1182 if (y < g_Map_MinY) or (y > g_Map_MaxY) then begin die(); exit; end;
1183 end;
1185 if (velY > -4) then velY += accelY;
1187 if waitTime > 0 then
1188 waitTime -= 1
1189 else
1190 time += 1;
1191 end;
1194 {.$DEFINE D2F_DEBUG_BUBBLES}
1195 procedure g_GFX_Bubbles (fX, fY: Integer; count: Word; devX, devY: Byte);
1196 var
1197 a, liquidx: Integer;
1198 devX1, devX2, devY1, devY2: Integer;
1199 l: Integer;
1200 {$IF DEFINED(D2F_DEBUG_BUBBLES)}
1201 stt: UInt64;
1202 nptr, ptr: Boolean;
1203 {$ENDIF}
1204 begin
1205 if not gpart_dbg_enabled then exit;
1207 l := Length(Particles);
1208 if (l = 0) then exit;
1209 if (count > l) then count := l;
1211 devX1 := devX div 2;
1212 devX2 := devX+1;
1213 devY1 := devY div 2;
1214 devY2 := devY+1;
1216 for a := 1 to count do
1217 begin
1218 with Particles[CurrentParticle] do
1219 begin
1220 x := fX-devX1+Random(devX2);
1221 y := fY-devY1+Random(devY2);
1223 // check for level bounds
1224 if (x < g_Map_MinX) or (y < g_Map_MinY) or (x > g_Map_MaxX) or (y > g_Map_MaxY) then continue;
1226 (*
1227 // don't spawn bubbles outside of the liquid
1228 if not isLiquidAt(X, Y) {ByteBool(gCollideMap[Y, X] and MARK_LIQUID)} then
1229 Continue;
1230 *)
1232 // trace liquid, so we'll know where it ends; do it in 8px steps for speed
1233 // tracer will return `false` if we started outside of the liquid
1235 {$IF DEFINED(D2F_DEBUG_BUBBLES)}
1236 stt := getTimeMicro();
1237 ptr := mapGrid.traceOrthoRayWhileIn(liquidx, liquidTopY, x, y, x, 0, GridTagWater or GridTagAcid1 or GridTagAcid2);
1238 stt := getTimeMicro()-stt;
1239 e_LogWritefln('traceOrthoRayWhileIn: time=%s (%s); liquidTopY=%s', [Integer(stt), ptr, liquidTopY]);
1240 //
1241 stt := getTimeMicro();
1242 nptr := g_Map_TraceLiquidNonPrecise(x, y, 0, -8, liquidx, liquidTopY);
1243 stt := getTimeMicro()-stt;
1244 e_LogWritefln('g_Map_TraceLiquidNonPrecise: time=%s (%s); liquidTopY=%s', [Integer(stt), nptr, liquidTopY]);
1245 if not nptr then continue;
1246 {$ELSE}
1247 if not g_Map_TraceLiquidNonPrecise(x, y, 0, -8, liquidx, ceilingY) then continue;
1248 if not g_Map_TraceLiquidNonPrecise(x, y, 0, +8, liquidx, floorY) then continue;
1249 {$ENDIF}
1251 velX := 0;
1252 velY := -1-Random;
1253 accelX := 0;
1254 accelY := velY/10;
1256 red := 255;
1257 green := 255;
1258 blue := 255;
1259 alpha := 255;
1261 state := TPartState.Normal;
1262 particleType := TPartType.Bubbles;
1263 time := 0;
1264 liveTime := 65535;
1265 waitTime := 0;
1266 end;
1268 if (CurrentParticle >= MaxParticles-1) then CurrentParticle := 0 else CurrentParticle += 1;
1269 end;
1270 end;
1273 // ////////////////////////////////////////////////////////////////////////// //
1274 procedure TParticle.thinkerSpark ();
1275 label
1276 _done;
1277 var
1278 dx, dy: SmallInt;
1279 pan: TPanel;
1280 ex, ey: Integer;
1281 begin
1282 if not gpart_dbg_phys_enabled then begin x += round(velX); y += round(velY); goto _done; end;
1284 dx := round(velX);
1285 dy := round(velY);
1287 //writeln('spark0: pos=(', x, ',', y, '); delta=(', dx, ',', dy, '); state=', state, '; ceilingY=', ceilingY, '; floorY=', floorY);
1289 // apply gravity
1290 if (abs(velX) < 0.1) and (abs(velY) < 0.1) then
1291 begin
1292 velY := 0.8;
1293 accelY := 0.5;
1294 end;
1296 // flying
1297 if (dx <> 0) then
1298 begin
1299 // has some horizontal velocity
1300 pan := g_Map_traceToNearest(x, y, x+dx, y+dy, (GridTagSolid or GridTagLiquid), @ex, @ey);
1301 if (x <> ex) then begin floorY := Unknown; ceilingY := Unknown; end; // dunno yet
1302 x := ex;
1303 y := ey;
1304 if (pan <> nil) then
1305 begin
1306 if ((pan.tag and GridTagLiquid) <> 0) then begin die(); exit; end; // die in liquid
1307 // hit the wall; falling down vertically
1308 velX := 0;
1309 accelX := 0;
1310 end;
1311 end
1312 else if (dy <> 0) then
1313 begin
1314 // has some vertical velocity
1315 if (dy < 0) then
1316 begin
1317 // flying up
1318 if (ceilingY = Unknown) then findCeiling(); // need to do this anyway
1319 y += dy;
1320 if (y <= ceilingY) then
1321 begin
1322 // oops, hit a ceiling
1323 y := ceilingY;
1324 velY := -velY;
1325 accelY := abs(accelY);
1326 end;
1327 // environment didn't changed
1328 end
1329 else
1330 begin
1331 // falling down
1332 if (floorY = Unknown) then findFloor(); // need to do this anyway
1333 y += dy;
1334 if (y >= floorY) then
1335 begin
1336 // hit something except a floor?
1337 if (floorType <> TFloorType.Wall) then begin die(); exit; end; // yep: just die
1338 // otherwise, go to sleep
1339 y := floorY;
1340 sleep();
1341 // environment didn't changed
1342 end;
1343 end;
1344 end;
1346 _done:
1347 if (x < g_Map_MinX) or (y < g_Map_MinY) or (x > g_Map_MaxX) or (y > g_Map_MaxY) then begin die(); end;
1349 if (velX <> 0.0) then velX += accelX;
1351 if (velY <> 0.0) then
1352 begin
1353 if (accelY < 10) then accelY += 0.08;
1354 velY += accelY;
1355 end;
1357 //writeln('spark1: pos=(', x, ',', y, '); delta=(', velX:6:3, ',', velY:6:3, '); state=', state, '; ceilingY=', ceilingY, '; floorY=', floorY);
1359 if waitTime > 0 then
1360 waitTime -= 1
1361 else
1362 time += 1;
1363 end;
1366 // ////////////////////////////////////////////////////////////////////////// //
1367 procedure g_GFX_SparkVel (fX, fY: Integer; count: Word; vx, vy: Integer; devX, devY: Byte);
1368 var
1369 a: Integer;
1370 devX1, devX2, devY1, devY2: Integer;
1371 l: Integer;
1372 pan: TPanel;
1373 begin
1374 if not gpart_dbg_enabled then exit;
1376 l := Length(Particles);
1377 if (l = 0) then exit;
1378 if (count > l) then count := l;
1380 devX1 := devX div 2;
1381 devX2 := devX+1;
1382 devY1 := devY div 2;
1383 devY2 := devY+1;
1385 for a := 1 to count do
1386 begin
1387 with Particles[CurrentParticle] do
1388 begin
1389 x := fX-devX1+Random(devX2);
1390 y := fY-devY1+Random(devY2);
1392 // check for level bounds
1393 if (x < g_Map_MinX) or (y < g_Map_MinY) or (x > g_Map_MaxX) or (y > g_Map_MaxY) then continue;
1395 // in what environment we are starting in?
1396 pan := g_Map_PanelAtPoint(x, y, (GridTagSolid or GridTagLiquid));
1397 if (pan <> nil) then
1398 begin
1399 // either in a wall, or in a liquid
1400 //if ((pan.tag and GridTagSolid) <> 0) then continue; // don't spawn in walls
1401 //env := TEnvType.ELiquid;
1402 continue;
1403 end
1404 else
1405 begin
1406 env := TEnvType.EAir;
1407 end;
1409 velX := vx+(Random-Random)*3;
1410 velY := vy+(Random-Random)*3;
1412 if (velY > -4) then
1413 begin
1414 if (velY-4 < -4) then velY := -4 else velY := velY-4;
1415 end;
1417 accelX := -sign(velX)*Random/100;
1418 accelY := 0.8;
1420 red := 255;
1421 green := 100+Random(155);
1422 blue := 64;
1423 alpha := 255;
1425 particleType := TPartType.Spark;
1426 state := TPartState.Normal;
1427 time := 0;
1428 liveTime := 30+Random(60);
1429 waitTime := 0;
1430 floorY := Unknown;
1431 ceilingY := Unknown;
1432 end;
1434 if (CurrentParticle >= MaxParticles-1) then CurrentParticle := 0 else CurrentParticle += 1;
1435 end;
1436 end;
1439 procedure g_GFX_Spark (fX, fY: Integer; count: Word; angle: SmallInt; devX, devY: Byte);
1440 var
1441 a: Integer;
1442 b: Single;
1443 devX1, devX2, devY1, devY2: Integer;
1444 baseVelX, baseVelY: Single;
1445 l: Integer;
1446 pan: TPanel;
1447 begin
1448 if not gpart_dbg_enabled then exit;
1450 l := Length(Particles);
1451 if (l = 0) then exit;
1452 if (count > l) then count := l;
1454 angle := 360-angle;
1456 devX1 := devX div 2;
1457 devX2 := devX+1;
1458 devY1 := devY div 2;
1459 devY2 := devY+1;
1461 b := DegToRad(angle);
1462 baseVelX := cos(b);
1463 baseVelY := 1.6*sin(b);
1464 if (abs(baseVelX) < 0.01) then baseVelX := 0.0;
1465 if (abs(baseVelY) < 0.01) then baseVelY := 0.0;
1467 for a := 1 to count do
1468 begin
1469 with Particles[CurrentParticle] do
1470 begin
1471 x := fX-devX1+Random(devX2);
1472 y := fY-devY1+Random(devY2);
1474 // check for level bounds
1475 if (x < g_Map_MinX) or (y < g_Map_MinY) or (x > g_Map_MaxX) or (y > g_Map_MaxY) then continue;
1477 // in what environment we are starting in?
1478 pan := g_Map_PanelAtPoint(x, y, (GridTagSolid or GridTagLiquid));
1479 if (pan <> nil) then
1480 begin
1481 // either in a wall, or in a liquid
1482 //if ((pan.tag and GridTagSolid) <> 0) then continue; // don't spawn in walls
1483 //env := TEnvType.ELiquid;
1484 continue;
1485 end
1486 else
1487 begin
1488 env := TEnvType.EAir;
1489 end;
1491 velX := baseVelX*Random;
1492 velY := baseVelY-Random;
1493 accelX := velX/3.0;
1494 accelY := velY/5.0;
1496 red := 255;
1497 green := 100+Random(155);
1498 blue := 64;
1499 alpha := 255;
1501 particleType := TPartType.Spark;
1502 state := TPartState.Normal;
1503 time := 0;
1504 liveTime := 30+Random(60);
1505 waitTime := 0;
1506 floorY := Unknown;
1507 ceilingY := Unknown;
1508 end;
1510 if (CurrentParticle >= MaxParticles-1) then CurrentParticle := 0 else CurrentParticle += 1;
1511 end;
1512 end;
1515 // ////////////////////////////////////////////////////////////////////////// //
1516 procedure g_GFX_SetMax (count: Integer);
1517 var
1518 a: Integer;
1519 begin
1520 if count > 50000 then count := 50000;
1521 if (count < 1) then count := 1;
1522 SetLength(Particles, count);
1523 for a := 0 to High(Particles) do Particles[a].die();
1524 MaxParticles := count;
1525 CurrentParticle := 0;
1526 end;
1529 function g_GFX_GetMax (): Integer;
1530 begin
1531 result := MaxParticles;
1532 end;
1535 function FindOnceAnim (): DWORD;
1536 var
1537 i: Integer;
1538 begin
1539 if OnceAnims <> nil then
1540 for i := 0 to High(OnceAnims) do
1541 if OnceAnims[i].Animation = nil then
1542 begin
1543 Result := i;
1544 Exit;
1545 end;
1547 if OnceAnims = nil then
1548 begin
1549 SetLength(OnceAnims, 16);
1550 Result := 0;
1551 end
1552 else
1553 begin
1554 Result := High(OnceAnims) + 1;
1555 SetLength(OnceAnims, Length(OnceAnims) + 16);
1556 end;
1557 end;
1560 procedure g_GFX_OnceAnim (x, y: Integer; Anim: TAnimation; AnimType: Byte = 0);
1561 var
1562 find_id: DWORD;
1563 begin
1564 if not gpart_dbg_enabled then exit;
1566 if (Anim = nil) then exit;
1568 find_id := FindOnceAnim();
1570 OnceAnims[find_id].AnimType := AnimType;
1571 OnceAnims[find_id].Animation := TAnimation.Create(Anim.FramesID, Anim.Loop, Anim.Speed);
1572 OnceAnims[find_id].Animation.Blending := Anim.Blending;
1573 OnceAnims[find_id].Animation.alpha := Anim.alpha;
1574 OnceAnims[find_id].x := x;
1575 OnceAnims[find_id].y := y;
1576 end;
1579 // ////////////////////////////////////////////////////////////////////////// //
1580 procedure g_GFX_Init ();
1581 begin
1582 //g_Game_SetLoadingText(_lc[I_LOAD_COLLIDE_MAP]+' 1/6', 0, False);
1583 //SetLength(gCollideMap, gMapInfo.Height+1);
1584 //for a := 0 to High(gCollideMap) do SetLength(gCollideMap[a], gMapInfo.Width+1);
1585 awmSetup();
1586 {$IFDEF HEADLESS}
1587 gpart_dbg_enabled := false;
1588 {$ENDIF}
1589 end;
1592 procedure g_GFX_Free ();
1593 var
1594 a: Integer;
1595 begin
1596 Particles := nil;
1597 SetLength(Particles, MaxParticles);
1598 for a := 0 to High(Particles) do Particles[a].die();
1599 CurrentParticle := 0;
1601 if (OnceAnims <> nil) then
1602 begin
1603 for a := 0 to High(OnceAnims) do OnceAnims[a].Animation.Free();
1604 OnceAnims := nil;
1605 end;
1607 awakeMap := nil;
1608 // why not?
1609 awakeMapH := -1;
1610 awakeMapW := -1;
1611 end;
1614 // ////////////////////////////////////////////////////////////////////////// //
1615 procedure g_GFX_Update ();
1616 var
1617 a: Integer;
1618 w, h: Integer;
1619 len: Integer;
1620 begin
1621 if not gpart_dbg_enabled then exit;
1623 if (Particles <> nil) then
1624 begin
1625 w := gMapInfo.Width;
1626 h := gMapInfo.Height;
1628 len := High(Particles);
1630 for a := 0 to len do
1631 begin
1632 if Particles[a].alive then
1633 begin
1634 with Particles[a] do
1635 begin
1636 if (time = liveTime) then begin die(); continue; end;
1637 if (x+1 >= w) or (y+1 >= h) or (x <= 0) or (y <= 0) then begin die(); end;
1638 think();
1639 end; // with
1640 end; // if
1641 end; // for
1642 end; // Particles <> nil
1644 // clear awake map
1645 awmClear();
1647 if OnceAnims <> nil then
1648 begin
1649 for a := 0 to High(OnceAnims) do
1650 if OnceAnims[a].Animation <> nil then
1651 begin
1652 case OnceAnims[a].AnimType of
1653 ONCEANIM_SMOKE:
1654 begin
1655 if Random(3) = 0 then
1656 OnceAnims[a].x := OnceAnims[a].x-1+Random(3);
1657 if Random(2) = 0 then
1658 OnceAnims[a].y := OnceAnims[a].y-Random(2);
1659 end;
1660 end;
1662 if OnceAnims[a].Animation.Played then
1663 begin
1664 OnceAnims[a].Animation.Free();
1665 OnceAnims[a].Animation := nil;
1666 end
1667 else
1668 OnceAnims[a].Animation.Update();
1669 end;
1670 end;
1671 end;
1674 procedure g_GFX_Draw ();
1675 var
1676 a, len: Integer;
1677 begin
1678 if not gpart_dbg_enabled then exit;
1680 if (Particles <> nil) then
1681 begin
1682 glDisable(GL_TEXTURE_2D);
1683 if (g_dbg_scale < 0.6) then glPointSize(1)
1684 else if (g_dbg_scale > 1.3) then glPointSize(g_dbg_scale+1)
1685 else glPointSize(2);
1686 glDisable(GL_POINT_SMOOTH);
1688 glEnable(GL_BLEND);
1689 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1691 glBegin(GL_POINTS);
1693 len := High(Particles);
1694 for a := 0 to len do
1695 begin
1696 with Particles[a] do
1697 begin
1698 if not alive then continue;
1699 if (x >= sX) and (y >= sY) and (x <= sX+sWidth) and (sY <= sY+sHeight) then
1700 begin
1701 glColor4ub(red, green, blue, alpha);
1702 glVertex2f(x+0.37, y+0.37);
1703 end;
1704 end;
1705 end;
1707 glEnd();
1709 glDisable(GL_BLEND);
1710 end;
1712 if (OnceAnims <> nil) then
1713 begin
1714 len := High(OnceAnims);
1715 for a := 0 to len do
1716 begin
1717 if (OnceAnims[a].Animation <> nil) then
1718 begin
1719 with OnceAnims[a] do Animation.Draw(x, y, TMirrorType.None);
1720 end;
1721 end;
1722 end;
1723 end;
1726 end.