Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions data/ai/geohandler.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
GeoSpotHandler = class(Module)


function GeoSpotHandler:Name()
return "GeoSpotHandler"
end

function GeoSpotHandler:internalName()
return "geospothandler"
end

function GeoSpotHandler:Init()
self.geos = self.game.map:GetGeoSpots()
end

function distance(pos1,pos2)
local xd = pos1.x-pos2.x
local zd = pos1.z-pos2.z
local yd = pos1.y-pos2.y
if yd < 0 then
yd = -yd
end
dist = math.sqrt(xd*xd + zd*zd + yd*yd*yd)
return dist
end

function GeoSpotHandler:ClosestFreeGeo(unittype,position)
local pos = nil
local bestDistance = 10000
geoCount = self.game.map:GeoCount()
for i,v in ipairs(self.geos) do
local p = v
local dist = distance(position,p)
if dist < bestDistance then
if self.game.map:CanBuildHere(unittype,p) then
--checking for unit positions
local spGetUnitsInRectangle = Spring.GetUnitsInRectangle
local radius = 100
local units_found = spGetUnitsInRectangle(p.x - radius, p.z - radius, p.x + radius, p.z + radius)
if #units_found == 0 then
bestDistance = dist
pos = p
end
end
end
end
return pos
end
13 changes: 13 additions & 0 deletions data/ai/preload/shard_null/map.lua
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,19 @@ function map:GetMetalSpots() -- returns a table of spot positions
return {}
end

function map:GeoCount() -- returns the nubmer of metal spots
return 0
end

function map:GetGeo(idx) -- returns a Position for the given spot
return nil
end

function map:GetGeoSpots() -- returns a table of spot positions
--
return {}
end

function map:GetControlPoints()
-- not sure this can be implemented in the Spring C++ AI interface
return {}
Expand Down
25 changes: 25 additions & 0 deletions data/ai/preload/spring_cpp/map.lua
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,31 @@ function map:GetMetalSpots() -- returns a table of spot positions
return f
end

function map:GeoCount() -- returns the nubmer of metal spots
local m = game_engine:Map()
return m:GeoCount()
end

function map:GetGeo(idx) -- returns a Position for the given spot
local m = game_engine:Map()
return m:GetGeo(idx)
end

function map:GetMetalSpots() -- returns a table of spot positions
--
local m = game_engine:Map()
local fv = game_engine:Map():GetGeoSpots()
local count = m:GeoCount()
local f = {}
local i = 0
while i < count do
table.insert( f, m:GetGeo(i) )
i = i + 1
end
--fv = nil
return f
end

function map:GetControlPoints()
-- not sure this can be implemented in the Spring C++ AI interface
return {}
Expand Down
18 changes: 18 additions & 0 deletions data/ai/preload/spring_lua/geo.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
local function GetGeos()
local geos = {}
local i = 1
for ct, featureID in pairs(Spring.GetAllFeatures()) do
local featureDefID = Spring.GetFeatureDefID(featureID)
if FeatureDefs[featureDefID].geoThermal == true then
local x, y, z = Spring.GetFeaturePosition(featureID)
local spot = {x = x, z = z, y = Spring.GetGroundHeight(x,z)}
geos[i] = spot
i = i + 1
end
end
return geos
end

local spots = GetGeos()

return spots
23 changes: 22 additions & 1 deletion data/ai/preload/spring_lua/map.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
local map = {}
map.spots = shard_include("spring_lua/metal")

map.geos = shard_include("spring_lua/geo")
-- function map:FindClosestBuildSite(unittype,builderpos, searchradius, minimumdistance)
-- function map:CanBuildHere(unittype,position)
-- function map:GetMapFeatures()
Expand Down Expand Up @@ -100,6 +100,27 @@ function map:GetMetalSpots() -- returns a table of spot positions
return f
end

function map:GeoCount() -- returns the nubmer of metal spots
Spring.Echo("GetGeoCount")
return #self.geos
end

function map:GetGeo(idx)
return self.geos[idx]
end

function map:GetGeoSpots() -- returns a table of spot positions
local fv = self.geos
local count = self:GeoCount()
local f = {}
local i = 0
while i < count do
table.insert( f, fv[i] )
i = i + 1
end
return f
end

function map:GetControlPoints()
if self.controlPoints then return self.controlPoints end
self.controlPoints = {}
Expand Down
5 changes: 3 additions & 2 deletions data/ai/preload/spring_lua/unit.lua
Original file line number Diff line number Diff line change
Expand Up @@ -205,14 +205,15 @@ function ShardUnit:MoveAndPatrol(p)
return true
end

function ShardUnit:Build(t, p) -- IUnitType*
function ShardUnit:Build(t, p, f) -- IUnitType*
if type(t) == "string" then
-- local ai = Shard.AIs[1]
-- t = ai.game:GetTypeByName(t)
t = game:GetTypeByName(t)
end
f = f or 0
if not p then p = self:GetPosition() end
Spring.GiveOrderToUnit( self.id, -t:ID(), { p.x, p.y, p.z }, {} )
Spring.GiveOrderToUnit( self.id, -t:ID(), { p.x, p.y, p.z, f}, {} )
return true
end

Expand Down
4 changes: 4 additions & 0 deletions data/ai/preload/spring_lua/unittype.lua
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,10 @@ function ShardUnitType:Extractor()
return self.def.extractsMetal > 0
end

function ShardUnitType:Geothermal()
return self.def.needGeo
end

function ShardUnitType:ExtractorEfficiency()
return self.def.extractsMetal
end
Expand Down
15 changes: 15 additions & 0 deletions data/ai/taskqueuebehaviour.lua
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,8 @@ function TaskQueueBehaviour:TryToBuild( unit_name )
local success = false
if utype:Extractor() then
success = self:BuildExtractor(utype)
elseif utype:Geothermal() then
success = self:BuildGeo(utype)
elseif unit:Type():IsFactory() then
success = self.unit:Internal():Build(utype)
else
Expand Down Expand Up @@ -259,6 +261,19 @@ function TaskQueueBehaviour:BuildOnMap(utype)
return true
end

function TaskQueueBehaviour:BuildGeo(utype)
-- find a free spot!
unit = self.unit:Internal()
p = unit:GetPosition()
p = self.ai.geospothandler:ClosestFreeGeo(utype,p)
if p == nil or self.game.map:CanBuildHere(utype,p) ~= true then

self:OnToNextTask()
return false
end
return self.unit:Internal():Build(utype,p)
end

function TaskQueueBehaviour:BuildExtractor(utype)
-- find a free spot!
unit = self.unit:Internal()
Expand Down