function gadget:GetInfo() return { name = "mining blabla2", desc = "some broken stuff", author = "knorke", date = "4 Okt 2010", license = "oh", layer = 0, enabled = true -- loaded by default } end if (gadgetHandler:IsSyncedCode()) then local miners = {} -- [unitID] alive: true oder false cargo: wieviel er trägt status: was er gerade macht: "loading" "unloading" "mining" "to_res" "to_hq" local dropoffs = {} --[unitID] local dropoffs_n = 0 -----config----- local miner_name = "bminer"; local ressource_name = "bminerals" local dropoff_name = "bflagshipbase" local dropoff_distance = 300 --how near do miners have to get to a dropoff to drop their cargo? ---------------- function gadget:UnitFinished(unitID, unitDefID, teamID) if (is_miner_type (unitDefID) == true) then add_miner (unitID) end if (is_dropoff_type (unitDefID) == true) then add_dropoff (unitID) end end function gadget:UnitDestroyed(unitID, unitDefID, teamID, attackerID) --remove destroyed miners: if is_miner (unitID) then remove_miner (unitID) end --remove destroyed dropoffs: if is_dropoff (unitID) then remove_dropoff (unitID) end --wenn etwas geerntet wurde: if (is_ressource_type (unitDefID)) then Spring.Echo ("ressource mined") end if (is_miner(attackerID)) then Spring.Echo ("miner mined") end if (is_ressource_type (unitDefID) and is_miner(attackerID)) then miners[attackerID].cargo = miners[attackerID].cargo + 25 if (miners[attackerID].cargo > 0) then --send full miners to dropoff local x, y, z = Spring.GetUnitPosition(attackerID) local tx, ty, tz = nearest_dropoff_position_from_miner (attackerID) if (tx ~= nil) then Spring.GiveOrderToUnit(attackerID, CMD.MOVE_STATE, { 2 }, {}) Spring.GiveOrderToUnit(attackerID, CMD.MOVE , {tx+50, ty, tz }, {}) end end Spring.Echo ("finished mining! cargo:" .. miners[attackerID].cargo) end end function gadget:GameFrame(frameNum) _G.miners = miners _G.dropoffs = dropoffs for i in pairs(miners) do if (is_miner_at_dropoff (i)) then --drop the cargo local minerteam = Spring.GetUnitAllyTeam (i) Spring.AddTeamResource (minerteam, "metal", miners[i].cargo*10) miners[i].cargo = 0 local x, y, z = Spring.GetUnitPosition(i) --back to mining: ***this needs work! --Spring.GiveOrderToUnit(i, CMD.MOVE_STATE, { 2 }, {}) Spring.GiveOrderToUnit(i, CMD.AREA_ATTACK , { x, y, z,5000 }, {}) Spring.Echo ("sending " .. i .. " to mine") end end end function gadget:Initialize() make_miner_table() _G.miners = miners; _G.dropoffs = dropoffs _G.dropoff_distance = dropoff_distance end function marker_on_unit (_uID, _text) if (markers == false) then return end if (_uID == nil) then return end if (_text == nil) then return end local x,y,z=Spring.GetUnitPosition (_uID) if (x == nil or y == nil or z == nil) then return end Spring.MarkerAddPoint (x,y,z, _text .. "id:" .. _uID) end function make_miner_table() miners = {} local all_units = Spring.GetAllUnits () for i in pairs(all_units) do local unitDefID = Spring.GetUnitDefID(all_units[i]) if (is_miner_type (unitDefID)==true) then add_miner (all_units[i]) end if (is_dropoff_type (unitDefID)==true) then add_dropoff (all_units[i]) end end end function is_miner_type (unitDefID) if (unitDefID == nil) then return false end local unitDef = UnitDefs[unitDefID] if (unitDef == nil) then return false end if (unitDef.name == miner_name) then return true end return false end function is_dropoff_type (unitDefID) if (unitDefID == nil) then return false end local unitDef = UnitDefs[unitDefID] if (unitDef == nil) then return false end if (unitDef.name == dropoff_name) then return true end return false end function is_ressource_type (unitDefID) if (unitDefID == nil) then return false end local unitDef = UnitDefs[unitDefID] if (unitDef == nil) then return false end if (unitDef.name == ressource_name) then return true end return false end function is_miner (unitID) if (miners [unitID] ~= nil) then return true else return false end end function is_dropoff (unitID) if (dropoffs [unitID] ~= nil) then return true else return false end end function nearest_dropoff_position_from_miner (minerID) local nearest_distance = 9999999999 local nearest_dropoffID = nil for i in pairs (dropoffs) do local d = Spring.GetUnitSeparation (minerID, i) if (d < nearest_distance) then nearest_distance = d nearest_dropoffID = i end end if (nearest_dropoffID ~= nil) then return Spring.GetUnitPosition (nearest_dropoffID) else return nil end end function add_miner (unitID) miners [unitID] = {} miners [unitID].alive = true miners [unitID].cargo = 0 marker_on_unit (unitID, "miner added") end function add_dropoff (unitID) dropoffs [unitID] = {} dropoffs [unitID].alive = true dropoffs [unitID].cargo = 0 marker_on_unit (unitID, "droppoff added") end function remove_miner (unitID) Spring.Echo ("removing miner id=" .. unitID) miners [unitID].alive=false miners [unitID] = nil miners_n = miners_n - 1 end function remove_dropoff (unitID) Spring.Echo ("removing dropoff id=" .. unitID) dropoffs [unitID].alive=false dropoffs [unitID] = nil dropoffs_n = dropoffs_n - 1 end function is_miner_at_dropoff (miner_unitID) for i in pairs (dropoffs) do if Spring.GetUnitSeparation (miner_unitID, i) < dropoff_distance then return true end end return false end -------------------------------------------------------- --Border between SYNC and UNSYNC --when i wear my frilly dress im a real touhou end if (not gadgetHandler:IsSyncedCode()) then -------------------------------------------------------- --Length of table, for synced (using spairs) function sxlen(tbl) local qq = 0; for v in spairs(tbl) do qq = qq + 1; end return qq; end function gadget:DrawScreen() gl.Color(0.5, 0.5, 0.5, 1) local ty = 0 for i in spairs(SYNCED.miners) do ty = ty + 1 gl.Text (ty .."| [" .. i .. "]".. "cargo:" .. SYNCED.miners[i].cargo, 100,300-ty*25 , 25, 'o') end gl.Text ("miners counted="..ty .. "#miners:" .. sxlen(SYNCED.miners), 100,300 , 25, 'o') end function gadget:DrawWorldPreUnit() gl.LineWidth(5.0) for i in spairs(SYNCED.dropoffs) do local x,y,z=Spring.GetUnitPosition (i) if (x ~= nil and y ~= nil and z ~= nil) then gl.Color(1, 1, 1, 1) gl.DrawGroundCircle (x,y,z, SYNCED.dropoff_distance, 8) --all dropoffs marked with octagon end end for i in spairs(SYNCED.miners) do if (SYNCED.miners[i].alive == true and i ~= nil and SYNCED.miners[i] ~=nil) then local x,y,z=Spring.GetUnitPosition (i) if (x ~= nil and y ~= nil and z ~= nil) then gl.Color(0, 1, 0, 1) --gl.DrawGroundCircle (x,y,z, 50, 4) --all miners marked with a diamond shape --cargo status gl.Color(1, 0, 0, 1) gl.DrawGroundCircle (x,y,z, 30, 3) gl.Color(1, 1, 0, 1) gl.DrawGroundCircle (x,y,z, SYNCED.miners[i].cargo, 3) --growing triangle shows cargo status end end end end end