Skip to content

Commit

Permalink
store/unit: On PlaceTower recalculate all units path in separated gor…
Browse files Browse the repository at this point in the history
…outines

So it could be a bit more faster than sychronously
  • Loading branch information
xescugc committed Jan 20, 2024
1 parent a9bcb90 commit 9534f16
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 9 deletions.
Binary file modified server/assets/wasm/maze-wars.wasm
Binary file not shown.
23 changes: 14 additions & 9 deletions store/units.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,21 +152,26 @@ func (us *Units) Reduce(state, a interface{}) interface{} {
if !p.CanPlaceTower(act.PlaceTower.Type) {
break
}
tws := make([]utils.Object, 0, 0)
for _, t := range ts.Towers {
if t.LineID == p.LineID {
tws = append(tws, t.Object)
}
}
var wg sync.WaitGroup
for _, u := range ustate.Units {
// Only need to recalculate path for each unit when the placed tower
// is on the same LineID as the unit
if u.CurrentLineID == p.LineID {
tws := make([]utils.Object, 0, 0)
for _, t := range ts.Towers {
if t.LineID == u.CurrentLineID {
tws = append(tws, t.Object)
}
}

u.Path = us.Astar(us.store.Map, u.CurrentLineID, u.MovingObject, tws)
u.HashPath = utils.HashSteps(u.Path)
wg.Add(1)
go func(u *Unit) {
u.Path = us.Astar(us.store.Map, u.CurrentLineID, u.MovingObject, tws)
u.HashPath = utils.HashSteps(u.Path)
wg.Done()
}(u)
}
}
wg.Wait()
case action.RemoveTower:
us.mxUnits.Lock()
defer us.mxUnits.Unlock()
Expand Down

0 comments on commit 9534f16

Please sign in to comment.