DEADSOFTWARE

fixed bug in grid updates for moving objects
[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;
108 // ////////////////////////////////////////////////////////////////////////// //
109 {$INCLUDE g_holmes.inc}
111 // ////////////////////////////////////////////////////////////////////////// //
112 procedure g_Holmes_VidModeChanged ();
113 begin
114 e_WriteLog(Format('Inspector: videomode changed: %dx%d', [gScreenWidth, gScreenHeight]), MSG_NOTIFY);
115 // texture space is possibly lost here, idc
116 curtexid := 0;
117 font6texid := 0;
118 font8texid := 0;
119 prfont6texid := 0;
120 prfont8texid := 0;
121 //createCursorTexture();
122 end;
124 procedure g_Holmes_WindowFocused ();
125 begin
126 msB := 0;
127 kbS := 0;
128 end;
130 procedure g_Holmes_WindowBlured ();
131 begin
132 end;
135 // ////////////////////////////////////////////////////////////////////////// //
136 var
137 vpSet: Boolean = false;
138 vpx, vpy: Integer;
139 vpw, vph: Integer;
140 laserSet: Boolean = false;
141 laserX0, laserY0, laserX1, laserY1: Integer;
142 monMarkedUID: Integer = -1;
144 procedure g_Holmes_plrView (viewPortX, viewPortY, viewPortW, viewPortH: Integer);
145 begin
146 vpSet := true;
147 vpx := viewPortX;
148 vpy := viewPortY;
149 vpw := viewPortW;
150 vph := viewPortH;
151 end;
153 procedure g_Holmes_plrLaser (ax0, ay0, ax1, ay1: Integer);
154 begin
155 laserSet := true;
156 laserX0 := ax0;
157 laserY0 := ay0;
158 laserX1 := ax1;
159 laserY1 := ay1;
160 end;
163 function pmsCurMapX (): Integer; inline; begin result := msX+vpx; end;
164 function pmsCurMapY (): Integer; inline; begin result := msY+vpy; end;
167 procedure plrDebugMouse (var ev: THMouseEvent);
169 function wallToggle (pan: TPanel; tag: Integer): Boolean;
170 begin
171 result := false; // don't stop
172 if pan.Enabled then g_Map_DisableWall(pan.arrIdx) else g_Map_EnableWall(pan.arrIdx);
173 end;
175 function monsAtDump (mon: TMonster; tag: Integer): Boolean;
176 begin
177 result := false; // don't stop
178 e_WriteLog(Format('monster #%d; UID=%d', [mon.arrIdx, mon.UID]), MSG_NOTIFY);
179 monMarkedUID := mon.UID;
180 //if pan.Enabled then g_Map_DisableWall(pan.arrIdx) else g_Map_EnableWall(pan.arrIdx);
181 end;
183 function monsInCell (mon: TMonster; tag: Integer): Boolean;
184 begin
185 result := false; // don't stop
186 e_WriteLog(Format('monster #%d (UID:%u) (proxyid:%d)', [mon.arrIdx, mon.UID, mon.proxyId]), MSG_NOTIFY);
187 end;
189 begin
190 //e_WriteLog(Format('mouse: x=%d; y=%d; but=%d; bstate=%d', [msx, msy, but, bstate]), MSG_NOTIFY);
191 if (gPlayer1 = nil) then exit;
192 if (ev.kind <> THMouseEvent.Press) then exit;
194 if (ev.but = THMouseEvent.Left) then
195 begin
196 if ((kbS and THKeyEvent.ModShift) <> 0) then
197 begin
198 // dump monsters in cell
199 e_WriteLog('===========================', MSG_NOTIFY);
200 monsGrid.forEachInCell(pmsCurMapX, pmsCurMapY, monsInCell);
201 e_WriteLog('---------------------------', MSG_NOTIFY);
202 end
203 else
204 begin
205 // toggle wall
206 mapGrid.forEachAtPoint(pmsCurMapX, pmsCurMapY, wallToggle, (GridTagWall or GridTagDoor));
207 end;
208 exit;
209 end;
211 if (ev.but = THMouseEvent.Right) then
212 begin
213 monMarkedUID := -1;
214 e_WriteLog('===========================', MSG_NOTIFY);
215 monsGrid.forEachAtPoint(pmsCurMapX, pmsCurMapY, monsAtDump);
216 e_WriteLog('---------------------------', MSG_NOTIFY);
217 exit;
218 end;
219 end;
222 procedure plrDebugDraw ();
224 procedure drawTileGrid ();
225 var
226 x, y: Integer;
227 begin
228 y := mapGrid.gridY0;
229 while (y < mapGrid.gridY0+mapGrid.gridHeight) do
230 begin
231 x := mapGrid.gridX0;
232 while (x < mapGrid.gridX0+mapGrid.gridWidth) do
233 begin
234 if (x+mapGrid.tileSize > vpx) and (y+mapGrid.tileSize > vpy) and
235 (x < vpx+vpw) and (y < vpy+vph) then
236 begin
237 //e_DrawQuad(x, y, x+mapGrid.tileSize-1, y+mapGrid.tileSize-1, 96, 96, 96, 96);
238 drawRect(x, y, mapGrid.tileSize, mapGrid.tileSize, 96, 96, 96, 255);
239 end;
240 Inc(x, mapGrid.tileSize);
241 end;
242 Inc(y, mapGrid.tileSize);
243 end;
244 end;
246 procedure hilightCell (cx, cy: Integer);
247 begin
248 fillRect(cx, cy, monsGrid.tileSize, monsGrid.tileSize, 0, 128, 0, 64);
249 end;
251 function monsCollector (mon: TMonster; tag: Integer): Boolean;
252 var
253 ex, ey: Integer;
254 mx, my, mw, mh: Integer;
255 begin
256 result := false;
257 mon.getMapBox(mx, my, mw, mh);
258 e_DrawQuad(mx, my, mx+mw-1, my+mh-1, 255, 255, 0, 96);
259 if lineAABBIntersects(laserX0, laserY0, laserX1, laserY1, mx, my, mw, mh, ex, ey) then
260 begin
261 e_DrawPoint(8, ex, ey, 0, 255, 0);
262 end;
263 end;
265 procedure drawMonsterInfo (mon: TMonster);
266 var
267 mx, my, mw, mh: Integer;
269 procedure drawMonsterTargetLine ();
270 var
271 emx, emy, emw, emh: Integer;
272 enemy: TMonster;
273 eplr: TPlayer;
274 ex, ey: Integer;
275 begin
276 if (g_GetUIDType(mon.MonsterTargetUID) = UID_PLAYER) then
277 begin
278 eplr := g_Player_Get(mon.MonsterTargetUID);
279 if (eplr <> nil) then eplr.getMapBox(emx, emy, emw, emh) else exit;
280 end
281 else if (g_GetUIDType(mon.MonsterTargetUID) = UID_MONSTER) then
282 begin
283 enemy := g_Monsters_ByUID(mon.MonsterTargetUID);
284 if (enemy <> nil) then enemy.getMapBox(emx, emy, emw, emh) else exit;
285 end
286 else
287 begin
288 exit;
289 end;
290 mon.getMapBox(mx, my, mw, mh);
291 drawLine(mx+mw div 2, my+mh div 2, emx+emw div 2, emy+emh div 2, 255, 0, 0, 128);
292 if (g_Map_traceToNearestWall(mx+mw div 2, my+mh div 2, emx+emw div 2, emy+emh div 2, @ex, @ey) <> nil) then
293 begin
294 drawLine(mx+mw div 2, my+mh div 2, ex, ey, 0, 255, 0, 128);
295 end;
296 end;
298 begin
299 if (mon = nil) then exit;
300 mon.getMapBox(mx, my, mw, mh);
301 //mx += mw div 2;
303 monsGrid.forEachBodyCell(mon.proxyId, hilightCell);
305 if showMonsInfo then
306 begin
307 //fillRect(mx-4, my-7*8-6, 110, 7*8+6, 0, 0, 94, 250);
308 shadeRect(mx-4, my-7*8-6, 110, 7*8+6, 128);
309 my -= 8;
310 my -= 2;
312 // type
313 drawText6(mx, my, Format('%s(U:%u)', [monsTypeToString(mon.MonsterType), mon.UID]), 255, 127, 0); my -= 8;
314 // beh
315 drawText6(mx, my, Format('Beh: %s', [monsBehToString(mon.MonsterBehaviour)]), 255, 127, 0); my -= 8;
316 // state
317 drawText6(mx, my, Format('State:%s (%d)', [monsStateToString(mon.MonsterState), mon.MonsterSleep]), 255, 127, 0); my -= 8;
318 // health
319 drawText6(mx, my, Format('Health:%d', [mon.MonsterHealth]), 255, 127, 0); my -= 8;
320 // ammo
321 drawText6(mx, my, Format('Ammo:%d', [mon.MonsterAmmo]), 255, 127, 0); my -= 8;
322 // target
323 drawText6(mx, my, Format('TgtUID:%u', [mon.MonsterTargetUID]), 255, 127, 0); my -= 8;
324 drawText6(mx, my, Format('TgtTime:%d', [mon.MonsterTargetTime]), 255, 127, 0); my -= 8;
325 end;
327 drawMonsterTargetLine();
329 property MonsterRemoved: Boolean read FRemoved write FRemoved;
330 property MonsterPain: Integer read FPain write FPain;
331 property MonsterAnim: Byte read FCurAnim write FCurAnim;
333 end;
335 var
336 mon: TMonster;
337 mx, my, mw, mh: Integer;
338 begin
339 //e_DrawPoint(4, plrMouseX, plrMouseY, 255, 0, 255);
340 if (gPlayer1 = nil) then exit;
342 //e_WriteLog(Format('(%d,%d)-(%d,%d)', [laserX0, laserY0, laserX1, laserY1]), MSG_NOTIFY);
344 glPushMatrix();
345 glTranslatef(-vpx, -vpy, 0);
347 drawTileGrid();
349 g_Mons_AlongLine(laserX0, laserY0, laserX1, laserY1, monsCollector, true);
351 if (monMarkedUID <> -1) then
352 begin
353 mon := g_Monsters_ByUID(monMarkedUID);
354 if (mon <> nil) then
355 begin
356 mon.getMapBox(mx, my, mw, mh);
357 e_DrawQuad(mx, my, mx+mw-1, my+mh-1, 255, 0, 0, 30);
358 drawMonsterInfo(mon);
359 end;
360 end;
362 //e_DrawPoint(16, laserX0, laserY0, 255, 255, 255);
364 glPopMatrix();
365 end;
368 // ////////////////////////////////////////////////////////////////////////// //
369 function g_Holmes_mouseEvent (var ev: THMouseEvent): Boolean;
370 begin
371 result := true;
372 msX := ev.x;
373 msY := ev.y;
374 msB := ev.bstate;
375 kbS := ev.kstate;
376 plrDebugMouse(ev);
377 end;
380 function g_Holmes_KeyEvent (var ev: THKeyEvent): Boolean;
381 begin
382 result := false;
383 msB := ev.bstate;
384 kbS := ev.kstate;
385 case ev.scan of
386 SDL_SCANCODE_LCTRL, SDL_SCANCODE_RCTRL,
387 SDL_SCANCODE_LALT, SDL_SCANCODE_RALT,
388 SDL_SCANCODE_LSHIFT, SDL_SCANCODE_RSHIFT:
389 result := true;
390 end;
391 // press
392 if (ev.kind = THKeyEvent.Press) then
393 begin
394 // M-M: one monster think step
395 if (ev.scan = SDL_SCANCODE_M) and ((ev.kstate and THKeyEvent.ModAlt) <> 0) then
396 begin
397 result := true;
398 gmon_debug_think := false;
399 gmon_debug_one_think_step := true; // do one step
400 exit;
401 end;
402 // M-I: toggle monster info
403 if (ev.scan = SDL_SCANCODE_I) and ((ev.kstate and THKeyEvent.ModAlt) <> 0) then
404 begin
405 result := true;
406 showMonsInfo := not showMonsInfo;
407 exit;
408 end;
409 end;
410 end;
413 // ////////////////////////////////////////////////////////////////////////// //
414 procedure g_Holmes_Draw ();
415 begin
416 glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE); // modify color buffer
417 glDisable(GL_STENCIL_TEST);
418 glDisable(GL_BLEND);
419 glDisable(GL_SCISSOR_TEST);
420 glDisable(GL_TEXTURE_2D);
422 if gGameOn then
423 begin
424 plrDebugDraw();
425 end;
427 //drawText6Prop(10, 10, 'Hi there, I''m Holmes!', 255, 255, 0);
428 //drawText8Prop(10, 20, 'Hi there, I''m Holmes!', 255, 255, 0);
430 drawCursor();
431 end;
434 end.