DEADSOFTWARE

moved some geometry functions to shared/geom.pas; TODO: move most of g_basic.pas...
[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 mempool, 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;
29 type
30 THMouseEvent = record
31 public
32 const
33 // both for but and for bstate
34 Left = $0001;
35 Right = $0002;
36 Middle = $0004;
37 WheelUp = $0008;
38 WheelDown = $0010;
40 // event types
41 Release = 0;
42 Press = 1;
43 Motion = 2;
45 public
46 kind: Byte; // motion, press, release
47 x, y: Integer;
48 dx, dy: Integer; // for wheel this is wheel motion, otherwise this is relative mouse motion
49 but: Word; // current pressed/released button, or 0 for motion
50 bstate: Word; // button state
51 kstate: Word; // keyboard state (see THKeyEvent);
52 end;
54 THKeyEvent = record
55 public
56 const
57 // modifiers
58 ModCtrl = $0001;
59 ModAlt = $0002;
60 ModShift = $0004;
62 // event types
63 Release = 0;
64 Press = 1;
66 public
67 kind: Byte;
68 scan: Word; // SDL_SCANCODE_XXX
69 sym: Word; // SDLK_XXX
70 bstate: Word; // button state
71 kstate: Word; // keyboard state
73 public
74 end;
76 procedure g_Holmes_VidModeChanged ();
77 procedure g_Holmes_WindowFocused ();
78 procedure g_Holmes_WindowBlured ();
80 procedure g_Holmes_Draw ();
81 procedure g_Holmes_DrawUI ();
83 function g_Holmes_MouseEvent (var ev: THMouseEvent): Boolean; // returns `true` if event was eaten
84 function g_Holmes_KeyEvent (var ev: THKeyEvent): Boolean; // returns `true` if event was eaten
86 // hooks for player
87 procedure g_Holmes_plrViewPos (viewPortX, viewPortY: Integer);
88 procedure g_Holmes_plrViewSize (viewPortW, viewPortH: Integer);
89 procedure g_Holmes_plrLaser (ax0, ay0, ax1, ay1: Integer);
92 operator = (constref ev: THKeyEvent; const s: AnsiString): Boolean;
93 operator = (const s: AnsiString; constref ev: THKeyEvent): Boolean;
95 operator = (constref ev: THMouseEvent; const s: AnsiString): Boolean;
96 operator = (const s: AnsiString; constref ev: THMouseEvent): Boolean;
99 var
100 g_holmes_enabled: Boolean = {$IF DEFINED(D2F_DEBUG)}true{$ELSE}false{$ENDIF};
101 g_holmes_ui_scale: Single = 1.0;
104 implementation
106 uses
107 {rttiobj,} typinfo, e_texture,
108 SysUtils, Classes, GL, SDL2,
109 MAPDEF, g_main, g_options,
110 utils, hashtable, xparser;
113 var
114 //globalInited: Boolean = false;
115 msX: Integer = -666;
116 msY: Integer = -666;
117 msB: Word = 0; // button state
118 kbS: Word = 0; // keyboard modifiers state
119 showGrid: Boolean = {$IF DEFINED(D2F_DEBUG)}false{$ELSE}false{$ENDIF};
120 showMonsInfo: Boolean = false;
121 showMonsLOS2Plr: Boolean = false;
122 showAllMonsCells: Boolean = false;
123 showMapCurPos: Boolean = false;
124 showLayersWindow: Boolean = false;
125 showOutlineWindow: Boolean = false;
126 showTriggers: Boolean = {$IF DEFINED(D2F_DEBUG)}false{$ELSE}false{$ENDIF};
127 showTraceBox: Boolean = {$IF DEFINED(D2F_DEBUG)}false{$ELSE}false{$ENDIF};
130 // ////////////////////////////////////////////////////////////////////////// //
131 {$INCLUDE g_holmes.inc}
132 {$INCLUDE g_holmes_ui.inc}
135 // ////////////////////////////////////////////////////////////////////////// //
136 // any mods = 255: nothing was defined
137 function parseModKeys (const s: AnsiString; out kmods: Byte; out mbuts: Byte): AnsiString;
138 var
139 pos, epos: Integer;
140 begin
141 kmods := 255;
142 mbuts := 255;
143 pos := 1;
144 //while (pos <= Length(s)) and (s[pos] <= ' ') do Inc(pos);
145 if (pos < Length(s)) and ((s[pos] = '+') or (s[pos] = '-') or (s[pos] = '*')) then Inc(pos);
146 while (pos < Length(s)) do
147 begin
148 if (Length(s)-pos >= 2) and (s[pos+1] = '-') then
149 begin
150 case s[pos] of
151 'C', 'c': begin if (kmods = 255) then kmods := 0; kmods := kmods or THKeyEvent.ModCtrl; Inc(pos, 2); continue; end;
152 'M', 'm': begin if (kmods = 255) then kmods := 0; kmods := kmods or THKeyEvent.ModAlt; Inc(pos, 2); continue; end;
153 'S', 's': begin if (kmods = 255) then kmods := 0; kmods := kmods or THKeyEvent.ModShift; Inc(pos, 2); continue; end;
154 end;
155 break;
156 end;
157 if (Length(s)-pos >= 4) and ((s[pos+1] = 'M') or (s[pos+1] = 'm')) and ((s[pos+2] = 'B') or (s[pos+1] = 'b')) and (s[pos+3] = '-') then
158 begin
159 case s[pos] of
160 'L', 'l': begin if (mbuts = 255) then mbuts := 0; mbuts := mbuts or THMouseEvent.Left; Inc(pos, 4); continue; end;
161 'R', 'r': begin if (mbuts = 255) then mbuts := 0; mbuts := mbuts or THMouseEvent.Right; Inc(pos, 4); continue; end;
162 'M', 'm': begin if (mbuts = 255) then mbuts := 0; mbuts := mbuts or THMouseEvent.Middle; Inc(pos, 4); continue; end;
163 end;
164 break;
165 end;
166 break;
167 end;
168 epos := Length(s)+1;
169 while (epos > pos) and (s[epos-1] <= ' ') do Dec(epos);
170 if (epos > pos) then result := Copy(s, pos, epos-pos) else result := '';
171 end;
174 operator = (constref ev: THKeyEvent; const s: AnsiString): Boolean;
175 var
176 f: Integer;
177 kmods: Byte = 255;
178 mbuts: Byte = 255;
179 kname: AnsiString;
180 begin
181 result := false;
182 if (Length(s) > 0) then
183 begin
184 if (s[1] = '+') then begin if (ev.kind <> ev.Press) then exit; end
185 else if (s[1] = '-') then begin if (ev.kind <> ev.Release) then exit; end
186 else if (s[1] = '*') then begin end
187 else if (ev.kind <> ev.Press) then exit;
188 end;
189 kname := parseModKeys(s, kmods, mbuts);
190 if (kmods = 255) then kmods := 0;
191 if (ev.kstate <> kmods) then exit;
192 if (mbuts <> 255) and (ev.bstate <> mbuts) then exit;
193 for f := 1 to High(e_KeyNames) do
194 begin
195 if (CompareText(kname, e_KeyNames[f]) = 0) then
196 begin
197 result := (ev.scan = f);
198 exit;
199 end;
200 end;
201 end;
204 operator = (const s: AnsiString; constref ev: THKeyEvent): Boolean;
205 begin
206 result := (ev = s);
207 end;
210 operator = (constref ev: THMouseEvent; const s: AnsiString): Boolean;
211 var
212 kmods: Byte = 255;
213 mbuts: Byte = 255;
214 kname: AnsiString;
215 but: Integer = -1;
216 begin
217 result := false;
219 if (Length(s) > 0) then
220 begin
221 if (s[1] = '+') then begin if (ev.kind <> ev.Press) then exit; end
222 else if (s[1] = '-') then begin if (ev.kind <> ev.Release) then exit; end
223 else if (s[1] = '*') then begin if (ev.kind <> ev.Motion) then exit; end
224 else if (ev.kind <> ev.Press) then exit;
225 end;
227 kname := parseModKeys(s, kmods, mbuts);
228 if (CompareText(kname, 'LMB') = 0) then but := THMouseEvent.Left
229 else if (CompareText(kname, 'RMB') = 0) then but := THMouseEvent.Right
230 else if (CompareText(kname, 'MMB') = 0) then but := THMouseEvent.Middle
231 else if (CompareText(kname, 'None') = 0) then but := 0
232 else exit;
234 //conwritefln('s=[%s]; kname=[%s]; kmods=%s; mbuts=%s; but=%s', [s, kname, kmods, mbuts, but]);
236 if (mbuts = 255) then mbuts := 0;
237 if (kmods = 255) then kmods := 0;
238 if (ev.kstate <> kmods) then exit;
240 if (ev.kind = ev.Press) then mbuts := mbuts or but
241 else if (ev.kind = ev.Release) then mbuts := mbuts and (not but);
243 //conwritefln(' ev.bstate=%s; ev.but=%s; mbuts=%s', [ev.bstate, ev.but, mbuts]);
245 result := (ev.bstate = mbuts) and (ev.but = but);
246 end;
249 operator = (const s: AnsiString; constref ev: THMouseEvent): Boolean;
250 begin
251 result := (ev = s);
252 end;
255 // ////////////////////////////////////////////////////////////////////////// //
256 function typeKind2Str (t: TTypeKind): AnsiString;
257 begin
258 case t of
259 tkUnknown: result := 'Unknown';
260 tkInteger: result := 'Integer';
261 tkChar: result := 'Char';
262 tkEnumeration: result := 'Enumeration';
263 tkFloat: result := 'Float';
264 tkSet: result := 'Set';
265 tkMethod: result := 'Method';
266 tkSString: result := 'SString';
267 tkLString: result := 'LString';
268 tkAString: result := 'AString';
269 tkWString: result := 'WString';
270 tkVariant: result := 'Variant';
271 tkArray: result := 'Array';
272 tkRecord: result := 'Record';
273 tkInterface: result := 'Interface';
274 tkClass: result := 'Class';
275 tkObject: result := 'Object';
276 tkWChar: result := 'WChar';
277 tkBool: result := 'Bool';
278 tkInt64: result := 'Int64';
279 tkQWord: result := 'QWord';
280 tkDynArray: result := 'DynArray';
281 tkInterfaceRaw: result := 'InterfaceRaw';
282 tkProcVar: result := 'ProcVar';
283 tkUString: result := 'UString';
284 tkUChar: result := 'UChar';
285 tkHelper: result := 'Helper';
286 tkFile: result := 'File';
287 tkClassRef: result := 'ClassRef';
288 tkPointer: result := 'Pointer';
289 else result := '<unknown>';
290 end;
291 end;
294 procedure dumpPublishedProperties (obj: TObject);
295 var
296 pt: PTypeData;
297 pi: PTypeInfo;
298 i, j: Integer;
299 pp: PPropList;
300 begin
301 if (obj = nil) then exit;
302 e_LogWritefln('Object of type ''%s'':', [obj.ClassName]);
303 pi := obj.ClassInfo;
304 pt := GetTypeData(pi);
305 e_LogWritefln('property count: %s', [pt.PropCount]);
306 GetMem(pp, pt^.PropCount*sizeof(Pointer));
307 try
308 j := GetPropList(pi, [tkInteger, tkBool, tkSString, tkLString, tkAString, tkSet, tkEnumeration], pp);
309 //e_LogWritefln('ordinal property count: %s', [j]);
310 for i := 0 to j-1 do
311 begin
312 if (typinfo.PropType(obj, pp^[i].name) in [tkSString, tkLString, tkAString]) then
313 begin
314 e_LogWritefln(' #%s: <%s>; type: %s; value: <%s>', [i+1, pp^[i].name, typeKind2Str(typinfo.PropType(obj, pp^[i].name)), GetStrProp(obj, pp^[i])]);
315 end
316 else if (typinfo.PropType(obj, pp^[i].name) = tkSet) then
317 begin
318 e_LogWritefln(' #%s: <%s>; type: %s; value: %s', [i+1, pp^[i].name, typeKind2Str(typinfo.PropType(obj, pp^[i].name)), GetSetProp(obj, pp^[i], true)]);
319 end
320 else if (typinfo.PropType(obj, pp^[i].name) = tkEnumeration) then
321 begin
322 e_LogWritefln(' #%s: <%s>; type: %s; value: <%s>', [i+1, pp^[i].name, typeKind2Str(typinfo.PropType(obj, pp^[i].name)), GetEnumProp(obj, pp^[i])]);
323 end
324 else
325 begin
326 e_LogWritefln(' #%s: <%s>; type: %s; value: %s', [i+1, pp^[i].name, typeKind2Str(typinfo.PropType(obj, pp^[i].name)), GetOrdProp(obj, pp^[i])]);
327 end;
328 end;
329 finally
330 FreeMem(pp);
331 end;
332 end;
335 //FIXME: autogenerate
336 function trigType2Str (ttype: Integer): AnsiString;
337 begin
338 result := '<unknown>';
339 case ttype of
340 TRIGGER_NONE: result := 'none';
341 TRIGGER_EXIT: result := 'exit';
342 TRIGGER_TELEPORT: result := 'teleport';
343 TRIGGER_OPENDOOR: result := 'opendoor';
344 TRIGGER_CLOSEDOOR: result := 'closedoor';
345 TRIGGER_DOOR: result := 'door';
346 TRIGGER_DOOR5: result := 'door5';
347 TRIGGER_CLOSETRAP: result := 'closetrap';
348 TRIGGER_TRAP: result := 'trap';
349 TRIGGER_PRESS: result := 'press';
350 TRIGGER_SECRET: result := 'secret';
351 TRIGGER_LIFTUP: result := 'liftup';
352 TRIGGER_LIFTDOWN: result := 'liftdown';
353 TRIGGER_LIFT: result := 'lift';
354 TRIGGER_TEXTURE: result := 'texture';
355 TRIGGER_ON: result := 'on';
356 TRIGGER_OFF: result := 'off';
357 TRIGGER_ONOFF: result := 'onoff';
358 TRIGGER_SOUND: result := 'sound';
359 TRIGGER_SPAWNMONSTER: result := 'spawnmonster';
360 TRIGGER_SPAWNITEM: result := 'spawnitem';
361 TRIGGER_MUSIC: result := 'music';
362 TRIGGER_PUSH: result := 'push';
363 TRIGGER_SCORE: result := 'score';
364 TRIGGER_MESSAGE: result := 'message';
365 TRIGGER_DAMAGE: result := 'damage';
366 TRIGGER_HEALTH: result := 'health';
367 TRIGGER_SHOT: result := 'shot';
368 TRIGGER_EFFECT: result := 'effect';
369 TRIGGER_SCRIPT: result := 'script';
370 end;
371 end;
373 // ////////////////////////////////////////////////////////////////////////// //
374 {$INCLUDE g_holmes_cmd.inc}
375 procedure holmesInitCommands (); forward;
376 procedure holmesInitBinds (); forward;
379 // ////////////////////////////////////////////////////////////////////////// //
380 var
381 g_ol_nice: Boolean = false;
382 g_ol_fill_walls: Boolean = false;
383 g_ol_rlayer_back: Boolean = false;
384 g_ol_rlayer_step: Boolean = false;
385 g_ol_rlayer_wall: Boolean = false;
386 g_ol_rlayer_door: Boolean = false;
387 g_ol_rlayer_acid1: Boolean = false;
388 g_ol_rlayer_acid2: Boolean = false;
389 g_ol_rlayer_water: Boolean = false;
390 g_ol_rlayer_fore: Boolean = false;
393 // ////////////////////////////////////////////////////////////////////////// //
394 var
395 winHelp: THTopWindow = nil;
396 winOptions: THTopWindow = nil;
397 winLayers: THTopWindow = nil;
398 winOutlines: THTopWindow = nil;
401 procedure createHelpWindow (); forward;
402 procedure createOptionsWindow (); forward;
403 procedure createLayersWindow (); forward;
404 procedure createOutlinesWindow (); forward;
407 procedure toggleLayersWindowCB (me: THControl; checked: Integer);
408 begin
409 if showLayersWindow then
410 begin
411 if (winLayers = nil) then createLayersWindow();
412 uiAddWindow(winLayers);
413 end
414 else
415 begin
416 uiRemoveWindow(winLayers);
417 end;
418 end;
421 procedure toggleOutlineWindowCB (me: THControl; checked: Integer);
422 begin
423 if showOutlineWindow then
424 begin
425 if (winOutlines = nil) then createOutlinesWindow();
426 uiAddWindow(winOutlines);
427 end
428 else
429 begin
430 uiRemoveWindow(winOutlines);
431 end;
432 end;
435 procedure createHelpWindow ();
436 var
437 llb: THCtlSimpleText;
438 slist: array of AnsiString = nil;
439 cmd: PHolmesCommand;
440 bind: THolmesBinding;
441 f, maxkeylen: Integer;
442 s: AnsiString;
443 begin
444 for cmd in cmdlist do cmd.helpmark := false;
446 maxkeylen := 0;
447 for bind in keybinds do
448 begin
449 if (Length(bind.key) = 0) then continue;
450 if cmdlist.get(bind.cmdName, cmd) then
451 begin
452 if (Length(cmd.help) > 0) then
453 begin
454 cmd.helpmark := true;
455 if (maxkeylen < Length(bind.key)) then maxkeylen := Length(bind.key);
456 end;
457 end;
458 end;
460 for cmd in cmdlist do
461 begin
462 if not cmd.helpmark then continue;
463 if (Length(cmd.help) = 0) then continue;
464 f := 0;
465 while (f < Length(slist)) and (CompareText(slist[f], cmd.section) <> 0) do Inc(f);
466 if (f = Length(slist)) then
467 begin
468 SetLength(slist, Length(slist)+1);
469 slist[High(slist)] := cmd.section;
470 end;
471 end;
473 llb := THCtlSimpleText.Create(0, 0);
474 for f := 0 to High(slist) do
475 begin
476 if (f > 0) then llb.appendItem('');
477 llb.appendItem(slist[f], true, true);
478 for cmd in cmdlist do
479 begin
480 if not cmd.helpmark then continue;
481 if (CompareText(cmd.section, slist[f]) <> 0) then continue;
482 for bind in keybinds do
483 begin
484 if (Length(bind.key) = 0) then continue;
485 if (cmd.name = bind.cmdName) then
486 begin
487 s := bind.key;
488 while (Length(s) < maxkeylen) do s += ' ';
489 s := ' '+s+' -- '+cmd.help;
490 llb.appendItem(s);
491 end;
492 end;
493 end;
494 end;
496 maxkeylen := 0;
497 for bind in msbinds do
498 begin
499 if (Length(bind.key) = 0) then continue;
500 if cmdlist.get(bind.cmdName, cmd) then
501 begin
502 if (Length(cmd.help) > 0) then
503 begin
504 cmd.helpmark := true;
505 if (maxkeylen < Length(bind.key)) then maxkeylen := Length(bind.key);
506 end;
507 end;
508 end;
510 llb.appendItem('');
511 llb.appendItem('mouse', true, true);
512 for bind in msbinds do
513 begin
514 if (Length(bind.key) = 0) then continue;
515 if cmdlist.get(bind.cmdName, cmd) then
516 begin
517 if (Length(cmd.help) > 0) then
518 begin
519 s := bind.key;
520 while (Length(s) < maxkeylen) do s += ' ';
521 s := ' '+s+' -- '+cmd.help;
522 llb.appendItem(s);
523 end;
524 end;
525 end;
527 winHelp := THTopWindow.Create('Holmes Help', 10, 10);
528 winHelp.escClose := true;
529 winHelp.appendChild(llb);
530 winHelp.centerInScreen();
531 end;
534 procedure winLayersClosed (me: THControl; dummy: Integer); begin showLayersWindow := false; end;
535 procedure winOutlinesClosed (me: THControl; dummy: Integer); begin showOutlineWindow := false; end;
537 procedure createLayersWindow ();
538 var
539 llb: THCtlCBListBox;
540 begin
541 llb := THCtlCBListBox.Create(0, 0);
542 llb.appendItem('background', @g_rlayer_back);
543 llb.appendItem('steps', @g_rlayer_step);
544 llb.appendItem('walls', @g_rlayer_wall);
545 llb.appendItem('doors', @g_rlayer_door);
546 llb.appendItem('acid1', @g_rlayer_acid1);
547 llb.appendItem('acid2', @g_rlayer_acid2);
548 llb.appendItem('water', @g_rlayer_water);
549 llb.appendItem('foreground', @g_rlayer_fore);
550 winLayers := THTopWindow.Create('layers', 10, 10);
551 winLayers.escClose := true;
552 winLayers.appendChild(llb);
553 winLayers.closeCB := winLayersClosed;
554 end;
557 procedure createOutlinesWindow ();
558 var
559 llb: THCtlCBListBox;
560 begin
561 llb := THCtlCBListBox.Create(0, 0);
562 llb.appendItem('background', @g_ol_rlayer_back);
563 llb.appendItem('steps', @g_ol_rlayer_step);
564 llb.appendItem('walls', @g_ol_rlayer_wall);
565 llb.appendItem('doors', @g_ol_rlayer_door);
566 llb.appendItem('acid1', @g_ol_rlayer_acid1);
567 llb.appendItem('acid2', @g_ol_rlayer_acid2);
568 llb.appendItem('water', @g_ol_rlayer_water);
569 llb.appendItem('foreground', @g_ol_rlayer_fore);
570 llb.appendItem('OPTIONS', nil);
571 llb.appendItem('fill walls', @g_ol_fill_walls);
572 llb.appendItem('contours', @g_ol_nice);
573 winOutlines := THTopWindow.Create('outlines', 100, 10);
574 winOutlines.escClose := true;
575 winOutlines.appendChild(llb);
576 winOutlines.closeCB := winOutlinesClosed;
577 end;
580 procedure createOptionsWindow ();
581 var
582 llb: THCtlCBListBox;
583 begin
584 llb := THCtlCBListBox.Create(0, 0);
585 llb.appendItem('map grid', @showGrid);
586 llb.appendItem('cursor position on map', @showMapCurPos);
587 llb.appendItem('monster info', @showMonsInfo);
588 llb.appendItem('monster LOS to player', @showMonsLOS2Plr);
589 llb.appendItem('monster cells (SLOW!)', @showAllMonsCells);
590 llb.appendItem('draw triggers (SLOW!)', @showTriggers);
591 llb.appendItem('WINDOWS', nil);
592 llb.appendItem('layers window', @showLayersWindow, toggleLayersWindowCB);
593 llb.appendItem('outline window', @showOutlineWindow, toggleOutlineWindowCB);
594 winOptions := THTopWindow.Create('Holmes Options', 100, 100);
595 winOptions.escClose := true;
596 winOptions.appendChild(llb);
597 winOptions.centerInScreen();
598 end;
601 procedure toggleLayersWindow (arg: Integer=-1);
602 begin
603 showLayersWindow := not showLayersWindow;
604 toggleLayersWindowCB(nil, 0);
605 end;
607 procedure toggleOutlineWindow (arg: Integer=-1);
608 begin
609 showOutlineWindow := not showOutlineWindow;
610 toggleOutlineWindowCB(nil, 0);
611 end;
613 procedure toggleHelpWindow (arg: Integer=-1);
614 begin
615 if (winHelp = nil) then createHelpWindow();
616 if not uiVisibleWindow(winHelp) then uiAddWindow(winHelp) else uiRemoveWindow(winHelp);
617 end;
619 procedure toggleOptionsWindow (arg: Integer=-1);
620 begin
621 if (winOptions = nil) then createOptionsWindow();
622 if not uiVisibleWindow(winOptions) then uiAddWindow(winOptions) else uiRemoveWindow(winOptions);
623 end;
626 // ////////////////////////////////////////////////////////////////////////// //
627 procedure g_Holmes_VidModeChanged ();
628 begin
629 e_WriteLog(Format('Holmes: videomode changed: %dx%d', [gScreenWidth, gScreenHeight]), TMsgType.Notify);
630 // texture space is possibly lost here, idc
631 curtexid := 0;
632 font6texid := 0;
633 font8texid := 0;
634 prfont6texid := 0;
635 prfont8texid := 0;
636 //createCursorTexture();
637 end;
639 procedure g_Holmes_WindowFocused ();
640 begin
641 msB := 0;
642 kbS := 0;
643 end;
645 procedure g_Holmes_WindowBlured ();
646 begin
647 end;
650 // ////////////////////////////////////////////////////////////////////////// //
651 var
652 vpSet: Boolean = false;
653 vpx, vpy: Integer;
654 vpw, vph: Integer;
655 laserSet: Boolean = false;
656 laserX0, laserY0, laserX1, laserY1: Integer;
657 monMarkedUID: Integer = -1;
658 platMarkedGUID: Integer = -1;
661 procedure g_Holmes_plrViewPos (viewPortX, viewPortY: Integer);
662 begin
663 vpSet := true;
664 vpx := viewPortX;
665 vpy := viewPortY;
666 end;
668 procedure g_Holmes_plrViewSize (viewPortW, viewPortH: Integer);
669 begin
670 vpSet := true;
671 vpw := viewPortW;
672 vph := viewPortH;
673 end;
675 procedure g_Holmes_plrLaser (ax0, ay0, ax1, ay1: Integer);
676 begin
677 laserSet := true;
678 laserX0 := ax0;
679 laserY0 := ay0;
680 laserX1 := ax1;
681 laserY1 := ay1;
682 laserSet := laserSet; // shut up, fpc!
683 end;
686 function pmsCurMapX (): Integer; inline; begin result := round(msX/g_dbg_scale)+vpx; end;
687 function pmsCurMapY (): Integer; inline; begin result := round(msY/g_dbg_scale)+vpy; end;
690 procedure plrDebugMouse (var ev: THMouseEvent);
691 begin
692 //e_WriteLog(Format('mouse: x=%d; y=%d; but=%d; bstate=%d', [msx, msy, but, bstate]), MSG_NOTIFY);
693 if (gPlayer1 = nil) or not vpSet then exit;
694 //if (ev.kind <> THMouseEvent.Press) then exit;
695 //e_WriteLog(Format('mev: %d', [Integer(ev.kind)]), MSG_NOTIFY);
696 msbindExecute(ev);
697 end;
700 var
701 edgeBmp: array of Byte = nil;
704 procedure drawOutlines ();
705 var
706 r, g, b: Integer;
708 procedure clearEdgeBmp ();
709 begin
710 SetLength(edgeBmp, (gWinSizeX+4)*(gWinSizeY+4));
711 FillChar(edgeBmp[0], Length(edgeBmp)*sizeof(edgeBmp[0]), 0);
712 end;
714 procedure drawPanel (pan: TPanel);
715 var
716 sx, len, y0, y1: Integer;
717 begin
718 if (pan = nil) or (pan.Width < 1) or (pan.Height < 1) then exit;
719 if (pan.X+pan.Width <= vpx-1) or (pan.Y+pan.Height <= vpy-1) then exit;
720 if (pan.X >= vpx+vpw+1) or (pan.Y >= vpy+vph+1) then exit;
721 if g_ol_nice or g_ol_fill_walls then
722 begin
723 sx := pan.X-(vpx-1);
724 len := pan.Width;
725 if (len > gWinSizeX+4) then len := gWinSizeX+4;
726 if (sx < 0) then begin len += sx; sx := 0; end;
727 if (sx+len > gWinSizeX+4) then len := gWinSizeX+4-sx;
728 if (len < 1) then exit;
729 assert(sx >= 0);
730 assert(sx+len <= gWinSizeX+4);
731 y0 := pan.Y-(vpy-1);
732 y1 := y0+pan.Height;
733 if (y0 < 0) then y0 := 0;
734 if (y1 > gWinSizeY+4) then y1 := gWinSizeY+4;
735 while (y0 < y1) do
736 begin
737 FillChar(edgeBmp[y0*(gWinSizeX+4)+sx], len*sizeof(edgeBmp[0]), 1);
738 Inc(y0);
739 end;
740 end
741 else
742 begin
743 drawRect(pan.X, pan.Y, pan.Width, pan.Height, r, g, b);
744 end;
745 end;
747 var
748 lsx: Integer = -1;
749 lex: Integer = -1;
750 lsy: Integer = -1;
752 procedure flushLine ();
753 begin
754 if (lsy > 0) and (lsx > 0) then
755 begin
756 if (lex = lsx) then
757 begin
758 glBegin(GL_POINTS);
759 glVertex2f(lsx-1+vpx+0.37, lsy-1+vpy+0.37);
760 glEnd();
761 end
762 else
763 begin
764 glBegin(GL_LINES);
765 glVertex2f(lsx-1+vpx+0.37, lsy-1+vpy+0.37);
766 glVertex2f(lex-0+vpx+0.37, lsy-1+vpy+0.37);
767 glEnd();
768 end;
769 end;
770 lsx := -1;
771 lex := -1;
772 end;
774 procedure startLine (y: Integer);
775 begin
776 flushLine();
777 lsy := y;
778 end;
780 procedure putPixel (x: Integer);
781 begin
782 if (x < 1) then exit;
783 if (lex+1 <> x) then flushLine();
784 if (lsx < 0) then lsx := x;
785 lex := x;
786 end;
788 procedure drawEdges ();
789 var
790 x, y: Integer;
791 a: PByte;
792 begin
793 glDisable(GL_BLEND);
794 glDisable(GL_TEXTURE_2D);
795 glLineWidth(1);
796 glPointSize(1);
797 glDisable(GL_LINE_SMOOTH);
798 glDisable(GL_POLYGON_SMOOTH);
799 glColor4f(r/255.0, g/255.0, b/255.0, 1.0);
800 for y := 1 to vph do
801 begin
802 a := @edgeBmp[y*(gWinSizeX+4)+1];
803 startLine(y);
804 for x := 1 to vpw do
805 begin
806 if (a[0] <> 0) then
807 begin
808 if (a[-1] = 0) or (a[1] = 0) or (a[-(gWinSizeX+4)] = 0) or (a[gWinSizeX+4] = 0) or
809 (a[-(gWinSizeX+4)-1] = 0) or (a[-(gWinSizeX+4)+1] = 0) or
810 (a[gWinSizeX+4-1] = 0) or (a[gWinSizeX+4+1] = 0) then
811 begin
812 putPixel(x);
813 end;
814 end;
815 Inc(a);
816 end;
817 flushLine();
818 end;
819 end;
821 procedure drawFilledWalls ();
822 var
823 x, y: Integer;
824 a: PByte;
825 begin
826 glDisable(GL_BLEND);
827 glDisable(GL_TEXTURE_2D);
828 glLineWidth(1);
829 glPointSize(1);
830 glDisable(GL_LINE_SMOOTH);
831 glDisable(GL_POLYGON_SMOOTH);
832 glColor4f(r/255.0, g/255.0, b/255.0, 1.0);
833 for y := 1 to vph do
834 begin
835 a := @edgeBmp[y*(gWinSizeX+4)+1];
836 startLine(y);
837 for x := 1 to vpw do
838 begin
839 if (a[0] <> 0) then putPixel(x);
840 Inc(a);
841 end;
842 flushLine();
843 end;
844 end;
846 procedure doWallsOld (parr: array of TPanel; ptype: Word; ar, ag, ab: Integer);
847 var
848 f: Integer;
849 pan: TPanel;
850 begin
851 r := ar;
852 g := ag;
853 b := ab;
854 if g_ol_nice or g_ol_fill_walls then clearEdgeBmp();
855 for f := 0 to High(parr) do
856 begin
857 pan := parr[f];
858 if (pan = nil) or not pan.Enabled or (pan.Width < 1) or (pan.Height < 1) then continue;
859 if ((pan.PanelType and ptype) = 0) then continue;
860 drawPanel(pan);
861 end;
862 if g_ol_nice then drawEdges();
863 if g_ol_fill_walls then drawFilledWalls();
864 end;
866 var
867 xptag: Word;
869 function doWallCB (pan: TPanel; tag: Integer): Boolean;
870 begin
871 result := false; // don't stop
872 //if (pan = nil) or not pan.Enabled or (pan.Width < 1) or (pan.Height < 1) then exit;
873 if ((pan.PanelType and xptag) = 0) then exit;
874 drawPanel(pan);
875 end;
877 procedure doWalls (parr: array of TPanel; ptype: Word; ar, ag, ab: Integer);
878 begin
879 r := ar;
880 g := ag;
881 b := ab;
882 xptag := ptype;
883 if ((ptype and (PANEL_WALL or PANEL_OPENDOOR or PANEL_CLOSEDOOR)) <> 0) then ptype := GridTagWall or GridTagDoor
884 else panelTypeToTag(ptype);
885 if g_ol_nice or g_ol_fill_walls then clearEdgeBmp();
886 mapGrid.forEachInAABB(vpx-1, vpy-1, vpw+2, vph+2, doWallCB, ptype);
887 if g_ol_nice then drawEdges();
888 if g_ol_fill_walls then drawFilledWalls();
889 end;
891 begin
892 if g_ol_rlayer_back then doWallsOld(gRenderBackgrounds, PANEL_BACK, 255, 127, 0);
893 if g_ol_rlayer_step then doWallsOld(gSteps, PANEL_STEP, 192, 192, 192);
894 if g_ol_rlayer_wall then doWallsOld(gWalls, PANEL_WALL, 255, 255, 255);
895 if g_ol_rlayer_door then doWallsOld(gWalls, PANEL_OPENDOOR or PANEL_CLOSEDOOR, 0, 255, 0);
896 if g_ol_rlayer_acid1 then doWallsOld(gAcid1, PANEL_ACID1, 255, 0, 0);
897 if g_ol_rlayer_acid2 then doWallsOld(gAcid2, PANEL_ACID2, 198, 198, 0);
898 if g_ol_rlayer_water then doWallsOld(gWater, PANEL_WATER, 0, 255, 255);
899 if g_ol_rlayer_fore then doWallsOld(gRenderForegrounds, PANEL_FORE, 210, 210, 210);
900 end;
903 procedure plrDebugDraw ();
904 procedure drawTileGrid ();
905 var
906 x, y: Integer;
907 begin
908 for y := 0 to (mapGrid.gridHeight div mapGrid.tileSize) do
909 begin
910 drawLine(mapGrid.gridX0, mapGrid.gridY0+y*mapGrid.tileSize, mapGrid.gridX0+mapGrid.gridWidth, mapGrid.gridY0+y*mapGrid.tileSize, 96, 96, 96, 255);
911 end;
913 for x := 0 to (mapGrid.gridWidth div mapGrid.tileSize) do
914 begin
915 drawLine(mapGrid.gridX0+x*mapGrid.tileSize, mapGrid.gridY0, mapGrid.gridX0+x*mapGrid.tileSize, mapGrid.gridY0+y*mapGrid.gridHeight, 96, 96, 96, 255);
916 end;
917 end;
919 procedure drawAwakeCells ();
920 var
921 x, y: Integer;
922 begin
923 for y := 0 to (mapGrid.gridHeight div mapGrid.tileSize) do
924 begin
925 for x := 0 to (mapGrid.gridWidth div mapGrid.tileSize) do
926 begin
927 if awmIsSetHolmes(x*mapGrid.tileSize+mapGrid.gridX0+1, y*mapGrid.tileSize++mapGrid.gridY0+1) then
928 begin
929 fillRect(x*mapGrid.tileSize++mapGrid.gridX0, y*mapGrid.tileSize++mapGrid.gridY0, monsGrid.tileSize, monsGrid.tileSize, 128, 0, 128, 64);
930 end;
931 end;
932 end;
933 end;
935 procedure drawTraceBox ();
936 var
937 plr: TPlayer;
938 px, py, pw, ph: Integer;
939 pdx, pdy: Integer;
940 ex, ey: Integer;
941 pan: TPanel;
942 begin
943 if (Length(gPlayers) < 1) then exit;
944 plr := gPlayers[0];
945 if (plr = nil) then exit;
946 plr.getMapBox(px, py, pw, ph);
947 drawRect(px, py, pw, ph, 255, 0, 255, 200);
948 pdx := pmsCurMapX-(px+pw div 2);
949 pdy := pmsCurMapY-(py+ph div 2);
950 drawLine(px+pw div 2, py+ph div 2, px+pw div 2+pdx, py+ph div 2+pdy, 255, 0, 255, 200);
951 pan := mapGrid.traceBox(ex, ey, px, py, pw, ph, pdx, pdy, nil, GridTagObstacle);
952 if (pan = nil) then
953 begin
954 drawRect(px+pdx, py+pdy, pw, ph, 255, 255, 255, 180);
955 end
956 else
957 begin
958 drawRect(px+pdx, py+pdy, pw, ph, 255, 255, 0, 180);
959 end;
960 drawRect(ex, ey, pw, ph, 255, 127, 0, 180);
961 end;
963 procedure hilightCell (cx, cy: Integer);
964 begin
965 fillRect(cx, cy, monsGrid.tileSize, monsGrid.tileSize, 0, 128, 0, 64);
966 end;
968 procedure hilightCell1 (cx, cy: Integer);
969 begin
970 //e_WriteLog(Format('h1: (%d,%d)', [cx, cy]), MSG_NOTIFY);
971 cx := cx and (not (monsGrid.tileSize-1));
972 cy := cy and (not (monsGrid.tileSize-1));
973 fillRect(cx, cy, monsGrid.tileSize, monsGrid.tileSize, 255, 255, 0, 92);
974 end;
976 function hilightWallTrc (pan: TPanel; tag: Integer; x, y, prevx, prevy: Integer): Boolean;
977 begin
978 result := false; // don't stop
979 if (pan = nil) then exit; // cell completion, ignore
980 //e_WriteLog(Format('h1: (%d,%d)', [cx, cy]), MSG_NOTIFY);
981 fillRect(pan.X, pan.Y, pan.Width, pan.Height, 0, 128, 128, 64);
982 end;
984 function monsCollector (mon: TMonster; tag: Integer): Boolean;
985 var
986 ex, ey: Integer;
987 mx, my, mw, mh: Integer;
988 begin
989 result := false;
990 mon.getMapBox(mx, my, mw, mh);
991 e_DrawQuad(mx, my, mx+mw-1, my+mh-1, 255, 255, 0, 96);
992 if lineAABBIntersects(laserX0, laserY0, laserX1, laserY1, mx, my, mw, mh, ex, ey) then
993 begin
994 e_DrawPoint(8, ex, ey, 0, 255, 0);
995 end;
996 end;
998 procedure drawMonsterInfo (mon: TMonster);
999 var
1000 mx, my, mw, mh: Integer;
1002 procedure drawMonsterTargetLine ();
1003 var
1004 emx, emy, emw, emh: Integer;
1005 enemy: TMonster;
1006 eplr: TPlayer;
1007 ex, ey: Integer;
1008 begin
1009 if (g_GetUIDType(mon.MonsterTargetUID) = UID_PLAYER) then
1010 begin
1011 eplr := g_Player_Get(mon.MonsterTargetUID);
1012 if (eplr <> nil) then eplr.getMapBox(emx, emy, emw, emh) else exit;
1013 end
1014 else if (g_GetUIDType(mon.MonsterTargetUID) = UID_MONSTER) then
1015 begin
1016 enemy := g_Monsters_ByUID(mon.MonsterTargetUID);
1017 if (enemy <> nil) then enemy.getMapBox(emx, emy, emw, emh) else exit;
1018 end
1019 else
1020 begin
1021 exit;
1022 end;
1023 mon.getMapBox(mx, my, mw, mh);
1024 drawLine(mx+mw div 2, my+mh div 2, emx+emw div 2, emy+emh div 2, 255, 0, 0, 255);
1025 if (g_Map_traceToNearestWall(mx+mw div 2, my+mh div 2, emx+emw div 2, emy+emh div 2, @ex, @ey) <> nil) then
1026 begin
1027 drawLine(mx+mw div 2, my+mh div 2, ex, ey, 0, 255, 0, 255);
1028 end;
1029 end;
1031 procedure drawLOS2Plr ();
1032 var
1033 emx, emy, emw, emh: Integer;
1034 eplr: TPlayer;
1035 ex, ey: Integer;
1036 begin
1037 eplr := gPlayers[0];
1038 if (eplr = nil) then exit;
1039 eplr.getMapBox(emx, emy, emw, emh);
1040 mon.getMapBox(mx, my, mw, mh);
1041 drawLine(mx+mw div 2, my+mh div 2, emx+emw div 2, emy+emh div 2, 255, 0, 0, 255);
1042 {$IF DEFINED(D2F_DEBUG)}
1043 mapGrid.dbgRayTraceTileHitCB := hilightCell1;
1044 {$ENDIF}
1045 if (g_Map_traceToNearestWall(mx+mw div 2, my+mh div 2, emx+emw div 2, emy+emh div 2, @ex, @ey) <> nil) then
1046 //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
1047 begin
1048 drawLine(mx+mw div 2, my+mh div 2, ex, ey, 0, 255, 0, 255);
1049 end;
1050 {$IF DEFINED(D2F_DEBUG)}
1051 mapGrid.dbgRayTraceTileHitCB := nil;
1052 {$ENDIF}
1053 end;
1055 begin
1056 if (mon = nil) then exit;
1057 mon.getMapBox(mx, my, mw, mh);
1058 //mx += mw div 2;
1060 monsGrid.forEachBodyCell(mon.proxyId, hilightCell);
1062 if showMonsInfo then
1063 begin
1064 //fillRect(mx-4, my-7*8-6, 110, 7*8+6, 0, 0, 94, 250);
1065 darkenRect(mx-4, my-7*8-6, 110, 7*8+6, 128);
1066 my -= 8;
1067 my -= 2;
1069 // type
1070 drawText6(mx, my, Format('%s(U:%u)', [monsTypeToString(mon.MonsterType), mon.UID]), 255, 127, 0); my -= 8;
1071 // beh
1072 drawText6(mx, my, Format('Beh: %s', [monsBehToString(mon.MonsterBehaviour)]), 255, 127, 0); my -= 8;
1073 // state
1074 drawText6(mx, my, Format('State:%s (%d)', [monsStateToString(mon.MonsterState), mon.MonsterSleep]), 255, 127, 0); my -= 8;
1075 // health
1076 drawText6(mx, my, Format('Health:%d', [mon.MonsterHealth]), 255, 127, 0); my -= 8;
1077 // ammo
1078 drawText6(mx, my, Format('Ammo:%d', [mon.MonsterAmmo]), 255, 127, 0); my -= 8;
1079 // target
1080 drawText6(mx, my, Format('TgtUID:%u', [mon.MonsterTargetUID]), 255, 127, 0); my -= 8;
1081 drawText6(mx, my, Format('TgtTime:%d', [mon.MonsterTargetTime]), 255, 127, 0); my -= 8;
1082 end;
1084 drawMonsterTargetLine();
1085 if showMonsLOS2Plr then drawLOS2Plr();
1087 property MonsterRemoved: Boolean read FRemoved write FRemoved;
1088 property MonsterPain: Integer read FPain write FPain;
1089 property MonsterAnim: Byte read FCurAnim write FCurAnim;
1091 end;
1093 function highlightAllMonsterCells (mon: TMonster): Boolean;
1094 begin
1095 result := false; // don't stop
1096 monsGrid.forEachBodyCell(mon.proxyId, hilightCell);
1097 end;
1099 procedure drawSelectedPlatformCells ();
1100 var
1101 pan: TPanel;
1102 begin
1103 if not showGrid then exit;
1104 pan := g_Map_PanelByGUID(platMarkedGUID);
1105 if (pan = nil) then exit;
1106 mapGrid.forEachBodyCell(pan.proxyId, hilightCell);
1107 drawRect(pan.x, pan.y, pan.width, pan.height, 0, 200, 0, 200);
1108 end;
1110 procedure drawTrigger (var trig: TTrigger);
1112 procedure drawPanelDest (pguid: Integer);
1113 var
1114 pan: TPanel;
1115 begin
1116 pan := g_Map_PanelByGUID(pguid);
1117 if (pan = nil) then exit;
1118 drawLine(
1119 trig.trigCenter.x, trig.trigCenter.y,
1120 pan.x+pan.width div 2, pan.y+pan.height div 2,
1121 255, 0, 255, 220);
1122 end;
1124 var
1125 tts: AnsiString;
1126 tx: Integer;
1127 begin
1128 fillRect(trig.x, trig.y, trig.width, trig.height, 255, 0, 255, 96);
1129 tts := trigType2Str(trig.TriggerType);
1130 tx := trig.x+(trig.width-Length(tts)*6) div 2;
1131 darkenRect(tx-2, trig.y-10, Length(tts)*6+4, 10, 64);
1132 drawText6(tx, trig.y-9, tts, 255, 127, 0);
1133 tx := trig.x+(trig.width-Length(trig.mapId)*6) div 2;
1134 darkenRect(tx-2, trig.y-20, Length(trig.mapId)*6+4, 10, 64);
1135 drawText6(tx, trig.y-19, trig.mapId, 255, 255, 0);
1136 drawPanelDest(trig.trigPanelGUID);
1137 case trig.TriggerType of
1138 TRIGGER_NONE: begin end;
1139 TRIGGER_EXIT: begin end;
1140 TRIGGER_TELEPORT: begin end;
1141 TRIGGER_OPENDOOR: begin end;
1142 TRIGGER_CLOSEDOOR: begin end;
1143 TRIGGER_DOOR: begin end;
1144 TRIGGER_DOOR5: begin end;
1145 TRIGGER_CLOSETRAP: begin end;
1146 TRIGGER_TRAP: begin end;
1147 TRIGGER_SECRET: begin end;
1148 TRIGGER_LIFTUP: begin end;
1149 TRIGGER_LIFTDOWN: begin end;
1150 TRIGGER_LIFT: begin end;
1151 TRIGGER_TEXTURE: begin end;
1152 TRIGGER_ON, TRIGGER_OFF, TRIGGER_ONOFF, TRIGGER_PRESS:
1153 begin
1154 if (trig.trigDataRec.trigTWidth > 0) and (trig.trigDataRec.trigTHeight > 0) then
1155 begin
1156 fillRect(
1157 trig.trigDataRec.trigTX, trig.trigDataRec.trigTY,
1158 trig.trigDataRec.trigTWidth, trig.trigDataRec.trigTHeight,
1159 0, 255, 255, 42);
1160 drawLine(
1161 trig.trigCenter.x, trig.trigCenter.y,
1162 trig.trigDataRec.trigTX+trig.trigDataRec.trigTWidth div 2,
1163 trig.trigDataRec.trigTY+trig.trigDataRec.trigTHeight div 2,
1164 255, 0, 255, 220);
1165 end;
1166 end;
1167 TRIGGER_SOUND: begin end;
1168 TRIGGER_SPAWNMONSTER: begin end;
1169 TRIGGER_SPAWNITEM: begin end;
1170 TRIGGER_MUSIC: begin end;
1171 TRIGGER_PUSH: begin end;
1172 TRIGGER_SCORE: begin end;
1173 TRIGGER_MESSAGE: begin end;
1174 TRIGGER_DAMAGE: begin end;
1175 TRIGGER_HEALTH: begin end;
1176 TRIGGER_SHOT: begin end;
1177 TRIGGER_EFFECT: begin end;
1178 TRIGGER_SCRIPT: begin end;
1179 end;
1180 //trigType2Str
1181 //trigPanelId: Integer;
1182 end;
1184 procedure drawTriggers ();
1185 var
1186 f: Integer;
1187 begin
1188 for f := 0 to High(gTriggers) do drawTrigger(gTriggers[f]);
1189 end;
1191 procedure drawGibsBoxes ();
1192 var
1193 f: Integer;
1194 px, py, pw, ph: Integer;
1195 gib: PGib;
1196 begin
1197 for f := 0 to High(gGibs) do
1198 begin
1199 gib := @gGibs[f];
1200 if gib.alive then
1201 begin
1202 gib.getMapBox(px, py, pw, ph);
1203 drawRect(px, py, pw, ph, 255, 0, 255);
1204 end;
1205 end;
1206 end;
1208 var
1209 mon: TMonster;
1210 mx, my, mw, mh: Integer;
1211 //pan: TPanel;
1212 //ex, ey: Integer;
1213 begin
1214 if (gPlayer1 = nil) then exit;
1216 glEnable(GL_SCISSOR_TEST);
1217 glScissor(0, gWinSizeY-gPlayerScreenSize.Y-1, vpw, vph);
1219 glPushMatrix();
1220 glScalef(g_dbg_scale, g_dbg_scale, 1.0);
1221 glTranslatef(-vpx, -vpy, 0);
1223 if (showGrid) then drawTileGrid();
1224 drawOutlines();
1226 if (laserSet) then g_Mons_AlongLine(laserX0, laserY0, laserX1, laserY1, monsCollector, true);
1228 if (monMarkedUID <> -1) then
1229 begin
1230 mon := g_Monsters_ByUID(monMarkedUID);
1231 if (mon <> nil) then
1232 begin
1233 mon.getMapBox(mx, my, mw, mh);
1234 e_DrawQuad(mx, my, mx+mw-1, my+mh-1, 255, 0, 0, 30);
1235 drawMonsterInfo(mon);
1236 end;
1237 end;
1239 if showAllMonsCells and showGrid then g_Mons_ForEach(highlightAllMonsterCells);
1240 if showTriggers then drawTriggers();
1241 if showGrid then drawSelectedPlatformCells();
1243 //drawAwakeCells();
1245 if showTraceBox then drawTraceBox();
1247 //drawGibsBoxes();
1250 //pan := g_Map_traceToNearest(16, 608, 16, 8, (GridTagObstacle or GridTagLiquid), @ex, @ey);
1251 (*
1252 {$IF DEFINED(D2F_DEBUG)}
1253 mapGrid.dbgRayTraceTileHitCB := hilightCell1;
1254 {$ENDIF}
1255 pan := mapGrid.traceRay(ex, ey, 16, 608, 16, 8, nil, (GridTagObstacle or GridTagLiquid));
1256 if (pan <> nil) then writeln('end=(', ex, ',', ey, ')');
1257 {$IF DEFINED(D2F_DEBUG)}
1258 mapGrid.dbgRayTraceTileHitCB := nil;
1259 {$ENDIF}
1261 pan := g_Map_PanelAtPoint(16, 608, (GridTagObstacle or GridTagLiquid));
1262 if (pan <> nil) then writeln('hit!');
1263 *)
1265 glPopMatrix();
1267 glDisable(GL_SCISSOR_TEST);
1269 if showMapCurPos then drawText8(4, gWinSizeY-10, Format('mappos:(%d,%d)', [pmsCurMapX, pmsCurMapY]), 255, 255, 0);
1270 end;
1273 // ////////////////////////////////////////////////////////////////////////// //
1274 function g_Holmes_MouseEvent (var ev: THMouseEvent): Boolean;
1275 var
1276 he: THMouseEvent;
1277 begin
1278 if g_Game_IsNet then begin result := false; exit; end;
1279 holmesInitCommands();
1280 holmesInitBinds();
1281 result := true;
1282 msX := trunc(ev.x/g_holmes_ui_scale);
1283 msY := trunc(ev.y/g_holmes_ui_scale);
1284 msB := ev.bstate;
1285 kbS := ev.kstate;
1286 msB := msB;
1287 he := ev;
1288 he.x := trunc(he.x/g_holmes_ui_scale);
1289 he.y := trunc(he.y/g_holmes_ui_scale);
1290 if not uiMouseEvent(he) then plrDebugMouse(he);
1291 end;
1294 // ////////////////////////////////////////////////////////////////////////// //
1295 function g_Holmes_KeyEvent (var ev: THKeyEvent): Boolean;
1296 {$IF DEFINED(D2F_DEBUG)}
1297 var
1298 pan: TPanel;
1299 ex, ey: Integer;
1300 dx, dy: Integer;
1301 {$ENDIF}
1303 procedure dummyWallTrc (cx, cy: Integer);
1304 begin
1305 end;
1307 begin
1308 if g_Game_IsNet then begin result := false; exit; end;
1309 holmesInitCommands();
1310 holmesInitBinds();
1311 result := false;
1312 msB := ev.bstate;
1313 kbS := ev.kstate;
1314 case ev.scan of
1315 SDL_SCANCODE_LCTRL, SDL_SCANCODE_RCTRL,
1316 SDL_SCANCODE_LALT, SDL_SCANCODE_RALT,
1317 SDL_SCANCODE_LSHIFT, SDL_SCANCODE_RSHIFT:
1318 result := true;
1319 end;
1320 if uiKeyEvent(ev) then begin result := true; exit; end;
1321 if keybindExecute(ev) then begin result := true; exit; end;
1322 // press
1323 if (ev.kind = THKeyEvent.Press) then
1324 begin
1325 {$IF DEFINED(D2F_DEBUG)}
1326 // C-UP, C-DOWN, C-LEFT, C-RIGHT: trace 10 pixels from cursor in the respective direction
1327 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
1328 ((ev.kstate and THKeyEvent.ModCtrl) <> 0) then
1329 begin
1330 result := true;
1331 dx := pmsCurMapX;
1332 dy := pmsCurMapY;
1333 case ev.scan of
1334 SDL_SCANCODE_UP: dy -= 120;
1335 SDL_SCANCODE_DOWN: dy += 120;
1336 SDL_SCANCODE_LEFT: dx -= 120;
1337 SDL_SCANCODE_RIGHT: dx += 120;
1338 end;
1339 {$IF DEFINED(D2F_DEBUG)}
1340 //mapGrid.dbgRayTraceTileHitCB := dummyWallTrc;
1341 mapGrid.dbgShowTraceLog := true;
1342 {$ENDIF}
1343 pan := g_Map_traceToNearest(pmsCurMapX, pmsCurMapY, dx, dy, (GridTagWall or GridTagDoor or GridTagStep or GridTagAcid1 or GridTagAcid2 or GridTagWater), @ex, @ey);
1344 {$IF DEFINED(D2F_DEBUG)}
1345 //mapGrid.dbgRayTraceTileHitCB := nil;
1346 mapGrid.dbgShowTraceLog := false;
1347 {$ENDIF}
1348 e_LogWritefln('v-trace: (%d,%d)-(%d,%d); end=(%d,%d); hit=%d', [pmsCurMapX, pmsCurMapY, dx, dy, ex, ey, (pan <> nil)]);
1349 exit;
1350 end;
1351 {$ENDIF}
1352 end;
1353 end;
1356 // ////////////////////////////////////////////////////////////////////////// //
1357 procedure g_Holmes_Draw ();
1358 begin
1359 if g_Game_IsNet then exit;
1360 {$IF not DEFINED(HEADLESS)}
1361 holmesInitCommands();
1362 holmesInitBinds();
1364 glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE); // modify color buffer
1365 glDisable(GL_STENCIL_TEST);
1366 glDisable(GL_BLEND);
1367 glDisable(GL_SCISSOR_TEST);
1368 glDisable(GL_TEXTURE_2D);
1370 if gGameOn then plrDebugDraw();
1371 {$ENDIF}
1373 laserSet := false;
1374 end;
1377 procedure g_Holmes_DrawUI ();
1378 begin
1379 if g_Game_IsNet then exit;
1380 {$IF not DEFINED(HEADLESS)}
1381 glPushMatrix();
1382 glScalef(g_holmes_ui_scale, g_holmes_ui_scale, 1.0);
1383 uiDraw();
1384 drawCursor();
1385 glPopMatrix();
1386 {$ENDIF}
1387 end;
1390 // ////////////////////////////////////////////////////////////////////////// //
1391 procedure bcOneMonsterThinkStep (); begin gmon_debug_think := false; gmon_debug_one_think_step := true; end;
1392 procedure bcOneMPlatThinkStep (); begin g_dbgpan_mplat_active := false; g_dbgpan_mplat_step := true; end;
1393 procedure bcMPlatToggle (); begin g_dbgpan_mplat_active := not g_dbgpan_mplat_active; end;
1395 procedure bcToggleMonsterInfo (arg: Integer=-1); begin if (arg < 0) then showMonsInfo := not showMonsInfo else showMonsInfo := (arg > 0); end;
1396 procedure bcToggleMonsterLOSPlr (arg: Integer=-1); begin if (arg < 0) then showMonsLOS2Plr := not showMonsLOS2Plr else showMonsLOS2Plr := (arg > 0); end;
1397 procedure bcToggleMonsterCells (arg: Integer=-1); begin if (arg < 0) then showAllMonsCells := not showAllMonsCells else showAllMonsCells := (arg > 0); end;
1398 procedure bcToggleDrawTriggers (arg: Integer=-1); begin if (arg < 0) then showTriggers := not showTriggers else showTriggers := (arg > 0); end;
1400 procedure bcToggleCurPos (arg: Integer=-1); begin if (arg < 0) then showMapCurPos := not showMapCurPos else showMapCurPos := (arg > 0); end;
1401 procedure bcToggleGrid (arg: Integer=-1); begin if (arg < 0) then showGrid := not showGrid else showGrid := (arg > 0); end;
1403 procedure bcMonsterSpawn (s: AnsiString);
1404 var
1405 mon: TMonster;
1406 begin
1407 if not gGameOn or g_Game_IsClient then
1408 begin
1409 conwriteln('cannot spawn monster in this mode');
1410 exit;
1411 end;
1412 mon := g_Mons_SpawnAt(s, pmsCurMapX, pmsCurMapY);
1413 if (mon = nil) then begin conwritefln('unknown monster id: ''%s''', [s]); exit; end;
1414 monMarkedUID := mon.UID;
1415 end;
1417 procedure bcMonsterWakeup ();
1418 var
1419 mon: TMonster;
1420 begin
1421 if (monMarkedUID <> -1) then
1422 begin
1423 mon := g_Monsters_ByUID(monMarkedUID);
1424 if (mon <> nil) then mon.WakeUp();
1425 end;
1426 end;
1428 procedure bcPlayerTeleport ();
1429 var
1430 x, y, w, h: Integer;
1431 begin
1432 //e_WriteLog(Format('TELEPORT: (%d,%d)', [pmsCurMapX, pmsCurMapY]), MSG_NOTIFY);
1433 if (gPlayers[0] <> nil) then
1434 begin
1435 gPlayers[0].getMapBox(x, y, w, h);
1436 gPlayers[0].TeleportTo(pmsCurMapX-w div 2, pmsCurMapY-h div 2, true, 69); // 69: don't change dir
1437 end;
1438 end;
1440 procedure dbgToggleTraceBox (arg: Integer=-1); begin if (arg < 0) then showTraceBox := not showTraceBox else showTraceBox := (arg > 0); end;
1442 procedure dbgToggleHolmesPause (arg: Integer=-1); begin if (arg < 0) then g_Game_HolmesPause(not gPauseHolmes) else g_Game_HolmesPause(arg > 0); end;
1444 procedure cbAtcurSelectMonster ();
1445 function monsAtDump (mon: TMonster; tag: Integer): Boolean;
1446 begin
1447 result := true; // stop
1448 e_WriteLog(Format('monster #%d (UID:%u) (proxyid:%d)', [mon.arrIdx, mon.UID, mon.proxyId]), TMsgType.Notify);
1449 monMarkedUID := mon.UID;
1450 dumpPublishedProperties(mon);
1451 end;
1452 var
1453 plr: TPlayer;
1454 x, y, w, h: Integer;
1455 begin
1456 monMarkedUID := -1;
1457 if (Length(gPlayers) > 0) then
1458 begin
1459 plr := gPlayers[0];
1460 if (plr <> nil) then
1461 begin
1462 plr.getMapBox(x, y, w, h);
1463 if (pmsCurMapX >= x) and (pmsCurMapY >= y) and (pmsCurMapX < x+w) and (pmsCurMapY < y+h) then
1464 begin
1465 dumpPublishedProperties(plr);
1466 end;
1467 end;
1468 end;
1469 //e_WriteLog('===========================', MSG_NOTIFY);
1470 monsGrid.forEachAtPoint(pmsCurMapX, pmsCurMapY, monsAtDump);
1471 //e_WriteLog('---------------------------', MSG_NOTIFY);
1472 end;
1474 procedure cbAtcurDumpMonsters ();
1475 function monsAtDump (mon: TMonster; tag: Integer): Boolean;
1476 begin
1477 result := false; // don't stop
1478 e_WriteLog(Format('monster #%d (UID:%u) (proxyid:%d)', [mon.arrIdx, mon.UID, mon.proxyId]), TMsgType.Notify);
1479 end;
1480 begin
1481 e_WriteLog('===========================', TMsgType.Notify);
1482 monsGrid.forEachAtPoint(pmsCurMapX, pmsCurMapY, monsAtDump);
1483 e_WriteLog('---------------------------', TMsgType.Notify);
1484 end;
1486 procedure cbAtcurDumpWalls ();
1487 function wallToggle (pan: TPanel; tag: Integer): Boolean;
1488 begin
1489 result := false; // don't stop
1490 if (platMarkedGUID = -1) then platMarkedGUID := pan.guid;
1491 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]);
1492 dumpPublishedProperties(pan);
1493 end;
1494 var
1495 hasTrigs: Boolean = false;
1496 f: Integer;
1497 trig: PTrigger;
1498 begin
1499 platMarkedGUID := -1;
1500 e_WriteLog('=== TOGGLE WALL ===', TMsgType.Notify);
1501 mapGrid.forEachAtPoint(pmsCurMapX, pmsCurMapY, wallToggle, (GridTagWall or GridTagDoor));
1502 e_WriteLog('--- toggle wall ---', TMsgType.Notify);
1503 if showTriggers then
1504 begin
1505 for f := 0 to High(gTriggers) do
1506 begin
1507 trig := @gTriggers[f];
1508 if (pmsCurMapX >= trig.x) and (pmsCurMapY >= trig.y) and (pmsCurMapX < trig.x+trig.width) and (pmsCurMapY < trig.y+trig.height) then
1509 begin
1510 if not hasTrigs then begin writeln('=== TRIGGERS ==='); hasTrigs := true; end;
1511 writeln('trigger ''', trig.mapId, ''' of type ''', trigType2Str(trig.TriggerType), '''');
1512 end;
1513 end;
1514 if hasTrigs then writeln('--- triggers ---');
1515 end;
1516 end;
1518 procedure cbAtcurToggleWalls ();
1519 function wallToggle (pan: TPanel; tag: Integer): Boolean;
1520 begin
1521 result := false; // don't stop
1522 //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);
1523 if pan.Enabled then g_Map_DisableWallGUID(pan.guid) else g_Map_EnableWallGUID(pan.guid);
1524 end;
1525 begin
1526 //e_WriteLog('=== TOGGLE WALL ===', MSG_NOTIFY);
1527 mapGrid.forEachAtPoint(pmsCurMapX, pmsCurMapY, wallToggle, (GridTagWall or GridTagDoor));
1528 //e_WriteLog('--- toggle wall ---', MSG_NOTIFY);
1529 end;
1532 // ////////////////////////////////////////////////////////////////////////// //
1533 procedure holmesInitCommands ();
1534 begin
1535 if (cmdlist <> nil) then exit;
1536 cmdAdd('win_layers', toggleLayersWindow, 'toggle layers window', 'window control');
1537 cmdAdd('win_outline', toggleOutlineWindow, 'toggle outline window', 'window control');
1538 cmdAdd('win_help', toggleHelpWindow, 'toggle help window', 'window control');
1539 cmdAdd('win_options', toggleOptionsWindow, 'toggle options window', 'window control');
1541 cmdAdd('mon_think_step', bcOneMonsterThinkStep, 'one monster think step', 'monster control');
1542 cmdAdd('mon_info', bcToggleMonsterInfo, 'toggle monster info', 'monster control');
1543 cmdAdd('mon_los_plr', bcToggleMonsterLOSPlr, 'toggle monster LOS to player', 'monster control');
1544 cmdAdd('mon_cells', bcToggleMonsterCells, 'toggle "show all cells occupied by monsters" (SLOW!)', 'monster control');
1545 cmdAdd('mon_wakeup', bcMonsterWakeup, 'wake up selected monster', 'monster control');
1547 cmdAdd('mon_spawn', bcMonsterSpawn, 'spawn monster', 'monster control');
1549 cmdAdd('mplat_step', bcOneMPlatThinkStep, 'one mplat think step', 'mplat control');
1550 cmdAdd('mplat_toggle', bcMPlatToggle, 'activate/deactivate moving platforms', 'mplat control');
1552 cmdAdd('plr_teleport', bcPlayerTeleport, 'teleport player', 'player control');
1554 cmdAdd('dbg_curpos', bcToggleCurPos, 'toggle "show cursor position on the map"', 'various');
1555 cmdAdd('dbg_grid', bcToggleGrid, 'toggle grid', 'various');
1556 cmdAdd('dbg_triggers', bcToggleDrawTriggers, 'show/hide triggers (SLOW!)', 'various');
1558 cmdAdd('atcur_select_monster', cbAtcurSelectMonster, 'select monster to operate', 'monster control');
1559 cmdAdd('atcur_dump_monsters', cbAtcurDumpMonsters, 'dump monsters in cell', 'monster control');
1560 cmdAdd('atcur_dump_walls', cbAtcurDumpWalls, 'dump walls in cell', 'wall control');
1561 cmdAdd('atcur_disable_walls', cbAtcurToggleWalls, 'disable walls', 'wall control');
1563 cmdAdd('dbg_tracebox', dbgToggleTraceBox, 'test traceBox()', 'player control');
1565 cmdAdd('hlm_pause', dbgToggleHolmesPause, '"Holmes" pause mode', 'game control');
1566 end;
1569 procedure holmesInitBinds ();
1570 var
1571 st: TStream = nil;
1572 pr: TTextParser = nil;
1573 s, kn, v: AnsiString;
1574 kmods: Byte;
1575 mbuts: Byte;
1576 begin
1577 kbS := kbS;
1578 if not keybindsInited then
1579 begin
1580 // keyboard
1581 keybindAdd('F1', 'win_help');
1582 keybindAdd('M-F1', 'win_options');
1583 keybindAdd('C-O', 'win_outline');
1584 keybindAdd('C-L', 'win_layers');
1586 keybindAdd('M-M', 'mon_think_step');
1587 keybindAdd('M-I', 'mon_info');
1588 keybindAdd('M-L', 'mon_los_plr');
1589 keybindAdd('M-G', 'mon_cells');
1590 keybindAdd('M-A', 'mon_wakeup');
1592 keybindAdd('M-P', 'mplat_step');
1593 keybindAdd('M-O', 'mplat_toggle');
1595 keybindAdd('C-T', 'plr_teleport');
1596 keybindAdd('M-T', 'dbg_tracebox');
1598 keybindAdd('C-P', 'dbg_curpos');
1599 keybindAdd('C-G', 'dbg_grid');
1600 keybindAdd('C-X', 'dbg_triggers');
1602 keybindAdd('C-1', 'mon_spawn zombie');
1604 keybindAdd('C-S-P', 'hlm_pause');
1606 // mouse
1607 msbindAdd('LMB', 'atcur_select_monster');
1608 msbindAdd('M-LMB', 'atcur_dump_monsters');
1609 msbindAdd('RMB', 'atcur_dump_walls');
1610 msbindAdd('M-RMB', 'atcur_disable_walls');
1612 // load bindings from file
1613 try
1614 st := openDiskFileRO(GameDir+'holmes.rc');
1615 pr := TFileTextParser.Create(st);
1616 conwriteln('parsing "holmes.rc"...');
1617 while (pr.tokType <> pr.TTEOF) do
1618 begin
1619 s := pr.expectId();
1620 if (s = 'stop') then break
1621 else if (s = 'unbind_keys') then keybinds := nil
1622 else if (s = 'unbind_mouse') then msbinds := nil
1623 else if (s = 'bind') then
1624 begin
1625 if (pr.tokType = pr.TTStr) then s := pr.expectStr(false)
1626 else if (pr.tokType = pr.TTInt) then s := Format('%d', [pr.expectInt()])
1627 else s := pr.expectId();
1629 if (pr.tokType = pr.TTStr) then v := pr.expectStr(false)
1630 else if (pr.tokType = pr.TTInt) then v := Format('%d', [pr.expectInt()])
1631 else v := pr.expectId();
1633 kn := parseModKeys(s, kmods, mbuts);
1634 if (CompareText(kn, 'lmb') = 0) or (CompareText(kn, 'rmb') = 0) or (CompareText(kn, 'mmb') = 0) or (CompareText(kn, 'None') = 0) then
1635 begin
1636 msbindAdd(s, v);
1637 end
1638 else
1639 begin
1640 keybindAdd(s, v);
1641 end;
1642 end;
1643 end;
1644 except on e: Exception do // sorry
1645 if (pr <> nil) then conwritefln('Holmes config parse error at (%s,%s): %s', [pr.tokLine, pr.tokCol, e.message]);
1646 end;
1647 if (pr <> nil) then pr.Free() else st.Free(); // ownership
1648 end;
1649 end;
1652 begin
1653 conRegVar('hlm_ui_scale', @g_holmes_ui_scale, 0.01, 5.0, 'Holmes UI scale', '', false);
1654 end.