X-Git-Url: http://deadsoftware.ru/gitweb?a=blobdiff_plain;f=src%2Fgame%2Fg_scripts.pas;h=d1113c9e97eb9b7cc51b99dae2f46ff6c6d707a0;hb=2a785940b8e595cffcc9e9d652c2ae9248c989ee;hp=43fe3afc42227e6d453417bc604a2b7e2265de7f;hpb=0aed9f9d49f034aad7d8856db00fe42f3303826d;p=d2df-sdl.git diff --git a/src/game/g_scripts.pas b/src/game/g_scripts.pas index 43fe3af..d1113c9 100644 --- a/src/game/g_scripts.pas +++ b/src/game/g_scripts.pas @@ -23,10 +23,13 @@ uses const // reset levels - RESET_ALL = 0; - RESET_SRV = 1; - RESET_WAD = 2; - RESET_MAP = 3; + RESET_SRV_BIT = 4; + RESET_WAD_BIT = 2; + RESET_MAP_BIT = 1; + RESET_SRV = RESET_SRV_BIT or RESET_WAD_BIT or RESET_MAP_BIT; + RESET_WAD = RESET_WAD_BIT or RESET_MAP_BIT; + RESET_MAP = RESET_MAP_BIT; + RESET_ALL = 255; type PScriptContext = Plua_State; @@ -37,8 +40,8 @@ var gScriptInit: Boolean = False; function g_Scripts_Init(): Boolean; -procedure g_Scripts_Reset(What: Integer); -function g_Scripts_ProcExec(PName: string; const Args: array of const): Integer; +procedure g_Scripts_Reset(What: Byte); +function g_Scripts_ProcExec(PName: string; const Args: array of const; Namespace: string = ''): Integer; function g_Scripts_ProcExists(PName: string): Boolean; function g_Scripts_ProcInstall(PName: string; PPtr: PScriptProc): Boolean; function g_Scripts_Load(Text: string): Boolean; @@ -50,18 +53,20 @@ uses SysUtils, g_console, g_scriptprocs; type - POpenFunc = function(L: Plua_State): LongBool; cdecl; TLuaReg = record name: PAnsiChar; - func: POpenFunc; + func: lua_CFunction; end; const - LUA_LIBS: array [0..3] of TLuaReg = ( + LUA_LIBS: array [0..6] of TLuaReg = ( (name: ''; func: luaopen_base), + (name: LUA_LOADLIBNAME; func: luaopen_package), (name: LUA_TABLIBNAME; func: luaopen_table), - (name: LUA_STRLINAME; func: luaopen_string), // STRLINAME is actually a typo in fpc's lua module - (name: LUA_MATHLIBNAME; func: luaopen_math) + (name: LUA_STRLIBNAME; func: luaopen_string), + (name: LUA_MATHLIBNAME; func: luaopen_math), + (name: LUA_OSLIBNAME; func: luaopen_os), // TODO: something to restrict these two + (name: LUA_IOLIBNAME; func: luaopen_io) ); function LuaInstallGameFuncs(): Boolean; @@ -69,6 +74,52 @@ begin Result := False; if not g_Scripts_ProcInstall('conprint', SP_Lua_ConPrint) then Exit; + if not g_Scripts_ProcInstall('message', SP_Lua_Message) then Exit; + if not g_Scripts_ProcInstall('sound', SP_Lua_PlaySound) then Exit; + if not g_Scripts_ProcInstall('get_gamemode', SP_Lua_GetGameMode) then Exit; + if not g_Scripts_ProcInstall('get_gametype', SP_Lua_GetGameType) then Exit; + if not g_Scripts_ProcInstall('get_time', SP_Lua_GetTime) then Exit; + + if not g_Scripts_ProcInstall('player_get_keys', SP_Lua_PlayerGetKeys) then Exit; + if not g_Scripts_ProcInstall('player_get_armor', SP_Lua_PlayerGetArmor) then Exit; + if not g_Scripts_ProcInstall('player_get_score', SP_Lua_PlayerGetScore) then Exit; + if not g_Scripts_ProcInstall('player_get_name', SP_Lua_PlayerGetName) then Exit; + if not g_Scripts_ProcInstall('player_get_team', SP_Lua_PlayerGetTeam) then Exit; + + if not g_Scripts_ProcInstall('uid_get_health', SP_Lua_ActorGetHealth) then Exit; + if not g_Scripts_ProcInstall('uid_get_pos', SP_Lua_ActorGetPos) then Exit; + if not g_Scripts_ProcInstall('uid_get_state', SP_Lua_ActorGetState) then Exit; + if not g_Scripts_ProcInstall('uid_get_type', SP_Lua_ActorGetType) then Exit; + if not g_Scripts_ProcInstall('uid_nearest', SP_Lua_ActorNearest) then Exit; + if not g_Scripts_ProcInstall('uid_farthest', SP_Lua_ActorFarthest) then Exit; + if not g_Scripts_ProcInstall('uid_damage', SP_Lua_ActorDamage) then Exit; + if not g_Scripts_ProcInstall('uid_push', SP_Lua_ActorPush) then Exit; + if not g_Scripts_ProcInstall('uid_teleport', SP_Lua_ActorTeleport) then Exit; + + if not g_Scripts_ProcInstall('trigger_get_enabled', SP_Lua_TriggerGetEnabled) then Exit; + if not g_Scripts_ProcInstall('trigger_set_enabled', SP_Lua_TriggerSetEnabled) then Exit; + if not g_Scripts_ProcInstall('trigger_activate', SP_Lua_TriggerActivate) then Exit; + if not g_Scripts_ProcInstall('trigger_get_pos', SP_Lua_TriggerGetPos) then Exit; + if not g_Scripts_ProcInstall('trigger_set_pos', SP_Lua_TriggerSetPos) then Exit; + + if not g_Scripts_ProcInstall('panel_get_type', SP_Lua_PanelGetType) then Exit; + if not g_Scripts_ProcInstall('panel_get_pos', SP_Lua_PanelGetPos) then Exit; + if not g_Scripts_ProcInstall('panel_get_size', SP_Lua_PanelGetSize) then Exit; + if not g_Scripts_ProcInstall('panel_set_pos', SP_Lua_PanelSetPos) then Exit; + if not g_Scripts_ProcInstall('panel_switch_texture', SP_Lua_PanelSwitchTexture) then Exit; + + if not g_Scripts_ProcInstall('door_get_open', SP_Lua_DoorGetState) then Exit; + if not g_Scripts_ProcInstall('door_close', SP_Lua_DoorClose) then Exit; + if not g_Scripts_ProcInstall('door_close_trap', SP_Lua_DoorCloseTrap) then Exit; + if not g_Scripts_ProcInstall('door_open', SP_Lua_DoorOpen) then Exit; + if not g_Scripts_ProcInstall('door_toggle', SP_Lua_DoorToggle) then Exit; + if not g_Scripts_ProcInstall('lift_get_dir', SP_Lua_LiftGetDir) then Exit; + if not g_Scripts_ProcInstall('lift_set_dir', SP_Lua_LiftSetDir) then Exit; + + if not g_Scripts_ProcInstall('spawn_item', SP_Lua_SpawnItem) then Exit; + if not g_Scripts_ProcInstall('spawn_shot', SP_Lua_SpawnShot) then Exit; + if not g_Scripts_ProcInstall('spawn_effect', SP_Lua_SpawnEffect) then Exit; + if not g_Scripts_ProcInstall('spawn_monster', SP_Lua_SpawnMonster) then Exit; Result := True; end; @@ -86,10 +137,9 @@ begin // don't open all the libs for i := 0 to High(LUA_LIBS) do begin - //lua_pushcfunction(gScriptCtx, LUA_LIBS[i].func); + lua_pushcfunction(gScriptCtx, LUA_LIBS[i].func); lua_pushstring(gScriptCtx, LUA_LIBS[i].name); - //lua_call(gScriptCtx, 1, 0); - LUA_LIBS[i].func(gScriptCtx); + lua_call(gScriptCtx, 1, 0); end; // create a table for game-related functions @@ -114,23 +164,29 @@ begin end; // TODO: maybe actually put some fields into these? -procedure g_Scripts_Reset(What: Integer); +procedure g_Scripts_Reset(What: Byte); begin if not gScriptInit then Exit; - if What in [RESET_ALL, RESET_SRV] then + if LongBool(What and RESET_SRV_BIT) then begin lua_newtable(gScriptCtx); lua_setglobal(gScriptCtx, 'srv'); end; - if What in [RESET_ALL, RESET_WAD] then + if LongBool(What and RESET_WAD_BIT) then begin lua_newtable(gScriptCtx); lua_setglobal(gScriptCtx, 'wad'); end; - if What in [RESET_ALL, RESET_MAP] then + if LongBool(What and RESET_MAP_BIT) then begin lua_newtable(gScriptCtx); lua_setglobal(gScriptCtx, 'map'); + + // disable io and os modules on any kind of reset + lua_pushnil(gScriptCtx); + lua_pushnil(gScriptCtx); + lua_setglobal(gScriptCtx, 'os'); + lua_setglobal(gScriptCtx, 'io'); end; end; @@ -167,19 +223,25 @@ begin Result := True; end; -function g_Scripts_ProcExec(PName: string; const Args: array of const): Integer; +function g_Scripts_ProcExec(PName: string; const Args: array of const; Namespace: string = ''): Integer; var i: Integer; begin Result := -255; if not gScriptInit then Exit; - lua_getglobal(gScriptCtx, 'game'); - lua_pushstring(gScriptCtx, PName); - lua_gettable(gScriptCtx, -2); + if Namespace = '' then + lua_getglobal(gScriptCtx, PChar(PName)) + else + begin + lua_getglobal(gScriptCtx, PChar(Namespace)); + lua_pushstring(gScriptCtx, PName); + lua_gettable(gScriptCtx, -2); + end; + if not lua_isfunction(gScriptCtx, -1) then begin - g_Console_Add('SCRIPT: ProcExec(' + PName + ') error: no such function'); + g_Console_Add('SCRIPT: ProcExec(' + Namespace + '.' + PName + ') error: no such function'); Exit; end; @@ -197,18 +259,16 @@ begin if lua_pcall(gScriptCtx, Length(Args), 1, 0) <> 0 then begin - g_Console_Add('SCRIPT: ProcExec(' + PName + ') error: ' + lua_tostring(gScriptCtx, -1)); + g_Console_Add('SCRIPT: ProcExec(' + Namespace + '.' + PName + ') error: ' + lua_tostring(gScriptCtx, -1)); Exit; end; - if not lua_isnumber(gScriptCtx, -1) then + Result := 0; + if lua_isnumber(gScriptCtx, -1) then begin - g_Console_Add('SCRIPT: ProcExec(' + PName + ') error: return value is not a number'); - Exit; + Result := lua_tointeger(gScriptCtx, -1); + lua_pop(gScriptCtx, 1); end; - - Result := lua_tointeger(gScriptCtx, -1); - lua_pop(gScriptCtx, 1); end; function g_Scripts_Load(Text: string): Boolean;