Skip to content

Commit a12f3af

Browse files
author
dashodanger
committed
Lua DDF state hook testing
1 parent 845314b commit a12f3af

File tree

10 files changed

+89
-6
lines changed

10 files changed

+89
-6
lines changed

edge_defs/scripts/lua/edge_hud.lua

+10
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,14 @@ local custom_stbar_darkest_color = vec3(0, 0, 0)
1616
local custom_stbar_lightest_color = vec3(0, 0, 0)
1717
local custom_stbar_average_hue = vec3(0, 0, 0)
1818

19+
local lua_state_test = 0
20+
21+
function lua_state_call_test(info)
22+
if info ~= nil then
23+
lua_state_test = lua_state_test + 1
24+
end
25+
end
26+
1927
function doom_weapon_icon(slot, x, y, off_pic, on_pic)
2028
if (player.has_weapon_slot(slot)) then
2129
hud.draw_image(x, y, on_pic)
@@ -207,6 +215,8 @@ function doom_status_bar_common()
207215
hud.draw_num2(90, 171, 3, player.health())
208216
hud.draw_num2(221, 171, 3, player.total_armor())
209217

218+
hud.draw_num2(50, 50, 3, lua_state_test)
219+
210220
if (hud.game_mode() == "dm") then
211221
hud.draw_num2(138, 171, 2, player.frags())
212222
else

edge_defs/scripts/things.ddf

+2-1
Original file line numberDiff line numberDiff line change
@@ -1980,7 +1980,8 @@ DROPITEM=CLIP;
19801980
RANGE_ATTACK=FORMER_HUMAN_PISTOL;
19811981

19821982
STATES(IDLE)=POSS:A:10:NORMAL:LOOKOUT,
1983-
POSS:B:10:NORMAL:LOOKOUT;
1983+
POSS:B:10:NORMAL:LOOKOUT,
1984+
POSS:B:0:NORMAL:LUA_RUN_SCRIPT("lua_state_call_test");
19841985

19851986
STATES(CHASE)=POSS:A:4:NORMAL:CHASE,
19861987
POSS:A:4:NORMAL:CHASE,

source_files/ddf/ddf_thing.cc

+13
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ static void DDFMobjStateGetDEHProjectile(const char *arg, State *cur_state);
5656
static void DDFMobjStateGetDEHBullet(const char *arg, State *cur_state);
5757
static void DDFMobjStateGetDEHMelee(const char *arg, State *cur_state);
5858
static void DDFMobjStateGetDEHFlagJump(const char *arg, State *cur_state);
59+
static void DDFMobjStateGetString(const char *arg, State *cur_state);
5960

6061
static void AddPickupEffect(PickupEffect **list, PickupEffect *cur);
6162

@@ -300,6 +301,7 @@ const DDFActionCode thing_actions[] = {{"NOTHING", nullptr, nullptr},
300301
{"ACTIVATE_LINETYPE", A_ActivateLineType, DDFStateGetIntPair},
301302
{"RTS_ENABLE_TAGGED", A_EnableRadTrig, DDFMobjStateGetRADTrigger},
302303
{"RTS_DISABLE_TAGGED", A_DisableRadTrig, DDFMobjStateGetRADTrigger},
304+
{"LUA_RUN_SCRIPT", A_RunLuaScript, DDFMobjStateGetString},
303305
{"TOUCHY_REARM", A_TouchyRearm, nullptr},
304306
{"TOUCHY_DISARM", A_TouchyDisarm, nullptr},
305307
{"BOUNCE_REARM", A_BounceRearm, nullptr},
@@ -1794,6 +1796,17 @@ static void DDFMobjGetAngleRange(const char *info, void *storage)
17941796
dest[1] = epi::BAMFromDegrees(val2);
17951797
}
17961798

1799+
//
1800+
// DDFMobjStateGetString
1801+
//
1802+
static void DDFMobjStateGetString(const char *arg, State *cur_state)
1803+
{
1804+
if (!arg || !arg[0])
1805+
return;
1806+
1807+
cur_state->action_par = epi::CStringDuplicate(arg);
1808+
}
1809+
17971810
//
17981811
// DDFMobjStateGetRADTrigger
17991812
//

