Advertisement
Guest User

Untitled

a guest
Jul 12th, 2014
318
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.52 KB | None | 0 0
  1. --
  2. -- code review version. Thanks go to JK, KingRaptor, Jools
  3. --
  4. function gadget:GetInfo()
  5.     return {
  6.         name        = "feature placer v3",
  7.         desc        = "Spawns Features and Units",
  8.         author      = "Gnome, Smoth",
  9.         date        = "August 2008, updated 7/11/2014",
  10.         license     = "PD",
  11.         layer       = 0,
  12.         enabled     = true  --  loaded by default?
  13.     }
  14. end
  15.  
  16. -- these are here because I like the way they look
  17. local   spSetUnitNeutral        = Spring.SetUnitNeutral
  18. local   spSetUnitBlocking       = Spring.SetUnitBlocking
  19. local   spSetUnitRotation       = Spring.SetUnitRotation
  20. local   spSetUnitLosState       = Spring.SetUnitLosState
  21. local   spSetUnitAlwaysVisible  = Spring.SetUnitAlwaysVisible
  22. local   spCreateUnit            = Spring.CreateUnit
  23. local   spGetGaiaTeamID         = Spring.GetGaiaTeamID
  24. local   spCreateFeature         = Spring.CreateFeature
  25. local   spGetGroundHeight       = Spring.GetGroundHeight
  26. local   spEcho                  = Spring.Echo
  27.  
  28. -- SYNCED only
  29. if not gadgetHandler:IsSyncedCode() then
  30.     return
  31. end
  32.  
  33. local   rotationMultiplier  = math.pi / 32768
  34. local   gaiaTeamID          = spGetGaiaTeamID()
  35. local   featureSetFile      = "config/featureconfig.lua"
  36. local   losState            = {los=true, prevLos=true, contRadar=true, radar=true}
  37. ----------------------------------------------------
  38. -- PARAMS
  39. -- featureList = table of values to storing feature
  40. --      name, location x, location z, rotation
  41. --      Ex: {   name = 'artbushweird1lo',
  42. --              x = 7847,
  43. --              z = 4152,
  44. --              rot = "0" },
  45. ----------------------------------------------------
  46. function ProcessFeatures(featureList)
  47.     spEcho("placing " .. #featureList .. " feature(s)")
  48.     for _,fDef in pairs(featureList) do
  49.         if FeatureDefNames[fDef.name] then
  50.             local yPlacement    = spGetGroundHeight(fDef.x,fDef.z)+5
  51.             spCreateFeature(fDef.name, fDef.x, yPlacement, fDef.z, fDef.rot)
  52.         else
  53.             spEcho("featureplacer warning: invalid feature name", fDef.name)
  54.         end
  55.     end
  56. end
  57.  
  58. ----------------------------------------------------
  59. -- Places units
  60. -- PARAMS
  61. -- unitList = table of values to storing a unit
  62. --      name, location x, location z, rotation
  63. --      Ex: {   name = 'badger',
  64. --              x = 7847,
  65. --              z = 4152,
  66. --              rot = "0" },
  67. ----------------------------------------------------
  68. function ProcessUnits(unitList)
  69.     spEcho("placing " .. #unitList .. " unit(s)")
  70.     for _,uDef in pairs(unitList) do
  71.         if UnitDefNames[uDef.name] then
  72.             local unitId = spCreateUnit(uDef.name, uDef.x, 0, uDef.z, 0, gaiaTeamID)
  73.             if unitId ~= nil then
  74.                 local unitRotation  = -uDef.rot * rotationMultiplier
  75.                 spSetUnitRotation(unitId, 0, unitRotation, 0)
  76.                 spSetUnitNeutral(unitId,true)
  77.                 spSetUnitBlocking(unitId,true)
  78.                 spSetUnitAlwaysVisible(unitId,true)
  79.                 spSetUnitLosState(unitId,0,losState)
  80.                 for _,allyTeam in ipairs(Spring.GetAllyTeamList()) do
  81.                     Spring.SetUnitLosMask(unitId, allyTeam, {prevLos=true,})
  82.                 end
  83.             end
  84.         else
  85.             spEcho("featureplacer warning: invalid unit def name", bDef.name)
  86.         end
  87.     end
  88. end
  89.  
  90. ----------------------------------------------------
  91. -- Places neutral structures
  92. -- PARAMS
  93. -- buildingList = table of values to storing buidking
  94. --      name, location x, location z, rotation
  95. --      Ex: {   name = 'neutralfueltank1',
  96. --              x = 7847,
  97. --              z = 4152,
  98. --              rot = "north" },
  99. ----------------------------------------------------
  100. function ProcessBuildings(buildingList)
  101.     spEcho("placing " .. #buildingList .. " building(s)")
  102.     for _,bDef in pairs(buildingList) do
  103.         if UnitDefNames[bDef.name] then
  104.             local buildingId = spCreateUnit(bDef.name, bDef.x, 0, bDef.z, bDef.rot, gaiaTeamID)
  105.             if buildingId ~= nil then
  106.                 spSetUnitNeutral(buildingId,true)
  107.                 spSetUnitBlocking(buildingId,true)
  108.                 spSetUnitAlwaysVisible(buildingId,true)
  109.                 spSetUnitLosState(buildingId,0,losState)
  110.                 for _,allyTeam in ipairs(Spring.GetAllyTeamList()) do
  111.                     Spring.SetUnitLosMask(buildingId, allyTeam, {prevLos=true,})
  112.                 end
  113.             end
  114.         else
  115.             spEcho("featureplacer warning: invalid building def name", bDef.name)
  116.         end
  117.     end
  118. end
  119.  
  120. ----------------------------------------------------
  121. -- gadgethander:GamePreload
  122. ----------------------------------------------------
  123. function gadget:GamePreload()  
  124.     if not VFS.FileExists(featureSetFile)   then
  125.         spEcho("No feature placer objects loaded")
  126.         return
  127.     end
  128.    
  129.     featurecfg      = VFS.Include(featureSetFile)
  130.     unitList        = featurecfg.unitlist
  131.     featureList     = featurecfg.objectlist
  132.     buildingList    = featurecfg.buildinglist
  133.    
  134.     if ( unitList ) then
  135.         ProcessUnits(unitList)
  136.     end
  137.     if ( featureList )  then
  138.         ProcessFeatures(featureList)
  139.     end
  140.     if ( buildingList ) then
  141.         ProcessBuildings(buildingList)
  142.     end
  143. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement