the script seems to work, everybody has the weapons i expect them to have, but once i change level (not always!) everything explodes with dozens of
Code: Select all
! [LUA] SCRIPT RUNTIME ERROR : evaluator [state_mgr_weapon] returns value with not a bool type!
sometimes even
Code: Select all
0023:757E4878 KERNELBASE.dll, RaiseException()
0023:6DE59339 MSVCR120.dll, CxxThrowException()
0023:6DEDE9D5 MSVCR120.dll, _RTCastToVoid()
0023:0042E720 xrEngine.exe, text_editor::line_edit_control::SwitchKL()
0023:0042C1C2 xrEngine.exe, text_editor::line_edit_control::~line_edit_control()
0023:044B4147 xrGame.dll, CDialogHolder::TopInputReceiver()
0023:044ABAFC xrGame.dll, CDialogHolder::TopInputReceiver()
0023:0451650A xrGame.dll, CDialogHolder::TopInputReceiver()
0023:04516232 xrGame.dll, CDialogHolder::TopInputReceiver()
0023:045162A8 xrGame.dll, CDialogHolder::TopInputReceiver()
0023:0451650A xrGame.dll, CDialogHolder::TopInputReceiver()
0023:04516232 xrGame.dll, CDialogHolder::TopInputReceiver()
0023:0436F518 xrGame.dll, CDialogHolder::IgnorePause()
0023:0451650A xrGame.dll, CDialogHolder::TopInputReceiver()
0023:04516232 xrGame.dll, CDialogHolder::TopInputReceiver()
0023:044FFB78 xrGame.dll, CDialogHolder::TopInputReceiver()
0023:044E0921 xrGame.dll, CDialogHolder::TopInputReceiver()
0023:6E88266D lua51.dll
0023:6E8A5671 lua51.dll, lua_error()
[error][ 6] : The handle is invalid.
can't post full logs because they're too large for the forum post limit and pastebin, but the parts above are the only ones out of the ordinary
the script
Code: Select all
local ammo_prefix = "ammo_"
local weapon_prefix = "wpn_"
local grenade_prefix = "grenade_"
local first_update_done_flag = "strelocc_de_first_update_done"
local sim = alife()
local ini = system_ini()
function is_first_update_done(npc)
return utils.se_obj_load_var(npc:id(),npc:name(),first_update_done_flag)
end
local function npc_on_update(npc,st)
if not (npc:alive()) then
return
end
if(is_first_update_done(npc)) then
return
end
remove_weapons_and_ammo(npc)
give_weapon_and_ammo(npc)
give_grenades(2, npc)
utils.se_obj_save_var(npc:id(),npc:name(),first_update_done_flag,true)
end
function give_weapon_and_ammo(npc)
local pos = npc:position()
local lvid = npc:level_vertex_id()
local gvid = npc:game_vertex_id()
local id = npc:id()
--primary
local w = get_primary(npc)
sim:create(w,pos,lvid,gvid,id)
local a = get_ammo(w)
sim:create(a,pos,lvid,gvid,id)
--pistol
w = get_pistol(npc)
printf("strelocc_DE_debug: checking %s (%s) - weapon: %s", npc:id(), character_community(npc), w)
sim:create(w,pos,lvid,gvid,id)
a = get_ammo(w)
sim:create(a,pos,lvid,gvid,id)
end
function get_ammo(weapon)
local ammo_class = alun_utils.parse_list(ini,weapon,"ammo_class")
local ammo = ammo_class[math.random(1,#ammo_class)]
return ammo
end
function give_grenades(n, npc)
local i = 0
local g = get_grenades(npc)
if (g == nil) then
return
end
local pos = npc:position()
local lvid = npc:level_vertex_id()
local gvid = npc:game_vertex_id()
local id = npc:id()
while i<n do
sim:create(g,pos,lvid,gvid,id)
i = i+1
end
end
function remove_weapons_and_ammo(npc)
local function itr(npc,itm)
if(string.find(itm:name(), ammo_prefix)~=nil or string.find(itm:name(),weapon_prefix)~=nil or string.find(itm:name(),grenade_prefix)~=nil) then
local sim = alife()
local se_obj = sim:object(itm:id())
alife():release(alife():object(itm:id()), true)
end
end
npc:iterate_inventory(itr,npc)
end
function on_game_start()
RegisterScriptCallback("npc_on_update",npc_on_update)
end
function concat_2tables(table1, table2)
local t3 = {}
local len = table.getn(t3)
for key, val in pairs(table1)do
table.insert(t3,val)
end
for key, val in pairs(table2)do
table.insert(t3,val)
end
return t3
end
function concatenateTables( tableList )
if tableList==nil then
return nil
elseif table.getn(tableList) == 1 then
return tableList[1]
else
local table1 = tableList[1]
local restTableList = {unpack(tableList, 2)}
return concat_2tables(table1, concatenateTables(restTableList))
end
end
function get_rank(npc)
local rank = ranks.get_obj_rank_name(npc)
if rank == "novice" then return 1
elseif rank == "trainee" then return 2
elseif rank == "experienced" then return 3
elseif rank == "professional" then return 4
elseif rank == "veteran" then return 5
elseif rank == "expert" then return 6
elseif rank == "master" then return 7
elseif rank == "legend" then return 8 end
printf("npc had no rank? %s",rank)
return 1
end
function day_passed()
local sec_passed = game.get_game_time():diffSec(level.get_start_time())
local day_divisor = 60*60*24
return sec_passed/day_divisor
end
---weapons
local pistols_9x18 = {"wpn_fort","wpn_pm","wpn_pb"}
local pistols_9x19 = {"wpn_beretta","wpn_hpsa","wpn_walther"}
local pistols_45 = {"wpn_colt1911","wpn_sig220","wpn_usp"}
local pistols_45_special = {"wpn_desert_eagle"}
function get_pistol(npc)
local faction = character_community(npc)
local rank = get_rank(npc)
local pool = concatenateTables({pistols_9x18, pistols_9x19, pistols_45})
--standard issue for army guys and ecologist
if(faction == "army" or faction == "ecolog") then
return pistols_9x18[math.random(#pistols_9x18)]
end
--bandits use whatever, veteran bandits like big toys so they can get deagle
if(faction == "bandit" and rank > 5) then
pool = concatenateTables({pool, pistols_45_special})
end
--csky
--csky uses whatever
--duty plays pretend military and gives rookies standard issue, experienced ones use whatever
if(faction == "dolg" and rank < 5) then
return pistols_9x18[math.random(#pistols_9x18)]
end
--ecologist
--see army
--freedom and mercs doesn't use slav pistols
if(faction == "freedom" or faction == "killer") then
pool = concatenateTables({pistols_9x19, pistols_45})
end
--killer
--see freedom
--monolith
--stalker
--zombied
--they all use whatever
return pool[math.random(#pool)]
end
function get_primary(npc)
return "wpn_bm16"
end
function get_grenades(npc)
local faction = character_community(npc)
local rank = get_rank(npc)
--standard issue
if(faction == "army") then
return "grenade_rgd5"
end
--scientist dont carry grenades
if(faction == "ecolog") then
return nil
end
--veterans and better regadless of faction have f1
--professional and worse have up to 40% chance to have f1
if(rank > 4) then
return "grenade_f1"
else
local f1_chance = 10*rank
local chance = math.random(101)-1
if(chance > f1_chance) then
return "grenade_f1"
else
return "grenade_rgd5"
end
end
return nil
end