DEADSOFTWARE

scripts: uid_get_pos() now returns center
[d2df-sdl.git] / src / game / g_scriptprocs.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 {$MODE DELPHI}
17 unit g_scriptprocs;
19 interface
21 uses
22 g_scripts;
24 function SP_Lua_ConPrint(L: PScriptContext): Integer; cdecl;
26 implementation
28 uses
29 <<<<<<< HEAD
30 lua, g_console;
31 =======
32 lua, lauxlib,
33 g_player, g_map, Math, g_gfx, g_game, g_textures,
34 g_console, g_monsters, g_items, g_phys, g_weapons,
35 g_main, SysUtils, e_log, g_language, g_basic,
36 g_options, g_net, g_netmsg, g_triggers, g_panel,
37 g_sound, MAPDEF;
39 function CheckArgs(L: PScriptContext; MinArgs: Integer; Error: Boolean = True): Boolean; inline;
40 begin
41 Result := True;
42 if lua_gettop(L) < MinArgs then
43 begin
44 if Error then g_Console_Add('SCRIPT: ERROR: expected at least ' + IntToStr(MinArgs) + ' argument(s)');
45 Result := False;
46 end;
47 end;
49 // system //
50 >>>>>>> 3132944... made TRIGGER_SCRIPT work
52 function SP_Lua_ConPrint(L: PScriptContext): Integer; cdecl;
53 <<<<<<< HEAD
54 =======
55 // game.con_print(text)
56 const
57 MINARGS = 1;
58 begin
59 Result := 0;
60 if not CheckArgs(L, MINARGS) then Exit;
61 g_Console_Add(luaL_checkstring(L, 1));
62 end;
64 function SP_Lua_Message(L: PScriptContext): Integer; cdecl;
65 // game.message(type, text, channel[, uid=0, timeout=144])
66 const
67 MINARGS = 3;
68 var
69 MText: string;
70 MChan, MKind, MID, MTime: Integer;
71 begin
72 Result := 0;
73 if not CheckArgs(L, MINARGS) then exit;
75 MKind := luaL_checkinteger(L, 1);
76 MText := luaL_checkstring(L, 2);
77 MChan := luaL_checkinteger(L, 3);
79 MID := lua_tointeger(L, 4);
80 MTime := lua_tointeger(L, 5);
81 if MTime = 0 then MTime := 144;
83 tr_Message(MKind, MText, MChan, MTime, MID);
84 end;
86 function SP_Lua_GetGameMode(L: PScriptContext): Integer; cdecl;
87 // game.get_gamemode()
88 const
89 MINARGS = 0;
90 begin
91 Result := 1;
92 lua_pushinteger(L, gGameSettings.GameMode);
93 end;
95 function SP_Lua_GetGameType(L: PScriptContext): Integer; cdecl;
96 // game.get_gametype()
97 const
98 MINARGS = 0;
99 begin
100 Result := 1;
101 lua_pushinteger(L, gGameSettings.GameType);
102 end;
104 function SP_Lua_GetTime(L: PScriptContext): Integer; cdecl;
105 // game.get_time()
106 const
107 MINARGS = 0;
108 begin
109 Result := 1;
110 lua_pushinteger(L, gTime);
111 end;
113 function SP_Lua_PlaySound(L: PScriptContext): Integer; cdecl;
114 // game.sound(name[positional=false, x=0, y=0])
115 const
116 MINARGS = 1;
117 var
118 SName: string;
119 SPos: Boolean;
120 SX, SY: Integer;
121 begin
122 Result := 0;
123 if not CheckArgs(L, MINARGS) then exit;
125 SName := luaL_checkstring(L, 1);
127 SPos := lua_toboolean(L, 2);
128 SX := lua_tointeger(L, 3);
129 SY := lua_tointeger(L, 4);
131 if SPos then
132 g_Sound_PlayExAt(SName, SX, SY)
133 else
134 g_Sound_PlayEx(SName);
136 if g_Game_IsNet then
137 MH_SEND_Sound(SX, SY, SName, SPos);
138 end;
140 // players //
142 function SP_Lua_PlayerGetKeys(L: PScriptContext): Integer; cdecl;
143 // game.player_get_keys(uid)
144 const
145 MINARGS = 1;
146 var
147 UID: Integer;
148 P: TPlayer;
149 begin
150 Result := 1;
151 if not CheckArgs(L, MINARGS) then
152 begin
153 Result := 0;
154 exit;
155 end;
157 UID := luaL_checkinteger(L, 1);
159 if g_GetUIDType(UID) <> UID_PLAYER then
160 lua_pushnil(L)
161 else
162 begin
163 P := g_Player_Get(UID);
164 if P = nil then
165 begin
166 lua_pushnil(L);
167 exit;
168 end;
169 lua_pushinteger(L, P.GetKeys);
170 end;
171 end;
173 function SP_Lua_PlayerGetArmor(L: PScriptContext): Integer; cdecl;
174 // game.player_get_armor(uid)
175 const
176 MINARGS = 1;
177 var
178 UID: Integer;
179 P: TPlayer;
180 begin
181 Result := 1;
182 if not CheckArgs(L, MINARGS) then
183 begin
184 Result := 0;
185 exit;
186 end;
188 UID := luaL_checkinteger(L, 1);
190 if g_GetUIDType(UID) <> UID_PLAYER then
191 lua_pushnil(L)
192 else
193 begin
194 P := g_Player_Get(UID);
195 if P = nil then
196 begin
197 lua_pushnil(L);
198 exit;
199 end;
200 lua_pushinteger(L, P.Armor);
201 end;
202 end;
204 function SP_Lua_PlayerGetName(L: PScriptContext): Integer; cdecl;
205 // game.player_get_name(uid)
206 const
207 MINARGS = 1;
208 var
209 UID: Integer;
210 P: TPlayer;
211 begin
212 Result := 1;
213 if not CheckArgs(L, MINARGS) then
214 begin
215 Result := 0;
216 exit;
217 end;
219 UID := luaL_checkinteger(L, 1);
221 if g_GetUIDType(UID) <> UID_PLAYER then
222 lua_pushnil(L)
223 else
224 begin
225 P := g_Player_Get(UID);
226 if P = nil then
227 begin
228 lua_pushnil(L);
229 exit;
230 end;
231 lua_pushstring(L, P.Name);
232 end;
233 end;
235 function SP_Lua_PlayerGetTeam(L: PScriptContext): Integer; cdecl;
236 // game.player_get_team(uid)
237 const
238 MINARGS = 1;
239 var
240 UID: Integer;
241 P: TPlayer;
242 begin
243 Result := 1;
244 if not CheckArgs(L, MINARGS) then
245 begin
246 Result := 0;
247 exit;
248 end;
250 UID := luaL_checkinteger(L, 1);
252 if g_GetUIDType(UID) <> UID_PLAYER then
253 lua_pushnil(L)
254 else
255 begin
256 P := g_Player_Get(UID);
257 if P = nil then
258 begin
259 lua_pushnil(L);
260 exit;
261 end;
262 lua_pushinteger(L, P.Team);
263 end;
264 end;
266 function SP_Lua_PlayerGetScore(L: PScriptContext): Integer; cdecl;
267 // game.player_get_score(uid)
268 const
269 MINARGS = 1;
270 var
271 UID: Integer;
272 P: TPlayer;
273 begin
274 Result := 1;
275 if not CheckArgs(L, MINARGS) then
276 begin
277 Result := 0;
278 exit;
279 end;
281 UID := luaL_checkinteger(L, 1);
283 if g_GetUIDType(UID) <> UID_PLAYER then
284 lua_pushnil(L)
285 else
286 begin
287 P := g_Player_Get(UID);
288 if P = nil then
289 begin
290 lua_pushnil(L);
291 exit;
292 end;
293 lua_pushinteger(L, P.Frags);
294 end;
295 end;
297 // actors //
299 function SP_Lua_ActorGetPos(L: PScriptContext): Integer; cdecl;
300 // game.uid_get_pos(uid)
301 const
302 MINARGS = 1;
303 var
304 UID: Integer;
305 P: TPlayer;
306 M: TMonster;
307 begin
308 Result := 2;
309 if not CheckArgs(L, MINARGS) then
310 begin
311 Result := 0;
312 exit;
313 end;
315 UID := luaL_checkinteger(L, 1);
317 if g_GetUIDType(UID) = UID_PLAYER then
318 begin
319 P := g_Player_Get(UID);
320 if P = nil then
321 begin
322 lua_pushnil(L);
323 lua_pushnil(L);
324 exit;
325 end;
326 lua_pushinteger(L, P.GameX + PLAYER_RECT_CX);
327 lua_pushinteger(L, P.GameY + PLAYER_RECT_CY);
328 end
329 else if g_GetUIDType(UID) = UID_MONSTER then
330 begin
331 M := g_Monsters_Get(UID);
332 if M = nil then
333 begin
334 lua_pushnil(L);
335 lua_pushnil(L);
336 exit;
337 end;
338 lua_pushinteger(L, M.Obj.X+M.Obj.Rect.X+(M.Obj.Rect.Width div 2));
339 lua_pushinteger(L, M.Obj.Y+M.Obj.Rect.Y+(M.Obj.Rect.Height div 2));
340 end
341 else
342 begin
343 lua_pushnil(L);
344 lua_pushnil(L);
345 end;
346 end;
348 function SP_Lua_ActorNearest(L: PScriptContext): Integer; cdecl;
349 // game.uid_nearest(x, y, [type=all, min, max])
350 const
351 MINARGS = 2;
352 var
353 i, UID, AType: Integer;
354 X, Y, Dist, MinDist, MaxDist, FMin: Double;
355 begin
356 Result := 1;
357 if not CheckArgs(L, MINARGS) then
358 begin
359 Result := 0;
360 exit;
361 end;
363 X := luaL_checkinteger(L, 1);
364 Y := luaL_checkinteger(L, 2);
366 AType := lua_tointeger(L, 3);
367 MinDist := Sqr(lua_tonumber(L, 4));
368 MaxDist := Sqr(lua_tonumber(L, 5));
369 if MaxDist < 0.01 then MaxDist := 1e15;
370 if MinDist < 0.01 then MinDist := 0;
371 FMin := MinDist;
372 UID := -1;
374 if (AType in [UID_GAME, UID_PLAYER]) and (gPlayers <> nil) then
375 for i := 0 to High(gPlayers) do
376 begin
377 if gPlayers[i] = nil then continue;
378 Dist := Sqr(X - gPlayers[i].GameX) + Sqr(Y - gPlayers[i].GameY);
379 if (Dist > MinDist) and (Dist < FMin) then
380 begin
381 UID := gPlayers[i].UID;
382 FMin := Dist;
383 end;
384 end;
385 if (AType in [UID_GAME, UID_MONSTER]) and (gMonsters <> nil) then
386 for i := 0 to High(gMonsters) do
387 begin
388 if gMonsters[i] = nil then continue;
389 Dist := Sqr(X - gMonsters[i].GameX) + Sqr(Y - gMonsters[i].GameY);
390 if (Dist > MinDist) and (Dist < FMin) then
391 begin
392 UID := gMonsters[i].UID;
393 FMin := Dist;
394 end;
395 end;
397 lua_pushinteger(L, UID);
398 end;
400 function SP_Lua_ActorFarthest(L: PScriptContext): Integer; cdecl;
401 // game.uid_farthest(x, y, [type=all, min, max])
402 const
403 MINARGS = 2;
404 var
405 i, UID, AType: Integer;
406 X, Y, Dist, MinDist, MaxDist, FMax: Double;
407 begin
408 Result := 1;
409 if not CheckArgs(L, MINARGS) then
410 begin
411 Result := 0;
412 exit;
413 end;
415 X := luaL_checkinteger(L, 1);
416 Y := luaL_checkinteger(L, 2);
418 AType := lua_tointeger(L, 3);
419 MinDist := Sqr(lua_tonumber(L, 4));
420 MaxDist := Sqr(lua_tonumber(L, 5));
421 if MaxDist < 0.01 then MaxDist := 1e15;
422 if MinDist < 0.01 then MinDist := 0;
423 FMax := MinDist;
424 UID := -1;
426 if (AType in [UID_GAME, UID_PLAYER]) and (gPlayers <> nil) then
427 for i := 0 to High(gPlayers) do
428 begin
429 if gPlayers[i] = nil then continue;
430 Dist := Sqr(X - gPlayers[i].GameX) + Sqr(Y - gPlayers[i].GameY);
431 if (Dist < MaxDist) and (Dist > FMax) then
432 begin
433 UID := gPlayers[i].UID;
434 FMax := Dist;
435 end;
436 end;
437 if (AType in [UID_GAME, UID_MONSTER]) and (gMonsters <> nil) then
438 for i := 0 to High(gMonsters) do
439 begin
440 if gMonsters[i] = nil then continue;
441 Dist := Sqr(X - gMonsters[i].GameX) + Sqr(Y - gMonsters[i].GameY);
442 if (Dist < MaxDist) and (Dist > FMax) then
443 begin
444 UID := gMonsters[i].UID;
445 FMax := Dist;
446 end;
447 end;
449 lua_pushinteger(L, UID);
450 end;
452 function SP_Lua_ActorGetType(L: PScriptContext): Integer; cdecl;
453 // game.uid_type(uid)
454 const
455 MINARGS = 2;
456 var
457 UID, UType: Integer;
458 begin
459 Result := 1;
460 if not CheckArgs(L, MINARGS) then
461 begin
462 Result := 0;
463 exit;
464 end;
466 UID := luaL_checkinteger(L, 1);
467 if (UID < 0) or (UID > $FFFF) then
468 lua_pushnil(L)
469 else
470 begin
471 UType := g_GetUIDType(UID);
472 if not (UType in [UID_PLAYER, UID_MONSTER{*, UID_ITEM*}]) then
473 lua_pushnil(L)
474 else
475 lua_pushinteger(L, UType);
476 end;
477 end;
479 function SP_Lua_ActorDamage(L: PScriptContext): Integer; cdecl;
480 // game.uid_damage(uid, amount[, hit_type=hit_some, vx=0, vy=0, attacker_uid=0])
481 const
482 MINARGS = 2;
483 var
484 UID, AUID, Dmg, Atk, VX, VY: Integer;
485 P: TPlayer;
486 M: TMonster;
487 >>>>>>> 081369f... scripts: uid_get_pos() now returns center
488 begin
489 g_Console_Add(lua_tostring(L, -1));
490 Result := 0;
491 end;
493 end.