DEADSOFTWARE

Merge branch 'master' of ssh://repo.or.cz/d2df-sdl
[d2df-sdl.git] / src / game / g_holmes.pas
1 (* Copyright (C) DooM 2D:Forever Developers
2 *
3 * This program is free software: you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation, either version 3 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 *)
16 {$INCLUDE ../shared/a_modes.inc}
17 unit g_holmes;
19 interface
21 uses
22 {$IFDEF USE_MEMPOOL}mempool,{$ENDIF} geom,
23 e_log, e_input,
24 g_textures, g_basic, e_graphics, g_phys, g_grid, g_player, g_monsters,
25 g_window, g_map, g_triggers, g_items, g_game, g_panel, g_console, g_gfx,
26 xprofiler,
27 sdlcarcass,
28 fui_common, fui_events, fui_ctls,
29 fui_gfx_gl;
32 procedure g_Holmes_Draw ();
33 procedure g_Holmes_DrawUI ();
35 procedure g_Holmes_MouseEvent (var ev: THMouseEvent);
36 procedure g_Holmes_KeyEvent (var ev: THKeyEvent);
38 // hooks for player
39 procedure g_Holmes_plrViewPos (viewPortX, viewPortY: Integer);
40 procedure g_Holmes_plrViewSize (viewPortW, viewPortH: Integer);
41 procedure g_Holmes_plrLaser (ax0, ay0, ax1, ay1: Integer);
44 var
45 g_holmes_enabled: Boolean = {$IF DEFINED(D2F_DEBUG)}true{$ELSE}false{$ENDIF};
48 implementation
50 uses
51 {rttiobj,} typinfo, e_texture,
52 SysUtils, Classes, GL, SDL2,
53 MAPDEF, g_main, g_options,
54 utils, hashtable, xparser;
57 var
58 hlmContext: TGxContext = nil;
59 //globalInited: Boolean = false;
60 msX: Integer = -666;
61 msY: Integer = -666;
62 msB: Word = 0; // button state
63 kbS: Word = 0; // keyboard modifiers state
64 showGrid: Boolean = {$IF DEFINED(D2F_DEBUG)}false{$ELSE}false{$ENDIF};
65 showMonsInfo: Boolean = false;
66 showMonsLOS2Plr: Boolean = false;
67 showAllMonsCells: Boolean = false;
68 showMapCurPos: Boolean = false;
69 showLayersWindow: Boolean = false;
70 showOutlineWindow: Boolean = false;
71 showTriggers: Boolean = {$IF DEFINED(D2F_DEBUG)}false{$ELSE}false{$ENDIF};
72 showTraceBox: Boolean = {$IF DEFINED(D2F_DEBUG)}false{$ELSE}false{$ENDIF};
75 // ////////////////////////////////////////////////////////////////////////// //
76 {$INCLUDE g_holmes.inc}
77 {$INCLUDE g_holmes_ol.inc} // outliner
80 // ////////////////////////////////////////////////////////////////////////// //
81 {$INCLUDE g_holmes_cmd.inc}
82 procedure holmesInitCommands (); forward;
83 procedure holmesInitBinds (); forward;
86 // ////////////////////////////////////////////////////////////////////////// //
87 var
88 g_ol_nice: Boolean = false;
89 g_ol_fill_walls: Boolean = false;
90 g_ol_rlayer_back: Boolean = false;
91 g_ol_rlayer_step: Boolean = false;
92 g_ol_rlayer_wall: Boolean = false;
93 g_ol_rlayer_door: Boolean = false;
94 g_ol_rlayer_acid1: Boolean = false;
95 g_ol_rlayer_acid2: Boolean = false;
96 g_ol_rlayer_water: Boolean = false;
97 g_ol_rlayer_fore: Boolean = false;
100 // ////////////////////////////////////////////////////////////////////////// //
101 var
102 winHelp: TUITopWindow = nil;
103 winOptions: TUITopWindow = nil;
104 winLayers: TUITopWindow = nil;
105 winOutlines: TUITopWindow = nil;
108 procedure createHelpWindow (); forward;
109 procedure createOptionsWindow (); forward;
110 procedure createLayersWindow (); forward;
111 procedure createOutlinesWindow (); forward;
114 procedure toggleLayersWindowCB (me: TUIControl);
115 begin
116 showLayersWindow := not showLayersWindow;
117 if showLayersWindow then
118 begin
119 if (winLayers = nil) then createLayersWindow();
120 uiAddWindow(winLayers);
121 end
122 else
123 begin
124 uiRemoveWindow(winLayers);
125 end;
126 end;
128 procedure toggleOutlineWindowCB (me: TUIControl);
129 begin
130 showOutlineWindow := not showOutlineWindow;
131 if showOutlineWindow then
132 begin
133 if (winOutlines = nil) then createOutlinesWindow();
134 uiAddWindow(winOutlines);
135 end
136 else
137 begin
138 uiRemoveWindow(winOutlines);
139 end;
140 end;
143 procedure createHelpWindow ();
144 procedure addHelpEmptyLine ();
145 var
146 stx: TUIStaticText;
147 begin
148 stx := TUIStaticText.Create();
149 stx.flExpand := true;
150 stx.halign := 0; // center
151 stx.text := '';
152 stx.header := false;
153 stx.line := false;
154 winHelp.appendChild(stx);
155 end;
157 procedure addHelpCaptionLine (const txt: AnsiString);
158 var
159 stx: TUIStaticText;
160 begin
161 stx := TUIStaticText.Create();
162 stx.flExpand := true;
163 stx.halign := 0; // center
164 stx.text := txt;
165 stx.header := true;
166 stx.line := true;
167 winHelp.appendChild(stx);
168 end;
170 procedure addHelpCaption (const txt: AnsiString);
171 var
172 stx: TUIStaticText;
173 begin
174 stx := TUIStaticText.Create();
175 stx.flExpand := true;
176 stx.halign := 0; // center
177 stx.text := txt;
178 stx.header := true;
179 stx.line := false;
180 winHelp.appendChild(stx);
181 end;
183 procedure addHelpKeyMouse (const key, txt, grp: AnsiString);
184 var
185 box: TUIHBox;
186 span: TUISpan;
187 stx: TUIStaticText;
188 begin
189 box := TUIHBox.Create();
190 box.flExpand := true;
191 // key
192 stx := TUIStaticText.Create();
193 stx.flExpand := true;
194 stx.halign := 1; // right
195 stx.valign := 0; // center
196 stx.text := key;
197 stx.header := true;
198 stx.line := false;
199 stx.flHGroup := grp;
200 box.appendChild(stx);
201 // span
202 span := TUISpan.Create();
203 span.flDefaultSize := TLaySize.Create(4, 1);
204 span.flExpand := true;
205 box.appendChild(span);
206 // text
207 stx := TUIStaticText.Create();
208 stx.flExpand := true;
209 stx.halign := -1; // left
210 stx.valign := 0; // center
211 stx.text := txt;
212 stx.header := false;
213 stx.line := false;
214 box.appendChild(stx);
215 winHelp.appendChild(box);
216 end;
218 procedure addHelpKey (const key, txt: AnsiString); begin addHelpKeyMouse(key, txt, 'help-keys'); end;
219 procedure addHelpMouse (const key, txt: AnsiString); begin addHelpKeyMouse(key, txt, 'help-mouse'); end;
221 var
222 slist: array of AnsiString = nil;
223 cmd: PHolmesCommand;
224 bind: THolmesBinding;
225 f: Integer;
227 llb: TUISimpleText;
228 maxkeylen: Integer;
229 s: AnsiString;
231 begin
232 winHelp := TUITopWindow.Create('Holmes Help');
233 winHelp.escClose := true;
234 winHelp.flHoriz := false;
236 // keyboard
237 for cmd in cmdlist do cmd.helpmark := false;
239 //maxkeylen := 0;
240 for bind in keybinds do
241 begin
242 if (Length(bind.key) = 0) then continue;
243 if cmdlist.get(bind.cmdName, cmd) then
244 begin
245 if (Length(cmd.help) > 0) then
246 begin
247 cmd.helpmark := true;
248 //if (maxkeylen < Length(bind.key)) then maxkeylen := Length(bind.key);
249 end;
250 end;
251 end;
253 for cmd in cmdlist do
254 begin
255 if not cmd.helpmark then continue;
256 if (Length(cmd.help) = 0) then begin cmd.helpmark := false; continue; end;
257 f := 0;
258 while (f < Length(slist)) and (CompareText(slist[f], cmd.section) <> 0) do Inc(f);
259 if (f = Length(slist)) then
260 begin
261 SetLength(slist, Length(slist)+1);
262 slist[High(slist)] := cmd.section;
263 end;
264 end;
266 addHelpCaptionLine('KEYBOARD');
267 //llb := TUISimpleText.Create(0, 0);
268 for f := 0 to High(slist) do
269 begin
270 //if (f > 0) then llb.appendItem('');
271 if (f > 0) then addHelpEmptyLine();
272 //llb.appendItem(slist[f], true, true);
273 addHelpCaption(slist[f]);
274 for cmd in cmdlist do
275 begin
276 if not cmd.helpmark then continue;
277 if (CompareText(cmd.section, slist[f]) <> 0) then continue;
278 for bind in keybinds do
279 begin
280 if (Length(bind.key) = 0) then continue;
281 if (cmd.name = bind.cmdName) then
282 begin
283 //s := bind.key;
284 //while (Length(s) < maxkeylen) do s += ' ';
285 //s := ' '+s+' -- '+cmd.help;
286 //llb.appendItem(s);
287 addHelpMouse(bind.key, cmd.help);
288 end;
289 end;
290 end;
291 end;
293 // mouse
294 for cmd in cmdlist do cmd.helpmark := false;
296 //maxkeylen := 0;
297 for bind in msbinds do
298 begin
299 if (Length(bind.key) = 0) then continue;
300 if cmdlist.get(bind.cmdName, cmd) then
301 begin
302 if (Length(cmd.help) > 0) then
303 begin
304 cmd.helpmark := true;
305 //if (maxkeylen < Length(bind.key)) then maxkeylen := Length(bind.key);
306 end;
307 end;
308 end;
310 //llb.appendItem('');
311 //llb.appendItem('mouse', true, true);
312 if (f > 0) then addHelpEmptyLine();
313 addHelpCaptionLine('MOUSE');
314 for bind in msbinds do
315 begin
316 if (Length(bind.key) = 0) then continue;
317 if cmdlist.get(bind.cmdName, cmd) then
318 begin
319 if (Length(cmd.help) > 0) then
320 begin
321 //s := bind.key;
322 //while (Length(s) < maxkeylen) do s += ' ';
323 //s := ' '+s+' -- '+cmd.help;
324 //llb.appendItem(s);
325 addHelpKey(bind.key, cmd.help);
326 end;
327 end;
328 end;
330 //winHelp.appendChild(llb);
332 uiLayoutCtl(winHelp);
333 winHelp.centerInScreen();
334 end;
337 procedure winLayersClosed (me: TUIControl); begin showLayersWindow := false; end;
338 procedure winOutlinesClosed (me: TUIControl); begin showOutlineWindow := false; end;
340 procedure addCheckBox (parent: TUIControl; const text: AnsiString; pvar: PBoolean; const aid: AnsiString='');
341 var
342 cb: TUICheckBox;
343 begin
344 cb := TUICheckBox.Create();
345 cb.flExpand := true;
346 cb.setVar(pvar);
347 cb.text := text;
348 cb.id := aid;
349 parent.appendChild(cb);
350 end;
352 procedure addButton (parent: TUIControl; const text: AnsiString; cb: TUIControl.TActionCB);
353 var
354 but: TUIButton;
355 begin
356 but := TUIButton.Create();
357 //but.flExpand := true;
358 but.actionCB := cb;
359 but.text := text;
360 parent.appendChild(but);
361 end;
364 procedure actionFillWalls (cb: TUIControl);
365 begin
366 TUICheckBox(cb).checked := not TUICheckBox(cb).checked;
367 TUICheckBox(cb.topLevel['cbcontour']).enabled := not TUICheckBox(cb).checked;
368 end;
370 procedure createLayersWindow ();
371 var
372 box: TUIVBox;
373 begin
374 winLayers := TUITopWindow.Create('layers');
375 winLayers.flHoriz := false;
376 winLayers.x0 := 10;
377 winLayers.y0 := 10;
378 winLayers.flHoriz := false;
379 winLayers.escClose := true;
380 winLayers.closeCB := winLayersClosed;
382 box := TUIVBox.Create();
383 addCheckBox(box, '~background', @g_rlayer_back);
384 addCheckBox(box, '~steps', @g_rlayer_step);
385 addCheckBox(box, '~walls', @g_rlayer_wall);
386 addCheckBox(box, '~doors', @g_rlayer_door);
387 addCheckBox(box, 'acid~1', @g_rlayer_acid1);
388 addCheckBox(box, 'acid~2', @g_rlayer_acid2);
389 addCheckBox(box, 'wate~r', @g_rlayer_water);
390 addCheckBox(box, '~foreground', @g_rlayer_fore);
391 winLayers.appendChild(box);
393 uiLayoutCtl(winLayers);
394 end;
397 procedure createOutlinesWindow ();
398 var
399 box: TUIVBox;
400 begin
401 winOutlines := TUITopWindow.Create('outlines');
402 winOutlines.flHoriz := false;
403 winOutlines.x0 := 100;
404 winOutlines.y0 := 30;
405 winOutlines.flHoriz := false;
406 winOutlines.escClose := true;
407 winOutlines.closeCB := winOutlinesClosed;
409 box := TUIVBox.Create();
410 box.hasFrame := true;
411 box.caption := 'layers';
412 addCheckBox(box, '~background', @g_ol_rlayer_back);
413 addCheckBox(box, '~steps', @g_ol_rlayer_step);
414 addCheckBox(box, '~walls', @g_ol_rlayer_wall);
415 addCheckBox(box, '~doors', @g_ol_rlayer_door);
416 addCheckBox(box, 'acid~1', @g_ol_rlayer_acid1);
417 addCheckBox(box, 'acid~2', @g_ol_rlayer_acid2);
418 addCheckBox(box, 'wate~r', @g_ol_rlayer_water);
419 addCheckBox(box, '~foreground', @g_ol_rlayer_fore);
420 winOutlines.appendChild(box);
422 box := TUIVBox.Create();
423 box.hasFrame := true;
424 box.caption := 'options';
425 addCheckBox(box, 'fi~ll walls', @g_ol_fill_walls, 'cbfill');
426 addCheckBox(box, 'con~tours', @g_ol_nice, 'cbcontour');
427 winOutlines.appendChild(box);
429 winOutlines.setActionCBFor('cbfill', actionFillWalls);
431 uiLayoutCtl(winOutlines);
432 end;
435 procedure createOptionsWindow ();
436 var
437 box: TUIBox;
438 span: TUISpan;
439 begin
440 winOptions := TUITopWindow.Create('Holmes Options');
441 winOptions.flHoriz := false;
442 winOptions.flHoriz := false;
443 winOptions.escClose := true;
445 box := TUIVBox.Create();
446 box.hasFrame := true;
447 box.caption := 'visual';
448 addCheckBox(box, 'map ~grid', @showGrid);
449 addCheckBox(box, 'cursor ~position on map', @showMapCurPos);
450 addCheckBox(box, '~monster info', @showMonsInfo);
451 addCheckBox(box, 'monster LO~S to player', @showMonsLOS2Plr);
452 addCheckBox(box, 'monster ~cells (SLOW!)', @showAllMonsCells);
453 addCheckBox(box, 'draw ~triggers (SLOW!)', @showTriggers);
454 winOptions.appendChild(box);
456 box := TUIHBox.Create();
457 box.hasFrame := true;
458 box.caption := 'windows';
459 box.captionAlign := 0;
460 box.flAlign := 0;
461 addButton(box, '~layers', toggleLayersWindowCB);
462 span := TUISpan.Create();
463 span.flExpand := true;
464 span.flDefaultSize := TLaySize.Create(4, 1);
465 box.appendChild(span);
466 addButton(box, '~outline', toggleOutlineWindowCB);
467 winOptions.appendChild(box);
469 uiLayoutCtl(winOptions);
470 winOptions.centerInScreen();
471 end;
474 procedure toggleLayersWindow (arg: Integer=-1);
475 begin
476 if (arg < 0) then showLayersWindow := not showLayersWindow else showLayersWindow := (arg > 0);
477 showLayersWindow := not showLayersWindow; // hack for callback
478 toggleLayersWindowCB(nil);
479 end;
481 procedure toggleOutlineWindow (arg: Integer=-1);
482 begin
483 if (arg < 0) then showOutlineWindow := not showOutlineWindow else showOutlineWindow := (arg > 0);
484 showOutlineWindow := not showOutlineWindow; // hack for callback
485 toggleOutlineWindowCB(nil);
486 end;
488 procedure toggleHelpWindow (arg: Integer=-1);
489 begin
490 if (winHelp = nil) then createHelpWindow();
491 if (arg < 0) then begin if not uiVisibleWindow(winHelp) then uiAddWindow(winHelp) else uiRemoveWindow(winHelp); end
492 else if (arg = 0) then begin if uiVisibleWindow(winHelp) then uiRemoveWindow(winHelp); end
493 else begin if not uiVisibleWindow(winHelp) then uiAddWindow(winHelp); end
494 end;
496 procedure toggleOptionsWindow (arg: Integer=-1);
497 begin
498 if (winOptions = nil) then createOptionsWindow();
499 if (arg < 0) then begin if not uiVisibleWindow(winOptions) then uiAddWindow(winOptions) else uiRemoveWindow(winOptions); end
500 else if (arg = 0) then begin if uiVisibleWindow(winOptions) then uiRemoveWindow(winOptions); end
501 else begin if not uiVisibleWindow(winOptions) then uiAddWindow(winOptions); end
502 end;
505 // ////////////////////////////////////////////////////////////////////////// //
506 var
507 vpSet: Boolean = false;
508 vpx, vpy: Integer;
509 vpw, vph: Integer;
510 laserSet: Boolean = false;
511 laserX0, laserY0, laserX1, laserY1: Integer;
512 monMarkedUID: Integer = -1;
513 platMarkedGUID: Integer = -1;
516 procedure g_Holmes_plrViewPos (viewPortX, viewPortY: Integer);
517 begin
518 vpSet := true;
519 vpx := viewPortX;
520 vpy := viewPortY;
521 end;
523 procedure g_Holmes_plrViewSize (viewPortW, viewPortH: Integer);
524 begin
525 vpSet := true;
526 vpw := viewPortW;
527 vph := viewPortH;
528 end;
530 procedure g_Holmes_plrLaser (ax0, ay0, ax1, ay1: Integer);
531 begin
532 laserSet := true;
533 laserX0 := ax0;
534 laserY0 := ay0;
535 laserX1 := ax1;
536 laserY1 := ay1;
537 laserSet := laserSet; // shut up, fpc!
538 end;
541 function pmsCurMapX (): Integer; inline; begin result := round(msX/g_dbg_scale)+vpx; end;
542 function pmsCurMapY (): Integer; inline; begin result := round(msY/g_dbg_scale)+vpy; end;
545 procedure plrDebugMouse (var ev: THMouseEvent);
546 begin
547 //e_WriteLog(Format('mouse: x=%d; y=%d; but=%d; bstate=%d', [msx, msy, but, bstate]), MSG_NOTIFY);
548 if (gPlayer1 = nil) or not vpSet then exit;
549 //if (ev.kind <> THMouseEvent.Press) then exit;
550 //e_WriteLog(Format('mev: %d', [Integer(ev.kind)]), MSG_NOTIFY);
551 msbindExecute(ev);
552 end;
555 {$IFDEF HOLMES_OLD_OUTLINES}
556 var
557 edgeBmp: array of Byte = nil;
560 procedure drawOutlines ();
561 var
562 r, g, b: Integer;
564 procedure clearEdgeBmp ();
565 begin
566 SetLength(edgeBmp, (gWinSizeX+4)*(gWinSizeY+4));
567 FillChar(edgeBmp[0], Length(edgeBmp)*sizeof(edgeBmp[0]), 0);
568 end;
570 procedure drawPanel (pan: TPanel);
571 var
572 sx, len, y0, y1: Integer;
573 begin
574 if (pan = nil) or (pan.Width < 1) or (pan.Height < 1) then exit;
575 if (pan.X+pan.Width <= vpx-1) or (pan.Y+pan.Height <= vpy-1) then exit;
576 if (pan.X >= vpx+vpw+1) or (pan.Y >= vpy+vph+1) then exit;
577 if g_ol_nice or g_ol_fill_walls then
578 begin
579 sx := pan.X-(vpx-1);
580 len := pan.Width;
581 if (len > gWinSizeX+4) then len := gWinSizeX+4;
582 if (sx < 0) then begin len += sx; sx := 0; end;
583 if (sx+len > gWinSizeX+4) then len := gWinSizeX+4-sx;
584 if (len < 1) then exit;
585 assert(sx >= 0);
586 assert(sx+len <= gWinSizeX+4);
587 y0 := pan.Y-(vpy-1);
588 y1 := y0+pan.Height;
589 if (y0 < 0) then y0 := 0;
590 if (y1 > gWinSizeY+4) then y1 := gWinSizeY+4;
591 while (y0 < y1) do
592 begin
593 FillChar(edgeBmp[y0*(gWinSizeX+4)+sx], len*sizeof(edgeBmp[0]), 1);
594 Inc(y0);
595 end;
596 end
597 else
598 begin
599 drawRect(pan.X, pan.Y, pan.Width, pan.Height, r, g, b);
600 end;
601 end;
603 var
604 lsx: Integer = -1;
605 lex: Integer = -1;
606 lsy: Integer = -1;
608 procedure flushLine ();
609 begin
610 if (lsy > 0) and (lsx > 0) then
611 begin
612 if (lex = lsx) then
613 begin
614 glBegin(GL_POINTS);
615 glVertex2f(lsx-1+vpx+0.37, lsy-1+vpy+0.37);
616 glEnd();
617 end
618 else
619 begin
620 glBegin(GL_LINES);
621 glVertex2f(lsx-1+vpx+0.37, lsy-1+vpy+0.37);
622 glVertex2f(lex-0+vpx+0.37, lsy-1+vpy+0.37);
623 glEnd();
624 end;
625 end;
626 lsx := -1;
627 lex := -1;
628 end;
630 procedure startLine (y: Integer);
631 begin
632 flushLine();
633 lsy := y;
634 end;
636 procedure putPixel (x: Integer);
637 begin
638 if (x < 1) then exit;
639 if (lex+1 <> x) then flushLine();
640 if (lsx < 0) then lsx := x;
641 lex := x;
642 end;
644 procedure drawEdges ();
645 var
646 x, y: Integer;
647 a: PByte;
648 begin
649 glDisable(GL_BLEND);
650 glDisable(GL_TEXTURE_2D);
651 glLineWidth(1);
652 glPointSize(1);
653 glDisable(GL_LINE_SMOOTH);
654 glDisable(GL_POLYGON_SMOOTH);
655 glColor4f(r/255.0, g/255.0, b/255.0, 1.0);
656 for y := 1 to vph do
657 begin
658 a := @edgeBmp[y*(gWinSizeX+4)+1];
659 startLine(y);
660 for x := 1 to vpw do
661 begin
662 if (a[0] <> 0) then
663 begin
664 if (a[-1] = 0) or (a[1] = 0) or (a[-(gWinSizeX+4)] = 0) or (a[gWinSizeX+4] = 0) or
665 (a[-(gWinSizeX+4)-1] = 0) or (a[-(gWinSizeX+4)+1] = 0) or
666 (a[gWinSizeX+4-1] = 0) or (a[gWinSizeX+4+1] = 0) then
667 begin
668 putPixel(x);
669 end;
670 end;
671 Inc(a);
672 end;
673 flushLine();
674 end;
675 end;
677 procedure drawFilledWalls ();
678 var
679 x, y: Integer;
680 a: PByte;
681 begin
682 glDisable(GL_BLEND);
683 glDisable(GL_TEXTURE_2D);
684 glLineWidth(1);
685 glPointSize(1);
686 glDisable(GL_LINE_SMOOTH);
687 glDisable(GL_POLYGON_SMOOTH);
688 glColor4f(r/255.0, g/255.0, b/255.0, 1.0);
689 for y := 1 to vph do
690 begin
691 a := @edgeBmp[y*(gWinSizeX+4)+1];
692 startLine(y);
693 for x := 1 to vpw do
694 begin
695 if (a[0] <> 0) then putPixel(x);
696 Inc(a);
697 end;
698 flushLine();
699 end;
700 end;
702 procedure doWallsOld (parr: array of TPanel; ptype: Word; ar, ag, ab: Integer);
703 var
704 f: Integer;
705 pan: TPanel;
706 begin
707 r := ar;
708 g := ag;
709 b := ab;
710 if g_ol_nice or g_ol_fill_walls then clearEdgeBmp();
711 for f := 0 to High(parr) do
712 begin
713 pan := parr[f];
714 if (pan = nil) or not pan.Enabled or (pan.Width < 1) or (pan.Height < 1) then continue;
715 if ((pan.PanelType and ptype) = 0) then continue;
716 drawPanel(pan);
717 end;
718 if g_ol_nice then drawEdges();
719 if g_ol_fill_walls then drawFilledWalls();
720 end;
722 var
723 xptag: Word;
725 function doWallCB (pan: TPanel; tag: Integer): Boolean;
726 begin
727 result := false; // don't stop
728 //if (pan = nil) or not pan.Enabled or (pan.Width < 1) or (pan.Height < 1) then exit;
729 if ((pan.PanelType and xptag) = 0) then exit;
730 drawPanel(pan);
731 end;
733 procedure doWalls (parr: array of TPanel; ptype: Word; ar, ag, ab: Integer);
734 begin
735 r := ar;
736 g := ag;
737 b := ab;
738 xptag := ptype;
739 if ((ptype and (PANEL_WALL or PANEL_OPENDOOR or PANEL_CLOSEDOOR)) <> 0) then ptype := GridTagWall or GridTagDoor
740 else panelTypeToTag(ptype);
741 if g_ol_nice or g_ol_fill_walls then clearEdgeBmp();
742 mapGrid.forEachInAABB(vpx-1, vpy-1, vpw+2, vph+2, doWallCB, ptype);
743 if g_ol_nice then drawEdges();
744 if g_ol_fill_walls then drawFilledWalls();
745 end;
747 begin
748 if g_ol_rlayer_back then doWallsOld(gRenderBackgrounds, PANEL_BACK, 255, 127, 0);
749 if g_ol_rlayer_step then doWallsOld(gSteps, PANEL_STEP, 192, 192, 192);
750 if g_ol_rlayer_wall then doWallsOld(gWalls, PANEL_WALL, 255, 255, 255);
751 if g_ol_rlayer_door then doWallsOld(gWalls, PANEL_OPENDOOR or PANEL_CLOSEDOOR, 0, 255, 0);
752 if g_ol_rlayer_acid1 then doWallsOld(gAcid1, PANEL_ACID1, 255, 0, 0);
753 if g_ol_rlayer_acid2 then doWallsOld(gAcid2, PANEL_ACID2, 198, 198, 0);
754 if g_ol_rlayer_water then doWallsOld(gWater, PANEL_WATER, 0, 255, 255);
755 if g_ol_rlayer_fore then doWallsOld(gRenderForegrounds, PANEL_FORE, 210, 210, 210);
756 end;
758 {$ELSE}
759 var
760 oliner: TOutliner = nil;
762 procedure drawOutlines ();
763 var
764 r, g, b: Integer;
766 procedure clearOliner ();
767 begin
768 //if (oliner <> nil) and ((oliner.height <> vph+2) or (oliner.width <> vpw+2)) then begin oliner.Free(); oliner := nil; end;
769 if (oliner = nil) then oliner := TOutliner.Create(vpw+2, vph+2) else oliner.setup(vpw+2, vph+2);
770 end;
772 procedure drawOutline (ol: TOutliner; sx, sy: Integer);
773 procedure xline (x0, x1, y: Integer);
774 var
775 x: Integer;
776 begin
777 if (g_dbg_scale < 1.0) then
778 begin
779 glBegin(GL_POINTS);
780 for x := x0 to x1 do glVertex2f(sx+x+0.375, sy+y+0.375);
781 glEnd();
782 end
783 else
784 begin
785 glBegin(GL_QUADS);
786 glVertex2f(sx+x0+0, sy+y+0);
787 glVertex2f(sx+x1+1, sy+y+0);
788 glVertex2f(sx+x1+1, sy+y+1);
789 glVertex2f(sx+x0+0, sy+y+1);
790 glEnd();
791 end;
792 end;
793 var
794 y: Integer;
795 sp: TOutliner.TSpanX;
796 begin
797 if (ol = nil) then exit;
798 glPointSize(1);
799 glDisable(GL_POINT_SMOOTH);
800 for y := 0 to ol.height-1 do
801 begin
802 for sp in ol.eachSpanAtY(y) do
803 begin
804 if (g_dbg_scale <= 1.0) then
805 begin
806 glBegin(GL_POINTS);
807 glVertex2f(sx+sp.x0+0.375, sy+y+0.375);
808 glVertex2f(sx+sp.x1+0.375, sy+y+0.375);
809 glEnd();
810 end
811 else
812 begin
813 glBegin(GL_QUADS);
814 glVertex2f(sx+sp.x0+0, sy+y+0);
815 glVertex2f(sx+sp.x0+1, sy+y+0);
816 glVertex2f(sx+sp.x0+1, sy+y+1);
817 glVertex2f(sx+sp.x0+0, sy+y+1);
819 glVertex2f(sx+sp.x1+0, sy+y+0);
820 glVertex2f(sx+sp.x1+1, sy+y+0);
821 glVertex2f(sx+sp.x1+1, sy+y+1);
822 glVertex2f(sx+sp.x1+0, sy+y+1);
823 glEnd();
824 end;
825 end;
826 for sp in ol.eachSpanEdgeAtY(y, -1) do
827 begin
828 xline(sp.x0, sp.x1, y);
830 glBegin(GL_QUADS);
831 glVertex2f(sx+sp.x0+0, sy+y+0);
832 glVertex2f(sx+sp.x1+1, sy+y+0);
833 glVertex2f(sx+sp.x1+1, sy+y+1);
834 glVertex2f(sx+sp.x0+0, sy+y+1);
835 glEnd();
837 end;
838 for sp in ol.eachSpanEdgeAtY(y, +1) do
839 begin
840 xline(sp.x0, sp.x1, y);
842 glBegin(GL_QUADS);
843 glVertex2f(sx+sp.x0+0, sy+y+0);
844 glVertex2f(sx+sp.x1+1, sy+y+0);
845 glVertex2f(sx+sp.x1+1, sy+y+1);
846 glVertex2f(sx+sp.x0+0, sy+y+1);
847 glEnd();
849 end;
850 end;
851 end;
853 procedure doWallsOld (parr: array of TPanel; ptype: Word; ar, ag, ab: Integer);
854 var
855 f: Integer;
856 pan: TPanel;
857 begin
858 r := ar;
859 g := ag;
860 b := ab;
861 if g_ol_nice then clearOliner();
862 hlmContext.color := TGxRGBA.Create(r, g, b);
863 for f := 0 to High(parr) do
864 begin
865 pan := parr[f];
866 if (pan = nil) or not pan.Enabled or (pan.Width < 1) or (pan.Height < 1) then continue;
867 if ((pan.PanelType and ptype) = 0) then continue;
868 if (pan.X > vpx+vpw+41) or (pan.Y > vpy+vph+41) then continue;
869 if (pan.X+pan.Width < vpx-41) then continue;
870 if (pan.Y+pan.Height < vpy-41) then continue;
871 if g_ol_nice then
872 begin
873 oliner.addRect(pan.X-(vpx+1), pan.Y-(vpy+1), pan.Width, pan.Height);
874 end;
875 if g_ol_fill_walls then
876 begin
877 hlmContext.fillRect(pan.X, pan.Y, pan.Width, pan.Height);
878 end
879 else if not g_ol_nice then
880 begin
881 hlmContext.rect(pan.X, pan.Y, pan.Width, pan.Height);
882 end;
883 end;
884 if g_ol_nice then
885 begin
886 glColor4f(r/255.0, g/255.0, b/255.0, 1.0);
887 drawOutline(oliner, vpx+1, vpy+1);
888 end;
889 end;
891 begin
892 if (vpw < 2) or (vph < 2) then exit;
893 if g_ol_rlayer_back then doWallsOld(gRenderBackgrounds, PANEL_BACK, 255, 127, 0);
894 if g_ol_rlayer_step then doWallsOld(gSteps, PANEL_STEP, 192, 192, 192);
895 if g_ol_rlayer_wall then doWallsOld(gWalls, PANEL_WALL, 255, 255, 255);
896 if g_ol_rlayer_door then doWallsOld(gWalls, PANEL_OPENDOOR or PANEL_CLOSEDOOR, 0, 255, 0);
897 if g_ol_rlayer_acid1 then doWallsOld(gAcid1, PANEL_ACID1, 255, 0, 0);
898 if g_ol_rlayer_acid2 then doWallsOld(gAcid2, PANEL_ACID2, 198, 198, 0);
899 if g_ol_rlayer_water then doWallsOld(gWater, PANEL_WATER, 0, 255, 255);
900 if g_ol_rlayer_fore then doWallsOld(gRenderForegrounds, PANEL_FORE, 210, 210, 210);
901 end;
902 {$ENDIF}
905 procedure plrDebugDraw ();
906 procedure drawTileGrid ();
907 var
908 x, y: Integer;
909 begin
910 hlmContext.color := TGxRGBA.Create(96, 96, 96);
911 for y := 0 to (mapGrid.gridHeight div mapGrid.tileSize) do
912 begin
913 hlmContext.line(mapGrid.gridX0, mapGrid.gridY0+y*mapGrid.tileSize, mapGrid.gridX0+mapGrid.gridWidth, mapGrid.gridY0+y*mapGrid.tileSize);
914 end;
916 hlmContext.color := TGxRGBA.Create(96, 96, 96);
917 for x := 0 to (mapGrid.gridWidth div mapGrid.tileSize) do
918 begin
919 hlmContext.line(mapGrid.gridX0+x*mapGrid.tileSize, mapGrid.gridY0, mapGrid.gridX0+x*mapGrid.tileSize, mapGrid.gridY0+y*mapGrid.gridHeight);
920 end;
921 end;
923 procedure drawAwakeCells ();
924 var
925 x, y: Integer;
926 begin
927 hlmContext.color := TGxRGBA.Create(128, 0, 128, 64);
928 for y := 0 to (mapGrid.gridHeight div mapGrid.tileSize) do
929 begin
930 for x := 0 to (mapGrid.gridWidth div mapGrid.tileSize) do
931 begin
932 if awmIsSetHolmes(x*mapGrid.tileSize+mapGrid.gridX0+1, y*mapGrid.tileSize++mapGrid.gridY0+1) then
933 begin
934 hlmContext.fillRect(x*mapGrid.tileSize++mapGrid.gridX0, y*mapGrid.tileSize++mapGrid.gridY0, monsGrid.tileSize, monsGrid.tileSize);
935 end;
936 end;
937 end;
938 end;
940 procedure drawTraceBox ();
941 var
942 plr: TPlayer;
943 px, py, pw, ph: Integer;
944 pdx, pdy: Integer;
945 ex, ey: Integer;
946 pan: TPanel;
947 begin
948 if (Length(gPlayers) < 1) then exit;
949 plr := gPlayers[0];
950 if (plr = nil) then exit;
951 plr.getMapBox(px, py, pw, ph);
952 hlmContext.color := TGxRGBA.Create(255, 0, 255, 200);
953 hlmContext.rect(px, py, pw, ph);
954 pdx := pmsCurMapX-(px+pw div 2);
955 pdy := pmsCurMapY-(py+ph div 2);
956 hlmContext.color := TGxRGBA.Create(255, 0, 255, 200);
957 hlmContext.line(px+pw div 2, py+ph div 2, px+pw div 2+pdx, py+ph div 2+pdy);
958 pan := mapGrid.traceBox(ex, ey, px, py, pw, ph, pdx, pdy, nil, GridTagObstacle);
959 if (pan = nil) then
960 begin
961 hlmContext.color := TGxRGBA.Create(255, 255, 255, 180);
962 hlmContext.rect(px+pdx, py+pdy, pw, ph);
963 end
964 else
965 begin
966 hlmContext.color := TGxRGBA.Create(255, 255, 0, 180);
967 hlmContext.rect(px+pdx, py+pdy, pw, ph);
968 end;
969 hlmContext.color := TGxRGBA.Create(255, 127, 0, 180);
970 hlmContext.rect(ex, ey, pw, ph);
971 end;
973 procedure hilightCell (cx, cy: Integer);
974 begin
975 hlmContext.color := TGxRGBA.Create(0, 128, 0, 64);
976 hlmContext.fillRect(cx, cy, monsGrid.tileSize, monsGrid.tileSize);
977 end;
979 procedure hilightCell1 (cx, cy: Integer);
980 begin
981 //e_WriteLog(Format('h1: (%d,%d)', [cx, cy]), MSG_NOTIFY);
982 cx := cx and (not (monsGrid.tileSize-1));
983 cy := cy and (not (monsGrid.tileSize-1));
984 hlmContext.color := TGxRGBA.Create(255, 255, 0, 92);
985 hlmContext.fillRect(cx, cy, monsGrid.tileSize, monsGrid.tileSize);
986 end;
988 function hilightWallTrc (pan: TPanel; tag: Integer; x, y, prevx, prevy: Integer): Boolean;
989 begin
990 result := false; // don't stop
991 if (pan = nil) then exit; // cell completion, ignore
992 //e_WriteLog(Format('h1: (%d,%d)', [cx, cy]), MSG_NOTIFY);
993 hlmContext.color := TGxRGBA.Create(0, 128, 128, 64);
994 hlmContext.fillRect(pan.X, pan.Y, pan.Width, pan.Height);
995 end;
997 function monsCollector (mon: TMonster; tag: Integer): Boolean;
998 var
999 ex, ey: Integer;
1000 mx, my, mw, mh: Integer;
1001 begin
1002 result := false;
1003 mon.getMapBox(mx, my, mw, mh);
1004 hlmContext.color := TGxRGBA.Create(255, 255, 0, 160);
1005 hlmContext.rect(mx, my, mw, mh);
1006 //e_DrawQuad(mx, my, mx+mw-1, my+mh-1, 255, 255, 0, 96);
1007 if lineAABBIntersects(laserX0, laserY0, laserX1, laserY1, mx, my, mw, mh, ex, ey) then
1008 begin
1009 //e_DrawPoint(8, ex, ey, 0, 255, 0);
1010 hlmContext.color := TGxRGBA.Create(0, 255, 0, 220);
1011 hlmContext.fillRect(ex-2, ey-2, 7, 7);
1012 end;
1013 end;
1015 procedure drawMonsterInfo (mon: TMonster);
1016 var
1017 mx, my, mw, mh: Integer;
1019 procedure drawMonsterTargetLine ();
1020 var
1021 emx, emy, emw, emh: Integer;
1022 enemy: TMonster;
1023 eplr: TPlayer;
1024 ex, ey: Integer;
1025 begin
1026 if (g_GetUIDType(mon.MonsterTargetUID) = UID_PLAYER) then
1027 begin
1028 eplr := g_Player_Get(mon.MonsterTargetUID);
1029 if (eplr <> nil) then eplr.getMapBox(emx, emy, emw, emh) else exit;
1030 end
1031 else if (g_GetUIDType(mon.MonsterTargetUID) = UID_MONSTER) then
1032 begin
1033 enemy := g_Monsters_ByUID(mon.MonsterTargetUID);
1034 if (enemy <> nil) then enemy.getMapBox(emx, emy, emw, emh) else exit;
1035 end
1036 else
1037 begin
1038 exit;
1039 end;
1040 mon.getMapBox(mx, my, mw, mh);
1041 hlmContext.color := TGxRGBA.Create(255, 0, 0);
1042 hlmContext.line(mx+mw div 2, my+mh div 2, emx+emw div 2, emy+emh div 2);
1043 if (g_Map_traceToNearestWall(mx+mw div 2, my+mh div 2, emx+emw div 2, emy+emh div 2, @ex, @ey) <> nil) then
1044 begin
1045 hlmContext.color := TGxRGBA.Create(0, 255, 0);
1046 hlmContext.line(mx+mw div 2, my+mh div 2, ex, ey);
1047 end;
1048 end;
1050 procedure drawLOS2Plr ();
1051 var
1052 emx, emy, emw, emh: Integer;
1053 eplr: TPlayer;
1054 ex, ey: Integer;
1055 begin
1056 eplr := gPlayers[0];
1057 if (eplr = nil) then exit;
1058 eplr.getMapBox(emx, emy, emw, emh);
1059 mon.getMapBox(mx, my, mw, mh);
1060 hlmContext.color := TGxRGBA.Create(255, 0, 0);
1061 hlmContext.line(mx+mw div 2, my+mh div 2, emx+emw div 2, emy+emh div 2);
1062 {$IF DEFINED(D2F_DEBUG)}
1063 mapGrid.dbgRayTraceTileHitCB := hilightCell1;
1064 {$ENDIF}
1065 if (g_Map_traceToNearestWall(mx+mw div 2, my+mh div 2, emx+emw div 2, emy+emh div 2, @ex, @ey) <> nil) then
1066 //if (mapGrid.traceRay(ex, ey, mx+mw div 2, my+mh div 2, emx+emw div 2, emy+emh div 2, hilightWallTrc, (GridTagWall or GridTagDoor)) <> nil) then
1067 begin
1068 hlmContext.color := TGxRGBA.Create(0, 255, 0);
1069 hlmContext.line(mx+mw div 2, my+mh div 2, ex, ey);
1070 end;
1071 {$IF DEFINED(D2F_DEBUG)}
1072 mapGrid.dbgRayTraceTileHitCB := nil;
1073 {$ENDIF}
1074 end;
1076 begin
1077 if (mon = nil) then exit;
1078 mon.getMapBox(mx, my, mw, mh);
1079 //mx += mw div 2;
1081 monsGrid.forEachBodyCell(mon.proxyId, hilightCell);
1083 if showMonsInfo then
1084 begin
1085 //fillRect(mx-4, my-7*8-6, 110, 7*8+6, 0, 0, 94, 250);
1086 hlmContext.font := 'msx';
1087 hlmContext.color := TGxRGBA.Create(255, 127, 0);
1089 hlmContext.darkenRect(mx-4, my-7*hlmContext.charWidth(' ')-6, 110, 7*hlmContext.charWidth(' ')+6, 128);
1090 my -= 8;
1091 my -= 2;
1093 // type
1094 hlmContext.drawText(mx, my, Format('%s(U:%u)', [monsTypeToString(mon.MonsterType), mon.UID])); my -= hlmContext.charWidth(' ');
1095 // beh
1096 hlmContext.drawText(mx, my, Format('Beh: %s', [monsBehToString(mon.MonsterBehaviour)])); my -= hlmContext.charWidth(' ');
1097 // state
1098 hlmContext.drawText(mx, my, Format('State:%s (%d)', [monsStateToString(mon.MonsterState), mon.MonsterSleep])); my -= hlmContext.charWidth(' ');
1099 // health
1100 hlmContext.drawText(mx, my, Format('Health:%d', [mon.MonsterHealth])); my -= hlmContext.charWidth(' ');
1101 // ammo
1102 hlmContext.drawText(mx, my, Format('Ammo:%d', [mon.MonsterAmmo])); my -= hlmContext.charWidth(' ');
1103 // target
1104 hlmContext.drawText(mx, my, Format('TgtUID:%u', [mon.MonsterTargetUID])); my -= hlmContext.charWidth(' ');
1105 hlmContext.drawText(mx, my, Format('TgtTime:%d', [mon.MonsterTargetTime])); my -= hlmContext.charWidth(' ');
1106 end;
1108 drawMonsterTargetLine();
1109 if showMonsLOS2Plr then drawLOS2Plr();
1111 property MonsterRemoved: Boolean read FRemoved write FRemoved;
1112 property MonsterPain: Integer read FPain write FPain;
1113 property MonsterAnim: Byte read FCurAnim write FCurAnim;
1115 end;
1117 function highlightAllMonsterCells (mon: TMonster): Boolean;
1118 begin
1119 result := false; // don't stop
1120 monsGrid.forEachBodyCell(mon.proxyId, hilightCell);
1121 end;
1123 procedure drawSelectedPlatformCells ();
1124 var
1125 pan: TPanel;
1126 begin
1127 if not showGrid then exit;
1128 pan := g_Map_PanelByGUID(platMarkedGUID);
1129 if (pan = nil) then exit;
1130 mapGrid.forEachBodyCell(pan.proxyId, hilightCell);
1131 hlmContext.color := TGxRGBA.Create(0, 200, 0, 200);
1132 hlmContext.rect(pan.x, pan.y, pan.width, pan.height);
1133 end;
1135 procedure drawTrigger (var trig: TTrigger);
1137 procedure drawPanelDest (pguid: Integer);
1138 var
1139 pan: TPanel;
1140 begin
1141 pan := g_Map_PanelByGUID(pguid);
1142 if (pan = nil) then exit;
1143 hlmContext.color := TGxRGBA.Create(255, 0, 255, 220);
1144 hlmContext.line(trig.trigCenter.x, trig.trigCenter.y, pan.x+pan.width div 2, pan.y+pan.height div 2);
1145 end;
1147 var
1148 tts: AnsiString;
1149 tx: Integer;
1150 begin
1151 hlmContext.font := 'msx';
1152 hlmContext.color := TGxRGBA.Create(255, 0, 255, 96);
1153 hlmContext.fillRect(trig.x, trig.y, trig.width, trig.height);
1154 tts := trigType2Str(trig.TriggerType);
1155 tx := trig.x+(trig.width-Length(tts)*6) div 2;
1156 hlmContext.darkenRect(tx-2, trig.y-10, Length(tts)*6+4, 10, 64);
1157 hlmContext.color := TGxRGBA.Create(255, 127, 0);
1158 hlmContext.drawText(tx, trig.y-9, tts);
1159 tx := trig.x+(trig.width-Length(trig.mapId)*6) div 2;
1160 hlmContext.darkenRect(tx-2, trig.y-20, Length(trig.mapId)*6+4, 10, 64);
1161 hlmContext.color := TGxRGBA.Create(255, 255, 0);
1162 hlmContext.drawText(tx, trig.y-19, trig.mapId);
1163 drawPanelDest(trig.trigPanelGUID);
1164 case trig.TriggerType of
1165 TRIGGER_NONE: begin end;
1166 TRIGGER_EXIT: begin end;
1167 TRIGGER_TELEPORT: begin end;
1168 TRIGGER_OPENDOOR: begin end;
1169 TRIGGER_CLOSEDOOR: begin end;
1170 TRIGGER_DOOR: begin end;
1171 TRIGGER_DOOR5: begin end;
1172 TRIGGER_CLOSETRAP: begin end;
1173 TRIGGER_TRAP: begin end;
1174 TRIGGER_SECRET: begin end;
1175 TRIGGER_LIFTUP: begin end;
1176 TRIGGER_LIFTDOWN: begin end;
1177 TRIGGER_LIFT: begin end;
1178 TRIGGER_TEXTURE: begin end;
1179 TRIGGER_ON, TRIGGER_OFF, TRIGGER_ONOFF, TRIGGER_PRESS:
1180 begin
1181 if (trig.trigDataRec.trigTWidth > 0) and (trig.trigDataRec.trigTHeight > 0) then
1182 begin
1183 hlmContext.color := TGxRGBA.Create(0, 255, 255, 42);
1184 hlmContext.fillRect(
1185 trig.trigDataRec.trigTX, trig.trigDataRec.trigTY,
1186 trig.trigDataRec.trigTWidth, trig.trigDataRec.trigTHeight);
1187 hlmContext.color := TGxRGBA.Create(255, 0, 255, 220);
1188 hlmContext.line(
1189 trig.trigCenter.x, trig.trigCenter.y,
1190 trig.trigDataRec.trigTX+trig.trigDataRec.trigTWidth div 2,
1191 trig.trigDataRec.trigTY+trig.trigDataRec.trigTHeight div 2);
1192 end;
1193 end;
1194 TRIGGER_SOUND: begin end;
1195 TRIGGER_SPAWNMONSTER: begin end;
1196 TRIGGER_SPAWNITEM: begin end;
1197 TRIGGER_MUSIC: begin end;
1198 TRIGGER_PUSH: begin end;
1199 TRIGGER_SCORE: begin end;
1200 TRIGGER_MESSAGE: begin end;
1201 TRIGGER_DAMAGE: begin end;
1202 TRIGGER_HEALTH: begin end;
1203 TRIGGER_SHOT: begin end;
1204 TRIGGER_EFFECT: begin end;
1205 TRIGGER_SCRIPT: begin end;
1206 end;
1207 //trigType2Str
1208 //trigPanelId: Integer;
1209 end;
1211 procedure drawTriggers ();
1212 var
1213 f: Integer;
1214 begin
1215 for f := 0 to High(gTriggers) do drawTrigger(gTriggers[f]);
1216 end;
1218 procedure drawGibsBoxes ();
1219 var
1220 f: Integer;
1221 px, py, pw, ph: Integer;
1222 gib: PGib;
1223 begin
1224 for f := 0 to High(gGibs) do
1225 begin
1226 gib := @gGibs[f];
1227 if gib.alive then
1228 begin
1229 gib.getMapBox(px, py, pw, ph);
1230 hlmContext.color := TGxRGBA.Create(255, 0, 255);
1231 hlmContext.rect(px, py, pw, ph);
1232 end;
1233 end;
1234 end;
1236 var
1237 mon: TMonster;
1238 mx, my, mw, mh: Integer;
1239 //pan: TPanel;
1240 //ex, ey: Integer;
1241 begin
1242 if (gPlayer1 = nil) then exit;
1244 if (hlmContext = nil) then hlmContext := TGxContext.Create();
1246 gxSetContext(hlmContext);
1247 try
1248 //glScissor(0, gWinSizeY-gPlayerScreenSize.Y-1, vpw, vph);
1249 //hlmContext.clip := TGxRect.Create(0, gScreenHeight-gPlayerScreenSize.Y-1, gPlayerScreenSize.X, gPlayerScreenSize.Y);
1252 glScalef(g_dbg_scale, g_dbg_scale, 1.0);
1253 glTranslatef(-vpx, -vpy, 0);
1255 hlmContext.glSetScaleTrans(g_dbg_scale, -vpx, -vpy);
1256 glEnable(GL_SCISSOR_TEST);
1257 glScissor(0, gScreenHeight-gPlayerScreenSize.Y-1, gPlayerScreenSize.X, gPlayerScreenSize.Y);
1259 if (showGrid) then drawTileGrid();
1260 drawOutlines();
1262 if (laserSet) then g_Mons_AlongLine(laserX0, laserY0, laserX1, laserY1, monsCollector, true);
1264 if (monMarkedUID <> -1) then
1265 begin
1266 mon := g_Monsters_ByUID(monMarkedUID);
1267 if (mon <> nil) then
1268 begin
1269 mon.getMapBox(mx, my, mw, mh);
1270 //e_DrawQuad(mx, my, mx+mw-1, my+mh-1, 255, 0, 0, 30);
1271 hlmContext.color := TGxRGBA.Create(255, 0, 0, 220);
1272 hlmContext.rect(mx, my, mw, mh);
1273 drawMonsterInfo(mon);
1274 end;
1275 end;
1277 if showAllMonsCells and showGrid then g_Mons_ForEach(highlightAllMonsterCells);
1278 if showTriggers then drawTriggers();
1279 if showGrid then drawSelectedPlatformCells();
1281 //drawAwakeCells();
1283 if showTraceBox then drawTraceBox();
1285 //drawGibsBoxes();
1288 //pan := g_Map_traceToNearest(16, 608, 16, 8, (GridTagObstacle or GridTagLiquid), @ex, @ey);
1289 (*
1290 {$IF DEFINED(D2F_DEBUG)}
1291 mapGrid.dbgRayTraceTileHitCB := hilightCell1;
1292 {$ENDIF}
1293 pan := mapGrid.traceRay(ex, ey, 16, 608, 16, 8, nil, (GridTagObstacle or GridTagLiquid));
1294 if (pan <> nil) then writeln('end=(', ex, ',', ey, ')');
1295 {$IF DEFINED(D2F_DEBUG)}
1296 mapGrid.dbgRayTraceTileHitCB := nil;
1297 {$ENDIF}
1299 pan := g_Map_PanelAtPoint(16, 608, (GridTagObstacle or GridTagLiquid));
1300 if (pan <> nil) then writeln('hit!');
1301 *)
1303 finally
1304 gxSetContext(nil);
1305 end;
1307 if showMapCurPos then
1308 begin
1309 gxSetContext(hlmContext);
1310 hlmContext.font := 'dos';
1311 hlmContext.color := TGxRGBA.Create(255, 255, 0);
1312 hlmContext.drawText(4, gWinSizeY-10, Format('mappos:(%d,%d)', [pmsCurMapX, pmsCurMapY]));
1313 gxSetContext(nil);
1314 end;
1315 end;
1318 // ////////////////////////////////////////////////////////////////////////// //
1319 procedure g_Holmes_MouseEvent (var ev: THMouseEvent);
1320 var
1321 he: THMouseEvent;
1322 begin
1323 if g_Game_IsNet then exit;
1324 if not g_holmes_enabled then exit;
1326 holmesInitCommands();
1327 holmesInitBinds();
1328 msX := ev.x;
1329 msY := ev.y;
1330 msB := ev.bstate;
1331 kbS := ev.kstate;
1332 msB := msB;
1333 he := ev;
1334 he.x := he.x;
1335 he.y := he.y;
1336 uiMouseEvent(he);
1337 if (not he.eaten) then plrDebugMouse(he);
1338 ev.eat();
1339 end;
1342 // ////////////////////////////////////////////////////////////////////////// //
1343 procedure g_Holmes_KeyEvent (var ev: THKeyEvent);
1344 var
1345 doeat: Boolean = false;
1346 {$IF DEFINED(D2F_DEBUG)}
1347 pan: TPanel;
1348 ex, ey: Integer;
1349 dx, dy: Integer;
1350 {$ENDIF}
1352 procedure dummyWallTrc (cx, cy: Integer);
1353 begin
1354 end;
1356 begin
1357 if g_Game_IsNet then exit;
1358 if not g_holmes_enabled then exit;
1360 holmesInitCommands();
1361 holmesInitBinds();
1363 msB := ev.bstate;
1364 kbS := ev.kstate;
1365 case ev.scan of
1366 SDL_SCANCODE_LCTRL, SDL_SCANCODE_RCTRL,
1367 SDL_SCANCODE_LALT, SDL_SCANCODE_RALT,
1368 SDL_SCANCODE_LSHIFT, SDL_SCANCODE_RSHIFT:
1369 doeat := true;
1370 end;
1372 uiKeyEvent(ev);
1373 if (ev.eaten) then exit;
1374 if keybindExecute(ev) then begin ev.eat(); exit; end;
1375 // press
1376 if (ev.press) then
1377 begin
1378 {$IF DEFINED(D2F_DEBUG)}
1379 // C-UP, C-DOWN, C-LEFT, C-RIGHT: trace 10 pixels from cursor in the respective direction
1380 if ((ev.scan = SDL_SCANCODE_UP) or (ev.scan = SDL_SCANCODE_DOWN) or (ev.scan = SDL_SCANCODE_LEFT) or (ev.scan = SDL_SCANCODE_RIGHT)) and
1381 ((ev.kstate and THKeyEvent.ModCtrl) <> 0) then
1382 begin
1383 ev.eat();
1384 dx := pmsCurMapX;
1385 dy := pmsCurMapY;
1386 case ev.scan of
1387 SDL_SCANCODE_UP: dy -= 120;
1388 SDL_SCANCODE_DOWN: dy += 120;
1389 SDL_SCANCODE_LEFT: dx -= 120;
1390 SDL_SCANCODE_RIGHT: dx += 120;
1391 end;
1392 {$IF DEFINED(D2F_DEBUG)}
1393 //mapGrid.dbgRayTraceTileHitCB := dummyWallTrc;
1394 mapGrid.dbgShowTraceLog := true;
1395 {$ENDIF}
1396 pan := g_Map_traceToNearest(pmsCurMapX, pmsCurMapY, dx, dy, (GridTagWall or GridTagDoor or GridTagStep or GridTagAcid1 or GridTagAcid2 or GridTagWater), @ex, @ey);
1397 {$IF DEFINED(D2F_DEBUG)}
1398 //mapGrid.dbgRayTraceTileHitCB := nil;
1399 mapGrid.dbgShowTraceLog := false;
1400 {$ENDIF}
1401 e_LogWritefln('v-trace: (%d,%d)-(%d,%d); end=(%d,%d); hit=%d', [pmsCurMapX, pmsCurMapY, dx, dy, ex, ey, (pan <> nil)]);
1402 exit;
1403 end;
1404 {$ENDIF}
1405 end;
1406 if (doeat) then ev.eat();
1407 end;
1410 // ////////////////////////////////////////////////////////////////////////// //
1411 procedure g_Holmes_Draw ();
1412 begin
1413 if g_Game_IsNet then exit;
1415 {$IF not DEFINED(HEADLESS)}
1416 holmesInitCommands();
1417 holmesInitBinds();
1419 glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE); // modify color buffer
1420 glDisable(GL_STENCIL_TEST);
1421 glDisable(GL_BLEND);
1422 glDisable(GL_SCISSOR_TEST);
1423 glDisable(GL_TEXTURE_2D);
1425 if gGameOn then plrDebugDraw();
1426 {$ENDIF}
1428 laserSet := false;
1429 end;
1432 procedure g_Holmes_DrawUI ();
1433 begin
1434 if g_Game_IsNet then exit;
1435 if not g_holmes_enabled then exit;
1436 {$IF not DEFINED(HEADLESS)}
1437 gGfxDoClear := false;
1438 //if assigned(prerenderFrameCB) then prerenderFrameCB();
1439 uiDraw();
1440 glMatrixMode(GL_MODELVIEW);
1441 glPushMatrix();
1442 try
1443 //glLoadIdentity();
1444 if assigned(postrenderFrameCB) then postrenderFrameCB();
1445 finally
1446 glPopMatrix();
1447 end;
1448 {$ENDIF}
1449 end;
1452 // ////////////////////////////////////////////////////////////////////////// //
1453 procedure bcOneMonsterThinkStep (); begin gmon_debug_think := false; gmon_debug_one_think_step := true; end;
1454 procedure bcOneMPlatThinkStep (); begin g_dbgpan_mplat_active := false; g_dbgpan_mplat_step := true; end;
1455 procedure bcMPlatToggle (); begin g_dbgpan_mplat_active := not g_dbgpan_mplat_active; end;
1457 procedure bcToggleMonsterInfo (arg: Integer=-1); begin if (arg < 0) then showMonsInfo := not showMonsInfo else showMonsInfo := (arg > 0); end;
1458 procedure bcToggleMonsterLOSPlr (arg: Integer=-1); begin if (arg < 0) then showMonsLOS2Plr := not showMonsLOS2Plr else showMonsLOS2Plr := (arg > 0); end;
1459 procedure bcToggleMonsterCells (arg: Integer=-1); begin if (arg < 0) then showAllMonsCells := not showAllMonsCells else showAllMonsCells := (arg > 0); end;
1460 procedure bcToggleDrawTriggers (arg: Integer=-1); begin if (arg < 0) then showTriggers := not showTriggers else showTriggers := (arg > 0); end;
1462 procedure bcToggleCurPos (arg: Integer=-1); begin if (arg < 0) then showMapCurPos := not showMapCurPos else showMapCurPos := (arg > 0); end;
1463 procedure bcToggleGrid (arg: Integer=-1); begin if (arg < 0) then showGrid := not showGrid else showGrid := (arg > 0); end;
1465 procedure bcMonsterSpawn (s: AnsiString);
1466 var
1467 mon: TMonster;
1468 begin
1469 if not gGameOn or g_Game_IsClient then
1470 begin
1471 conwriteln('cannot spawn monster in this mode');
1472 exit;
1473 end;
1474 mon := g_Mons_SpawnAt(s, pmsCurMapX, pmsCurMapY);
1475 if (mon = nil) then begin conwritefln('unknown monster id: ''%s''', [s]); exit; end;
1476 monMarkedUID := mon.UID;
1477 end;
1479 procedure bcMonsterWakeup ();
1480 var
1481 mon: TMonster;
1482 begin
1483 if (monMarkedUID <> -1) then
1484 begin
1485 mon := g_Monsters_ByUID(monMarkedUID);
1486 if (mon <> nil) then mon.WakeUp();
1487 end;
1488 end;
1490 procedure bcPlayerTeleport ();
1491 var
1492 x, y, w, h: Integer;
1493 begin
1494 //e_WriteLog(Format('TELEPORT: (%d,%d)', [pmsCurMapX, pmsCurMapY]), MSG_NOTIFY);
1495 if (gPlayers[0] <> nil) then
1496 begin
1497 gPlayers[0].getMapBox(x, y, w, h);
1498 gPlayers[0].TeleportTo(pmsCurMapX-w div 2, pmsCurMapY-h div 2, true, 69); // 69: don't change dir
1499 end;
1500 end;
1502 procedure dbgToggleTraceBox (arg: Integer=-1); begin if (arg < 0) then showTraceBox := not showTraceBox else showTraceBox := (arg > 0); end;
1504 procedure dbgToggleHolmesPause (arg: Integer=-1); begin if (arg < 0) then g_Game_HolmesPause(not gPauseHolmes) else g_Game_HolmesPause(arg > 0); end;
1506 procedure cbAtcurSelectMonster ();
1507 function monsAtDump (mon: TMonster; tag: Integer): Boolean;
1508 begin
1509 result := true; // stop
1510 e_WriteLog(Format('monster #%d (UID:%u) (proxyid:%d)', [mon.arrIdx, mon.UID, mon.proxyId]), TMsgType.Notify);
1511 monMarkedUID := mon.UID;
1512 dumpPublishedProperties(mon);
1513 end;
1514 var
1515 plr: TPlayer;
1516 x, y, w, h: Integer;
1517 begin
1518 monMarkedUID := -1;
1519 if (Length(gPlayers) > 0) then
1520 begin
1521 plr := gPlayers[0];
1522 if (plr <> nil) then
1523 begin
1524 plr.getMapBox(x, y, w, h);
1525 if (pmsCurMapX >= x) and (pmsCurMapY >= y) and (pmsCurMapX < x+w) and (pmsCurMapY < y+h) then
1526 begin
1527 dumpPublishedProperties(plr);
1528 end;
1529 end;
1530 end;
1531 //e_WriteLog('===========================', MSG_NOTIFY);
1532 monsGrid.forEachAtPoint(pmsCurMapX, pmsCurMapY, monsAtDump);
1533 //e_WriteLog('---------------------------', MSG_NOTIFY);
1534 end;
1536 procedure cbAtcurDumpMonsters ();
1537 function monsAtDump (mon: TMonster; tag: Integer): Boolean;
1538 begin
1539 result := false; // don't stop
1540 e_WriteLog(Format('monster #%d (UID:%u) (proxyid:%d)', [mon.arrIdx, mon.UID, mon.proxyId]), TMsgType.Notify);
1541 end;
1542 begin
1543 e_WriteLog('===========================', TMsgType.Notify);
1544 monsGrid.forEachAtPoint(pmsCurMapX, pmsCurMapY, monsAtDump);
1545 e_WriteLog('---------------------------', TMsgType.Notify);
1546 end;
1548 procedure cbAtcurDumpWalls ();
1549 function wallToggle (pan: TPanel; tag: Integer): Boolean;
1550 begin
1551 result := false; // don't stop
1552 if (platMarkedGUID = -1) then platMarkedGUID := pan.guid;
1553 e_LogWritefln('wall ''%s'' #%d(%d); enabled=%d (%d); (%d,%d)-(%d,%d)', [pan.mapId, pan.arrIdx, pan.proxyId, Integer(pan.Enabled), Integer(mapGrid.proxyEnabled[pan.proxyId]), pan.X, pan.Y, pan.Width, pan.Height]);
1554 dumpPublishedProperties(pan);
1555 end;
1556 var
1557 hasTrigs: Boolean = false;
1558 f: Integer;
1559 trig: PTrigger;
1560 begin
1561 platMarkedGUID := -1;
1562 e_WriteLog('=== TOGGLE WALL ===', TMsgType.Notify);
1563 mapGrid.forEachAtPoint(pmsCurMapX, pmsCurMapY, wallToggle, (GridTagWall or GridTagDoor));
1564 e_WriteLog('--- toggle wall ---', TMsgType.Notify);
1565 if showTriggers then
1566 begin
1567 for f := 0 to High(gTriggers) do
1568 begin
1569 trig := @gTriggers[f];
1570 if (pmsCurMapX >= trig.x) and (pmsCurMapY >= trig.y) and (pmsCurMapX < trig.x+trig.width) and (pmsCurMapY < trig.y+trig.height) then
1571 begin
1572 if not hasTrigs then begin writeln('=== TRIGGERS ==='); hasTrigs := true; end;
1573 writeln('trigger ''', trig.mapId, ''' of type ''', trigType2Str(trig.TriggerType), '''');
1574 end;
1575 end;
1576 if hasTrigs then writeln('--- triggers ---');
1577 end;
1578 end;
1580 procedure cbAtcurToggleWalls ();
1581 function wallToggle (pan: TPanel; tag: Integer): Boolean;
1582 begin
1583 result := false; // don't stop
1584 //e_WriteLog(Format('wall #%d(%d); enabled=%d (%d); (%d,%d)-(%d,%d)', [pan.arrIdx, pan.proxyId, Integer(pan.Enabled), Integer(mapGrid.proxyEnabled[pan.proxyId]), pan.X, pan.Y, pan.Width, pan.Height]), MSG_NOTIFY);
1585 if pan.Enabled then g_Map_DisableWallGUID(pan.guid) else g_Map_EnableWallGUID(pan.guid);
1586 end;
1587 begin
1588 //e_WriteLog('=== TOGGLE WALL ===', MSG_NOTIFY);
1589 mapGrid.forEachAtPoint(pmsCurMapX, pmsCurMapY, wallToggle, (GridTagWall or GridTagDoor));
1590 //e_WriteLog('--- toggle wall ---', MSG_NOTIFY);
1591 end;
1594 // ////////////////////////////////////////////////////////////////////////// //
1595 procedure holmesInitCommands ();
1596 begin
1597 if (cmdlist <> nil) then exit;
1598 cmdAdd('win_layers', toggleLayersWindow, 'toggle layers window', 'window control');
1599 cmdAdd('win_outline', toggleOutlineWindow, 'toggle outline window', 'window control');
1600 cmdAdd('win_help', toggleHelpWindow, 'toggle help window', 'window control');
1601 cmdAdd('win_options', toggleOptionsWindow, 'toggle options window', 'window control');
1603 cmdAdd('mon_think_step', bcOneMonsterThinkStep, 'one monster think step', 'monster control');
1604 cmdAdd('mon_info', bcToggleMonsterInfo, 'toggle monster info', 'monster control');
1605 cmdAdd('mon_los_plr', bcToggleMonsterLOSPlr, 'toggle monster LOS to player', 'monster control');
1606 cmdAdd('mon_cells', bcToggleMonsterCells, 'toggle "show all cells occupied by monsters" (SLOW!)', 'monster control');
1607 cmdAdd('mon_wakeup', bcMonsterWakeup, 'wake up selected monster', 'monster control');
1609 cmdAdd('mon_spawn', bcMonsterSpawn, 'spawn monster', 'monster control');
1611 cmdAdd('mplat_step', bcOneMPlatThinkStep, 'one mplat think step', 'mplat control');
1612 cmdAdd('mplat_toggle', bcMPlatToggle, 'activate/deactivate moving platforms', 'mplat control');
1614 cmdAdd('plr_teleport', bcPlayerTeleport, 'teleport player', 'player control');
1616 cmdAdd('dbg_curpos', bcToggleCurPos, 'toggle "show cursor position on the map"', 'various');
1617 cmdAdd('dbg_grid', bcToggleGrid, 'toggle grid', 'various');
1618 cmdAdd('dbg_triggers', bcToggleDrawTriggers, 'show/hide triggers (SLOW!)', 'various');
1620 cmdAdd('atcur_select_monster', cbAtcurSelectMonster, 'select monster to operate', 'monster control');
1621 cmdAdd('atcur_dump_monsters', cbAtcurDumpMonsters, 'dump monsters in cell', 'monster control');
1622 cmdAdd('atcur_dump_walls', cbAtcurDumpWalls, 'dump walls in cell', 'wall control');
1623 cmdAdd('atcur_disable_walls', cbAtcurToggleWalls, 'disable walls', 'wall control');
1625 cmdAdd('dbg_tracebox', dbgToggleTraceBox, 'test traceBox()', 'player control');
1627 cmdAdd('hlm_pause', dbgToggleHolmesPause, '"Holmes" pause mode', 'game control');
1628 end;
1631 procedure holmesInitBinds ();
1632 var
1633 st: TStream = nil;
1634 pr: TTextParser = nil;
1635 s, kn, v: AnsiString;
1636 kmods: Byte;
1637 mbuts: Byte;
1638 begin
1639 kbS := kbS;
1640 if not keybindsInited then
1641 begin
1642 // keyboard
1643 keybindAdd('F1', 'win_help');
1644 keybindAdd('M-F1', 'win_options');
1645 keybindAdd('C-O', 'win_outline');
1646 keybindAdd('C-L', 'win_layers');
1648 keybindAdd('M-M', 'mon_think_step');
1649 keybindAdd('M-I', 'mon_info');
1650 keybindAdd('M-L', 'mon_los_plr');
1651 keybindAdd('M-G', 'mon_cells');
1652 keybindAdd('M-A', 'mon_wakeup');
1654 keybindAdd('M-P', 'mplat_step');
1655 keybindAdd('M-O', 'mplat_toggle');
1657 keybindAdd('C-T', 'plr_teleport');
1658 keybindAdd('M-T', 'dbg_tracebox');
1660 keybindAdd('C-P', 'dbg_curpos');
1661 keybindAdd('C-G', 'dbg_grid');
1662 keybindAdd('C-X', 'dbg_triggers');
1664 keybindAdd('C-1', 'mon_spawn zombie');
1666 keybindAdd('C-S-P', 'hlm_pause');
1668 // mouse
1669 msbindAdd('LMB', 'atcur_select_monster');
1670 msbindAdd('M-LMB', 'atcur_dump_monsters');
1671 msbindAdd('RMB', 'atcur_dump_walls');
1672 msbindAdd('M-RMB', 'atcur_disable_walls');
1674 // load bindings from file
1675 try
1676 st := openDiskFileRO(GameDir+'holmes.rc');
1677 pr := TFileTextParser.Create(st);
1678 conwriteln('parsing "holmes.rc"...');
1679 while (pr.tokType <> pr.TTEOF) do
1680 begin
1681 s := pr.expectId();
1682 if (s = 'stop') then break
1683 else if (s = 'unbind_keys') then keybinds := nil
1684 else if (s = 'unbind_mouse') then msbinds := nil
1685 else if (s = 'bind') then
1686 begin
1687 if (pr.tokType = pr.TTStr) then s := pr.expectStr(false)
1688 else if (pr.tokType = pr.TTInt) then s := Format('%d', [pr.expectInt()])
1689 else s := pr.expectId();
1691 if (pr.tokType = pr.TTStr) then v := pr.expectStr(false)
1692 else if (pr.tokType = pr.TTInt) then v := Format('%d', [pr.expectInt()])
1693 else v := pr.expectId();
1695 kn := parseModKeys(s, kmods, mbuts);
1696 if (CompareText(kn, 'lmb') = 0) or (CompareText(kn, 'rmb') = 0) or (CompareText(kn, 'mmb') = 0) or (CompareText(kn, 'None') = 0) then
1697 begin
1698 msbindAdd(s, v);
1699 end
1700 else
1701 begin
1702 keybindAdd(s, v);
1703 end;
1704 end;
1705 end;
1706 except on e: Exception do // sorry
1707 if (pr <> nil) then conwritefln('Holmes config parse error at (%s,%s): %s', [pr.tokLine, pr.tokCol, e.message]);
1708 end;
1709 if (pr <> nil) then pr.Free() else st.Free(); // ownership
1710 end;
1711 end;
1714 procedure onMouseEvent (var ev: THMouseEvent);
1715 begin
1716 if not g_holmes_enabled then exit;
1717 g_Holmes_MouseEvent(ev);
1718 end;
1720 procedure onKeyEvent (var ev: THKeyEvent);
1721 begin
1722 if not g_holmes_enabled then exit;
1723 g_Holmes_KeyEvent(ev);
1724 end;
1727 begin
1728 evMouseCB := onMouseEvent;
1729 evKeyCB := onKeyEvent;
1731 conRegVar('hlm_ui_scale', @fuiRenderScale, 0.01, 5.0, 'Holmes UI scale', '', false);
1732 end.