source_files/ddf/ddf_weapon.cc

+13
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ static void DDFWStateGetRADTrigger(const char *arg, State *cur_state);
4444
static void DDFWStateGetDEHMelee(const char *arg, State *cur_state);
4545
static void DDFWStateGetDEHBullet(const char *arg, State *cur_state);
4646
static void DDFWStateGetDEHProjectile(const char *arg, State *cur_state);
47+
static void DDFWStateGetString(const char *arg, State *cur_state);
4748

4849
static WeaponDefinition dummy_weapon;
4950

@@ -201,6 +202,7 @@ static const DDFActionCode weapon_actions[] = {{"NOTHING", nullptr, nullptr},
201202

202203
{"RTS_ENABLE_TAGGED", A_WeaponEnableRadTrig, DDFWStateGetRADTrigger},
203204
{"RTS_DISABLE_TAGGED", A_WeaponDisableRadTrig, DDFWStateGetRADTrigger},
205+
{"LUA_RUN_SCRIPT", A_WeaponRunLuaScript, DDFWStateGetString},
204206
{"SEC_SHOOT", A_WeaponShootSA, DDFStateGetAttack},
205207
{"SEC_REFIRE", A_ReFireSA, nullptr},
206208
{"SEC_REFIRE_TO", A_ReFireToSA, DDFStateGetJump},
@@ -879,6 +881,17 @@ static void DDFWStateGetDEHProjectile(const char *arg, State *cur_state)
879881
cur_state->action_par = atk;
880882
}
881883

884+
//
885+
// DDFWStateGetString
886+
//
887+
static void DDFWStateGetString(const char *arg, State *cur_state)
888+
{
889+
if (!arg || !arg[0])
890+
return;
891+
892+
cur_state->action_par = epi::CStringDuplicate(arg);
893+
}
894+
882895
//
883896
// DDFWStateGetRADTrigger
884897
//

source_files/edge/p_action.cc

+17
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
#include "r_state.h"
5959
#include "rad_trig.h"
6060
#include "s_sound.h"
61+
#include "script/compat/lua_compat.h"
6162
#include "w_wad.h"
6263

6364
extern FlatDefinition *P_IsThingOnLiquidFloor(MapObject *thing);
@@ -138,6 +139,22 @@ void A_DisableRadTrig(MapObject *mo)
138139
ScriptEnableByTag(mo, value[0], true, (RADScriptTag)mo->state_->rts_tag_type);
139140
}
140141

142+
//
143+
// A_RunLuaScript
144+
//
145+
// Allows things to execute Lua scripts, passing themselves
146+
// as a parameter
147+
//
148+
void A_RunLuaScript(MapObject *mo)
149+
{
150+
if (!mo->state_ || !mo->state_->action_par)
151+
return;
152+
153+
const char *script = (const char *)mo->state_->action_par;
154+
155+
LuaCallGlobalFunction(LuaGetGlobalVM(), script, mo);
156+
}
157+
141158
//
142159
// A_LookForTargets
143160
//

source_files/edge/p_action.h

+2
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ void A_WeaponTransSet(MapObject *mo);
6060
void A_WeaponTransFade(MapObject *mo);
6161
void A_WeaponEnableRadTrig(MapObject *mo);
6262
void A_WeaponDisableRadTrig(MapObject *mo);
63+
void A_WeaponRunLuaScript(MapObject *mo);
6364

6465
void A_SetCrosshair(MapObject *mo);
6566
void A_TargetJump(MapObject *mo);
@@ -166,6 +167,7 @@ void A_Explode(MapObject *mo);
166167
void A_ActivateLineType(MapObject *mo);
167168
void A_EnableRadTrig(MapObject *mo);
168169
void A_DisableRadTrig(MapObject *mo);
170+
void A_RunLuaScript(MapObject *mo);
169171
void A_TouchyRearm(MapObject *mo);
170172
void A_TouchyDisarm(MapObject *mo);
171173
void A_BounceRearm(MapObject *mo);

source_files/edge/p_weapon.cc

