Advertisement
Guest User

Untitled

a guest
Feb 4th, 2013
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 25.53 KB | None | 0 0
  1. --problem: tptetra unit remains and blocks shots / small units can hide in its colvol. and blob shadows draws shadow
  2. --fixed by making colvol of tptetra very small. but now it is harder to click on them with miners
  3.  
  4. --speichern welcher miner gerade welches mineral mined
  5. --wenn dieses dann destroyed ist, muss er ein neues suchen
  6. --thanks  Andrej,  zwzsg, quantum and #ca and google_frog
  7. function gadget:GetInfo()
  8.   return {
  9.     name      = "mining blabla2",
  10.     desc      = "some broken stuff",
  11.     author    = "knorke, mineral los handling by google frog",
  12.     date      = "4 Okt 2010",
  13.     license   = "oh",
  14.     layer     = 0,
  15.     enabled   = true  --  loaded by default
  16.   }
  17. end
  18.  
  19. if (gadgetHandler:IsSyncedCode()) then
  20. include("LuaRules/Configs/tp_CMDID.lua")
  21. local currentframe = 0  --what frame the game is currently in (orly)
  22. local miners = {}       -- [unitID] alive: true/false   cargo: metal being carried  last_mined_id: which resourceunit was last mined from status: what miner is doing: "search" "loading" "unloading" "mining" "to_res" "to_hq" <-only "search" is really used to controll things, the others are just for displaying
  23.                         --.manualTimer: is increased when player manually controlls the miner. miner will not automine if timer>0
  24.                         --.lastMineActionTimer: increases in gameframe, gets reset when the miner attacks a mineral. used to stop the mining animation
  25. local dropoffs = {}     -- [unitID]
  26.  
  27. local resources = {}
  28. local resourcesCount = 0
  29. local resourcesUnit = {}
  30.  
  31. local debug = false
  32. function DoNotWantEchoSpam ()
  33. end
  34. Spring.Echo = DoNotWantEchoSpam
  35. -----config-----
  36. local fake_unit = "fakeunit"
  37. local fake_block_unit = "fakeblockunit"
  38. local miner_name = {"tpgripper"};           --the unit used for mining
  39. local resource_name = {"tptetra"}   --{"bminerals","bmeteorimpact","bmeteorimpact_big"}     --the stuff that gets mined
  40. local dropoff_name = {"tphq_small", "tphq",}    --where the miners bring the resources to
  41. local dropoff_distance = 200        --how near do miners have to get to a dropoff to drop their cargo? (this value is added to unitRadius)
  42. local maxcargo = 500--600--500          --how much a miner can carry before having to return to a drop off
  43. local resreturneffect = "tpsmokecloud"  --ceg effect played at miners location when a miner returns its cargo
  44. local animationStopTime = 10            --time without mineral-attack until the unitscript is asked to stop the mining animation. compared vs lastMineActionTimer
  45. ----------------
  46.  
  47. local fake_unit_DefID = UnitDefNames[fake_unit].id
  48. local fake_block_unit_DefID = UnitDefNames[fake_block_unit].id
  49.  
  50. ----------------"go mining" command
  51. local goMiningCmd = {
  52.       id      = CMD_GO_MINING,
  53.       --name    = " Mine\nMinerals!",   --text on the button
  54.       action  = "goMining",
  55.       type    = CMDTYPE.ICON_MODE, -- CMDTYPE.NUMBER
  56.       --texture = "&.9x.9&unitpics/sigterm.png&bitmaps/frame.tga",
  57.       texture = "unitpics/mining.png&", --& = larger icon due to no frame
  58.       tooltip = "Mine resources!   Hotkey: m",
  59.       params  = { 'do it!'}
  60. }
  61.  
  62.  
  63. -----RESOURCE HANDLER-----
  64.  
  65. local spSetUnitLosState     = Spring.SetUnitLosState
  66. local spSetUnitLosMask      = Spring.SetUnitLosMask
  67.  
  68. local function SpawnResource (sx,sz, unitname)
  69.     if (sx == nil or sz == nil) then
  70.         Spring.Echo ("tp_resourcespawner: sx or sz was NIL in SpawnResource (sx,sz)")
  71.         return
  72.     end
  73.     unitname = unitname or resource_name[1]
  74.     local sh = Spring.GetGroundHeight(sx, sz)  
  75.     ---oil pumps---
  76.     if (unitname == "tpoilpump") then
  77.         local unitID = Spring.CreateUnit(unitname, sx, sh, sz, rotation, Spring.GetGaiaTeamID())
  78.         Spring.SetUnitAlwaysVisible(unitID, true)
  79.         return
  80.     end
  81.     ---------------
  82.     Spring.Echo ("gaia=" .. Spring.GetGaiaTeamID() )
  83.     Spring.Echo ("fake_unit=" .. fake_unit)
  84.     Spring.Echo ("tp_resourcespawner: spawning resource at " .. sx ..":"..sz .. " in height " .. sh)
  85.     resourcesCount = resourcesCount + 1
  86.     local losCheck = Spring.CreateUnit(fake_unit, sx, sh, sz, 0, Spring.GetGaiaTeamID())   
  87.     Spring.Echo ("returned unitID:   losCheck=" .. (losCheck or "nil") )
  88.     local block = Spring.CreateUnit(fake_block_unit, sx, sh, sz, 0, Spring.GetGaiaTeamID())    
  89.     Spring.SetUnitCollisionVolumeData(losCheck, 1,1,1,0,-10,0,0,0,0)
  90.     Spring.SetUnitCollisionVolumeData(block, 1,1,1,0,-10,0,0,0,0)
  91.  
  92.     resources[resourcesCount] = {unit = {}, health = UnitDefNames[unitname].health, losCheck = losCheck, block = block}
  93.     resourcesUnit[losCheck] = resourcesCount
  94.    
  95.     local rotation = math.random(0,math.pi)
  96.    
  97.     for _,allyTeam in ipairs(Spring.GetAllyTeamList()) do
  98.         local unitID = Spring.CreateUnit(unitname, sx, sh, sz, rotation, Spring.GetGaiaTeamID())
  99.         Spring.SetUnitCollisionVolumeData(unitID, 10,10,10  ,0,0,0, 0,1,0)   --test
  100.         Spring.SetUnitBlocking(unitID, false, false)
  101.         resources[resourcesCount].unit[allyTeam] = unitID
  102.         resourcesUnit[unitID] = resourcesCount
  103.         Spring.GiveOrderToUnit(unitID, CMD.ONOFF, { 0 }, {} )
  104.         Spring.SetUnitAlwaysVisible(unitID, true)
  105.         for _,otherAllyTeam in ipairs(Spring.GetAllyTeamList()) do
  106.             if otherAllyTeam ~= allyTeam then
  107.                 spSetUnitLosMask(unitID, otherAllyTeam, {los=true,radar = true, prevLos = true, contRadar = true} )
  108.             end
  109.         end
  110.     end
  111. end
  112.  
  113. GG.SpawnResource = SpawnResource
  114.  
  115. function gadget:UnitEnteredLos(unitID, unitTeam, allyTeam, unitDefID)
  116.     if unitDefID == fake_unit_DefID then
  117.         local res = resources[resourcesUnit[unitID]]
  118.         local resID = res.unit[allyTeam]
  119.         if Spring.ValidUnitID(resID) and select(1, Spring.GetUnitHealth(resID)) ~= res.health then
  120.             if res.health then
  121.                 --Spring.AddUnitDamage(resID, select(1, Spring.GetUnitHealth(resID)) - res.health)             
  122.                 Spring.AddUnitDamage(unitID, select(1, Spring.GetUnitHealth(unitID)) - res.health,0, unitID, -2)
  123.                 --Spring.SetUnitHealth(resID, res.health)
  124.             else
  125.                 Spring.DestroyUnit(resID)
  126.             end
  127.         end
  128.     end
  129. end
  130.  
  131. local function damageResource(resourceID, damage)
  132.     local res = resources[resourceID]
  133.     if not res then --to prevent "res.health is nil" error in line below
  134.         --Spring.Echo ("res was nil in damageResource")
  135.         return
  136.     end
  137.     if res.health then
  138.         res.health = res.health - damage
  139.         if res.health <= 0 then
  140.             Spring.DestroyUnit(res.block)
  141.             res.health = nil
  142.         end
  143.     end
  144.     for _,allyTeam in ipairs(Spring.GetAllyTeamList()) do
  145.         local unitID = res.unit[allyTeam]
  146.         if Spring.ValidUnitID(unitID) then
  147.             local los = Spring.GetUnitLosState(res.losCheck, allyTeam)
  148.             if los and los.los then
  149.                 if res.health then
  150.                     --Spring.AddUnitDamage(unitID, select(1, Spring.GetUnitHealth(unitID)) - res.health)
  151.                     Spring.AddUnitDamage(unitID, select(1, Spring.GetUnitHealth(unitID)) - res.health,0, unitID, -2)
  152.                    
  153.                     --Spring.SetUnitHealth(unitID, res.health)
  154.                 else
  155.                     Spring.DestroyUnit(unitID)
  156.                 end
  157.             end
  158.         end
  159.     end
  160. end
  161.  
  162. ----------------
  163.  
  164.  
  165. function gadget:UnitFinished(unitID, unitDefID, teamID)
  166.     if (is_miner_type (unitDefID) == true) then
  167.         Spring.GiveOrderToUnit(unitID, CMD.FIRE_STATE , { 0 }, {}) --hold fire
  168.         add_miner (unitID)
  169.         --search_res (unitID)
  170.     end
  171.     if (is_dropoff_type (unitDefID) == true) then add_dropoff (unitID) end
  172. end
  173.  
  174. function gadget:UnitDestroyed(unitID, unitDefID, teamID, attackerID, attackerDefID, attackerTeamID)
  175.     --remove destroyed miners:
  176.     if is_miner (unitID) then remove_miner (unitID) end
  177.     --remove destroyed dropoffs:
  178.     if is_dropoff (unitID) then remove_dropoff (unitID) end
  179.     --wenn etwas geerntet wurde:
  180.     --if (is_resource_type (unitDefID) and is_miner (attackerID)) then Spring.Echo ("resource mined out") end
  181.     --if (is_miner(attackerID)) then Spring.Echo ("miner mined") end
  182.     if resourcesUnit[unitID] then
  183.         if unitDefID ~= fake_block_unit and unitDefID ~= fake_unit then
  184.             resources[resourcesUnit[unitID]].unit[Spring.GetUnitAllyTeam(unitID)] = nil
  185.         end
  186.         resourcesUnit[unitID] = nil
  187.     end
  188.     --[[
  189.     if (is_resource_type (unitDefID) and is_miner(attackerID)) then
  190.         --send full miners to dropoff:
  191.         --if (miners[attackerID].cargo > 25) then
  192.             --return_to_dropoff (attackerID)
  193.             miners[attackerID].cargo = maxcargo
  194.             miners[attackerID].last_mined_id = nil
  195.             --miners[attackerID].status = "search"
  196.             env = Spring.UnitScript.GetScriptEnv(attackerID)
  197.             if (env) then Spring.UnitScript.CallAsUnit(attackerID, env.miningEnd) end
  198.             return_to_dropoff (attackerID)
  199.             --return_to_mine (attackerID)
  200.             --search_res (attackerID)
  201.         --end
  202.         end --]]   
  203.     --end
  204. end
  205.  
  206.  
  207. function gadget:UnitPreDamaged (unitID, unitDefID, unitTeam, damage, paralyzer, weaponID, attackerID, attackerDefID, attackerTeam)  
  208.     --try to fix: minerals get damaged by units but look unharmed until they get mined: then they suddendly lose all pieces
  209.     --das passiert aber weil script.hitbyweapon for unitpredamaged aufgerufen wird (?)
  210.     Spring.Echo (unitID.."attacked by " .. (attackerID or "nil"))
  211.     Spring.Echo ("attacked unit is a "  .. UnitDefs[Spring.GetUnitDefID(unitID)].name .. " of team " .. unitTeam)
  212.    
  213.    
  214.     if (unitDefID == fake_unit_DefID or unitDefID == fake_block_unit_DefID) and (attackerID ~=unitID) then return 0,0 end
  215.     --if (is_resource_type (unitDefID) and not is_miner(attackerID)) and weaponID~=-100 then return 0,0 end --other units can not damage minerals
  216.     --test
  217.    
  218.     if (is_resource_type (unitDefID) and is_miner(attackerID)) then
  219.         miners[attackerID].lastMineActionTimer = 0
  220.         --fill the cargo bay of the miner, alot of options here....:
  221.         if (miners[attackerID].cargo < maxcargo) then
  222.             miners[attackerID].cargo = miners[attackerID].cargo + 10--damage--math.ceil(damage)
  223.             env = Spring.UnitScript.GetScriptEnv(attackerID)       
  224.             if (env) then Spring.UnitScript.CallAsUnit(attackerID, env.miningWork, miners[attackerID].cargo,math.ceil(damage)) end         
  225.         end
  226.         if (unitID ~=nil and attackerID~=nil) then --added attackerID~=nil check after getting "attempt to index field '?' (a nil value)"
  227.            
  228.             --originally there was only this line by googlefrog:
  229.                 --miners[attackerID].last_mined_id = resources[resourcesUnit[unitID]].unit[Spring.GetUnitAllyTeam(attackerID)]
  230.            
  231.             if (Spring.ValidUnitID (unitID)) then
  232.                 -- Set last mined to the unit that that team can see
  233.                 local u = resources[resourcesUnit[unitID]]
  234.                 if (u) then
  235.                     miners[attackerID].last_mined_id = u.unit[Spring.GetUnitAllyTeam(attackerID)]
  236.                 else
  237.                     Spring.Echo ("u was nil - last_mined_id not set")
  238.                     Spring.Echo ("resourcesUnit[unitID] :" .. (resourcesUnit[unitID] or "nil!") .. " | resourcesCount:" .. resourcesCount)
  239.                 end
  240.             end
  241.            
  242.             --resources[unitID] = {} --*** -> add_resource und so...
  243.             --resources[unitID].frame_last_mined = currentframe
  244.         end
  245.        
  246.         --send full miners to dropoff:
  247.         if (miners[attackerID].cargo >= maxcargo) then
  248.             return_to_dropoff (attackerID)
  249.             env = Spring.UnitScript.GetScriptEnv(attackerID)
  250.             if (env) then Spring.UnitScript.CallAsUnit(attackerID, env.miningEnd) end
  251.         end
  252.         --no self damaging miners
  253.         if (unitID == attackerID) then
  254.             return 0,0
  255.         end
  256.         damageResource(resourcesUnit[unitID], 10)
  257.         --tell the mineral it was indeed mined so it can lose pieces:
  258.         --env = Spring.UnitScript.GetScriptEnv(unitID)
  259.         --if (env) then Spring.UnitScript.CallAsUnit(unitID, env.notifyDamage, 10) end
  260.         return 0
  261.     end
  262.     if unitDefID == fake_unit_DefID then
  263.         return 0
  264.     end
  265.     return (damage or 0)    --added due to "..should return a number"
  266. end
  267.  
  268.  
  269. function gadget:GameFrame(frameNum)
  270.     _G.miners = miners
  271.     _G.dropoffs = dropoffs     
  272.     currentframe=frameNum
  273.     if (frameNum % 16 ~=0) then return end
  274.     for i in pairs(miners) do
  275.         --send cargo values to the unit scripts
  276.         env = Spring.UnitScript.GetScriptEnv(i)    
  277.         if (env) then Spring.UnitScript.CallAsUnit(i, env.miningCargo, (miners[i].cargo or -1)) end
  278.         if (miners[i].lastMineActionTimer < 9001) then miners[i].lastMineActionTimer = miners[i].lastMineActionTimer+1 end  --if ist nur wegen evtl overflow in seeehr langen games
  279.         --Spring.Echo ("lastmine timer:" .. miners[i].lastMineActionTimer )
  280.         if (miners[i].lastMineActionTimer > animationStopTime) then
  281.             env = Spring.UnitScript.GetScriptEnv(i)
  282.             if (env) then Spring.UnitScript.CallAsUnit(i, env.miningEnd) end
  283.         end
  284.         ----*****with this, miners REALLY want to mine
  285.         --if (frameNum % 64 == 0 and miners[i].status == "search") then
  286.             --search_res (i)
  287.         --end
  288.        
  289.         if (miners[i].manualTimer < 0) then
  290.             --if (frameNum % 64 == 0 and miners[i].status == "search") then
  291.                 --search_res (i)
  292.             --end
  293.        
  294.             if (frameNum % 64 == 0 and miners[i].status == "to_hq") then
  295.                 --return_to_dropoff (i)
  296.             end        
  297.         else
  298.             miners[i].manualTimer=miners[i].manualTimer-1
  299.             --Spring.Echo ("timer:" ..miners[i].manualTimer)
  300.         end
  301.        
  302.        
  303.        
  304.         if (miners[i].cargo > 5 and is_miner_at_dropoff (i)) then   --drop the cargo
  305.             local minerteam = Spring.GetUnitTeam (i)
  306.             Spring.AddTeamResource (minerteam, "metal", miners[i].cargo)           
  307.             local x,y,z=Spring.GetUnitPosition (i)
  308.             if (x and y and z) then
  309.                 for i = 1, miners[i].cargo/50 , 1 do
  310.                     Spring.SpawnCEG(resreturneffect, x, y, z)
  311.                 end
  312.             end
  313.             env = Spring.UnitScript.GetScriptEnv(i)    
  314.             if (env) then Spring.UnitScript.CallAsUnit(i, env.miningUnload, miners[i].cargo) end
  315.             miners[i].cargo = 0
  316.             return_to_mine (i)
  317.         end
  318.     end
  319. end
  320.  
  321. function gadget:Initialize()
  322.     make_miner_table()
  323.     make_resource_name_table ()
  324.     _G.miners = miners;
  325.     _G.dropoffs = dropoffs
  326.     _G.dropoff_distance = dropoff_distance
  327. end
  328.  
  329. function marker_on_unit (_uID, _text)
  330.     if (not debug) then return end
  331.     if (markers == false) then return end
  332.     if (_uID == nil) then return end
  333.     if (_text == nil) then return end
  334.     local x,y,z=Spring.GetUnitPosition (_uID)
  335.     if (x == nil or y == nil or z == nil) then return end
  336.     Spring.MarkerAddPoint (x,y,z, _text .. "id:" .. _uID)
  337. end
  338.  
  339. function make_miner_table()
  340.     miners = {}
  341.     local all_units = Spring.GetAllUnits ()
  342.     for i in pairs(all_units) do
  343.         local unitDefID = Spring.GetUnitDefID(all_units[i])
  344.         if (is_miner_type (unitDefID)==true) then
  345.             add_miner (all_units[i])
  346.         end
  347.         if (is_dropoff_type (unitDefID)==true) then
  348.             add_dropoff (all_units[i])
  349.         end
  350.     end
  351. end
  352.  
  353. function make_resource_name_table ()
  354. if (debug) then Spring.Echo ("tp_mining.lua: looking for mineable unitdefs") end
  355.     for id,unitDef in pairs(UnitDefs) do
  356.         local cp = UnitDefs[id].customParams
  357.         if (cp) then
  358.             if (cp.is_mineable) then
  359.                 local resname = unitDef.name
  360.                 if (debug) then Spring.Echo ("tp_mining.lua: found mineable resource:" .. resname) end
  361.                 table.insert (resource_name, resname)
  362.             end
  363.         end
  364.     end
  365. end
  366.  
  367. function is_miner_type (unitDefID)
  368.     if (unitDefID == nil) then return false end
  369.     local unitDef = UnitDefs[unitDefID]
  370.     if (unitDef == nil) then return false end
  371.     local unitname = unitDef.name  
  372.     for _,v in pairs(miner_name) do
  373.         if v == unitname then return true end
  374.     end
  375.     return false
  376. end
  377.  
  378.  
  379. function is_dropoff_type (unitDefID)
  380.     if (unitDefID == nil) then return false end
  381.     local unitDef = UnitDefs[unitDefID]
  382.     if (unitDef == nil) then return false end
  383.     local unitname = unitDef.name
  384.     for _,v in pairs(dropoff_name) do
  385.         if v == unitname then return true end
  386.     end    
  387.     return false
  388. end
  389.  
  390.  
  391. function is_resource_type (unitDefID)
  392.     if (unitDefID == nil) then return false end
  393.     local unitDef = UnitDefs[unitDefID]
  394.     if (unitDef == nil) then return false end
  395.     for schluessel, wert in pairs(resource_name) do                            
  396.         if (wert == unitDef.name) then return true end
  397.     end
  398.     --if (unitDef.name == resource_name) then return true end
  399.     return false
  400. end
  401.  
  402.  
  403. function is_miner (unitID)
  404.     if (miners [unitID] ~= nil) then return true else return false end
  405. end
  406.  
  407. function is_dropoff (unitID)   
  408.     if (dropoffs [unitID] ~= nil) then return true else return false end
  409. end
  410.  
  411.  
  412. function nearest_dropoff_position_from_miner (minerID)
  413.     local minerteam = Spring.GetUnitTeam (minerID)
  414.     local nearest_distance = 9999999999
  415.     local nearest_dropoffID = nil
  416.     for i in pairs (dropoffs) do
  417.         local dropoffteam = Spring.GetUnitTeam (i) 
  418.         if (minerteam == dropoffteam) then
  419.             local d = Spring.GetUnitSeparation (minerID, i)
  420.             if (d < nearest_distance) then
  421.                 nearest_distance = d
  422.                 nearest_dropoffID = i
  423.             end
  424.         end
  425.     end
  426.     if (nearest_dropoffID ~= nil) then
  427.         return Spring.GetUnitPosition (nearest_dropoffID)
  428.     else
  429.         return nil
  430.     end
  431. end
  432.  
  433.  
  434.  --( number x, number z, number radius [,number teamID] )
  435. --*** nicht wirklich das nächste mineral sondern erstmal nur das nächstbeste  
  436. function nearest_resID_from_miner (minerID)
  437.     local nearest_resID = nil
  438.     local nearest_res_distance = 9999999999
  439.     local nearest_unmined_res = nil
  440.     local nearest_unmined_res_distance = 9999999999
  441.     local x,y,z = Spring.GetUnitPosition(minerID)
  442.     local res=CallAsTeam(Spring.GetUnitTeam(minerID), function () return Spring.GetUnitsInCylinder (x,z, 5000, Spring.GetGaiaTeamID()) end)
  443.     if (res == nil) then return nil end --no near units at all :/
  444.     for i in pairs (res) do
  445.         if (is_resource_type (Spring.GetUnitDefID(res[i])) == true) then
  446.             if (not Spring.GetUnitIsDead (res[i])) then     --when miners are asked to search a new mineral in UnitDestroyed, the mined out mineral is still "alive" for 1 frame. So they try to mine that again but it actually does not exist anymore -> miner sits around
  447.                 local d = Spring.GetUnitSeparation (minerID, res[i])
  448.                 if (d < nearest_res_distance) then
  449.                     nearest_res_distance = d
  450.                     nearest_resID = res[i]
  451.                 end
  452.             end
  453.         end      
  454.     end
  455.     if (nearest_resID~=nil) then return nearest_resID else return nil end
  456. end
  457.  
  458. function add_miner (unitID)
  459.     miners [unitID] = {}
  460.     miners [unitID].alive = true
  461.     miners [unitID].cargo = 0
  462.     miners[unitID].manualTimer = 0
  463.     miners[unitID].lastMineActionTimer = 0
  464.     Spring.InsertUnitCmdDesc(unitID, goMiningCmd)
  465.     marker_on_unit (unitID, "miner added")
  466. end
  467.  
  468. function add_dropoff (unitID)
  469.     dropoffs [unitID] = {}
  470.     dropoffs [unitID].alive = true
  471.     dropoffs [unitID].cargo = 0
  472.     marker_on_unit (unitID, "droppoff added")
  473. end
  474.  
  475. function remove_miner (unitID)
  476.     if (debug) then Spring.Echo ("removing miner id=" .. unitID) end
  477. --  if (Spring.ValidUnitID  (miners[unitID].last_mined_id)) then
  478. --      if (resources [miners[unitID].last_mined_id] ~= nil) then resources [miners[unitID].last_mined_id].mined_by=nil end
  479. --  end
  480.     miners [unitID].alive=false
  481.     miners [unitID] = nil  
  482. end
  483.  
  484. function remove_dropoff (unitID)
  485.     if (debug) then Spring.Echo ("removing dropoff id=" .. unitID) end
  486.     dropoffs [unitID].alive=false
  487.     dropoffs [unitID] = nil
  488. end
  489.  
  490. function is_miner_at_dropoff (miner_unitID)
  491. local minerteam = Spring.GetUnitTeam (miner_unitID)
  492.     for i in pairs (dropoffs) do
  493.         local dropoffteam = Spring.GetUnitTeam (i)
  494.         if (minerteam == dropoffteam) then
  495.             local d = Spring.GetUnitRadius (i) + dropoff_distance
  496.             if Spring.GetUnitSeparation (miner_unitID, i) < d then return true end
  497.             end
  498.         end
  499.     return false
  500. end
  501.  
  502. -----MINER AI-----
  503. --idle miners will go search for minerals if set not set to "hold pos"
  504. --if you want miners to stay in place, you have to use "wait" command
  505. --probally still most noob friendly this way...
  506. function gadget:UnitIdle(unitID, unitDefID, teamID)
  507.     if (is_miner (unitID)) then
  508.         local unitstates = Spring.GetUnitStates (unitID)
  509.         local movestate = unitstates["movestate"]
  510.         if (debug) then Spring.Echo ("miner " .. unitID .. " movestate=" .. movestate) end
  511.         --if (movestate ==0) then return end
  512.         if (debug) then Spring.Echo ("idle miner" .. unitID) end
  513.  
  514.         --if (movestate ~=0) then --roam or manoever
  515.            
  516.             if (miners[unitID].cargo < maxcargo) then
  517.                 search_res(unitID)
  518.             else
  519.                 return_to_dropoff (unitID)
  520.             end
  521.         --end
  522.     end
  523. end
  524.  
  525.  
  526. function return_to_mine (unitID)
  527.     if (Spring.ValidUnitID  (miners[unitID].last_mined_id)) then
  528.         if (debug) then Spring.Echo ("miner " .. i .. " returns to mineral") end
  529.         --Spring.SetUnitTarget (unitID, miners[i].last_mined_id) --return to the mineral last mined from
  530.         --Spring.GiveOrderToUnit(unitID, CMD.MOVE_STATE, { 2 }, {})
  531.         --Spring.GiveOrderToUnit(unitID, CMD.FIRE_STATE , { 2 }, {})
  532.         Spring.GiveOrderToUnit(unitID, CMD.ATTACK  , { miners[unitID].last_mined_id  }, {CMD.OPT_INTERNAL})
  533.         miners[unitID].status = "to_res"
  534.     else
  535.         --search for new minerals
  536.         search_res (unitID)
  537.     end
  538. end
  539.  
  540. function search_res (unitID)
  541.     local x, y, z = Spring.GetUnitPosition(unitID)
  542.     --Spring.GiveOrderToUnit(unitID, CMD.FIRE_STATE , { 2 }, {})
  543.     --Spring.GiveOrderToUnit(unitID, CMD.AREA_ATTACK  , { x, y, z,50000  }, {})
  544.     --miners[unitID].last_mined_id = nil
  545.     --miners[unitID].status = "send to search"
  546.     local res = nearest_resID_from_miner (unitID)
  547.     if (res) then
  548.         Spring.GiveOrderToUnit(unitID, CMD.ATTACK  , { res }, {CMD.OPT_INTERNAL})
  549.     end
  550.     miners[unitID].status = "search finished"
  551. end
  552.  
  553. function return_to_dropoff (unitID)
  554.     local x, y, z = Spring.GetUnitPosition(unitID)
  555.     local tx, ty, tz = nearest_dropoff_position_from_miner (unitID)
  556.     if (tx ~= nil) then
  557. --      Spring.GiveOrderToUnit(unitID, CMD.FIRE_STATE , { 0 }, {})
  558. --      Spring.GiveOrderToUnit(unitID, CMD.MOVE_STATE, { 0 }, {})
  559.         Spring.GiveOrderToUnit(unitID, CMD.MOVE , {tx, ty, tz  }, {CMD.OPT_INTERNAL})
  560.         miners[unitID].status = "to_hq"
  561.         if (debug) then Spring.Echo ("returning to base with cargo:" .. miners[unitID].cargo) end
  562.     end
  563. end
  564.  
  565. --currently only for AI because of recursion bla when it uses GiveOrder (... CMD_GO_MINING)
  566. function GG.cmd_go_mining (unitID)
  567.     if (miners[unitID]) then
  568.         if (miners[unitID].cargo > 450) then
  569.             return_to_dropoff (unitID)
  570.             return false --cmd taken care of
  571.         else
  572.             --return_to_mine (unitID)
  573.             search_res (unitID)
  574.             return false --cmd taken care of
  575.         end
  576.     end
  577. end
  578.  
  579. function gadget:AllowCommand(unitID, unitDefID, teamID, cmdID, cmdParams, cmdOptions,cmdTag, synced)
  580.     if (miners[unitID]) then
  581.     --------------"resume/go back to mining" button:
  582.         if (cmdID == CMD_GO_MINING) then
  583.             if (miners[unitID].cargo > 450) then
  584.                 return_to_dropoff (unitID)
  585.                 return false --cmd taken care of
  586.             else
  587.                 --return_to_mine (unitID)
  588.                 search_res (unitID)
  589.                 return false --cmd taken care of
  590.             end
  591.         end
  592.         --for k,v in pairs (cmdOptions) do
  593.             --Spring.Echo (k, v)
  594.         --end
  595.     --------------various test crap:
  596.         --[[
  597.         local cmdq = Spring.GetCommandQueue(unitID)
  598.         local manualCommand = false
  599.         if (#cmdq>0) then
  600.             --Spring.Echo ("internal:" , cmdq[1].options.internal)
  601.             manualCommand = not cmdq[1].options.internal or false
  602.         end
  603.         --Spring.Echo ("cmdID:"..cmdID,CMD[cmdID], " | ",  "synced:",synced)
  604.         if (cmdID == CMD.MOVE) then Spring.Echo ("move!") end
  605.         --Spring.Echo (synced)
  606.         --for k,v in pairs (cmdOptions) do
  607.             --Spring.Echo (k, v)
  608.         --end
  609.         --Spring.Echo ("CMD.OPT_INTERNAL:" .. cmdOptions.internal)
  610.         --Spring.Echo ("CMD.OPT_SHIFT:" .. cmdOptions["internal"]) --CMD.OPT_SHIFT
  611.         --Spring.Echo ("command to miner " .. unitID .."->" .. CMD.OPT_INTERNAL)
  612.         if (manualCommand) then        
  613.             --if (cmdID == CMD.MOVE) then
  614.                 miners[unitID].manualTimer = 30
  615.                 Spring.Echo ("manual miner micro!")
  616.             --end
  617.         end
  618.         --]]
  619.     end
  620.     return true
  621. end
  622.  
  623. --------------------------------------------------------
  624. --Border between SYNC and UNSYNC
  625. --when i wear my frilly dress im a real touhou
  626. end
  627.  
  628. if (not gadgetHandler:IsSyncedCode()) then
  629. --------------------------------------------------------
  630.  
  631. --Length of table, for synced (using spairs)
  632. function sxlen(tbl)
  633.     local qq = 0;
  634.     for v in spairs(tbl) do
  635.         qq = qq + 1;
  636.     end
  637.     return qq;
  638. end
  639.  
  640.  
  641. --[[
  642. function gadget:DrawScreen()
  643.  
  644.  
  645.     gl.Color(0.5, 0.5, 0.5, 1)
  646.     local ty = 0
  647.     for i in spairs(SYNCED.miners) do
  648.         ty = ty + 1
  649.         gl.Text (ty .."| [" .. i .. "]".. "cargo:" .. SYNCED.miners[i].cargo, 100,300-ty*25 , 25, 'o')
  650.     end
  651.     gl.Text ("miners counted="..ty .. "#miners:" .. sxlen(SYNCED.miners), 100,300 , 25, 'o')
  652. end
  653. --]]
  654.  
  655. --[[
  656. function gadget:DrawWorldPreUnit() 
  657.     local localplayerally = Spring.GetMyAllyTeamID ()  
  658.     gl.LineWidth(5.0)
  659.     for i in spairs(SYNCED.dropoffs) do
  660.         local unitally = Spring.GetUnitAllyTeam  (i)
  661.         if (unitally == localplayerally) then ----only mark our own dropoffs
  662.             local x,y,z=Spring.GetUnitPosition (i)                          --das nicht jeden drawframe machen sondern zwischenspeichern
  663.             if (x ~= nil and y ~= nil and z ~= nil) then               
  664.                 local d = Spring.GetUnitRadius (i) + SYNCED.dropoff_distance
  665.                 gl.Color(1, 1, 1, 1)
  666.                 gl.DrawGroundCircle (x,y,z, d, 8) --all dropoffs marked with octagon
  667.             end
  668.         end
  669.     end
  670.    
  671.     for i in spairs(SYNCED.miners) do
  672.             if (SYNCED.miners[i].alive == true and i ~= nil and SYNCED.miners[i] ~=nil) then
  673.                 local unitally = Spring.GetUnitAllyTeam  (i)
  674.                 if (unitally == localplayerally) then --only mark our own miners
  675.                     local x,y,z=Spring.GetUnitPosition (i)
  676.                     if (x ~= nil and y ~= nil and z ~= nil) then
  677.                         gl.Color(0, 1, 0, 1)
  678.                         --gl.DrawGroundCircle (x,y,z, 50, 4) --all miners marked with a diamond shape
  679.                         --cargo status
  680.                         --gl.Color(1, 0, 0, 1)
  681.                         --gl.DrawGroundCircle (x,y,z, 30, 3)
  682. --                      gl.Color(1, 1, 0, 1)
  683. --                      gl.DrawGroundCircle (x,y,z, SYNCED.miners[i].cargo, 3)  --growing triangle shows cargo status
  684.                         --mark last_mined minerals
  685. --                      if (Spring.ValidUnitID  (SYNCED.miners[i].last_mined_id)) then
  686. --                          local x,y,z=Spring.GetUnitPosition (SYNCED.miners[i].last_mined_id)
  687. --                          gl.Color(0, 0.8, 0, 1)
  688. --                          gl.DrawGroundCircle (x,y,z, 30, 15)  --growing triangle shows cargo status                         
  689. --                      end
  690.                     end
  691.                 end
  692.             end
  693.     end
  694. end
  695. --]]
  696. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement