WAREHOUSE

This commit is contained in:
funkyfranky 2018-08-02 00:29:14 +02:00
parent d4449f7913
commit 52e69cb697
2 changed files with 159 additions and 89 deletions

View File

@ -81,11 +81,11 @@ do -- world
--- Searches a defined volume of 3d space for the specified objects within it and then can run function on each returned object. See [hoggit](https://wiki.hoggitworld.com/view/DCS_func_searchObjects). --- Searches a defined volume of 3d space for the specified objects within it and then can run function on each returned object. See [hoggit](https://wiki.hoggitworld.com/view/DCS_func_searchObjects).
-- @function [parent=#world] searchObjects -- @function [parent=#world] searchObjects
-- @param #DCS.Object.Category objectcategory Category (can be a table) of objects to search. -- @param DCS#Object.Category objectcategory Category (can be a table) of objects to search.
-- @param #DCS word.VolumeType volume Shape of the search area/volume. -- @param DCS#word.VolumeType volume Shape of the search area/volume.
-- @param #ObjectSeachHandler handler A function that handles the search. -- @param ObjectSeachHandler handler A function that handles the search.
-- @param #table any Additional data. -- @param #table any Additional data.
-- @return #DCS.unit -- @return DCS#Unit
--- Returns a table of mark panels indexed numerically that are present within the mission. See [hoggit](https://wiki.hoggitworld.com/view/DCS_func_getMarkPanels) --- Returns a table of mark panels indexed numerically that are present within the mission. See [hoggit](https://wiki.hoggitworld.com/view/DCS_func_getMarkPanels)
-- @function [parent=#world] getMarkPanels -- @function [parent=#world] getMarkPanels

View File

@ -1,4 +1,4 @@
--- **Functional** - (R2.4) - Manages assets of an airbase and transportation to other airbases. --- **Functional** - (R2.4) - Manages assets of an airbase and transportation to other airbases upon request.
-- --
-- --
-- Features: -- Features:
@ -19,7 +19,7 @@
-- @field #string ClassName Name of the class. -- @field #string ClassName Name of the class.
-- @field DCS#Coalition coalition Coalition the warehouse belongs to. -- @field DCS#Coalition coalition Coalition the warehouse belongs to.
-- @field Core.Point#COORDINATE coordinate Coordinate of the warehouse. -- @field Core.Point#COORDINATE coordinate Coordinate of the warehouse.
-- @field Wrapper.Airbase#AIRBASE airbase Airbase the warehouse belongs to. -- @field Wrapper.Airbase#AIRBASE homebase Airbase the warehouse belongs to.
-- @field #table stock Table holding all assets in stock. Table entries are of type @{#WAREHOUSE.Stock}. -- @field #table stock Table holding all assets in stock. Table entries are of type @{#WAREHOUSE.Stock}.
-- @extends Core.Fsm#FSM -- @extends Core.Fsm#FSM
@ -58,50 +58,50 @@ WAREHOUSE = {
stock = {}, stock = {},
} }
--- Type Warehouse stock table. table.insert(self.stock, {templatename=templategroupname, category=DCScategory, type=DCStype, transport=transport, fighther=fighter, tanker=tanker, awacs=awacs, artillery=artillery}) --- Item of the warehouse stock table.
-- @type WAREHOUSE.Stock -- @type WAREHOUSE.Stockitem
-- @field #string templatename Name of the template group. -- @field #string templatename Name of the template group.
-- @field DCS#Category category Category of the group. Airplane, helicopter, ... -- @field DCS#Group.Category category Category of the group.
-- @field #string type Type of the group -- @field #string unittype Type of the first unit of the group as obtained by the Object.getTypeName() DCS API function.
-- @field #boolean fighter If true, group is a fighter airplane. -- @field #WAREHOUSE.Attribute attribute Generalized attribute of the group.
-- @field #boolean attackhelo If true, group is an attack helicopter.
-- @field #boolean transport If truie, group can transport other units either by air or ground.
-- @field #boolean tanker If true, group is a tanker and can refuel other air units.
-- @field #boolean awacs If true, group has AWACS capabilities.
-- @field #boolean artillery If true, group is an artillery unit.
--- Asset descriptor. --- Descriptors enumerator describing the type of the asset in stock.
-- @field Warehouse.AssetDescriptor Assetdescriptor -- @type WAREHOUSE.Descriptor
WAREHOUSE.Descriptor = { WAREHOUSE.Descriptor = {
TEMPLATENAME="templatename", TEMPLATENAME="templatename",
CATEGORY="category", CATEGORY="category",
UNITTYPE="unittype",
ATTRIBUTE="attribute",
} }
--- Warehouse classes. --- Warehouse unit categories. These are used for
-- @field Warehouse.Class Class -- @type WAREHOUSE.Attribute
WAREHOUSE.Class = { WAREHOUSE.Attribute = {
TRANSPORT=1, TRANSPORT="transport",
FIGHTER=2, FIGHTER="fighter",
TANKER=3, TANKER="tanker",
AWACS=4, AWACS="awacs",
ARTY=5, ARTILLERY="artillery",
ATTACKHELO=6, ATTACKHELICOPTER="attackhelicopter",
INFANTRY="infantry",
BOMBER="bomber",
TANK="tank",
} }
--- Warehouse categories --- Cargo transport type.
-- @field Category -- @type WAREHOUSE.TransportType
WAREHOUSE.Category = { -- @field #string AIRPLANE plane blabla
WAREHOUSE.TransportType = {
AIRPLANE = "plane", AIRPLANE = "plane",
HELICOPTER = "helo", HELICOPTER = "helo",
GROUND = "apc", GROUND = "ground",
SHIP = "ship", SHIP = "ship",
TRAIN = "train", TRAIN = "train",
SELF = "self", SELFPROPELLED = "selfporpelled",
} }
--- Warehouse class version. --- Warehouse class version.
-- @field #number version -- @field #string version
WAREHOUSE.version="0.1.0" WAREHOUSE.version="0.1.0"
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
@ -164,9 +164,13 @@ function WAREHOUSE:NewAirbase(airbase)
-- @function [parent=#WAREHOUSE] Request -- @function [parent=#WAREHOUSE] Request
-- @param #WAREHOUSE self -- @param #WAREHOUSE self
-- @param Wrapper.Airbase#AIRBASE Airbase Airbase requesting supply. -- @param Wrapper.Airbase#AIRBASE Airbase Airbase requesting supply.
-- @param #string Asset Asset that is requested. -- @param #WAREHOUSE.Descriptor AssetDescriptor Descriptor describing the asset that is requested.
-- @param #number nAsset Number of assets requested. Default 1. -- @param #depends AssetDescriptorvalue Value of the asset descriptor. Type depends on descriptor, i.e. could be a string, etc.
-- @param #string TransportType Type of transport: "Plane", "Helicopter", "APC" -- @param #number nAsset Number of groups requested that match the asset specification.
-- @param #WAREHOUSE.TransportType TransportType Type of transport.
-- @return boolean If true, request is granted.
--
-- @usage mywarehouse:Request(AIRBASE:)...
--- Triggers the FSM event "Request" after a delay. --- Triggers the FSM event "Request" after a delay.
-- @function [parent=#WAREHOUSE] __Request -- @function [parent=#WAREHOUSE] __Request
@ -221,22 +225,24 @@ end
function WAREHOUSE:onafterStatus(From, Event, To) function WAREHOUSE:onafterStatus(From, Event, To)
env.info("FF checking warehouse status of airbase "..self.homebase:GetName()) env.info("FF checking warehouse status of airbase "..self.homebase:GetName())
env.info(string.format("FF warehouse at %s: number of stock = %d", self.homebase:GetName(), #self.stock)) --env.info(string.format("FF warehouse at %s: number of stock = %d", self.homebase:GetName(), #self.stock))
self:_DisplayStockItems(self.stock)
self:__Status(30) self:__Status(30)
end end
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
--- On before "Request" event. Checks if the request can be fullfilled. --- On before "Request" event. Checks if the request can be fullfilled.
-- @param #WAREHOUSE self -- @param #WAREHOUSE self
-- @param #string From From state. -- @param #string From From state.
-- @param #string Event Event. -- @param #string Event Event.
-- @param #string To To state. -- @param #string To To state.
-- @param Wrapper.Airbase#AIRBASE Airbase Airbase requesting supply. -- @param Wrapper.Airbase#AIRBASE Airbase Airbase requesting supply.
-- @param #string AssetDescriptor Asset that is requested. Can be "templatename", ... -- @param #WAREHOUSE.Descriptor AssetDescriptor Descriptor describing the asset that is requested.
-- @param depends AssetDescriptorvalue Value of the asset descriptor. Type depends on descriptor, i.e. could be a string, -- @param depends AssetDescriptorvalue Value of the asset descriptor. Type depends on descriptor, i.e. could be a string, etc.
-- @param #number nAssed Number of groups of that asset requested. -- @param #number nAsset Number of groups requested that match the asset specification.
-- @param #string TransportType Type of transport: "Plane", "Helicopter", "APC" -- @param #WAREHOUSE.TransportType TransportType Type of transport.
-- @return boolean If true, request is granted. -- @return boolean If true, request is granted.
-- --
-- @usage mywarehouse:Request(AIRBASE:)... -- @usage mywarehouse:Request(AIRBASE:)...
@ -244,78 +250,83 @@ function WAREHOUSE:onbeforeRequest(From, Event, To, Airbase, AssetDescriptor, As
-- Distance from warehouse to -- Distance from warehouse to
local distance=self.coordinate:Get2DDistance(Airbase:GetCoordinate()) local distance=self.coordinate:Get2DDistance(Airbase:GetCoordinate())
-- -- Filter the requested assets.
local _stockrequest=self._FilterStock(self.stock, AssetDescriptor, AssetDescriptorValue) local _stockrequest=self:_FilterStock(self.stock, AssetDescriptor, AssetDescriptorValue)
-- Asset is not in stock ==> request denied. -- Asset is not in stock ==> request denied.
if #_stockrequest==0 then if #_stockrequest < nAsset then
self:E(self.wid..string.format("Request denied! Asset is currently not in stock.")) self:E(self.wid..string.format("Request denied! Not enought assets currently in stock. Requested %d < %d in stock.", nAsset, #_stockrequest))
return false return false
end end
-- Shortcut
local _TT=TransportType:lower() local _TT=TransportType:lower()
if _TT==nil then
if AssetDescriptor=="" then
end
end
if TransportType:lower() == "plane" then if _TT == WAREHOUSE.TransportType.AIRPLANE then
-- here check the availability of transport units!
elseif _TT == WAREHOUSE.TransportType.HELICOPTER then
elseif TransportType:lower() == "helicopter" then elseif _TT == WAREHOUSE.TransportType.GROUND then
elseif TransportType:lower() == "apc" then elseif _TT == WAREHOUSE.TransportType.SHIP then
elseif TransportType:lower() == "train" then elseif _TT == WAREHOUSE.TransportType.TRAIN then
elseif TransportType:lower() == "ship" then elseif _TT == WAREHOUSE.TransportType.SELFPROPELLED then
elseif TransportType:lower() == "self" then
else else
self:E(self.wid..string.format("ERROR: unknown transport type requested! type = %s", tostring(TransportType))) self:E(self.wid..string.format("ERROR: unknown transport type requested! type = %s", tostring(TransportType)))
end end
return true
end end
--- On after "Request" event. --- On before "Request" event. Checks if the request can be fullfilled.
-- @param #WAREHOUSE self -- @param #WAREHOUSE self
-- @param #string From From state. -- @param #string From From state.
-- @param #string Event Event. -- @param #string Event Event.
-- @param #string To To state. -- @param #string To To state.
-- @param Wrapper.Airbase#AIRBASE Airbase Airbase requesting supply. -- @param Wrapper.Airbase#AIRBASE Airbase Airbase requesting supply.
-- @param #string Asset Asset that is requested. -- @param #WAREHOUSE.Descriptor AssetDescriptor Descriptor describing the asset that is requested.
-- @param #number nAssed Number of groups of that asset requested. -- @param depends AssetDescriptorvalue Value of the asset descriptor. Type depends on descriptor, i.e. could be a string, etc.
-- @param #string TransportType Type of transport: "Plane", "Helicopter", "APC" -- @param #number nAsset Number of groups requested that match the asset specification.
function WAREHOUSE:onafterRequest(From, Event, To, Airbase, Asset, nAsset, TransportType) -- @param #WAREHOUSE.TransportType TransportType Type of transport.
env.info(string.format("FF airbase %s is requesting asset %s from warehouse %s", Airbase:GetName(), Asset, self.homebase:GetName())) -- @return boolean If true, request is granted.
--
-- @usage mywarehouse:Request(AIRBASE:)...
function WAREHOUSE:onafterRequest(From, Event, To, Airbase, AssetDescriptor, AssetDescriptorValue, nAsset, TransportType)
env.info(self.wid..string.format("Airbase %s requesting asset %s = %s.", Airbase:GetName(), tostring(AssetDescriptor), tostring(AssetDescriptorValue)))
local nAsset=nAsset or 1 local nAsset=nAsset or 1
if TransportType=="Air" then -- Filter the requested assets.
local _stock=self:_FilterStock(self.stock, AssetDescriptor, AssetDescriptorValue)
-- Get a random template from the stock list. -- Get a random template from the stock list.
local _chosenone=math.random(#self.stock) --#WAREHOUSE.Stock local _chosenone=math.random(#_stock)
-- Select template group name.
local template=_stock[_chosenone].templatename
-- Select template group name.
--TODO: FILTER HERE!
local template=self.stock[_chosenone].templatename if TransportType==WAREHOUSE.TransportType.AIRPLANE then
if template then if template then
-- Spawn plane at warehouse homebase. -- Spawn plane at warehouse homebase.
--TODO: this is wrong. we need to filter the transports and get the right template!
local Plane=SPAWN:New(template):SpawnAtAirbase(Airbase, nil, nil, nil, false) local Plane=SPAWN:New(template):SpawnAtAirbase(Airbase, nil, nil, nil, false)
if Plane==nil then if Plane==nil then
-- Plane was not spawned correctly. Try again in 60 seconds. -- Plane was not spawned correctly. Try again in 60 seconds.
self:__Request( 60, Airbase, Asset, nAsset, TransportType) self:__Request(60, Airbase, AssetDescriptor, AssetDescriptorValue, nAsset, TransportType)
return return
else else
-- Remove chosen plane from list. -- Remove chosen asset from list.
table.remove(self.stock,_chosenone) table.remove(self.stock,_stock.pos)
end end
-- New empty cargo set. -- New empty cargo set.
@ -354,8 +365,13 @@ function WAREHOUSE:onafterRequest(From, Event, To, Airbase, Asset, nAsset, Trans
-- Trigger Delivered event. -- Trigger Delivered event.
warehouse:__Delivered(1, group) warehouse:__Delivered(1, group)
end end
else
self:E(self.wid.."ERROR: template does not exist!")
end end
else
self:E(self.wid.."ERROR: unknown transport type!")
end end
end end
@ -406,25 +422,60 @@ function WAREHOUSE:AddAsset(templategroupname, ngroups, istransport)
env.info(string.format("display name = %s", DCSdisplay)) env.info(string.format("display name = %s", DCSdisplay))
env.info(string.format("category = %s", DCScategory)) env.info(string.format("category = %s", DCScategory))
env.info(string.format("type = %s", DCStype)) env.info(string.format("type = %s", DCStype))
env.info(string.format("attribute infantry = %s", tostring(group:HasAttribute("Infantry"))))
self:E({desc=DCSdesc}) self:E({desc=DCSdesc})
-- Get generalized attributes.
local transport=group:HasAttribute("Transport helicopters") or group:HasAttribute("Transports") or group:HasAttribute("Infantry carriers") local transport=group:HasAttribute("Transport helicopters") or group:HasAttribute("Transports") or group:HasAttribute("Infantry carriers")
local fighter=group:HasAttribute("Fighters") or group:HasAttribute("Interceptors") or group:HasAttribute("Multirole fighters") local fighter=group:HasAttribute("Fighters") or group:HasAttribute("Interceptors") or group:HasAttribute("Multirole fighters")
local tanker=group:HasAttribute("Tankers") local tanker=group:HasAttribute("Tankers")
local awacs=group:HasAttribute("AWACS") local awacs=group:HasAttribute("AWACS")
local apc=group:HasAttribute("Infantry carriers")
local artillery=group:HasAttribute("Artillery") local artillery=group:HasAttribute("Artillery")
env.info(string.format("attribute transport = %s", tostring(transport))) local infantry=group:HasAttribute("Infantry")
env.info(string.format("attribute apc = %s", tostring(apc))) local attackhelicopter=group:HasAttribute("Attack helicopters")
env.info(string.format("attribute figther = %s", tostring(fighter))) local bomber=group:HasAttribute("Bombers")
env.info(string.format("attribute tanker = %s", tostring(tanker))) local tank=group:HasAttribute("Old Tanks") or group:HasAttribute("Modern Tanks")
env.info(string.format("attribute awacs = %s", tostring(awacs)))
env.info(string.format("attribute artillery = %s", tostring(artillery))) -- Debug output.
env.info(string.format("attribute transport = %s", tostring(transport)))
env.info(string.format("attribute figther = %s", tostring(fighter)))
env.info(string.format("attribute tanker = %s", tostring(tanker)))
env.info(string.format("attribute awacs = %s", tostring(awacs)))
env.info(string.format("attribute artillery = %s", tostring(artillery)))
env.info(string.format("attribute infantry = %s", tostring(infantry)))
env.info(string.format("attribute attackhelo = %s", tostring(attackhelicopter)))
env.info(string.format("attribute bomber = %s", tostring(bomber)))
env.info(string.format("attribute tank = %s", tostring(tank)))
local attribute="unknown" --#WAREHOUSE.Attribute
if transport then
attribute=WAREHOUSE.Attribute.TRANSPORT
elseif fighter then
attribute=WAREHOUSE.Attribute.FIGHTER
elseif tanker then
attribute=WAREHOUSE.Attribute.TANKER
elseif awacs then
attribute=WAREHOUSE.Attribute.AWACS
elseif artillery then
attribute=WAREHOUSE.Attribute.ARTILLERY
elseif infantry then
attribute=WAREHOUSE.Attribute.INFANTRY
elseif attackhelicopter then
attribute=WAREHOUSE.Attribute.ATTACKHELICOPTER
elseif bomber then
attribute=WAREHOUSE.Attribute.BOMBER
elseif tank then
attribute=WAREHOUSE.Attribute.TANK
end
-- Add this n times to the table. -- Add this n times to the table.
for i=1,n do for i=1,n do
table.insert(self.stock, {templatename=templategroupname, category=DCScategory, type=DCStype, transport=transport, fighther=fighter, tanker=tanker, awacs=awacs, artillery=artillery}) local stockitem={} --#WAREHOUSE.Stockitem
stockitem.templatename=templategroupname
stockitem.category=DCScategory
stockitem.attribute=attribute
stockitem.unittype=DCStype
table.insert(self.stock, stockitem)
end end
else else
@ -441,15 +492,19 @@ end
--- Filter stock assets by table entry. --- Filter stock assets by table entry.
-- @param #WAREHOUSE self -- @param #WAREHOUSE self
-- @param #WAREHOUSE.Stock stock -- @param #table stock Table holding all assets in stock of the warehouse. Each entry is of type @{#WAREHOUSE.Stockitem}.
-- @param #string item Descriptor -- @param #string item Descriptor
-- @param depends value -- @param depends value Value of the descriptor.
-- @return #table Filtered stock items table.
function WAREHOUSE:_FilterStock(stock, item, value) function WAREHOUSE:_FilterStock(stock, item, value)
-- Filtered array.
local filtered={} local filtered={}
for _,_stock in pairs(stock) do -- Loop over stock items.
for _i,_stock in ipairs(stock) do
if _stock[item]==value then if _stock[item]==value then
_stock.pos=_i
table.insert(filtered, _stock) table.insert(filtered, _stock)
end end
end end
@ -457,6 +512,21 @@ function WAREHOUSE:_FilterStock(stock, item, value)
return filtered return filtered
end end
--- Filter stock assets by table entry.
-- @param #WAREHOUSE self
-- @param #table stock Table holding all assets in stock of the warehouse. Each entry is of type @{#WAREHOUSE.Stockitem}.
function WAREHOUSE:_DisplayStockItems(stock)
local text=self.wid..string.format("Warehouse %s stock assets:\n", self.homebase:GetName())
for _,_stock in pairs(stock) do
local mystock=_stock --#WAREHOUSE.Stockitem
text=text..string.format("template = %s, category = %d, unittype = %s, attribute = %s\n", mystock.templatename, mystock.category, mystock.unittype, mystock.attribute)
end
env.info(text)
MESSAGE:New(text,30):ToAll()
end
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------