+13
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
#include "p_local.h"
3939
#include "rad_trig.h"
4040
#include "s_sound.h"
41+
#include "script/compat/lua_compat.h"
4142
#include "w_sprite.h"
4243
#include "w_wad.h"
4344

@@ -1929,6 +1930,18 @@ void A_WeaponTransFade(MapObject *mo)
19291930
psp->target_visibility = value;
19301931
}
19311932

1933+
void A_WeaponRunLuaScript(MapObject *mo)
1934+
{
1935+
Player *p = mo->player_;
1936+
PlayerSprite *psp = &p->player_sprites_[p->action_player_sprite_];
1937+
1938+
if (psp->state && psp->state->action_par)
1939+
{
1940+
const char *script = (const char *)mo->state_->action_par;
1941+
LuaCallGlobalFunction(LuaGetGlobalVM(), script, mo);
1942+
}
1943+
}
1944+
19321945
void A_WeaponEnableRadTrig(MapObject *mo)
19331946
{
19341947
Player *p = mo->player_;

source_files/edge/script/compat/lua_compat.h

+3-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include "con_var.h"
55
#include "epi.h"
66
#include "lua.hpp"
7+
#include "p_mobj.h"
78

89
lua_State *LuaCreateVM();
910

@@ -13,14 +14,15 @@ void LuaLoadScripts();
1314

1415
// Do a file, returns the number of return values on stack
1516
int LuaDoFile(lua_State *L, const char *filename, const char *source);
16-
void LuaCallGlobalFunction(lua_State *L, const char *function_name);
17+
void LuaCallGlobalFunction(lua_State *L, const char *function_name, MapObject *mo = nullptr);
1718

1819
// Game
1920
void LuaNewGame(void);
2021
void LuaLoadGame(void);
2122
void LuaSaveGame(void);
2223
void LuaBeginLevel(void);
2324
void LuaEndLevel(void);
25+
void CreateLuaTable_Mobj(lua_State *L, MapObject *mo);
2426

2527
// Core
2628
void LuaRegisterCoreLibraries(lua_State *L);

source_files/edge/script/compat/lua_player.cc

+1-1
Original file line numberDiff line numberDiff line change
@@ -1562,7 +1562,7 @@ static void CreateLuaTable_Benefits(lua_State *L, MapObject *obj, bool KillBenef
15621562

15631563
// CreateLuaTable_Mobj(LuaState, mobj)
15641564
//
1565-
static void CreateLuaTable_Mobj(lua_State *L, MapObject *mo)
1565+
void CreateLuaTable_Mobj(lua_State *L, MapObject *mo)
15661566
{
15671567
std::string temp_value;
15681568
int NumberOfItems = 12; // how many fields in a row

source_files/edge/script/compat/lua_vm.cc

+15-3
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ static int LuaDbgNOP(lua_State *L)
145145
return 0;
146146
}
147147

148-
void LuaCallGlobalFunction(lua_State *L, const char *function_name)
148+
void LuaCallGlobalFunction(lua_State *L, const char *function_name, MapObject *mo)
149149
{
150150
EDGE_ZoneScoped;
151151

@@ -154,15 +154,27 @@ void LuaCallGlobalFunction(lua_State *L, const char *function_name)
154154
int status = 0;
155155
if (lua_debug.d_)
156156
{
157-
status = dbg_pcall(L, 0, 0, 0);
157+
if (mo)
158+
{
159+
CreateLuaTable_Mobj(L, mo);
160+
status = dbg_pcall(L, 1, 0, 0);
161+
}
162+
else
163+
status = dbg_pcall(L, 0, 0, 0);
158164
}
159165
else
160166
{
161167
int base = lua_gettop(L); // function index
162168
lua_pushcfunction(L, LuaMsgHandler); // push message handler */
163169
lua_insert(L, base); // put it under function and args */
164170

165-
status = lua_pcall(L, 0, 0, base);
171+
if (mo)
172+
{
173+
CreateLuaTable_Mobj(L, mo);
174+
status = lua_pcall(L, 1, 0, base);
175+
}
176+
else
177+
status = lua_pcall(L, 0, 0, base);
166178
}
167179

168180
if (status != LUA_OK)

0 commit comments

Comments
 (0)