Page 1 of 1

Numpad army switching problem

Posted: Mon 28 Nov, 2016 6:57 am
by Black Relic
Ok so i had myradal ages ago make me a piece of code that allowed players to bind all squads that are binded from 1 to 0 to the numpad. Meaning you could have ten tactical marines (that are binded to 1,2,3,4,5,6,7,8,9,0) binded to the numpad 1 and ten scouts (binded to 1,2,3,4,5,6,7,8,9,0) to num pad 2. And that player could switch between numpad 1 and 2 to micro each squad in their respective armies individually.

Now I have run into some problems and a few i have been able to fix however there is one problem i could use some help on.

As we all know as we play this game we tend to mash on a squads key multiple times. I have done the same thing to the numpads. However when i do this all of my squads are then binded to that army like i am selecting all of my squads. Then i have to go and rebind all of the squads to their proper armies.

Now this is the problem I want to fix the most atm. I'll put his code down below.

Code: Select all

cope_ShowCursor = 1


function GameSpeed_Half()
    if (Game_GetSimRate() > 10) then
      Game_SetSimRate(10)
   else
      if (Game_GetSimRate() > 0.5) then
         Game_SetSimRate(Game_GetSimRate() / 2)
      else
         Game_SetSimRate(0.25)
      end
    end
end

function GameSpeed_Double()
    if (Game_GetSimRate() < 10) then
      Game_SetSimRate(10)
   else
      if (Game_GetSimRate() < 1000) then
         Game_SetSimRate(Game_GetSimRate() * 2)
      else
         Game_SetSimRate(2000)
      end
   end
end

function ScaleUIPlus()
   local UISize = UI_GetScale()
   if (UISize < 0.65) then
      UI_SetScale(0.65)
   elseif (UISize < 0.8) then
      UI_SetScale(0.8)
   elseif (UISize < 1) then
      UI_SetScale(1)
   elseif (UISize < 1.15) then
      UI_SetScale(1.15)
   end
end

function ScaleUIMinus()
   local UISize = UI_GetScale()
   if (UISize >= 1.15) then
      UI_SetScale(1)
   elseif (UISize >= 1) then
      UI_SetScale(0.8)
   elseif (UISize >= 0.8) then
      UI_SetScale(0.65)
   elseif (UISize >= 0.65) then
      UI_SetScale(0.57)
   end
end

function UI_ShowGP2(boolean)
   if boolean == false then
      Game_SetMode(UI_Fullscreen)
   else
      Game_SetMode(UI_Normal)
      
   end
end

function UI_ShowGP(boolean)
   if boolean == false then
      UI_ShowCoverOnNonSelectedUnits(false)
      UI_ShowMiniMapPanel(false)
      UI_ShowSelectionPanel(false)
      UI_ShowObjectivesPanel(false)
      UI_ShowSquadTabs(false)
      UI_ShowDecorators(false)
      UI_ShowLevelUpKickers(false)
      UI_ShowMenuButtonPanel(false)
      UI_ShowIntelEventsPanel(false)
      UI_ShowInGameChatPanel(false)
      UI_ShowInGameChatPanelMP(false)
      UI_HideSquadControlGroupBindings(true)
      UI_SetSelectionUIVisible(false)
      UI_CoverPreviewHide()
      UI_EnableSquadDecorator(false)
   elseif boolean == true then
      UI_ShowCoverOnNonSelectedUnits(true)
      UI_ShowMiniMapPanel(true)
      UI_ShowSelectionPanel(true)
      UI_ShowObjectivesPanel(true)
      UI_ShowSquadTabs(true)
      UI_ShowDecorators(true)
      UI_ShowLevelUpKickers(true)
      UI_ShowMenuButtonPanel(true)
      UI_ShowIntelEventsPanel(true)
      UI_ShowInGameChatPanel(true)
      UI_ShowInGameChatPanelMP(true)
      UI_HideSquadControlGroupBindings(false)
      UI_SetSelectionUIVisible(true)
      UI_CoverPreviewShow()
      UI_EnableSquadDecorator(true)
   end
end

function ToggleCursor()
   if (cope_ShowCursor == 1) then
      cursor_hide()
      cope_ShowCursor = 0
   elseif (cope_ShowCursor == 0) then
      cursor_show()
      cope_ShowCursor = 1
   end
end

custom_slomo = 0
function toggle_slomo_no_ui()
   if custom_slomo == 0 then
      game_showui(false)
      UI_ShowDecorators(true)
      Game_SetSimRate(1)
      custom_slomo = 1
   else
      game_showui(true)
      Game_SetSimRate(10)
      custom_slomo = 0
   end
end

unbind("Numpad0")
unbind("Numpad1")
unbind("Numpad2")
unbind("Numpad3")
unbind("Numpad4")
unbind("Numpad5")
unbind("Numpad6")
unbind("Numpad7")
unbind("Numpad8")
unbind("Numpad9")

bind("Control+Down", "GameSpeed_Half()")
bind("Control+Up", "GameSpeed_Double()")
bind("Control+Right", "ScaleUIPlus()")
bind("Control+Left", "ScaleUIMinus()")
bind("Control+F3", "UI_ShowGP(true)")
bind("Control+F4", "UI_ShowGP(false)")
bind("Control+F5", "ToggleCursor()")
bind("Control+F6", "Misc_SuperScreenshot()")
bind("Control+F7", "VIS_Wireframe()")
bind("Control+F12", "toggle_slomo_no_ui()")

-- globals
active_army = nil
squad_tbl = {} -- lookup squad id -> {army, group, Squad}

function _wait_lock(f)
   repeat -- noop
   until not lock
   lock = true; f(); lock = false
end

-- creates army groups and places a starting unit in each.
-- Active army group defaults to 1
function _ui_init_groups(squads)
   -- divide first units into separate armies
   local _init_grps = function (gid, idx, sid)
      squad_tbl[sid.id] = {army = idx, grp = 1, squad = sid}
   end
   SGroup_ForEach(squads, _init_grps)

   _wait_lock(function() active_army = -1 end)
   ui_switch_group(1)
end

function get_player_squads()
   if Game_HasLocalPlayer() then
      return Player_GetSquads(Game_GetLocalPlayer())
   end
   error("invalid player")
end

-- saves the active army config, needs to be called continuously in order
-- to keep up with changes like new/lost units, switched ctrl groups etc.
function ui_update_groups()
   local squads = get_player_squads()
   
   if active_army == nil then
      _ui_init_groups(squads)
      return
   end

   local ids = {}
   local st = squad_tbl
   local _update_grps = function (gid, idx, sid)
      local ctrl_grp = Misc_GetSquadControlGroup(sid)

      if st[sid.id] == nil or st[sid.id].army == active_army then
         st[sid.id] = {army = active_army, squad = sid}
         if ctrl_grp ~= 0 then
            st[sid.id].grp = ctrl_grp
         end
         UI_AddSquadTab(sid, ctrl_grp)
      end

      ids[sid.id] = true
   end
   SGroup_ForEach(squads, _update_grps)
   -- remove non-existing squads
   for i,_ in pairs(squad_tbl) do
      if ids[i] == nil then st[i] = nil end
   end
   _wait_lock(function() squad_tbl = st end)
end

-- switch between army groups, a switch will clear the HUD to only
-- display squads in that particular army and will mark them selected.
function ui_switch_group(army_id)
   if active_army == army_id then return end

   for _,v in pairs(squad_tbl) do
      if v.army ~= army_id then
         UI_RemoveSquadTab(v.squad)
         Misc_SetSquadControlGroup(v.squad, 0)
         Misc_SelectSquad(v.squad, false)
      else
         UI_AddSquadTab(v.squad, v.grp)
         Misc_SetSquadControlGroup(v.squad, v.grp)
         Misc_SelectSquad(v.squad, true)
      end
   end
   _wait_lock(function () active_army = army_id end)
end

-- set army group, create an army from selection on chosen group index
function ui_set_group(army_id)
   local squads = get_player_squads()
   local st = squad_tbl
   local _set_grp = function (gid, idx, sid)
      if Misc_IsSquadSelected(sid) then
         st[sid.id].army = army_id
         st[sid.id].grp  = idx
      end
   end
   SGroup_ForEach(squads, _set_grp)
   _wait_lock(function() squad_tbl = st; active_army = -1 end)

   ui_switch_group(army_id)
end

timer_add("ui_update_groups", 0.5)

bind("Numpad0", "ui_switch_group(0)")
bind("Numpad1", "ui_switch_group(1)")
bind("Numpad2", "ui_switch_group(2)")
bind("Numpad3", "ui_switch_group(3)")
bind("Numpad4", "ui_switch_group(4)")
bind("Numpad5", "ui_switch_group(5)")
bind("Numpad6", "ui_switch_group(6)")
bind("Numpad7", "ui_switch_group(7)")
bind("Numpad8", "ui_switch_group(8)")
bind("Numpad9", "ui_switch_group(9)")

bind("Control+Numpad0", "ui_set_group(0)")
bind("Control+Numpad1", "ui_set_group(1)")
bind("Control+Numpad2", "ui_set_group(2)")
bind("Control+Numpad3", "ui_set_group(3)")
bind("Control+Numpad4", "ui_set_group(4)")
bind("Control+Numpad5", "ui_set_group(5)")
bind("Control+Numpad6", "ui_set_group(6)")
bind("Control+Numpad7", "ui_set_group(7)")
bind("Control+Numpad8", "ui_set_group(8)")
bind("Control+Numpad9", "ui_set_group(9)")



My other requests are:
2- When the numpad # is hit multiple times is it possible to move the camera to the squad that is binded to the "1" spot?
3- When I hit "~" it tells me parameter 2 is invalid, expects int but received nil. What does that mean exactly?
4- Is it also possible to automatically bind all that squad to 1-0 only when they are moved to a specific num pad? An example being i have 2 tacs binded to numpad 1 but i want to move then to num pad 2. Atm when i do, the number they are binded to is the when the squad was actually produced/spawned in. So the tacs would be immediately bound to 3 or 5 or no binding.

Sorry if i don't make any sense. I can try and explain better if needed.

Re: Numpad army switching problem

Posted: Mon 28 Nov, 2016 10:25 am
by Black Relic
Having another problem with the file not working when the my is packed into an sga.

Any help would be awesome.