From 704957e16983e5c3d80e9648947b0bda46d054d4 Mon Sep 17 00:00:00 2001 From: Nils Brugger Date: Tue, 15 Mar 2022 23:37:32 +0100 Subject: [PATCH 1/4] feature: typesave sensor mappings --- go.sum | 4 - m/base.go | 12 +-- m/control.go | 6 +- m/custom.go | 10 ++- m/enums.go | 129 ++++++++++++++++++++++++++++++ m/impl/custom.go | 3 + m/impl/types.go | 199 +++++++++++++++++++++++++++++++++++++--------- m/types.go | 125 +---------------------------- m/unit.go | 6 +- m/unit_control.go | 8 +- 10 files changed, 318 insertions(+), 184 deletions(-) create mode 100644 m/enums.go diff --git a/go.sum b/go.sum index a14d19f..4848466 100644 --- a/go.sum +++ b/go.sum @@ -54,8 +54,6 @@ github.com/MarvinJWendt/testza v0.2.1/go.mod h1:God7bhG8n6uQxwdScay+gjm9/LnO4D3k github.com/MarvinJWendt/testza v0.2.8/go.mod h1:nwIcjmr0Zz+Rcwfh3/4UhBp7ePKVhuBExvZqnKYWlII= github.com/MarvinJWendt/testza v0.2.10/go.mod h1:pd+VWsoGUiFtq+hRKSU1Bktnn+DMCSrDrXDpX2bG66k= github.com/MarvinJWendt/testza v0.2.12/go.mod h1:JOIegYyV7rX+7VZ9r77L/eH6CfJHHzXjB69adAhzZkI= -github.com/MarvinJWendt/testza v0.2.14 h1:xSuCRRRlNNCHOONg0iUEb6hkx8fYNWPSbV2w1LIgvtA= -github.com/MarvinJWendt/testza v0.2.14/go.mod h1:JOIegYyV7rX+7VZ9r77L/eH6CfJHHzXjB69adAhzZkI= github.com/MarvinJWendt/testza v0.2.15 h1:suR1fQ/folIjEJw4GTbIBahhmZ16zatr9JghbSU+LV0= github.com/MarvinJWendt/testza v0.2.15/go.mod h1:eFcL4I0idjtIx8P9C6KkAuLgATNKpX4/2oUqKc6bF2c= github.com/OneOfOne/xxhash v1.2.2/go.mod h1:HSdplMjZKSmBqAxg5vPj2TmRDmfkzw+cTzAElWljhcU= @@ -256,7 +254,6 @@ github.com/jstemmer/go-junit-report v0.9.1/go.mod h1:Brl9GWCQeLvo8nXZwPNNblvFj/X github.com/julienschmidt/httprouter v1.2.0/go.mod h1:SYymIcj16QtmaHHD7aYtjjsJG7VTCxuUUipMqKk8s4w= github.com/kisielk/errcheck v1.5.0/go.mod h1:pFxgyoBC7bSaBwPgfKdkLd5X25qrDl4LWUI2bnpBCr8= github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck= -github.com/klauspost/cpuid/v2 v2.0.9 h1:lgaqFMSdTdQYdZ04uHyN2d/eKdOMyi2YLSvlQIBFYa4= github.com/klauspost/cpuid/v2 v2.0.9/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg= github.com/klauspost/cpuid/v2 v2.0.10 h1:fv5GKR+e2UgD+gcxQECVT5rBwAmlFLl2mkKm7WK3ODY= github.com/klauspost/cpuid/v2 v2.0.10/go.mod h1:g2LTdtYhdyuGPqyWyv7qRAmj1WBqxuObKfj5c0PQa7c= @@ -336,7 +333,6 @@ github.com/pterm/pterm v0.12.27/go.mod h1:PhQ89w4i95rhgE+xedAoqous6K9X+r6aSOI2eF github.com/pterm/pterm v0.12.29/go.mod h1:WI3qxgvoQFFGKGjGnJR849gU0TsEOvKn5Q8LlY1U7lg= github.com/pterm/pterm v0.12.30/go.mod h1:MOqLIyMOgmTDz9yorcYbcw+HsgoZo3BQfg2wtl3HEFE= github.com/pterm/pterm v0.12.31/go.mod h1:32ZAWZVXD7ZfG0s8qqHXePte42kdz8ECtRyEejaWgXU= -github.com/pterm/pterm v0.12.33 h1:XiT50Pvdqn5O8FAiIqZMpXP6NkVEcmlUa+mkA1yWVCg= github.com/pterm/pterm v0.12.33/go.mod h1:x+h2uL+n7CP/rel9+bImHD5lF3nM9vJj80k9ybiiTTE= github.com/pterm/pterm v0.12.36 h1:Ui5zZj7xA8lXR0CxWXlKGCQMW1cZVUMOS8jEXs6ur/g= github.com/pterm/pterm v0.12.36/go.mod h1:NjiL09hFhT/vWjQHSj1athJpx6H8cjpHXNAK5bUw8T8= diff --git a/m/base.go b/m/base.go index bbc81a6..e11c635 100644 --- a/m/base.go +++ b/m/base.go @@ -14,23 +14,25 @@ func PrintFlush(targetMessage string) { } // Get the linked tile at the specified address -func GetLink[A integer](address A) Link { +func GetLink[A integer](address A) UnspecifiedBuilding { return nil } // Retrieve a list of units that match specified conditions // // Conditions are combined using an `and` operation -func Radar(from Ranged, target1 RadarTarget, target2 RadarTarget, target3 RadarTarget, sortOrder bool, sort RadarSort) Unit { +func Radar(from Building, target1 RadarTarget, target2 RadarTarget, target3 RadarTarget, sortOrder bool, sort RadarSort) Unit { return nil } -// Extract information indicated by sense from the provided block -func Sensor(block HealthC, sense string) float64 { +// Extract information indicated by sense from the provided block. +//Use this only if the needed information is not available using The getters of the building itself +//main purpose is to use generic "senses" or use things that are not mapped +func Sensor(block UnspecifiedBuilding, sense string) float64 { return 0 } // String equivalent of Sensor -func SensorStr(block HealthC, sense string) string { +func SensorStr(block UnspecifiedBuilding, sense string) string { return "" } diff --git a/m/control.go b/m/control.go index 019e07d..6f780a6 100644 --- a/m/control.go +++ b/m/control.go @@ -7,7 +7,7 @@ func ControlEnabled(target Building, enabled bool) { // Shoot with the provided turret at the target absolute position // // If shoot parameter is false, it will cease firing -func ControlShoot[A integer, B integer](turret Building, x A, y B, shoot bool) { +func ControlShoot[A integer, B integer](turret HasAmmo, x A, y B, shoot bool) { } // Smart version of ControlShoot @@ -15,9 +15,9 @@ func ControlShoot[A integer, B integer](turret Building, x A, y B, shoot bool) { // Shoot with the provided turret at the predicted position of target unit // // If shoot parameter is false, it will cease firing -func ControlShootP(turret Building, target HealthC, shoot bool) { +func ControlShootP(turret HasAmmo, target Shootable, shoot bool) { } // Set the configuration of the target building -func ControlConfigure[A integer](target Building, configuration A) { +func ControlConfigure[A integer](target HasConfig, configuration A) { } diff --git a/m/custom.go b/m/custom.go index 257cb0e..cdf0b02 100644 --- a/m/custom.go +++ b/m/custom.go @@ -2,7 +2,8 @@ package m // Process provided string as a game constant // -// Example m.Const("@copper") +// Example m.Const("@copper") (only use this if you cannot use the enums like ItemType or UnitType +// use this if you have dynamic values like m.Const("@"+"fl"+"are") (whyever you want to do this) func Const(constant string) string { return "" } @@ -10,6 +11,11 @@ func Const(constant string) string { // Return a building of the provided name // // Example m.B("message1") -func B(name string) Building { +func B(name string) UnspecifiedBuilding { + return nil +} + +// GetTurret Get a block that is specificaly a turret alias for B with better typing +func GetTurret(name string) Turret { return nil } diff --git a/m/enums.go b/m/enums.go new file mode 100644 index 0000000..17c27b5 --- /dev/null +++ b/m/enums.go @@ -0,0 +1,129 @@ +package m + +type BlockFlag = string + +const ( + BCore = BlockFlag("core") + BStorage = BlockFlag("storage") + BGenerator = BlockFlag("generator") + BTurret = BlockFlag("turret") + BFactory = BlockFlag("factory") + BRepair = BlockFlag("repair") + BRally = BlockFlag("rally") + BBattery = BlockFlag("battery") + BResupply = BlockFlag("resupply") + BReactor = BlockFlag("reactor") + BUnitModifier = BlockFlag("unitModifier") + BExtinguisher = BlockFlag("extinguisher") +) + +type RadarSort = string + +const ( + RSDistance = RadarSort("distance") + RSHealth = RadarSort("health") + RSShield = RadarSort("shield") + RSArmor = RadarSort("armor") + RSMaxHealth = RadarSort("maxHealth") +) + +type RadarTarget = string + +const ( + // Target anything + RTAny = RadarTarget("any") + RTEnemy = RadarTarget("enemy") + RTAlly = RadarTarget("ally") + RTPlayer = RadarTarget("player") + RTAttacker = RadarTarget("attacker") + RTFlying = RadarTarget("flying") + RTBoss = RadarTarget("boss") + RTGround = RadarTarget("ground") +) + +type UnitType = string + +const ( + UDagger = UnitType("@dagger") + UMace = UnitType("@mace") + UFortress = UnitType("@fortress") + UScepter = UnitType("@scepter") + UReign = UnitType("@reign") + + UNova = UnitType("@nova") + UPulsar = UnitType("@pulsar") + UQuasar = UnitType("@quasar") + UVela = UnitType("@vela") + UCorvus = UnitType("@corvus") + + UCrawler = UnitType("@crawler") + UAtrax = UnitType("@atrax") + USpiroct = UnitType("@spiroct") + UArkyid = UnitType("@arkyid") + UToxopid = UnitType("@toxopid") + + UFlare = UnitType("@flare") + UHorizon = UnitType("@horizon") + UZenith = UnitType("@zenith") + UAntumbra = UnitType("@antumbra") + UEclipse = UnitType("@eclipse") + + UMono = UnitType("@mono") + UPoly = UnitType("@poly") + UMega = UnitType("@mega") + UQuad = UnitType("@quad") + UOct = UnitType("@oct") + + URisso = UnitType("@risso") + UMinke = UnitType("@minke") + UBryde = UnitType("@bryde") + USei = UnitType("@sei") + UOmura = UnitType("@omura") + + UPlayerAlpha = UnitType("@alpha") + UPlayerBeta = UnitType("@beta") + UPlayerGamma = UnitType("@gamma") +) + +type ItemType = string + +const ( + ITCopper = ItemType("@copper") + ITLead = ItemType("@lead") + ITMetaGlass = ItemType("@metaglass") + ITGraphite = ItemType("@graphite") + ITSand = ItemType("@sand") + ITCoal = ItemType("@coal") + ITTitanium = ItemType("@titanium") + ITThorium = ItemType("@thorium") + ITScrap = ItemType("@scrap") + ITSilicon = ItemType("@silicon") + ITPlastanium = ItemType("@plastanium") + ITPhaseFabric = ItemType("@phase-fab") + ITSurgeAlloy = ItemType("@surge-alloy") + ITSporePod = ItemType("@spore-pod") + ITBlastCompound = ItemType("@blast-compound") + ITPyratite = ItemType("@pyratite") + // ITNone Used for checks where no item is found + ITNone = ItemType("") +) + +type FluidType = string + +const ( + FWater = FluidType("@water") + FOil = FluidType("@oil") + FSlag = FluidType("@slag") + FCryofluid = FluidType("@cryofluid") +) + +type Controller = int + +const ( + CtrlProcessor = Controller(1) + //TODO: doc + CtrlFormation = Controller(2) + CtrlPlayer = Controller(3) + // CtrlSelf The unit is idle or doing its own stuff -> not controlled + CtrlSelf = Controller(0) +) diff --git a/m/impl/custom.go b/m/impl/custom.go index f1b3d29..7ae102a 100644 --- a/m/impl/custom.go +++ b/m/impl/custom.go @@ -11,4 +11,7 @@ func init() { transpiler.RegisterInlineTranslation("m.B", func(args []transpiler.Resolvable) (transpiler.Resolvable, error) { return &transpiler.InlineVariable{Value: args[0]}, nil }) + transpiler.RegisterInlineTranslation("m.GetTurret", func(args []transpiler.Resolvable) (transpiler.Resolvable, error) { + return &transpiler.InlineVariable{Value: args[0]}, nil + }) } diff --git a/m/impl/types.go b/m/impl/types.go index efc31e9..dc11b43 100644 --- a/m/impl/types.go +++ b/m/impl/types.go @@ -3,10 +3,166 @@ package impl import ( "github.com/Vilsol/go-mlog/m" "github.com/Vilsol/go-mlog/transpiler" + "strconv" "strings" ) func init() { + registerConstants() + registerUnitEnum() + registerItemEnum() + registerNativeVariables() + registerArchtypeMethods() + + // HealthC's attributes + transpiler.RegisterFuncTranslation("GetHealth", createSensorFuncTranslation("@health")) + transpiler.RegisterFuncTranslation("GetName", createSensorFuncTranslation("@name")) + transpiler.RegisterFuncTranslation("GetX", createSensorFuncTranslation("@x")) + transpiler.RegisterFuncTranslation("GetY", createSensorFuncTranslation("@y")) + + transpiler.RegisterFuncTranslation("GetTotalItems", createSensorFuncTranslation("@totalItems")) + transpiler.RegisterFuncTranslation("GetItemCapacity", createSensorFuncTranslation("@itemCapacity")) + transpiler.RegisterFuncTranslation("GetRotation", createSensorFuncTranslation("@rotation")) + transpiler.RegisterFuncTranslation("GetShootX", createSensorFuncTranslation("@shootX")) + transpiler.RegisterFuncTranslation("GetShootY", createSensorFuncTranslation("@shootY")) + transpiler.RegisterFuncTranslation("IsShooting", createSensorFuncTranslation("@shooting")) + + // Building's attributes + transpiler.RegisterFuncTranslation("GetTotalLiquids", createSensorFuncTranslation("@totalLiquids")) + transpiler.RegisterFuncTranslation("GetLiquidCapaticy", createSensorFuncTranslation("@liquidCapaticy")) + transpiler.RegisterFuncTranslation("GetTotalPower", createSensorFuncTranslation("@totalPower")) + transpiler.RegisterFuncTranslation("GetPowerCapaticy", createSensorFuncTranslation("@powerCapaticy")) + transpiler.RegisterFuncTranslation("GetPowerNetStored", createSensorFuncTranslation("@powerNetStored")) + transpiler.RegisterFuncTranslation("GetPowerNetCapacity", createSensorFuncTranslation("@powerNetCapacity")) + transpiler.RegisterFuncTranslation("GetPowerNetIn", createSensorFuncTranslation("@powerNetIn")) + transpiler.RegisterFuncTranslation("GetPowerNetOut", createSensorFuncTranslation("@powerNetOut")) + transpiler.RegisterFuncTranslation("GetHeat", createSensorFuncTranslation("@heat")) + transpiler.RegisterFuncTranslation("GetEfficiency", createSensorFuncTranslation("@efficiency")) +} + +func registerArchtypeMethods() { + transpiler.RegisterFuncTranslation("IsEnabled", createSensorFuncTranslation("@enabled")) + transpiler.RegisterFuncTranslation("GetHealth", createSensorFuncTranslation("@health")) + transpiler.RegisterFuncTranslation("GetMaxHealth", createSensorFuncTranslation("@maxHealth")) + transpiler.RegisterFuncTranslation("GetTotalItems", createSensorFuncTranslation("@totalItems")) + transpiler.RegisterFuncTranslation("GetFirstItem", createSensorFuncTranslation("@firstItem")) + transpiler.RegisterFuncTranslation("GetItemCapacity", createSensorFuncTranslation("@itemCapacity")) + transpiler.RegisterFuncTranslation("GetTotalLiquids", createSensorFuncTranslation("@totalLiquids")) + transpiler.RegisterFuncTranslation("GetLiquidCapacity", createSensorFuncTranslation("@liquidCapacity")) + transpiler.RegisterFuncTranslation("GetTotalPower", createSensorFuncTranslation("@totalPower")) + transpiler.RegisterFuncTranslation("GetPowerCapacity", createSensorFuncTranslation("@powerCapacity")) + transpiler.RegisterFuncTranslation("GetPowerNetStored", createSensorFuncTranslation("@powerNetStored")) + transpiler.RegisterFuncTranslation("GetPowerNetCapacity", createSensorFuncTranslation("@powerNetCapacity")) + transpiler.RegisterFuncTranslation("GetPowerNetIn", createSensorFuncTranslation("@powerNetIn")) + transpiler.RegisterFuncTranslation("GetPowerNetOut", createSensorFuncTranslation("@powerNetOut")) + transpiler.RegisterFuncTranslation("GetAmmo", createSensorFuncTranslation("@ammo")) + transpiler.RegisterFuncTranslation("GetAmmoCapacity", createSensorFuncTranslation("@mmoCapacity")) + transpiler.RegisterFuncTranslation("IsShooting", createSensorFuncTranslation("@hooting")) + transpiler.RegisterFuncTranslation("GetHeat", createSensorFuncTranslation("@heat")) + transpiler.RegisterFuncTranslation("GetEfficiency", createSensorFuncTranslation("@Efficiency")) + transpiler.RegisterFuncTranslation("GetX", createSensorFuncTranslation("@x")) + transpiler.RegisterFuncTranslation("GetY", createSensorFuncTranslation("@y")) + transpiler.RegisterFuncTranslation("GetSize", createSensorFuncTranslation("@size")) + transpiler.RegisterFuncTranslation("GetRotation", createSensorFuncTranslation("@rotation")) + transpiler.RegisterFuncTranslation("GetShootX", createSensorFuncTranslation("@shootX")) + transpiler.RegisterFuncTranslation("GetShootY", createSensorFuncTranslation("@shootY")) + transpiler.RegisterFuncTranslation("GetShootPosition", createSensorFuncTranslation("@shootPosition")) + transpiler.RegisterFuncTranslation("GetControlled", createSensorFuncTranslation("@Controlled")) + transpiler.RegisterFuncTranslation("GetController", createSensorFuncTranslation("@Controller")) + transpiler.RegisterFuncTranslation("GetType", createSensorFuncTranslation("@type")) + transpiler.RegisterFuncTranslation("GetType", createSensorFuncTranslation("@type")) + transpiler.RegisterFuncTranslation("IsDead", createSensorFuncTranslation("@ead")) + transpiler.RegisterFuncTranslation("IsBoosting", createSensorFuncTranslation("@oosting")) + transpiler.RegisterFuncTranslation("GetFlag", createSensorFuncTranslation("@Flag")) + transpiler.RegisterFuncTranslation("GetTeam", createSensorFuncTranslation("@team")) + transpiler.RegisterFuncTranslation("GetRange", createSensorFuncTranslation("@range")) + transpiler.RegisterFuncTranslation("GetMineX", createSensorFuncTranslation("@mineX")) + transpiler.RegisterFuncTranslation("GetMineY", createSensorFuncTranslation("@mineY")) + transpiler.RegisterFuncTranslation("IsMining", createSensorFuncTranslation("@ining")) + transpiler.RegisterFuncTranslation("GetName", createSensorFuncTranslation("@name")) + transpiler.RegisterFuncTranslation("GetConfig", createSensorFuncTranslation("@configure")) + transpiler.RegisterFuncTranslation("GetPayloadType", createSensorFuncTranslation("@payloadType")) + transpiler.RegisterFuncTranslation("GetPayloadCount", createSensorFuncTranslation("@payloadCount")) +} + +func registerItemEnum() { + transpiler.RegisterSelector("m.ITCopper", m.ITCopper) + transpiler.RegisterSelector("m.ITLead", m.ITLead) + transpiler.RegisterSelector("m.ITMetaGlass", m.ITMetaGlass) + transpiler.RegisterSelector("m.ITGraphite", m.ITGraphite) + transpiler.RegisterSelector("m.ITSand", m.ITSand) + transpiler.RegisterSelector("m.ITCoal", m.ITCoal) + transpiler.RegisterSelector("m.ITTitanium", m.ITTitanium) + transpiler.RegisterSelector("m.ITThorium", m.ITThorium) + transpiler.RegisterSelector("m.ITScrap", m.ITScrap) + transpiler.RegisterSelector("m.ITSilicon", m.ITSilicon) + transpiler.RegisterSelector("m.ITPlastanium", m.ITPlastanium) + transpiler.RegisterSelector("m.ITPhaseFabric", m.ITPhaseFabric) + transpiler.RegisterSelector("m.ITSurgeAlloy", m.ITSurgeAlloy) + transpiler.RegisterSelector("m.ITSporePod", m.ITSporePod) + transpiler.RegisterSelector("m.ITBlastCompound", m.ITBlastCompound) + transpiler.RegisterSelector("m.ITPyratite", m.ITPyratite) + transpiler.RegisterSelector("m.ITNone", m.ITNone) + + transpiler.RegisterSelector("m.FWater", m.FWater) + transpiler.RegisterSelector("m.FOil", m.FOil) + transpiler.RegisterSelector("m.FSlag", m.FSlag) + transpiler.RegisterSelector("m.FCryofluid", m.FCryofluid) +} + +func registerUnitEnum() { + transpiler.RegisterSelector("m.UDagger", m.UDagger) + transpiler.RegisterSelector("m.UMace", m.UMace) + transpiler.RegisterSelector("m.UFortress", m.UFortress) + transpiler.RegisterSelector("m.UScepter", m.UScepter) + transpiler.RegisterSelector("m.UReign", m.UReign) + transpiler.RegisterSelector("m.UNova", m.UNova) + transpiler.RegisterSelector("m.UPulsar", m.UPulsar) + transpiler.RegisterSelector("m.UQuasar", m.UQuasar) + transpiler.RegisterSelector("m.UVela", m.UVela) + transpiler.RegisterSelector("m.UCorvus", m.UCorvus) + transpiler.RegisterSelector("m.UCrawler", m.UCrawler) + transpiler.RegisterSelector("m.UAtrax", m.UAtrax) + transpiler.RegisterSelector("m.USpiroct", m.USpiroct) + transpiler.RegisterSelector("m.UArkyid", m.UArkyid) + transpiler.RegisterSelector("m.UToxopid", m.UToxopid) + transpiler.RegisterSelector("m.UFlare", m.UFlare) + transpiler.RegisterSelector("m.UHorizon", m.UHorizon) + transpiler.RegisterSelector("m.UZenith", m.UZenith) + transpiler.RegisterSelector("m.UAntumbra", m.UAntumbra) + transpiler.RegisterSelector("m.UEclipse", m.UEclipse) + transpiler.RegisterSelector("m.UMono", m.UMono) + transpiler.RegisterSelector("m.UPoly", m.UPoly) + transpiler.RegisterSelector("m.UMega", m.UMega) + transpiler.RegisterSelector("m.UQuad", m.UQuad) + transpiler.RegisterSelector("m.UOct", m.UOct) + transpiler.RegisterSelector("m.URisso", m.URisso) + transpiler.RegisterSelector("m.UMinke", m.UMinke) + transpiler.RegisterSelector("m.UBryde", m.UBryde) + transpiler.RegisterSelector("m.USei", m.USei) + transpiler.RegisterSelector("m.UOmura", m.UOmura) + transpiler.RegisterSelector("m.UPlayerAlpha", m.UPlayerAlpha) + transpiler.RegisterSelector("m.UPlayerBeta", m.UPlayerBeta) + transpiler.RegisterSelector("m.UPlayerGamma", m.UPlayerGamma) +} + +func registerNativeVariables() { + transpiler.RegisterSelector("m.This", "@this") + transpiler.RegisterSelector("m.ThisX", "@thisx") + transpiler.RegisterSelector("m.ThisXf", "@thisx") + transpiler.RegisterSelector("m.ThisY", "@thisy") + transpiler.RegisterSelector("m.ThisYf", "@thisy") + transpiler.RegisterSelector("m.Ipt", "@ipt") + transpiler.RegisterSelector("m.Counter", "@counter") + transpiler.RegisterSelector("m.Links", "@links") + transpiler.RegisterSelector("m.CurUnit", "@unit") + transpiler.RegisterSelector("m.Time", "@time") + transpiler.RegisterSelector("m.Tick", "@tick") + transpiler.RegisterSelector("m.MapW", "@mapw") + transpiler.RegisterSelector("m.MapH", "@maph") +} + +func registerConstants() { transpiler.RegisterSelector("m.RTAny", m.RTAny) transpiler.RegisterSelector("m.RTEnemy", m.RTEnemy) transpiler.RegisterSelector("m.RTAlly", m.RTAlly) @@ -35,45 +191,10 @@ func init() { transpiler.RegisterSelector("m.BUnitModifier", m.BUnitModifier) transpiler.RegisterSelector("m.BExtinguisher", m.BExtinguisher) - transpiler.RegisterSelector("m.This", "@this") - transpiler.RegisterSelector("m.ThisX", "@thisx") - transpiler.RegisterSelector("m.ThisXf", "@thisx") - transpiler.RegisterSelector("m.ThisY", "@thisy") - transpiler.RegisterSelector("m.ThisYf", "@thisy") - transpiler.RegisterSelector("m.Ipt", "@ipt") - transpiler.RegisterSelector("m.Counter", "@counter") - transpiler.RegisterSelector("m.Links", "@links") - transpiler.RegisterSelector("m.CurUnit", "@unit") - transpiler.RegisterSelector("m.Time", "@time") - transpiler.RegisterSelector("m.Tick", "@tick") - transpiler.RegisterSelector("m.MapW", "@mapw") - transpiler.RegisterSelector("m.MapH", "@maph") - - // HealthC's attributes - transpiler.RegisterFuncTranslation("GetHealth", createSensorFuncTranslation("@health")) - transpiler.RegisterFuncTranslation("GetName", createSensorFuncTranslation("@name")) - transpiler.RegisterFuncTranslation("GetX", createSensorFuncTranslation("@x")) - transpiler.RegisterFuncTranslation("GetY", createSensorFuncTranslation("@y")) - - transpiler.RegisterFuncTranslation("GetTotalItems", createSensorFuncTranslation("@totalItems")) - transpiler.RegisterFuncTranslation("GetItemCapacity", createSensorFuncTranslation("@itemCapacity")) - transpiler.RegisterFuncTranslation("GetRotation", createSensorFuncTranslation("@rotation")) - transpiler.RegisterFuncTranslation("GetShootX", createSensorFuncTranslation("@shootX")) - transpiler.RegisterFuncTranslation("GetShootY", createSensorFuncTranslation("@shootY")) - transpiler.RegisterFuncTranslation("IsShooting", createSensorFuncTranslation("@shooting")) - - // Building's attributes - transpiler.RegisterFuncTranslation("GetTotalLiquids", createSensorFuncTranslation("@totalLiquids")) - transpiler.RegisterFuncTranslation("GetLiquidCapaticy", createSensorFuncTranslation("@liquidCapaticy")) - transpiler.RegisterFuncTranslation("GetTotalPower", createSensorFuncTranslation("@totalPower")) - transpiler.RegisterFuncTranslation("GetPowerCapaticy", createSensorFuncTranslation("@powerCapaticy")) - transpiler.RegisterFuncTranslation("GetPowerNetStored", createSensorFuncTranslation("@powerNetStored")) - transpiler.RegisterFuncTranslation("GetPowerNetCapacity", createSensorFuncTranslation("@powerNetCapacity")) - transpiler.RegisterFuncTranslation("GetPowerNetIn", createSensorFuncTranslation("@powerNetIn")) - transpiler.RegisterFuncTranslation("GetPowerNetOut", createSensorFuncTranslation("@powerNetOut")) - transpiler.RegisterFuncTranslation("GetHeat", createSensorFuncTranslation("@heat")) - transpiler.RegisterFuncTranslation("GetEfficiency", createSensorFuncTranslation("@efficiency")) - transpiler.RegisterFuncTranslation("IsEnabled", createSensorFuncTranslation("@enabled")) + transpiler.RegisterSelector("m.ctrlProcessor", strconv.Itoa(m.CtrlProcessor)) + transpiler.RegisterSelector("m.ctrlFormation", strconv.Itoa(m.CtrlFormation)) + transpiler.RegisterSelector("m.ctrlPlayer", strconv.Itoa(m.CtrlPlayer)) + transpiler.RegisterSelector("m.ctrlSelf", strconv.Itoa(m.CtrlSelf)) } func createSensorFuncTranslation(attribute string) transpiler.Translator { diff --git a/m/types.go b/m/types.go index eb2a539..12f51ef 100644 --- a/m/types.go +++ b/m/types.go @@ -1,111 +1,5 @@ package m -type RadarTarget = string - -const ( - // Target anything - RTAny = RadarTarget("any") - RTEnemy = RadarTarget("enemy") - RTAlly = RadarTarget("ally") - RTPlayer = RadarTarget("player") - RTAttacker = RadarTarget("attacker") - RTFlying = RadarTarget("flying") - RTBoss = RadarTarget("boss") - RTGround = RadarTarget("ground") -) - -type RadarSort = string - -const ( - RSDistance = RadarSort("distance") - RSHealth = RadarSort("health") - RSShield = RadarSort("shield") - RSArmor = RadarSort("armor") - RSMaxHealth = RadarSort("maxHealth") -) - -type Link = interface{} - -type HealthC = interface { - //Get the current health value of Unit or Building - GetHealth() int - - //Gets the name of Unit or Building - GetName() string - - //Get the X coordinate of Unit or Building - GetX() float64 - - //Get the Y coordinate of Unit or Building - GetY() float64 - - // Get the total number of items stored in Unit or Building - GetTotalItems() int - - // Get the capacity of items stored in Unit or Building - GetItemCapacity() int - - // Get the angle of view of Unit or Building - GetRotation() float64 - - // Get the X coordinate of Unit or Building's collimator - GetShootX() float64 - - // Get the Y coordinate of Unit or Building's collimator - GetShootY() float64 - - // Check whether Unit or Building fires - IsShooting() bool -} - -type Ranged = interface{} - -type Unit = interface { - HealthC - Ranged -} - -type Building = interface { - HealthC - Ranged - - // Gets total amount of liquids currently stored in the container(Building) - GetTotalLiquids() float64 - - // Gets the maximum amount of liquids stored in the container(Building) - GetLiquidCapaticy() float64 - - // In case of unbuffered consumers, this is the percentage (1.0f = 100%) of the demanded power which can be supplied. - // Blocks will work at a reduced efficiency if this is not equal to 1.0f. - // In case of buffered consumers, this is storage capacity. - GetTotalPower() float64 - - // In case of unbuffered consumers, this is the 0 - // n case of buffered consumers, this is the maximum storage capacity. - GetPowerCapaticy() float64 - - // Get the total power currently stored in the grid (Only machines connected to the grid) - GetPowerNetStored() float64 - - // Get the maximum power capacity in the grid (Only machines connected to the grid) - GetPowerNetCapacity() float64 - - // Get the input power of the current grid (Only machines connected to the grid) - GetPowerNetIn() float64 - - // Get the output power of the current grid (Only machines connected to the grid) - GetPowerNetOut() float64 - - // Get the heat from the machine (Just Thorium Reactor) - GetHeat() float64 - - //Get machine efficiency - GetEfficiency() float64 - - //Check whether the machine is available - IsEnabled() bool -} - var ( // A Building Object that represents the processor itself. // You can use this with sensor to find various properties about the processor. @@ -140,7 +34,7 @@ var ( Time = 0 // Represents the amount of ticks (60 ticks/second) since the map began. - Tick = float64(0) + Tick = 0 // Width of the map, in tiles. MapW = 0 @@ -148,20 +42,3 @@ var ( // Height of the map, in tiles. MapH = 0 ) - -type BlockFlag = string - -const ( - BCore = BlockFlag("core") - BStorage = BlockFlag("storage") - BGenerator = BlockFlag("generator") - BTurret = BlockFlag("turret") - BFactory = BlockFlag("factory") - BRepair = BlockFlag("repair") - BRally = BlockFlag("rally") - BBattery = BlockFlag("battery") - BResupply = BlockFlag("resupply") - BReactor = BlockFlag("reactor") - BUnitModifier = BlockFlag("unitModifier") - BExtinguisher = BlockFlag("extinguisher") -) diff --git a/m/unit.go b/m/unit.go index db4b319..b656065 100644 --- a/m/unit.go +++ b/m/unit.go @@ -3,7 +3,7 @@ package m // Load the next cached unit of the provided type into memory // // Will loop over once it reaches the end of the cache -func UnitBind(unitType string) { +func UnitBind(unitType UnitType) { } // Like Radar but originates from the cached units @@ -27,7 +27,7 @@ func UnitLocateOre(ore string) (x int, y int, found bool) { // If enemy is true, derelict blocks cannot be located // // Also locates blocks outside the range of the unit -func UnitLocateBuilding(buildingType BlockFlag, enemy bool) (x int, y int, found bool, building Building) { +func UnitLocateBuilding(buildingType BlockFlag, enemy bool) (x int, y int, found bool, building UnspecifiedBuilding) { return 0, 0, false, nil } @@ -41,6 +41,6 @@ func UnitLocateSpawn() (x int, y int, found bool, building Building) { // Locate a damaged building // // Also locates blocks outside the range of the unit -func UnitLocateDamaged() (x int, y int, found bool, building Building) { +func UnitLocateDamaged() (x int, y int, found bool, building UnspecifiedBuilding) { return 0, 0, false, nil } diff --git a/m/unit_control.go b/m/unit_control.go index 5938221..8b46ff4 100644 --- a/m/unit_control.go +++ b/m/unit_control.go @@ -39,19 +39,19 @@ func UnitTarget[A float, B float](x A, y B, shoot bool) { // Shoot with the cached unit at the predicted position of target unit // // If shoot parameter is false, it will cease firing -func UnitTargetP(target HealthC, shoot bool) { +func UnitTargetP(target Shootable, shoot bool) { } // Drops items into the provided building // // Will not drop more than provided amount -func UnitItemDrop[A integer](to Building, amount A) { +func UnitItemDrop[A integer](to HasInventory, amount A) { } // Takes the provided item type from the provided building // // Will not take more than provided amount -func UnitItemTake[A integer](from Building, item string, amount A) { +func UnitItemTake[A integer](from HasInventory, item ItemType, amount A) { } // Drops the current payload @@ -83,7 +83,7 @@ func UnitBuild[A float, B float, C integer, D integer](x A, y B, block string, r } // Retrieve the building and its type at the specified absolute position -func UnitGetBlock[A float, B float](x A, y B) (blockType string, building Building) { +func UnitGetBlock[A float, B float](x A, y B) (blockType string, building UnspecifiedBuilding) { return "", nil } From f91726eaf94e3ec6ffffd68511169ce41b0a6b67 Mon Sep 17 00:00:00 2001 From: Nils Brugger Date: Wed, 16 Mar 2022 22:24:25 +0100 Subject: [PATCH 2/4] tests: new type system test --- m/base.go | 4 +-- m/impl/types.go | 42 ++++++---------------------- tests/transpiler/type_test.go | 52 +++++++++++++++++++++++++++++++++++ 3 files changed, 62 insertions(+), 36 deletions(-) diff --git a/m/base.go b/m/base.go index e11c635..f0e58b9 100644 --- a/m/base.go +++ b/m/base.go @@ -28,11 +28,11 @@ func Radar(from Building, target1 RadarTarget, target2 RadarTarget, target3 Rada // Extract information indicated by sense from the provided block. //Use this only if the needed information is not available using The getters of the building itself //main purpose is to use generic "senses" or use things that are not mapped -func Sensor(block UnspecifiedBuilding, sense string) float64 { +func Sensor(block GameElement, sense string) float64 { return 0 } // String equivalent of Sensor -func SensorStr(block UnspecifiedBuilding, sense string) string { +func SensorStr(block GameElement, sense string) string { return "" } diff --git a/m/impl/types.go b/m/impl/types.go index dc11b43..f558519 100644 --- a/m/impl/types.go +++ b/m/impl/types.go @@ -13,31 +13,6 @@ func init() { registerItemEnum() registerNativeVariables() registerArchtypeMethods() - - // HealthC's attributes - transpiler.RegisterFuncTranslation("GetHealth", createSensorFuncTranslation("@health")) - transpiler.RegisterFuncTranslation("GetName", createSensorFuncTranslation("@name")) - transpiler.RegisterFuncTranslation("GetX", createSensorFuncTranslation("@x")) - transpiler.RegisterFuncTranslation("GetY", createSensorFuncTranslation("@y")) - - transpiler.RegisterFuncTranslation("GetTotalItems", createSensorFuncTranslation("@totalItems")) - transpiler.RegisterFuncTranslation("GetItemCapacity", createSensorFuncTranslation("@itemCapacity")) - transpiler.RegisterFuncTranslation("GetRotation", createSensorFuncTranslation("@rotation")) - transpiler.RegisterFuncTranslation("GetShootX", createSensorFuncTranslation("@shootX")) - transpiler.RegisterFuncTranslation("GetShootY", createSensorFuncTranslation("@shootY")) - transpiler.RegisterFuncTranslation("IsShooting", createSensorFuncTranslation("@shooting")) - - // Building's attributes - transpiler.RegisterFuncTranslation("GetTotalLiquids", createSensorFuncTranslation("@totalLiquids")) - transpiler.RegisterFuncTranslation("GetLiquidCapaticy", createSensorFuncTranslation("@liquidCapaticy")) - transpiler.RegisterFuncTranslation("GetTotalPower", createSensorFuncTranslation("@totalPower")) - transpiler.RegisterFuncTranslation("GetPowerCapaticy", createSensorFuncTranslation("@powerCapaticy")) - transpiler.RegisterFuncTranslation("GetPowerNetStored", createSensorFuncTranslation("@powerNetStored")) - transpiler.RegisterFuncTranslation("GetPowerNetCapacity", createSensorFuncTranslation("@powerNetCapacity")) - transpiler.RegisterFuncTranslation("GetPowerNetIn", createSensorFuncTranslation("@powerNetIn")) - transpiler.RegisterFuncTranslation("GetPowerNetOut", createSensorFuncTranslation("@powerNetOut")) - transpiler.RegisterFuncTranslation("GetHeat", createSensorFuncTranslation("@heat")) - transpiler.RegisterFuncTranslation("GetEfficiency", createSensorFuncTranslation("@efficiency")) } func registerArchtypeMethods() { @@ -56,8 +31,8 @@ func registerArchtypeMethods() { transpiler.RegisterFuncTranslation("GetPowerNetIn", createSensorFuncTranslation("@powerNetIn")) transpiler.RegisterFuncTranslation("GetPowerNetOut", createSensorFuncTranslation("@powerNetOut")) transpiler.RegisterFuncTranslation("GetAmmo", createSensorFuncTranslation("@ammo")) - transpiler.RegisterFuncTranslation("GetAmmoCapacity", createSensorFuncTranslation("@mmoCapacity")) - transpiler.RegisterFuncTranslation("IsShooting", createSensorFuncTranslation("@hooting")) + transpiler.RegisterFuncTranslation("GetAmmoCapacity", createSensorFuncTranslation("@ammoCapacity")) + transpiler.RegisterFuncTranslation("IsShooting", createSensorFuncTranslation("@shooting")) transpiler.RegisterFuncTranslation("GetHeat", createSensorFuncTranslation("@heat")) transpiler.RegisterFuncTranslation("GetEfficiency", createSensorFuncTranslation("@Efficiency")) transpiler.RegisterFuncTranslation("GetX", createSensorFuncTranslation("@x")) @@ -67,18 +42,17 @@ func registerArchtypeMethods() { transpiler.RegisterFuncTranslation("GetShootX", createSensorFuncTranslation("@shootX")) transpiler.RegisterFuncTranslation("GetShootY", createSensorFuncTranslation("@shootY")) transpiler.RegisterFuncTranslation("GetShootPosition", createSensorFuncTranslation("@shootPosition")) - transpiler.RegisterFuncTranslation("GetControlled", createSensorFuncTranslation("@Controlled")) - transpiler.RegisterFuncTranslation("GetController", createSensorFuncTranslation("@Controller")) - transpiler.RegisterFuncTranslation("GetType", createSensorFuncTranslation("@type")) + transpiler.RegisterFuncTranslation("GetControlled", createSensorFuncTranslation("@controlled")) + transpiler.RegisterFuncTranslation("GetController", createSensorFuncTranslation("@controller")) transpiler.RegisterFuncTranslation("GetType", createSensorFuncTranslation("@type")) - transpiler.RegisterFuncTranslation("IsDead", createSensorFuncTranslation("@ead")) - transpiler.RegisterFuncTranslation("IsBoosting", createSensorFuncTranslation("@oosting")) - transpiler.RegisterFuncTranslation("GetFlag", createSensorFuncTranslation("@Flag")) + transpiler.RegisterFuncTranslation("IsDead", createSensorFuncTranslation("@dead")) + transpiler.RegisterFuncTranslation("IsBoosting", createSensorFuncTranslation("@boosting")) + transpiler.RegisterFuncTranslation("GetFlag", createSensorFuncTranslation("@flag")) transpiler.RegisterFuncTranslation("GetTeam", createSensorFuncTranslation("@team")) transpiler.RegisterFuncTranslation("GetRange", createSensorFuncTranslation("@range")) transpiler.RegisterFuncTranslation("GetMineX", createSensorFuncTranslation("@mineX")) transpiler.RegisterFuncTranslation("GetMineY", createSensorFuncTranslation("@mineY")) - transpiler.RegisterFuncTranslation("IsMining", createSensorFuncTranslation("@ining")) + transpiler.RegisterFuncTranslation("IsMining", createSensorFuncTranslation("@mining")) transpiler.RegisterFuncTranslation("GetName", createSensorFuncTranslation("@name")) transpiler.RegisterFuncTranslation("GetConfig", createSensorFuncTranslation("@configure")) transpiler.RegisterFuncTranslation("GetPayloadType", createSensorFuncTranslation("@payloadType")) diff --git a/tests/transpiler/type_test.go b/tests/transpiler/type_test.go index 69a02c8..d0336f4 100644 --- a/tests/transpiler/type_test.go +++ b/tests/transpiler/type_test.go @@ -1,6 +1,7 @@ package transpiler import ( + "github.com/Vilsol/go-mlog/m" "testing" ) @@ -22,6 +23,57 @@ print(x)`, true, false), sensor _main_x _main_b @health print _main_x`, }, + { + name: "Access_Unit", + input: TestMain( + `flg := m.CurUnit.GetFlag() +print(flg)`, + true, false), + output: `sensor _main_flg @unit @flag +print _main_flg`, + }, + { + name: "Access_Turret", + input: TestMain( + `turret := m.GetTurret("duo1") +capa := turret.GetAmmoCapacity() +print(capa)`, + true, false), + output: `set _main_turret duo1 +sensor _main_capa _main_turret @ammoCapacity +print _main_capa`, + }, + { + name: "Bind_constant", + input: TestMain("m.UnitBind(m.UZenith)", true, false), + output: "ubind @zenith", + }, + { + name: "Compare_ItemTypes", + input: TestMain(`item := m.CurUnit.GetFirstItem() +if(item == m.ITSilicon) { + print("Test") +}`, true, false), + output: `sensor _main_item @unit @firstItem +set _main_0 @silicon +op equal _main_1 _main_item _main_0 +jump 5 equal _main_1 1 +jump 6 always +print "Test"`, + }, + { + name: "Compare_ItemTypes_old", + input: TestMain(`item := m.SensorStr(m.CurUnit, "@firstItem") +if item == m.Const("@silicon") { + print("Test") +}`, true, false), + output: `sensor _main_item @unit @firstItem +set _main_0 @silicon +op equal _main_1 _main_item _main_0 +jump 5 equal _main_1 1 +jump 6 always +print "Test"`, + }, } RunTests(t, tests) } From 6148028d93ad586980669e4eda51610d2a803aa2 Mon Sep 17 00:00:00 2001 From: Nils Brugger Date: Thu, 17 Mar 2022 18:53:55 +0100 Subject: [PATCH 3/4] docs: add example tests: new type system test --- examples/refuel_turret.go | 27 ++++++ m/archetypes.go | 164 ++++++++++++++++++++++++++++++++++ tests/transpiler/type_test.go | 1 - 3 files changed, 191 insertions(+), 1 deletion(-) create mode 100644 examples/refuel_turret.go create mode 100644 m/archetypes.go diff --git a/examples/refuel_turret.go b/examples/refuel_turret.go new file mode 100644 index 0000000..e11c902 --- /dev/null +++ b/examples/refuel_turret.go @@ -0,0 +1,27 @@ +package main + +import "github.com/Vilsol/go-mlog/m" + +//Requires a unit to be flagged +func main() { + flag := 2 + unitType := m.UMono + const turretName = "duo1" + ammoType := m.ITCopper + m.UnitBind(unitType) + if m.CurUnit.GetFlag() != flag { + return + } + item := m.CurUnit.GetFirstItem() + if item != ammoType { //go to core and pick up + coreX, coreY, _, core := m.UnitLocateBuilding(m.BCore, false) + m.UnitApproach(coreX, coreY, m.CurUnit.GetRange()-1) + unitCapacity := m.CurUnit.GetTotalItems() + m.UnitItemDrop(core, unitCapacity) //drop non-ammo item + m.UnitItemTake(core, ammoType, unitCapacity) //take ammo item + } else { // go to turret and refuel + turret := m.GetTurret(turretName) + m.UnitApproach(turret.GetX(), turret.GetY(), m.CurUnit.GetRange()-1) + m.UnitItemDrop(turret, m.CurUnit.GetTotalItems()) + } +} diff --git a/m/archetypes.go b/m/archetypes.go new file mode 100644 index 0000000..46d9de1 --- /dev/null +++ b/m/archetypes.go @@ -0,0 +1,164 @@ +package m + +type Turret interface { + Building + HasAmmo + HasInventory + HasRange + HasLiquid + HasPower + Controllable + // GetRotation the rotation at a scale of 0f..360f. 0 and 360 deg is when the turret looks at the very right side + GetRotation() int + // GetShootX the x cord of the targeted tile + GetShootX() int + // GetShootY the y cord of the targeted tile + GetShootY() int + // GetShootPosition the position of the targeted tile + GetShootPosition() HasPosition +} + +type Building interface { + HasPosition + HasHealth + Teamed + GameElement + // GetType the type of the block (e.g. "cyclone" "microprocessor") not compatible with BlockFlag + GetType() string + IsEnabled() bool +} +type Unit interface { + HasPosition + HasHealth + HasRange + Controllable + Teamed + GameElement + HasInventory + GetType() UnitType + // IsDead true if unit is not alive or invalid. PS: Why anuken why "dead" and not alive? + IsDead() bool + IsBoosting() bool + GetFlag() int +} +type MiningUnit interface { + Unit + CanMine +} +type CarrierUnit interface { + HasPayload + Unit +} +type UnspecifiedBuilding interface { + Building + HasPower + HasAmmo + HasInventory + HasLiquid + HasConfig +} +type GameElement interface{} + +type HasHealth interface { + // GetHealth Get the current health value of Unit or Building + GetHealth() int + GetMaxHealth() int +} +type HasInventory interface { + // GetTotalItems items in inventory + GetTotalItems() int + // GetFirstItem Type of the first item in the inventory + GetFirstItem() ItemType + // GetItemCapacity The maximum amount of items this unit/block can hold + GetItemCapacity() int +} +type HasLiquid interface { + // GetTotalLiquids liquids in internal storage + GetTotalLiquids() int + // GetLiquidCapacity The maximum amount of liquid this unit/block can hold + GetLiquidCapacity() int +} +type Shootable interface { + HasHealth + HasPosition +} +type HasPower interface { + // GetTotalPower In case of unbuffered consumers, this is the percentage (1.0f = 100%) of the demanded power which can be supplied. + // Blocks will work at a reduced efficiency if this is not equal to 1.0f. + // In case of buffered consumers, this is storage capacity. + GetTotalPower() int + // GetPowerCapacity The maximum amount of power this unit/block can hold + // In case of unbuffered consumers, this is the 0 + GetPowerCapacity() int + // GetPowerNetStored power stored in the connected power network + GetPowerNetStored() int64 + // GetPowerNetCapacity power the connected power network is able to store + GetPowerNetCapacity() int64 + // GetPowerNetIn power the connected power network produces + GetPowerNetIn() int64 + // GetPowerNetOut power the connected power network consumes + GetPowerNetOut() int64 +} +type HasAmmo interface { + // GetAmmo The amount of ammo (in shots not units) this unit/turret holds + GetAmmo() int + // GetAmmoCapacity the amount of shots this turret/unit can store + GetAmmoCapacity() int + IsShooting() bool +} +type HasHeat interface { + // GetHeat The heat this building has accumulated (in nuclear reactor for example) + GetHeat() +} +type Productive interface { + // GetEfficiency The effi ciency at which this item produces/consumes, scale of 0..1 efficiencies + //above 100% (1) are not measurable. A Thermal generator with 300% efficiency + //and one with 100% will both return 1 + //Note: Only seems to return the "electric efficiency" + GetEfficiency() float64 +} + +//TODO: sensor timescale -> no idea + +type HasPosition interface { + GetX() int + GetY() int + // GetSize the side length of the block measured in tiles + GetSize() int +} + +type Controllable interface { + GetControlled() Controller + // GetController name of the thing that commands this thing. "processor" or the unit itself + //TODO: dont offer this method or find a more typesave way + GetController() string +} +type Teamed interface { + // GetTeam The team the thing belongs to + GetTeam() int +} +type HasRange interface { + // GetRange To shoot and or detect who knows (TODO: better docs) + GetRange() float64 +} +type CanMine interface { + GetMineX() int + GetMineY() int + IsMining() bool +} +type HasName interface { + // GetName the exact name of the blockType or unit type. Use GetType preferably + GetName() string +} +type HasConfig interface { + // GetConfig The filtered item + GetConfig() string //mapped to "@configure" not "@config" nobody knows what latter one is +} + +// HasPayload TODO needs closer clarification from MLOG side +type HasPayload interface { + // GetPayloadType block or unit type this unit is carrying + GetPayloadType() string + // GetPayloadCount How many things this unit is carrying (not items) + GetPayloadCount() int +} diff --git a/tests/transpiler/type_test.go b/tests/transpiler/type_test.go index d0336f4..b05b44a 100644 --- a/tests/transpiler/type_test.go +++ b/tests/transpiler/type_test.go @@ -1,7 +1,6 @@ package transpiler import ( - "github.com/Vilsol/go-mlog/m" "testing" ) From b4ae4d25a2a9247b6a4fb04ec6357fc5792d7146 Mon Sep 17 00:00:00 2001 From: Nils Brugger Date: Thu, 24 Mar 2022 19:38:28 +0100 Subject: [PATCH 4/4] refactor: apply review --- examples/refuel_turret.go | 27 --------------------------- m/archetypes.go | 26 +++++++++++++------------- m/base.go | 2 +- m/impl/types.go | 8 ++++---- 4 files changed, 18 insertions(+), 45 deletions(-) delete mode 100644 examples/refuel_turret.go diff --git a/examples/refuel_turret.go b/examples/refuel_turret.go deleted file mode 100644 index e11c902..0000000 --- a/examples/refuel_turret.go +++ /dev/null @@ -1,27 +0,0 @@ -package main - -import "github.com/Vilsol/go-mlog/m" - -//Requires a unit to be flagged -func main() { - flag := 2 - unitType := m.UMono - const turretName = "duo1" - ammoType := m.ITCopper - m.UnitBind(unitType) - if m.CurUnit.GetFlag() != flag { - return - } - item := m.CurUnit.GetFirstItem() - if item != ammoType { //go to core and pick up - coreX, coreY, _, core := m.UnitLocateBuilding(m.BCore, false) - m.UnitApproach(coreX, coreY, m.CurUnit.GetRange()-1) - unitCapacity := m.CurUnit.GetTotalItems() - m.UnitItemDrop(core, unitCapacity) //drop non-ammo item - m.UnitItemTake(core, ammoType, unitCapacity) //take ammo item - } else { // go to turret and refuel - turret := m.GetTurret(turretName) - m.UnitApproach(turret.GetX(), turret.GetY(), m.CurUnit.GetRange()-1) - m.UnitItemDrop(turret, m.CurUnit.GetTotalItems()) - } -} diff --git a/m/archetypes.go b/m/archetypes.go index 46d9de1..68876ea 100644 --- a/m/archetypes.go +++ b/m/archetypes.go @@ -4,7 +4,7 @@ type Turret interface { Building HasAmmo HasInventory - HasRange + Ranged HasLiquid HasPower Controllable @@ -20,8 +20,8 @@ type Turret interface { type Building interface { HasPosition - HasHealth - Teamed + Healthc + Teamc GameElement // GetType the type of the block (e.g. "cyclone" "microprocessor") not compatible with BlockFlag GetType() string @@ -29,10 +29,10 @@ type Building interface { } type Unit interface { HasPosition - HasHealth - HasRange + Healthc + Ranged Controllable - Teamed + Teamc GameElement HasInventory GetType() UnitType @@ -46,7 +46,7 @@ type MiningUnit interface { CanMine } type CarrierUnit interface { - HasPayload + Payloadc Unit } type UnspecifiedBuilding interface { @@ -56,10 +56,11 @@ type UnspecifiedBuilding interface { HasInventory HasLiquid HasConfig + Ranged } type GameElement interface{} -type HasHealth interface { +type Healthc interface { // GetHealth Get the current health value of Unit or Building GetHealth() int GetMaxHealth() int @@ -79,7 +80,7 @@ type HasLiquid interface { GetLiquidCapacity() int } type Shootable interface { - HasHealth + Healthc HasPosition } type HasPower interface { @@ -133,11 +134,11 @@ type Controllable interface { //TODO: dont offer this method or find a more typesave way GetController() string } -type Teamed interface { +type Teamc interface { // GetTeam The team the thing belongs to GetTeam() int } -type HasRange interface { +type Ranged interface { // GetRange To shoot and or detect who knows (TODO: better docs) GetRange() float64 } @@ -155,8 +156,7 @@ type HasConfig interface { GetConfig() string //mapped to "@configure" not "@config" nobody knows what latter one is } -// HasPayload TODO needs closer clarification from MLOG side -type HasPayload interface { +type Payloadc interface { // GetPayloadType block or unit type this unit is carrying GetPayloadType() string // GetPayloadCount How many things this unit is carrying (not items) diff --git a/m/base.go b/m/base.go index f0e58b9..1337bfc 100644 --- a/m/base.go +++ b/m/base.go @@ -21,7 +21,7 @@ func GetLink[A integer](address A) UnspecifiedBuilding { // Retrieve a list of units that match specified conditions // // Conditions are combined using an `and` operation -func Radar(from Building, target1 RadarTarget, target2 RadarTarget, target3 RadarTarget, sortOrder bool, sort RadarSort) Unit { +func Radar(from Ranged, target1 RadarTarget, target2 RadarTarget, target3 RadarTarget, sortOrder bool, sort RadarSort) Unit { return nil } diff --git a/m/impl/types.go b/m/impl/types.go index f558519..d72ae48 100644 --- a/m/impl/types.go +++ b/m/impl/types.go @@ -165,10 +165,10 @@ func registerConstants() { transpiler.RegisterSelector("m.BUnitModifier", m.BUnitModifier) transpiler.RegisterSelector("m.BExtinguisher", m.BExtinguisher) - transpiler.RegisterSelector("m.ctrlProcessor", strconv.Itoa(m.CtrlProcessor)) - transpiler.RegisterSelector("m.ctrlFormation", strconv.Itoa(m.CtrlFormation)) - transpiler.RegisterSelector("m.ctrlPlayer", strconv.Itoa(m.CtrlPlayer)) - transpiler.RegisterSelector("m.ctrlSelf", strconv.Itoa(m.CtrlSelf)) + transpiler.RegisterSelector("m.CtrlProcessor", strconv.Itoa(m.CtrlProcessor)) + transpiler.RegisterSelector("m.CtrlFormation", strconv.Itoa(m.CtrlFormation)) + transpiler.RegisterSelector("m.CtrlPlayer", strconv.Itoa(m.CtrlPlayer)) + transpiler.RegisterSelector("m.CtrlSelf", strconv.Itoa(m.CtrlSelf)) } func createSensorFuncTranslation(attribute string) transpiler.Translator {