Skip to content

Commit

Permalink
Add more sub-selector fun, optimize storage structure (#7)
Browse files Browse the repository at this point in the history
* Add more sub-selector fun, optimize storage structure

* Optimize sub-selector fun name

* Add function comment
  • Loading branch information
wu452148993 committed Apr 8, 2021
1 parent b3e5eb5 commit 95a1670
Show file tree
Hide file tree
Showing 5 changed files with 160 additions and 43 deletions.
27 changes: 0 additions & 27 deletions m/impl/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
},
}
}
51 changes: 51 additions & 0 deletions m/impl/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package impl
import (
"github.com/Vilsol/go-mlog/m"
"github.com/Vilsol/go-mlog/transpiler"
"strings"
)

func init() {
Expand Down Expand Up @@ -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
},
}
}
61 changes: 61 additions & 0 deletions m/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}
Expand All @@ -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 (
Expand Down
16 changes: 0 additions & 16 deletions tests/base_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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`,
},
}
Expand Down
48 changes: 48 additions & 0 deletions tests/type_test.go
Original file line number Diff line number Diff line change
@@ -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"))
})
}
}

0 comments on commit 95a1670

Please sign in to comment.