Advertisement
Guest User

Untitled

a guest
Jan 23rd, 2015
427
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.23 KB | None | 0 0
  1. -- Add Weapon Mod 1.0
  2. -- Script to add individual weapon mods to inventory
  3. PD2 = class()
  4.  
  5. function PD2:getWeaponModPairs()
  6.     return pairs(tweak_data.blackmarket.weapon_mods)
  7. end
  8.  
  9. function PD2:getWeaponModName(mod_id)
  10.     return self:localize(tweak_data.blackmarket.weapon_mods[mod_id].name_id)
  11. end
  12.  
  13. function PD2:isLocalized(name_id)
  14.     return name_id and managers.localization:exists(name_id)
  15. end
  16.  
  17. function PD2:localize(name_id)
  18.     if (self:isLocalized(name_id)) then
  19.         return managers.localization:text(name_id)
  20.     end
  21.  
  22.     return name_id
  23. end
  24.  
  25. function PD2:getWeaponModAmount(mod_id)
  26.     return managers.blackmarket:get_item_amount("normal", "weapon_mods", mod_id, true)
  27. end
  28.  
  29. function PD2:addWeaponMod(mod_id)
  30.     self:println("Adding " .. self:getWeaponModName(mod_id) .. " to inventory")
  31.  
  32.     managers.blackmarket:add_to_inventory("normal", "weapon_mods", mod_id, true)
  33.  
  34.     self:println("Added " .. self:getWeaponModName(mod_id) .. " to inventory, current stock " .. self:getWeaponModAmount(mod_id))
  35. end
  36.  
  37. function PD2:println(text)
  38.     io.stdout:write(text .. "\n")
  39. end
  40.  
  41. function PD2:showDialog(title, options)
  42.     show_sorted_dialog(title, "", options, nil, 20, 1)
  43. end
  44.  
  45. WeaponModDialog = class()
  46.  
  47. function WeaponModDialog:init(pd2)
  48.     self.pd2 = pd2
  49. end
  50.  
  51. function WeaponModDialog:showIfNotInGame()
  52.     if not inGame() then
  53.         self:show()
  54.     end
  55. end
  56.  
  57. function WeaponModDialog:show()
  58.     self.pd2:showDialog("Add Weapon Mods", self:getDialogOptions())
  59. end
  60.  
  61. function WeaponModDialog:getDialogOptions()
  62.     local dialog_options = {}
  63.     local pd2 = self.pd2
  64.  
  65.     local function addWeaponModCallback(mod_id)
  66.         pd2:addWeaponMod(mod_id)
  67.     end
  68.  
  69.     local function sortByName(a, b)
  70.         return a.text < b.text
  71.     end
  72.  
  73.     for mod_id, data in pd2.getWeaponModPairs() do
  74.         local name_id = data.name_id
  75.  
  76.         if (pd2:isLocalized(name_id)) then
  77.             local qty = pd2:getWeaponModAmount(mod_id)
  78.             local name = pd2:localize(name_id) .. " / " .. mod_id .. " (" .. qty .. ")"
  79.             table.insert(dialog_options, {
  80.                 text = name,
  81.                 callback = addWeaponModCallback,
  82.                 data = mod_id })
  83.         end
  84.     end
  85.  
  86.     table.sort(dialog_options, sortByName)
  87.  
  88.     return dialog_options
  89. end
  90.  
  91. local pd2 = PD2:new()
  92. local weaponModDialog = WeaponModDialog:new(pd2)
  93. weaponModDialog:showIfNotInGame()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement