DEADSOFTWARE

holmes now can work without lasersight
[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 e_log,
23 g_textures, g_basic, e_graphics, g_phys, g_grid, g_player, g_monsters,
24 g_window, g_map, g_triggers, g_items, g_game, g_panel, g_console,
25 xprofiler;
28 type
29 THMouseEvent = record
30 public
31 const
32 // both for but and for bstate
33 Left = $0001;
34 Right = $0002;
35 Middle = $0004;
36 WheelUp = $0008;
37 WheelDown = $0010;
39 // event types
40 Release = 0;
41 Press = 1;
42 Motion = 2;
44 public
45 kind: Byte; // motion, press, release
46 x, y: Integer;
47 dx, dy: Integer; // for wheel this is wheel motion, otherwise this is relative mouse motion
48 but: Word; // current pressed/released button, or 0 for motion
49 bstate: Word; // button state
50 kstate: Word; // keyboard state (see THKeyEvent);
51 end;
53 THKeyEvent = record
54 public
55 const
56 // modifiers
57 ModCtrl = $0001;
58 ModAlt = $0002;
59 ModShift = $0004;
61 // event types
62 Release = 0;
63 Press = 1;
65 public
66 kind: Byte;
67 scan: Word; // SDL_SCANCODE_XXX
68 sym: Word; // SDLK_XXX
69 bstate: Word; // button state
70 kstate: Word; // keyboard state
71 end;
74 procedure g_Holmes_VidModeChanged ();
75 procedure g_Holmes_WindowFocused ();
76 procedure g_Holmes_WindowBlured ();
78 procedure g_Holmes_Draw ();
80 function g_Holmes_mouseEvent (var ev: THMouseEvent): Boolean; // returns `true` if event was eaten
81 function g_Holmes_KeyEvent (var ev: THKeyEvent): Boolean; // returns `true` if event was eaten
83 // hooks for player
84 procedure g_Holmes_plrView (viewPortX, viewPortY, viewPortW, viewPortH: Integer);
85 procedure g_Holmes_plrLaser (ax0, ay0, ax1, ay1: Integer);
88 var
89 g_holmes_enabled: Boolean = {$IF DEFINED(D2F_DEBUG)}true{$ELSE}false{$ENDIF};
92 implementation
94 uses
95 SysUtils, GL, SDL2,
96 MAPDEF, g_options;
99 var
100 //globalInited: Boolean = false;
101 msX: Integer = -666;
102 msY: Integer = -666;
103 msB: Word = 0; // button state
104 kbS: Word = 0; // keyboard modifiers state
105 showMonsInfo: Boolean = false;
106 showMonsLOS2Plr: Boolean = false;
107 showAllMonsCells: Boolean = false;
110 // ////////////////////////////////////////////////////////////////////////// //
111 {$INCLUDE g_holmes.inc}
113 // ////////////////////////////////////////////////////////////////////////// //
114 procedure g_Holmes_VidModeChanged ();
115 begin
116 e_WriteLog(Format('Inspector: videomode changed: %dx%d', [gScreenWidth, gScreenHeight]), MSG_NOTIFY);
117 // texture space is possibly lost here, idc
118 curtexid := 0;
119 font6texid := 0;
120 font8texid := 0;
121 prfont6texid := 0;
122 prfont8texid := 0;
123 //createCursorTexture();
124 end;
126 procedure g_Holmes_WindowFocused ();
127 begin
128 msB := 0;
129 kbS := 0;
130 end;
132 procedure g_Holmes_WindowBlured ();
133 begin
134 end;
137 // ////////////////////////////////////////////////////////////////////////// //
138 var
139 vpSet: Boolean = false;
140 vpx, vpy: Integer;
141 vpw, vph: Integer;
142 laserSet: Boolean = false;
143 laserX0, laserY0, laserX1, laserY1: Integer;
144 monMarkedUID: Integer = -1;
146 procedure g_Holmes_plrView (viewPortX, viewPortY, viewPortW, viewPortH: Integer);
147 begin
148 vpSet := true;
149 vpx := viewPortX;
150 vpy := viewPortY;
151 vpw := viewPortW;
152 vph := viewPortH;
153 end;
155 procedure g_Holmes_plrLaser (ax0, ay0, ax1, ay1: Integer);
156 begin
157 laserSet := true;
158 laserX0 := ax0;
159 laserY0 := ay0;
160 laserX1 := ax1;
161 laserY1 := ay1;
162 end;
165 function pmsCurMapX (): Integer; inline; begin result := msX+vpx; end;
166 function pmsCurMapY (): Integer; inline; begin result := msY+vpy; end;
169 procedure plrDebugMouse (var ev: THMouseEvent);
171 function wallToggle (pan: TPanel; tag: Integer): Boolean;
172 begin
173 result := false; // don't stop
174 if pan.Enabled then g_Map_DisableWall(pan.arrIdx) else g_Map_EnableWall(pan.arrIdx);
175 end;
177 function monsAtDump (mon: TMonster; tag: Integer): Boolean;
178 begin
179 result := false; // don't stop
180 e_WriteLog(Format('monster #%d; UID=%d', [mon.arrIdx, mon.UID]), MSG_NOTIFY);
181 monMarkedUID := mon.UID;
182 //if pan.Enabled then g_Map_DisableWall(pan.arrIdx) else g_Map_EnableWall(pan.arrIdx);
183 end;
185 function monsInCell (mon: TMonster; tag: Integer): Boolean;
186 begin
187 result := false; // don't stop
188 e_WriteLog(Format('monster #%d (UID:%u) (proxyid:%d)', [mon.arrIdx, mon.UID, mon.proxyId]), MSG_NOTIFY);
189 end;
191 begin
192 //e_WriteLog(Format('mouse: x=%d; y=%d; but=%d; bstate=%d', [msx, msy, but, bstate]), MSG_NOTIFY);
193 if (gPlayer1 = nil) then exit;
194 if (ev.kind <> THMouseEvent.Press) then exit;
196 if (ev.but = THMouseEvent.Left) then
197 begin
198 if ((kbS and THKeyEvent.ModShift) <> 0) then
199 begin
200 // dump monsters in cell
201 e_WriteLog('===========================', MSG_NOTIFY);
202 monsGrid.forEachInCell(pmsCurMapX, pmsCurMapY, monsInCell);
203 e_WriteLog('---------------------------', MSG_NOTIFY);
204 end
205 else
206 begin
207 // toggle wall
208 mapGrid.forEachAtPoint(pmsCurMapX, pmsCurMapY, wallToggle, (GridTagWall or GridTagDoor));
209 end;
210 exit;
211 end;
213 if (ev.but = THMouseEvent.Right) then
214 begin
215 monMarkedUID := -1;
216 e_WriteLog('===========================', MSG_NOTIFY);
217 monsGrid.forEachAtPoint(pmsCurMapX, pmsCurMapY, monsAtDump);
218 e_WriteLog('---------------------------', MSG_NOTIFY);
219 exit;
220 end;
221 end;
224 procedure plrDebugDraw ();
226 procedure drawTileGrid ();
227 var
228 x, y: Integer;
229 begin
230 y := mapGrid.gridY0;
231 while (y < mapGrid.gridY0+mapGrid.gridHeight) do
232 begin
233 x := mapGrid.gridX0;
234 while (x < mapGrid.gridX0+mapGrid.gridWidth) do
235 begin
236 if (x+mapGrid.tileSize > vpx) and (y+mapGrid.tileSize > vpy) and
237 (x < vpx+vpw) and (y < vpy+vph) then
238 begin
239 //e_DrawQuad(x, y, x+mapGrid.tileSize-1, y+mapGrid.tileSize-1, 96, 96, 96, 96);
240 drawRect(x, y, mapGrid.tileSize, mapGrid.tileSize, 96, 96, 96, 255);
241 end;
242 Inc(x, mapGrid.tileSize);
243 end;
244 Inc(y, mapGrid.tileSize);
245 end;
246 end;
248 procedure hilightCell (cx, cy: Integer);
249 begin
250 fillRect(cx, cy, monsGrid.tileSize, monsGrid.tileSize, 0, 128, 0, 64);
251 end;
253 procedure hilightCell1 (cx, cy: Integer);
254 begin
255 //e_WriteLog(Format('h1: (%d,%d)', [cx, cy]), MSG_NOTIFY);
256 fillRect(cx, cy, monsGrid.tileSize, monsGrid.tileSize, 255, 255, 0, 92);
257 end;
259 function hilightWallTrc (pan: TPanel; tag: Integer; x, y, prevx, prevy: Integer): Boolean;
260 begin
261 result := false; // don't stop
262 if (pan = nil) then exit; // cell completion, ignore
263 //e_WriteLog(Format('h1: (%d,%d)', [cx, cy]), MSG_NOTIFY);
264 fillRect(pan.X, pan.Y, pan.Width, pan.Height, 0, 128, 128, 64);
265 end;
267 function monsCollector (mon: TMonster; tag: Integer): Boolean;
268 var
269 ex, ey: Integer;
270 mx, my, mw, mh: Integer;
271 begin
272 result := false;
273 mon.getMapBox(mx, my, mw, mh);
274 e_DrawQuad(mx, my, mx+mw-1, my+mh-1, 255, 255, 0, 96);
275 if lineAABBIntersects(laserX0, laserY0, laserX1, laserY1, mx, my, mw, mh, ex, ey) then
276 begin
277 e_DrawPoint(8, ex, ey, 0, 255, 0);
278 end;
279 end;
281 procedure drawMonsterInfo (mon: TMonster);
282 var
283 mx, my, mw, mh: Integer;
285 procedure drawMonsterTargetLine ();
286 var
287 emx, emy, emw, emh: Integer;
288 enemy: TMonster;
289 eplr: TPlayer;
290 ex, ey: Integer;
291 begin
292 if (g_GetUIDType(mon.MonsterTargetUID) = UID_PLAYER) then
293 begin
294 eplr := g_Player_Get(mon.MonsterTargetUID);
295 if (eplr <> nil) then eplr.getMapBox(emx, emy, emw, emh) else exit;
296 end
297 else if (g_GetUIDType(mon.MonsterTargetUID) = UID_MONSTER) then
298 begin
299 enemy := g_Monsters_ByUID(mon.MonsterTargetUID);
300 if (enemy <> nil) then enemy.getMapBox(emx, emy, emw, emh) else exit;
301 end
302 else
303 begin
304 exit;
305 end;
306 mon.getMapBox(mx, my, mw, mh);
307 drawLine(mx+mw div 2, my+mh div 2, emx+emw div 2, emy+emh div 2, 255, 0, 0, 255);
308 if (g_Map_traceToNearestWall(mx+mw div 2, my+mh div 2, emx+emw div 2, emy+emh div 2, @ex, @ey) <> nil) then
309 begin
310 drawLine(mx+mw div 2, my+mh div 2, ex, ey, 0, 255, 0, 255);
311 end;
312 end;
314 procedure drawLOS2Plr ();
315 var
316 emx, emy, emw, emh: Integer;
317 eplr: TPlayer;
318 ex, ey: Integer;
319 begin
320 eplr := gPlayers[0];
321 if (eplr = nil) then exit;
322 eplr.getMapBox(emx, emy, emw, emh);
323 mon.getMapBox(mx, my, mw, mh);
324 drawLine(mx+mw div 2, my+mh div 2, emx+emw div 2, emy+emh div 2, 255, 0, 0, 255);
325 {$IF DEFINED(D2F_DEBUG)}
326 //mapGrid.dbgRayTraceTileHitCB := hilightCell1;
327 {$ENDIF}
328 if (g_Map_traceToNearestWall(mx+mw div 2, my+mh div 2, emx+emw div 2, emy+emh div 2, @ex, @ey) <> nil) then
329 //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
330 begin
331 drawLine(mx+mw div 2, my+mh div 2, ex, ey, 0, 255, 0, 255);
332 end;
333 {$IF DEFINED(D2F_DEBUG)}
334 //mapGrid.dbgRayTraceTileHitCB := nil;
335 {$ENDIF}
336 end;
338 begin
339 if (mon = nil) then exit;
340 mon.getMapBox(mx, my, mw, mh);
341 //mx += mw div 2;
343 monsGrid.forEachBodyCell(mon.proxyId, hilightCell);
345 if showMonsInfo then
346 begin
347 //fillRect(mx-4, my-7*8-6, 110, 7*8+6, 0, 0, 94, 250);
348 shadeRect(mx-4, my-7*8-6, 110, 7*8+6, 128);
349 my -= 8;
350 my -= 2;
352 // type
353 drawText6(mx, my, Format('%s(U:%u)', [monsTypeToString(mon.MonsterType), mon.UID]), 255, 127, 0); my -= 8;
354 // beh
355 drawText6(mx, my, Format('Beh: %s', [monsBehToString(mon.MonsterBehaviour)]), 255, 127, 0); my -= 8;
356 // state
357 drawText6(mx, my, Format('State:%s (%d)', [monsStateToString(mon.MonsterState), mon.MonsterSleep]), 255, 127, 0); my -= 8;
358 // health
359 drawText6(mx, my, Format('Health:%d', [mon.MonsterHealth]), 255, 127, 0); my -= 8;
360 // ammo
361 drawText6(mx, my, Format('Ammo:%d', [mon.MonsterAmmo]), 255, 127, 0); my -= 8;
362 // target
363 drawText6(mx, my, Format('TgtUID:%u', [mon.MonsterTargetUID]), 255, 127, 0); my -= 8;
364 drawText6(mx, my, Format('TgtTime:%d', [mon.MonsterTargetTime]), 255, 127, 0); my -= 8;
365 end;
367 drawMonsterTargetLine();
368 if showMonsLOS2Plr then drawLOS2Plr();
370 property MonsterRemoved: Boolean read FRemoved write FRemoved;
371 property MonsterPain: Integer read FPain write FPain;
372 property MonsterAnim: Byte read FCurAnim write FCurAnim;
374 end;
376 function highlightAllMonsterCells (mon: TMonster): Boolean;
377 begin
378 result := false; // don't stop
379 monsGrid.forEachBodyCell(mon.proxyId, hilightCell);
380 end;
382 var
383 mon: TMonster;
384 mx, my, mw, mh: Integer;
385 begin
386 //e_DrawPoint(4, plrMouseX, plrMouseY, 255, 0, 255);
387 if (gPlayer1 = nil) then exit;
389 //e_WriteLog(Format('(%d,%d)-(%d,%d)', [laserX0, laserY0, laserX1, laserY1]), MSG_NOTIFY);
391 glPushMatrix();
392 glTranslatef(-vpx, -vpy, 0);
394 drawTileGrid();
396 g_Mons_AlongLine(laserX0, laserY0, laserX1, laserY1, monsCollector, true);
398 if (monMarkedUID <> -1) then
399 begin
400 mon := g_Monsters_ByUID(monMarkedUID);
401 if (mon <> nil) then
402 begin
403 mon.getMapBox(mx, my, mw, mh);
404 e_DrawQuad(mx, my, mx+mw-1, my+mh-1, 255, 0, 0, 30);
405 drawMonsterInfo(mon);
406 end;
407 end;
409 if showAllMonsCells then g_Mons_ForEach(highlightAllMonsterCells);
411 //e_DrawPoint(16, laserX0, laserY0, 255, 255, 255);
413 glPopMatrix();
414 end;
417 // ////////////////////////////////////////////////////////////////////////// //
418 function g_Holmes_mouseEvent (var ev: THMouseEvent): Boolean;
419 begin
420 result := true;
421 msX := ev.x;
422 msY := ev.y;
423 msB := ev.bstate;
424 kbS := ev.kstate;
425 plrDebugMouse(ev);
426 end;
429 function g_Holmes_KeyEvent (var ev: THKeyEvent): Boolean;
430 var
431 mon: TMonster;
432 begin
433 result := false;
434 msB := ev.bstate;
435 kbS := ev.kstate;
436 case ev.scan of
437 SDL_SCANCODE_LCTRL, SDL_SCANCODE_RCTRL,
438 SDL_SCANCODE_LALT, SDL_SCANCODE_RALT,
439 SDL_SCANCODE_LSHIFT, SDL_SCANCODE_RSHIFT:
440 result := true;
441 end;
442 // press
443 if (ev.kind = THKeyEvent.Press) then
444 begin
445 // M-M: one monster think step
446 if (ev.scan = SDL_SCANCODE_M) and ((ev.kstate and THKeyEvent.ModAlt) <> 0) then
447 begin
448 result := true;
449 gmon_debug_think := false;
450 gmon_debug_one_think_step := true; // do one step
451 exit;
452 end;
453 // M-I: toggle monster info
454 if (ev.scan = SDL_SCANCODE_I) and ((ev.kstate and THKeyEvent.ModAlt) <> 0) then
455 begin
456 result := true;
457 showMonsInfo := not showMonsInfo;
458 exit;
459 end;
460 // M-L: toggle monster LOS to player
461 if (ev.scan = SDL_SCANCODE_L) and ((ev.kstate and THKeyEvent.ModAlt) <> 0) then
462 begin
463 result := true;
464 showMonsLOS2Plr := not showMonsLOS2Plr;
465 exit;
466 end;
467 // M-G: toggle "show all cells occupied by monsters"
468 if (ev.scan = SDL_SCANCODE_G) and ((ev.kstate and THKeyEvent.ModAlt) <> 0) then
469 begin
470 result := true;
471 showAllMonsCells := not showAllMonsCells;
472 exit;
473 end;
474 // M-A: wake up monster
475 if (ev.scan = SDL_SCANCODE_A) and ((ev.kstate and THKeyEvent.ModAlt) <> 0) then
476 begin
477 result := true;
478 if (monMarkedUID <> -1) then
479 begin
480 mon := g_Monsters_ByUID(monMarkedUID);
481 if (mon <> nil) then mon.WakeUp();
482 end;
483 exit;
484 end;
485 end;
486 end;
489 // ////////////////////////////////////////////////////////////////////////// //
490 procedure g_Holmes_Draw ();
491 begin
492 glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE); // modify color buffer
493 glDisable(GL_STENCIL_TEST);
494 glDisable(GL_BLEND);
495 glDisable(GL_SCISSOR_TEST);
496 glDisable(GL_TEXTURE_2D);
498 if gGameOn then
499 begin
500 plrDebugDraw();
501 end;
503 //drawText6Prop(10, 10, 'Hi there, I''m Holmes!', 255, 255, 0);
504 //drawText8Prop(10, 20, 'Hi there, I''m Holmes!', 255, 255, 0);
506 drawCursor();
508 laserSet := false;
509 end;
512 end.