Skip to content

Commit

Permalink
Optimize call and add “Processor Variables and Constants” support (#3)
Browse files Browse the repository at this point in the history
* optimize sensor call

* add Processor Variables and Constants support

* add test case

* modify createSensorFuncTranslation to private function
  • Loading branch information
wu452148993 authored Apr 5, 2021
1 parent 932ca33 commit 814f181
Show file tree
Hide file tree
Showing 6 changed files with 102 additions and 11 deletions.
32 changes: 30 additions & 2 deletions m/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,11 @@ func init() {
}, nil
},
})

transpiler.RegisterFuncTranslation("GetHealth", createSensorFuncTranslation("@health"))
transpiler.RegisterFuncTranslation("GetName", createSensorFuncTranslation("@name"))
transpiler.RegisterFuncTranslation("GetX", createSensorFuncTranslation("@x"))
transpiler.RegisterFuncTranslation("GetY", createSensorFuncTranslation("@y"))
}

// Read a float64 value from memory at specified position
Expand Down Expand Up @@ -172,11 +177,34 @@ func GetLink(address int) Link {
// Retrieve a list of units that match specified conditions
//
// Conditions are combined using an `and` operation
func Radar(from string, 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 interface{}, sense string) float64 {
func Sensor(block HealthC, sense string) float64 {
return 0
}

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
},
}
}
48 changes: 45 additions & 3 deletions m/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,18 @@ func init() {
transpiler.RegisterSelector("m.BReactor", BReactor)
transpiler.RegisterSelector("m.BUnitModifier", BUnitModifier)
transpiler.RegisterSelector("m.BExtinguisher", BExtinguisher)

transpiler.RegisterSelector("m.This", ThisVar)
transpiler.RegisterSelector("m.ThisX", ThisXVar)
transpiler.RegisterSelector("m.ThisY", ThisYVar)
transpiler.RegisterSelector("m.Ipt", IptVar)
transpiler.RegisterSelector("m.Counter", CounterVar)
transpiler.RegisterSelector("m.Links", LinksVar)
transpiler.RegisterSelector("m.CurUnit", CurUnitVar)
transpiler.RegisterSelector("m.Time", TimeVar)
transpiler.RegisterSelector("m.Tick", TickVar)
transpiler.RegisterSelector("m.MapW", MapWVar)
transpiler.RegisterSelector("m.MapH", MapHVar)
}

type RadarTarget = string
Expand Down Expand Up @@ -58,14 +70,44 @@ const (

type Link = interface{}

type Unit = interface{}
type HealthC = interface {
GetHealth() int
GetName() string
GetX() float64
GetY() float64
}

type HealthC = interface{}
type Unit = interface {
HealthC
}

type Building = struct {
type Building = interface {
HealthC
}

var (
This Building
ThisX float64
ThisY float64
CurUnit Unit
)

type SpecialVar = string

const (
ThisVar = SpecialVar("@this")
ThisXVar = SpecialVar("@thisx")
ThisYVar = SpecialVar("@thisy")
IptVar = SpecialVar("@ipt")
CounterVar = SpecialVar("@counter")
LinksVar = SpecialVar("@links")
CurUnitVar = SpecialVar("@unit")
TimeVar = SpecialVar("@time")
TickVar = SpecialVar("@tick")
MapWVar = SpecialVar("@mapw")
MapHVar = SpecialVar("@maph")
)

type BlockFlag = string

const (
Expand Down
6 changes: 3 additions & 3 deletions m/unit.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,19 +177,19 @@ func UnitLocateOre(ore string) (x int, y int, found bool) {
//
// Also locates blocks outside the range of the unit
func UnitLocateBuilding(buildingType BlockFlag, enemy bool) (x int, y int, found bool, building Building) {
return 0, 0, false, Building{}
return 0, 0, false, nil
}

// Locate the enemy spawn
//
// Also locates blocks outside the range of the unit
func UnitLocateSpawn() (x int, y int, found bool, building Building) {
return 0, 0, false, Building{}
return 0, 0, false, nil
}

// Locate a damaged building
//
// Also locates blocks outside the range of the unit
func UnitLocateDamaged() (x int, y int, found bool, building Building) {
return 0, 0, false, Building{}
return 0, 0, false, nil
}
2 changes: 1 addition & 1 deletion m/unit_control.go
Original file line number Diff line number Diff line change
Expand Up @@ -396,7 +396,7 @@ func UnitBuild(x float64, y float64, block string, rotation int, config int) {

// Retrieve the building and its type at the specified absolute position
func UnitGetBlock(x float64, y float64) (blockType string, building Building) {
return "", Building{}
return "", nil
}

// Checks whether there is a unit within the specified radius around the provided absolute position
Expand Down
10 changes: 10 additions & 0 deletions tests/base_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,16 @@ func TestBase(t *testing.T) {
input: TestMain(`x := m.Sensor("A", "B")`),
output: `sensor _main_x A B`,
},
{
name: "Sensor_GetHealth",
input: TestMain(`x := y.GetHealth()`),
output: `sensor _main_x _main_y @health`,
},
{
name: "Radar_This",
input: TestMain(`x := m.Radar(m.This, m.RTAlly, m.RTEnemy, m.RTBoss, 0, m.RSArmor)`),
output: `radar ally enemy boss armor @this 0 _main_x`,
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
Expand Down
15 changes: 13 additions & 2 deletions transpiler/expression.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,13 +86,15 @@ func selectorExprToMLOG(ctx context.Context, ident Resolvable, selectorExpr *ast
func callExprToMLOG(ctx context.Context, callExpr *ast.CallExpr, ident []Resolvable) ([]MLOGStatement, error) {
results := make([]MLOGStatement, 0)

var funcName string
var funcName, exprName, selName string
switch funType := callExpr.Fun.(type) {
case *ast.Ident:
funcName = funType.Name
break
case *ast.SelectorExpr:
funcName = funType.X.(*ast.Ident).Name + "." + funType.Sel.Name
exprName = funType.X.(*ast.Ident).Name
selName = funType.Sel.Name
funcName = exprName + "." + selName
break
default:
return nil, Err(ctx, fmt.Sprintf("unknown call expression: %T", callExpr.Fun))
Expand All @@ -110,6 +112,15 @@ func callExprToMLOG(ctx context.Context, callExpr *ast.CallExpr, ident []Resolva
Variables: ident,
SourcePos: callExpr,
})
} else if translatedFunc, ok := funcTranslations[selName]; ok {
results = append(results, &MLOGFunc{
Function: translatedFunc,
Arguments: []Resolvable{
&NormalVariable{Name: exprName},
},
Variables: ident,
SourcePos: callExpr,
})
} else {
results = append(results, &MLOGCustomFunction{
Arguments: callExpr.Args,
Expand Down

0 comments on commit 814f181

Please sign in to comment.