Advertisement
Guest User

Spawn Secondary Primary by gir489 v3

a guest
Jul 5th, 2014
272
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.78 KB | None | 0 0
  1. --[[ * SimpleMenu implementation of Spawn Primary Weapons by gir489 v3
  2.      *
  3.      * Credits;
  4.      *          Harfatus for SimpleMenu.
  5.      *          SquareOne for the precache fix for U31.
  6.      *          90e for helping me log set_unit shit.
  7.      *
  8.      * Changelog:
  9.      *      v1: Initial release
  10.      *      v2: Compensated for the U31 update
  11.      *      v3: Fixed clients crashing when spawning weapons.
  12.      *
  13.      *  Not for use in Pirate Perfagtion Trainer]]
  14.  
  15. if not SimpleMenu then
  16.     SimpleMenu = class()
  17.  
  18.     function SimpleMenu:init(title, message, options)
  19.         self.dialog_data = { title = title, text = message, button_list = {},
  20.                              id = tostring(math.random(0,0xFFFFFFFF)) }
  21.         self.visible = false
  22.         for _,opt in ipairs(options) do
  23.             local elem = {}
  24.             elem.text = opt.text
  25.             opt.data = opt.data or nil
  26.             opt.callback = opt.callback or nil
  27.             elem.callback_func = callback(self, self, "_do_callback",
  28.                                           { data = opt.data,
  29.                                             callback = opt.callback})
  30.             elem.cancel_button = opt.is_cancel_button or false
  31.             if opt.is_focused_button then
  32.                 self.dialog_data.focus_button = #self.dialog_data.button_list+1
  33.             end
  34.             table.insert(self.dialog_data.button_list, elem)
  35.         end
  36.         return self
  37.     end
  38.  
  39.     function SimpleMenu:_do_callback(info)
  40.         if info.callback then
  41.             if info.data then
  42.                 info.callback(info.data)
  43.             else
  44.                 info.callback()
  45.             end
  46.         end
  47.         self.visible = false
  48.     end
  49.  
  50.     function SimpleMenu:show()
  51.         if self.visible then
  52.             return
  53.         end
  54.         self.visible = true
  55.         managers.system_menu:show(self.dialog_data)
  56.     end
  57.  
  58.     function SimpleMenu:hide()
  59.         if self.visible then
  60.             managers.system_menu:close(self.dialog_data.id)
  61.             self.visible = false
  62.             return
  63.         end
  64.     end
  65. end
  66.  
  67. patched_update_input = patched_update_input or function (self, t, dt )
  68.     if self._data.no_buttons then
  69.         return
  70.     end
  71.    
  72.     local dir, move_time
  73.     local move = self._controller:get_input_axis( "menu_move" )
  74.  
  75.     if( self._controller:get_input_bool( "menu_down" )) then
  76.         dir = 1
  77.     elseif( self._controller:get_input_bool( "menu_up" )) then
  78.         dir = -1
  79.     end
  80.    
  81.     if dir == nil then
  82.         if move.y > self.MOVE_AXIS_LIMIT then
  83.             dir = 1
  84.         elseif move.y < -self.MOVE_AXIS_LIMIT then
  85.             dir = -1
  86.         end
  87.     end
  88.  
  89.     if dir ~= nil then
  90.         if( ( self._move_button_dir == dir ) and self._move_button_time and ( t < self._move_button_time + self.MOVE_AXIS_DELAY ) ) then
  91.             move_time = self._move_button_time or t
  92.         else
  93.             self._panel_script:change_focus_button( dir )
  94.             move_time = t
  95.         end
  96.     end
  97.  
  98.     self._move_button_dir = dir
  99.     self._move_button_time = move_time
  100.    
  101.     local scroll = self._controller:get_input_axis( "menu_scroll" )
  102.     -- local sdir
  103.     if( scroll.y > self.MOVE_AXIS_LIMIT ) then
  104.         self._panel_script:scroll_up()
  105.         -- sdir = 1
  106.     elseif( scroll.y < -self.MOVE_AXIS_LIMIT ) then
  107.         self._panel_script:scroll_down()
  108.         -- sdir = -1
  109.     end
  110. end
  111. managers.system_menu.DIALOG_CLASS.update_input = patched_update_input
  112. managers.system_menu.GENERIC_DIALOG_CLASS.update_input = patched_update_input
  113.  
  114. spawnprimarycallback = spawnprimarycallback or function(info)
  115.     if ( managers.player:player_unit() ) then
  116.         local weapon = Global.blackmarket_manager.crafted_items.primaries[info]
  117.         if weapon then
  118.             managers.blackmarket:equip_weapon("primaries", info)
  119.             managers.network:session():send_to_peers_synched("set_unit", managers.player:player_unit(), managers.network:session():local_peer():character(), managers.blackmarket:outfit_string(), managers.network:session():local_peer():outfit_version(), managers.network:session():local_peer():id())
  120.             managers.dyn_resource:load(Idstring("unit"), Idstring(tweak_data.weapon.factory[weapon.factory_id].unit), "packages/dyn_resources", false)
  121.             managers.player:player_unit():inventory():add_unit_by_factory_name( weapon.factory_id, false, false, weapon.blueprint, weapon.texture_switches )
  122.         end
  123.     end
  124. end
  125.  
  126. opts = {}
  127. for i,weapon in pairs(Global.blackmarket_manager.crafted_items.primaries) do
  128.     if ( weapon ) then
  129.         table.insert( opts, { text = managers.blackmarket:get_weapon_name_by_category_slot("primaries",i):gsub("\"", ""), callback = spawnprimarycallback, data = i } )
  130.     end
  131. end
  132. table.insert( opts, { text = "Close", is_cancel_button = true } )
  133. mymenu = SimpleMenu:new("Spawn Primary Weapons", "Select the weapon you want to spawn.", opts)
  134. mymenu:show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement