Advertisement
Guest User

Untitled

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