Guest User

Untitled

a guest
Feb 4th, 2015
364
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.82 KB | None | 0 0
  1. --------------------------------------------------------------------------------
  2. --------------------------------------------------------------------------------
  3. --
  4. --  file:    game_spawn.lua
  5. --  brief:   spawns start unit and sets storage levels
  6. --  author:  Tobi Vollebregt
  7. --
  8. --  Copyright (C) 2010.
  9. --  Licensed under the terms of the GNU GPL, v2 or later.
  10. --
  11. --------------------------------------------------------------------------------
  12. --------------------------------------------------------------------------------
  13.  
  14. function gadget:GetInfo()
  15.     return {
  16.         name      = "Spawn",
  17.         desc      = "spawns start unit and sets storage levels",
  18.         author    = "Tobi Vollebregt",
  19.         date      = "January, 2010",
  20.         license   = "GNU GPL, v2 or later",
  21.         layer     = 0,
  22.         enabled   = true  --  loaded by default?
  23.     }
  24. end
  25.  
  26. --------------------------------------------------------------------------------
  27. --------------------------------------------------------------------------------
  28.  
  29. -- synced only
  30. if (not gadgetHandler:IsSyncedCode()) then
  31.     return false
  32. end
  33.  
  34. --------------------------------------------------------------------------------
  35. --------------------------------------------------------------------------------
  36.  
  37. local modOptions = Spring.GetModOptions()
  38.  
  39.  
  40. local function GetStartUnit(teamID)
  41.     -- get the team startup info
  42.     local side = select(5, Spring.GetTeamInfo(teamID))
  43.     local startUnit
  44.     if (side == "") then
  45.         -- startscript didn't specify a side for this team
  46.         local sidedata = Spring.GetSideData()
  47.         if (sidedata and #sidedata > 0) then
  48.             startUnit = sidedata[1 + teamID % #sidedata].startUnit
  49.         end
  50.     else
  51.         startUnit = Spring.GetSideData(side)
  52.     end
  53.     return startUnit
  54. end
  55.  
  56. local function SpawnStartUnit(teamID)
  57.     local startUnit = GetStartUnit(teamID)
  58.     if (startUnit and startUnit ~= "") then
  59.         -- spawn the specified start unit
  60.         local x,y,z = Spring.GetTeamStartPosition(teamID)
  61.         -- snap to 16x16 grid
  62.         x, z = 16*math.floor((x+8)/16), 16*math.floor((z+8)/16)
  63.         y = Spring.GetGroundHeight(x, z)
  64.         -- facing toward map center
  65.         local facing=math.abs(Game.mapSizeX/2 - x) > math.abs(Game.mapSizeZ/2 - z)
  66.             and ((x>Game.mapSizeX/2) and "west" or "east")
  67.             or ((z>Game.mapSizeZ/2) and "north" or "south")
  68.         local unitID = Spring.CreateUnit(startUnit, x, y, z, facing, teamID)
  69.     end
  70.  
  71.     -- set start resources, either from mod options or custom team keys
  72.     local teamOptions = select(7, Spring.GetTeamInfo(teamID))
  73.     local m = teamOptions.startmetal  or modOptions.startmetal  or 100
  74.     local e = teamOptions.startenergy or modOptions.startenergy or 250
  75.  
  76.     -- using SetTeamResource to get rid of any existing resource without affecting stats
  77.     -- using AddTeamResource to add starting resource and counting it as income
  78.     if (m and tonumber(m) ~= 0) then
  79.         -- remove the pre-existing storage
  80.         --   must be done after the start unit is spawned,
  81.         --   otherwise the starting resources are lost!
  82.         Spring.SetTeamResource(teamID, "ms", tonumber(m))
  83.         Spring.SetTeamResource(teamID, "m", 0)
  84.         Spring.AddTeamResource(teamID, "m", tonumber(m))
  85.     end
  86.     if (e and tonumber(e) ~= 0) then
  87.         -- remove the pre-existing storage
  88.         --   must be done after the start unit is spawned,
  89.         --   otherwise the starting resources are lost!
  90.         Spring.SetTeamResource(teamID, "es", tonumber(e))
  91.         Spring.SetTeamResource(teamID, "e", 0)
  92.         Spring.AddTeamResource(teamID, "e", tonumber(e))
  93.     end
  94. end
  95.  
  96.  
  97. function gadget:GameStart()
  98.     -- only activate if engine didn't already spawn units (compatibility)
  99.     if (#Spring.GetAllUnits() > 0) then
  100.         return
  101.     end
  102.  
  103.     -- spawn start units
  104.     local gaiaTeamID = Spring.GetGaiaTeamID()
  105.     local teams = Spring.GetTeamList()
  106.     for i = 1,#teams do
  107.         local teamID = teams[i]
  108.         -- don't spawn a start unit for the Gaia team
  109.         if (teamID ~= gaiaTeamID) then
  110.             SpawnStartUnit(teamID)
  111.         end
  112.     end
  113. end
Advertisement
Add Comment
Please, Sign In to add comment