DEADSOFTWARE

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