From 95a16708a4769527feebabc642c8d05b25dee429 Mon Sep 17 00:00:00 2001 From: wu452148993 <38494302+wu452148993@users.noreply.github.com> Date: Fri, 9 Apr 2021 02:39:59 +0800 Subject: [PATCH] Add more sub-selector fun, optimize storage structure (#7) * Add more sub-selector fun, optimize storage structure * Optimize sub-selector fun name * Add function comment --- m/impl/base.go | 27 -------------------- m/impl/types.go | 51 ++++++++++++++++++++++++++++++++++++++ m/types.go | 61 ++++++++++++++++++++++++++++++++++++++++++++++ tests/base_test.go | 16 ------------ tests/type_test.go | 48 ++++++++++++++++++++++++++++++++++++ 5 files changed, 160 insertions(+), 43 deletions(-) create mode 100644 tests/type_test.go diff --git a/m/impl/base.go b/m/impl/base.go index 4135c3d..bb5ae19 100644 --- a/m/impl/base.go +++ b/m/impl/base.go @@ -141,32 +141,5 @@ func init() { }, nil }, }) - - transpiler.RegisterFuncTranslation("GetHealth", createSensorFuncTranslation("@health")) - transpiler.RegisterFuncTranslation("GetName", createSensorFuncTranslation("@name")) - transpiler.RegisterFuncTranslation("GetX", createSensorFuncTranslation("@x")) - transpiler.RegisterFuncTranslation("GetY", createSensorFuncTranslation("@y")) } -func createSensorFuncTranslation(attribute string) transpiler.Translator { - return transpiler.Translator{ - Count: func(args []transpiler.Resolvable, vars []transpiler.Resolvable) int { - return 1 - }, - Variables: 1, - Translate: func(args []transpiler.Resolvable, vars []transpiler.Resolvable) ([]transpiler.MLOGStatement, error) { - return []transpiler.MLOGStatement{ - &transpiler.MLOG{ - Statement: [][]transpiler.Resolvable{ - { - &transpiler.Value{Value: "sensor"}, - vars[0], - &transpiler.Value{Value: strings.Trim(args[0].GetValue(), "\"")}, - &transpiler.Value{Value: attribute}, - }, - }, - }, - }, nil - }, - } -} diff --git a/m/impl/types.go b/m/impl/types.go index a56a409..f145398 100644 --- a/m/impl/types.go +++ b/m/impl/types.go @@ -3,6 +3,7 @@ package impl import ( "github.com/Vilsol/go-mlog/m" "github.com/Vilsol/go-mlog/transpiler" + "strings" ) func init() { @@ -47,4 +48,54 @@ func init() { 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")) + +} + +func createSensorFuncTranslation(attribute string) transpiler.Translator { + return transpiler.Translator{ + Count: func(args []transpiler.Resolvable, vars []transpiler.Resolvable) int { + return 1 + }, + Variables: 1, + Translate: func(args []transpiler.Resolvable, vars []transpiler.Resolvable) ([]transpiler.MLOGStatement, error) { + return []transpiler.MLOGStatement{ + &transpiler.MLOG{ + Statement: [][]transpiler.Resolvable{ + { + &transpiler.Value{Value: "sensor"}, + vars[0], + &transpiler.Value{Value: strings.Trim(args[0].GetValue(), "\"")}, + &transpiler.Value{Value: attribute}, + }, + }, + }, + }, nil + }, + } } diff --git a/m/types.go b/m/types.go index 857af5a..ba133fd 100644 --- a/m/types.go +++ b/m/types.go @@ -27,10 +27,35 @@ const ( 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{} @@ -43,6 +68,42 @@ type Unit = interface { 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 ( diff --git a/tests/base_test.go b/tests/base_test.go index 3d68ab2..db9b304 100644 --- a/tests/base_test.go +++ b/tests/base_test.go @@ -51,22 +51,6 @@ x := m.Sensor(b, "B") print(x)`, true, false), output: `ulocate damaged core true @copper @_ @_ @_ _main_b sensor _main_x _main_b B -print _main_x`, - }, - { - name: "Sensor_GetHealth", - input: TestMain(`_, _, _, b := m.UnitLocateDamaged() -x := b.GetHealth() -print(x)`, true, false), - output: `ulocate damaged core true @copper @_ @_ @_ _main_b -sensor _main_x _main_b @health -print _main_x`, - }, - { - name: "Radar_This", - input: TestMain(`x := m.Radar(m.This, m.RTAlly, m.RTEnemy, m.RTBoss, false, m.RSArmor) -print(x)`, true, false), - output: `radar ally enemy boss armor @this false _main_x print _main_x`, }, } diff --git a/tests/type_test.go b/tests/type_test.go new file mode 100644 index 0000000..953d863 --- /dev/null +++ b/tests/type_test.go @@ -0,0 +1,48 @@ +package tests + +import ( + "github.com/Vilsol/go-mlog/transpiler" + "github.com/stretchr/testify/assert" + "strings" + "testing" +) + +func TestType(t *testing.T) { + tests := []struct { + name string + input string + output string + }{ + { + name: "Radar_This", + input: TestMain(`x := m.Radar(m.This, m.RTAlly, m.RTEnemy, m.RTBoss, false, m.RSArmor) + print(x)`, true, false), + output: `radar ally enemy boss armor @this false _main_x +print _main_x`, + }, + { + name: "Sensor_GetHealth", + input: TestMain(`_, _, _, b := m.UnitLocateDamaged() +x := b.GetHealth() +print(x)`, true, false), + output: `ulocate damaged core true @copper @_ @_ @_ _main_b +sensor _main_x _main_b @health +print _main_x`, + }, + } + for _, test := range tests { + t.Run(test.name, func(t *testing.T) { + mlog, err := transpiler.GolangToMLOG(test.input, transpiler.Options{ + NoStartup: true, + }) + + if err != nil { + t.Error(err) + return + } + + test.output = test.output + "\nend" + assert.Equal(t, test.output, strings.Trim(mlog, "\n")) + }) + } +}