Guest User

Untitled

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