Advertisement
Guest User

xrs_rnd_npc_loadout.script

a guest
Sep 1st, 2015
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.34 KB | None | 0 0
  1. --[[
  2. ************************************************************
  3. xrs_rnd_npc_loadout
  4. by Alundaio
  5. ************************************************************
  6.  
  7. Copyright (C) 2012 Alundaio
  8. This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License-]]
  9. --]]
  10.  
  11. local ini = ini_file("ai_tweaks\\xrs_rnd_npc_loadout.ltx")
  12. function on_game_start()
  13. local Enabled = alun_utils.read_from_ini(ini,"plugin","enable","bool",false)
  14. if not (Enabled) then return end
  15. RegisterScriptCallback("se_stalker_on_spawn",on_creation)
  16. end
  17.  
  18. function on_creation(se_npc)
  19. if not (se_npc) then
  20. return
  21. end
  22. local squad = se_npc.group_id and se_npc.group_id ~= 65535 and alife():object(se_npc.group_id)
  23. local player_id = squad and squad.player_id
  24. local comm = se_npc:community()
  25. local rank = ranks.get_obj_rank_name(se_npc)
  26.  
  27. create_weapon_on_npc(se_npc,comm,rank,player_id,"primary")
  28. create_weapon_on_npc(se_npc,comm,rank,player_id,"secondary")
  29. create_weapon_on_npc(se_npc,comm,rank,player_id,"tertiary")
  30. create_items_on_npc(se_npc,comm,rank,player_id)
  31. end
  32.  
  33. function ini_get_list(se_npc,comm,rank,player_id,val)
  34. local src = alun_utils.read_from_ini(ini,se_npc:section_name(),val,"string",nil) or player_id and alun_utils.read_from_ini(ini,player_id.."_"..rank,val,"string",nil) or alun_utils.read_from_ini(ini,comm.."_"..rank,val,"string",nil)
  35.  
  36. if not (src) then
  37. return
  38. end
  39.  
  40. local sec = xr_logic.pick_section_from_condlist(db.actor,se_npc,xr_logic.parse_condlist(se_npc,se_npc:section_name(),val,src))
  41.  
  42. if (ini:section_exist(sec)) then
  43. return alun_utils.collect_section(ini,sec)
  44. end
  45. end
  46.  
  47. function create_items_on_npc(se_npc,comm,rank,player_id)
  48. local t = ini_get_list(se_npc,comm,rank,player_id,"extra")
  49. if not (t) then return end
  50. for i,v in ipairs(t) do
  51. spawn_extra_items(se_npc,v)
  52. end
  53. end
  54.  
  55. function spawn_extra_items(se_npc,str)
  56. if not (str) then return end
  57. if (str == "") then return end
  58.  
  59. local item = alun_utils.str_explode(str,":")
  60.  
  61. if (system_ini():section_exist(item[1])) then
  62. item[2] = item[2] and tonumber(item[2]) or 1
  63. item[3] = item[3] and tonumber(item[3]) or 1
  64. local amt = math.random(item[2],item[3])
  65. if (amt > 0) then
  66. for i=1,amt do
  67. alife():create(item[1],se_npc.position,0,0,se_npc.id)
  68. end
  69. end
  70. end
  71. end
  72.  
  73. function create_weapon_on_npc(se_npc,comm,rank,player_id,slot)
  74. local t = ini_get_list(se_npc,comm,rank,player_id,slot)
  75. if not (t) then
  76. return
  77. end
  78.  
  79. local info = select_random_index(t)
  80. if not (info) or (info == "") or (info == "none") then
  81. return
  82. end
  83.  
  84. local weapon = alun_utils.str_explode(info,":")
  85.  
  86. if (system_ini():section_exist(weapon[1])) then
  87.  
  88. --printf("xrs_rnd_npc_loadout: before create %s",weapon[1])
  89. local se_wpn = alife():create(weapon[1],se_npc.position,0,0,se_npc.id)
  90. --printf("xrs_rnd_npc_loadout: after create %s",weapon[1])
  91. if (se_wpn) then
  92.  
  93. --printf("xrs_rnd_npc_loadout: spawn weapon [start] = %s",se_wpn:section_name())
  94. local ammos = alun_utils.parse_list(system_ini(),se_wpn:section_name(),"ammo_class")
  95.  
  96. local ct = ammos and #ammos
  97. local ammo_type = ammos and weapon[3] and weapon[3] == "r" and ct and math.random(0,ct-1) or ammos and weapon[3] and tonumber(weapon[3]) or 0
  98. local ammo_section = ammo_type and ammos[ammo_type+1]
  99.  
  100. if not (ammo_section) then
  101. ammo_type = 0
  102. ammo_section = ammo_type and ammos[ammo_type+1]
  103. printf("xrs_rnd_npc_loadout: error wrong ammo_type set for %s, missing value in ammo_class",weapon[1])
  104. end
  105.  
  106.  
  107. if (weapon[2] or weapon[3]) then
  108. local data = stpk_utils.get_weapon_data(se_wpn)
  109.  
  110. local flag = tonumber(weapon[2]) or 0
  111. if (weapon[2] == "r") then
  112. flag = 0
  113. if (alun_utils.read_from_ini(nil,weapon[1],"scope_status","float",nil)) then
  114. flag = flag + 1
  115. end
  116.  
  117. if (alun_utils.read_from_ini(nil,weapon[1],"grenade_launcher_status","float",nil)) then
  118. flag = flag + 2
  119. end
  120.  
  121. if (alun_utils.read_from_ini(nil,weapon[1],"silencer_status","float",nil)) then
  122. flag = flag + 4
  123. end
  124.  
  125. flag = math.random(0,flag)
  126. end
  127.  
  128. data.addon_flags = flag
  129.  
  130.  
  131. data.ammo_type = ammo_type
  132. stpk_utils.set_weapon_data(data,se_wpn)
  133. end
  134.  
  135. local ammo_count
  136. if (weapon[4] and weapon[5]) then
  137. ammo_count = math.random( tonumber(weapon[4]) or 1, tonumber(weapon[5]) or 1 )
  138. else
  139. ammo_count = ini_get_list(se_npc,comm,rank,player_id,slot == "primary" and "ammo_1" or slot == "secondary" and "ammo_2" or slot == "tertiary" and "ammo_3")
  140.  
  141. if (ammo_count) then
  142. ammo_count[1] = ammo_count[1] and tonumber(ammo_count[1]) or 1
  143. ammo_count[2] = ammo_count[2] and tonumber(ammo_count[2]) or 1
  144.  
  145. ammo_count = ammo_count[1] < ammo_count[2] and math.random(ammo_count[1],ammo_count[2]) or math.random(ammo_count[2],ammo_count[1])
  146. end
  147. end
  148.  
  149. if not (ammo_count) then
  150. ammo_count = 1
  151. end
  152.  
  153. if (system_ini():section_exist(ammo_section)) then
  154. create_ammo(ammo_section,se_npc.position,0,0,se_npc.id,ammo_count)
  155. else
  156. --printf("xrs_rnd_npc_loadout: Section %s does not exist",ammo_section)
  157. end
  158.  
  159. --printf("xrs_rnd_npc_loadout: spawn weapon [END] = %s",se_wpn:section_name())
  160. end
  161. end
  162. end
  163.  
  164. function select_random_index(t)
  165. if not (t) or (#t == 0) then
  166. return
  167. end
  168. local n = alun_utils.rand_num(1,#t)
  169. return t[n]
  170. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement