Advertisement
Guest User

zoa_npc_items.script

a guest
Mar 1st, 2014
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.15 KB | None | 0 0
  1. local npcTable
  2.  
  3. --condition minimum and maximum (respectively) per faction (0 to 100); if absent, assumes 40-80
  4. local factionCond = {
  5.     [ "stalker" ]   = { 40, 80 },
  6.     [ "bandit" ]    = { 20, 70 },
  7.     [ "military" ]  = { 40, 70 },
  8.     [ "dolg" ]      = { 70, 90 },
  9.     [ "freedom" ]   = { 50, 100 },
  10.     [ "killer" ]    = { 70, 80 },
  11.     [ "ecolog" ]    = { 60, 80 },
  12.     [ "zombie" ]    = { 0, 60 },
  13.     }
  14.  
  15. local rankZero      = 400 --rank below this point subtracts condition
  16. local rankFactor    = 0.001 --how much rank affects condition
  17.  
  18. --sets condition and attachments if applicable
  19. function customData( target, character )
  20.     local weaponData = packet_utils.get_object_data( target )
  21.     if weaponData then
  22.         local minCond = 40
  23.         local maxCond = 80
  24.         if factionCond[ character:character_community() ] then
  25.             minCond = factionCond[ character:character_community() ][ 1 ]
  26.             maxCond = factionCond[ character:character_community() ][ 2 ]
  27.         end
  28.         local condition = ( math.random( minCond, maxCond ) / 100 ) + ( ( character:rank() - rankZero ) * rankFactor )
  29.         if condition > 1 then
  30.             condition = 1
  31.         elseif condition < 0 then
  32.             condition = 0
  33.         end
  34.         tkgp_utils.debugMessage( "zoa_npc_items.customData | Attempting to set condition to "..condition, "console" )
  35.         weaponData.upd_condition = condition
  36.         if npcTable[ "addons" ] then
  37.             local addonFlags    = 0
  38.             local scopeVal      = tkgp_utils.getLtxValue( target:section_name(), "scope_status", "float" )
  39.             local launcherVal   = tkgp_utils.getLtxValue( target:section_name(), "grenade_launcher_status", "float" )
  40.             local suppressorVal = tkgp_utils.getLtxValue( target:section_name(), "silencer_status", "float" )
  41.             if math.random( 100 ) <= npcTable[ "addons" ][ 1 ] and scopeVal and scopeVal == 2 then
  42.                 addonFlags = addonFlags + 1
  43.             end
  44.             if math.random( 100 ) <= npcTable[ "addons" ][ 2 ] and launcherVal and launcherVal == 2 then
  45.                 addonFlags = addonFlags + 2
  46.             end
  47.             if math.random( 100 ) <= npcTable[ "addons" ][ 3 ] and suppressorVal and suppressorVal == 2 then
  48.                 addonFlags = addonFlags + 4
  49.             end
  50.             tkgp_utils.debugMessage( "zoa_npc_items.customData | Attempting to set addon flags to "..addonFlags, "console" )
  51.             weaponData.upd_addon_flags = addonFlags
  52.         end
  53.         if not packet_utils.set_object_data( target, weaponData ) then
  54.             tkgp_utils.debugMessage( "zoa_npc_items.customData | Failed to set object data", "console" )
  55.         else
  56.             local testData = packet_utils.get_object_data( target )
  57.             tkgp_utils.debugMessage( "zoa_npc_items.customData | condition: "..testData.condition, "console" )
  58.             tkgp_utils.debugMessage( "zoa_npc_items.customData | upd_addon_flags: "..testData.upd_addon_flags, "console" )
  59.         end
  60.     else
  61.         tkgp_utils.debugMessage( "zoa_npc_items.customData | Failed to get weaponData", "console" )
  62.     end
  63. end
  64.  
  65. --choose a weapon from the appropriate table and spawn it
  66. function pickWeapon( target, options )
  67.     local totalProb = 0
  68.     for section, chance in pairs( options ) do
  69.         totalProb = totalProb + chance
  70.     end
  71.     local roll = math.random( totalProb )
  72.     local currentChance = 0
  73.     for section, chance in pairs( options ) do
  74.         currentChance = currentChance + chance
  75.         if roll <= currentChance then
  76.             if section ~= "empty" then
  77.                 customData( alife():create( section, target:position(), target:level_vertex_id(), target:game_vertex_id() ), target )
  78.             end
  79.             return
  80.         end
  81.     end
  82. end
  83.  
  84. --on every NPC's creation, attempt to give them appropriate weapons
  85. function firstUpdate( levelObj )
  86.     local tableKey = levelObj:profile_name()
  87.     if zoa_item_tables.weaponTables[ tableKey ] then
  88.         tkgp_utils.debugMessage( "zoa_npc_items.onInit | Processing "..tableKey, "console" )
  89.         npcTable = zoa_item_tables.weaponTables[ tableKey ]
  90.         if npcTable[ "primary" ] then
  91.             pickWeapon( levelObj, npcTable[ "primary" ] )
  92.         else
  93.             tkgp_utils.debugMessage( "zoa_npc_items.onInit | No primary table found for "..tableKey, "console" )
  94.         end
  95.         if npcTable[ "secondary" ] then
  96.             pickWeapon( levelObj, npcTable[ "secondary" ] )
  97.         else
  98.             tkgp_utils.debugMessage( "zoa_npc_items.onInit | No secondary table found for "..tableKey, "console" )
  99.         end
  100.     else
  101.         tkgp_utils.debugMessage( "zoa_npc_items.onInit | No weapon tables found for "..tableKey, "console" )
  102.     end
  103. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement