Skip to content

Commit

Permalink
修复#17
Browse files Browse the repository at this point in the history
  • Loading branch information
magicsea committed Nov 15, 2024
1 parent 96a74ce commit 4012d7c
Show file tree
Hide file tree
Showing 4 changed files with 524 additions and 2 deletions.
15 changes: 13 additions & 2 deletions core/BehaviorTree.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ func (this *BehaviorTree) SetDebug(debug interface{}) {
this.debug = debug
}

func (this *BehaviorTree) GetRoot() IBaseNode {
func (this *BehaviorTree) GetRoot() IBaseNode {
return this.root
}

Expand Down Expand Up @@ -295,10 +295,21 @@ func (this *BehaviorTree) Tick(target interface{}, blackboard *Blackboard) b3.St
var currOpenNodes []IBaseNode
currOpenNodes = append(currOpenNodes, tick._openNodes...)

var same bool
l := len(lastOpenNodes)
if l == len(currOpenNodes) {
if l == 0 || lastOpenNodes[l-1] == currOpenNodes[l-1] {
same = true
}
}
if same {
return state
}

// does not close if it is still open in this tick
var start = 0
for i := 0; i < b3.MinInt(len(lastOpenNodes), len(currOpenNodes)); i++ {
start = i + 1
start = i
if lastOpenNodes[i] != currOpenNodes[i] {
break
}
Expand Down
119 changes: 119 additions & 0 deletions examples/testwork/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
# 修正第一个记忆节点无法关闭示例
关联问题https://github.com/magicsea/behavior3go/issues/17
追踪原版的老bug,记忆节点链关闭的第一个会执行不了onclose,导致memseq节点会错误的记录runningChild不清理。
错误表现为,因为runningChild没重置,直接从Wait0-2跳入Wait1-2。
### 修复方法
start=i+1改start,增加same判断是否调用链修改了(不判断会导致一直在running节点)。

### 修复前后日志
重现需开打Wait节点的print日志。
- 修复前日志
```
logtest: <nil> job 0-0
wait: Wait0-1 <milliseconds>ms <nil> => 0
wait: Wait0-1 <milliseconds>ms <nil> => 108
wait: Wait0-1 <milliseconds>ms <nil> => 216
wait: Wait0-1 <milliseconds>ms <nil> => 325
wait: Wait0-1 <milliseconds>ms <nil> => 434
wait: Wait0-1 <milliseconds>ms <nil> => 542
wait: Wait0-1 <milliseconds>ms <nil> => 652
wait: Wait0-1 <milliseconds>ms <nil> => 759
wait: Wait0-1 <milliseconds>ms <nil> => 869
wait: Wait0-1 <milliseconds>ms <nil> => 978
wait: Wait0-1 <milliseconds>ms <nil> => 1087
logtest: <nil> job 0-1
logtest: <nil> job 0-2
wait: Wait0-2 <milliseconds>ms <nil> => 0
wait: Wait0-2 <milliseconds>ms <nil> => 109
logtest: <nil> job 1-0
wait: Wait1-1 <milliseconds>ms <nil> => 0
wait: Wait1-1 <milliseconds>ms <nil> => 111
wait: Wait1-1 <milliseconds>ms <nil> => 221
wait: Wait1-1 <milliseconds>ms <nil> => 331
wait: Wait1-1 <milliseconds>ms <nil> => 442
wait: Wait1-1 <milliseconds>ms <nil> => 551
wait: Wait1-1 <milliseconds>ms <nil> => 662
wait: Wait1-1 <milliseconds>ms <nil> => 771
wait: Wait1-1 <milliseconds>ms <nil> => 880
wait: Wait1-1 <milliseconds>ms <nil> => 991
wait: Wait1-1 <milliseconds>ms <nil> => 1098
logtest: <nil> job 1-1
logtest: <nil> job 1-2
wait: Wait1-2 <milliseconds>ms <nil> => 0
logtest: <nil> job 0-0
wait: Wait0-1 <milliseconds>ms <nil> => 0
wait: Wait0-1 <milliseconds>ms <nil> => 109
wait: Wait0-1 <milliseconds>ms <nil> => 219
wait: Wait0-1 <milliseconds>ms <nil> => 329
wait: Wait0-1 <milliseconds>ms <nil> => 438
wait: Wait0-1 <milliseconds>ms <nil> => 545
wait: Wait0-1 <milliseconds>ms <nil> => 652
wait: Wait0-1 <milliseconds>ms <nil> => 760
wait: Wait0-1 <milliseconds>ms <nil> => 869
wait: Wait0-1 <milliseconds>ms <nil> => 976
wait: Wait0-1 <milliseconds>ms <nil> => 1084
logtest: <nil> job 0-1
logtest: <nil> job 0-2
wait: Wait0-2 <milliseconds>ms <nil> => 0
wait: Wait0-2 <milliseconds>ms <nil> => 108
wait: Wait1-2 <milliseconds>ms <nil> => 0<--------------------错误行,应该进入:job 1-0
wait: Wait1-2 <milliseconds>ms <nil> => 107
logtest: <nil> job 1-0
wait: Wait1-1 <milliseconds>ms <nil> => 0
wait: Wait1-1 <milliseconds>ms <nil> => 108
wait: Wait1-1 <milliseconds>ms <nil> => 216
```

- 修复后日志
```
logtest: <nil> job 0-0
wait: Wait0-1 <milliseconds>ms <nil> => 0
wait: Wait0-1 <milliseconds>ms <nil> => 110
wait: Wait0-1 <milliseconds>ms <nil> => 217
wait: Wait0-1 <milliseconds>ms <nil> => 327
wait: Wait0-1 <milliseconds>ms <nil> => 437
wait: Wait0-1 <milliseconds>ms <nil> => 545
wait: Wait0-1 <milliseconds>ms <nil> => 653
wait: Wait0-1 <milliseconds>ms <nil> => 763
wait: Wait0-1 <milliseconds>ms <nil> => 872
wait: Wait0-1 <milliseconds>ms <nil> => 980
wait: Wait0-1 <milliseconds>ms <nil> => 1088
logtest: <nil> job 0-1
logtest: <nil> job 0-2
wait: Wait0-2 <milliseconds>ms <nil> => 0
wait: Wait0-2 <milliseconds>ms <nil> => 109
logtest: <nil> job 1-0
wait: Wait1-1 <milliseconds>ms <nil> => 0
wait: Wait1-1 <milliseconds>ms <nil> => 109
wait: Wait1-1 <milliseconds>ms <nil> => 219
wait: Wait1-1 <milliseconds>ms <nil> => 328
wait: Wait1-1 <milliseconds>ms <nil> => 436
wait: Wait1-1 <milliseconds>ms <nil> => 545
wait: Wait1-1 <milliseconds>ms <nil> => 652
wait: Wait1-1 <milliseconds>ms <nil> => 759
wait: Wait1-1 <milliseconds>ms <nil> => 868
wait: Wait1-1 <milliseconds>ms <nil> => 976
wait: Wait1-1 <milliseconds>ms <nil> => 1085
logtest: <nil> job 1-1
logtest: <nil> job 1-2
wait: Wait1-2 <milliseconds>ms <nil> => 0
logtest: <nil> job 0-0
wait: Wait0-1 <milliseconds>ms <nil> => 753
wait: Wait0-1 <milliseconds>ms <nil> => 861
wait: Wait0-1 <milliseconds>ms <nil> => 967
wait: Wait0-1 <milliseconds>ms <nil> => 1075
logtest: <nil> job 0-1
logtest: <nil> job 0-2
wait: Wait0-2 <milliseconds>ms <nil> => 0
wait: Wait0-2 <milliseconds>ms <nil> => 109
<---------------------------------------------------------------------正确切换到1-0
logtest: <nil> job 1-0
wait: Wait1-1 <milliseconds>ms <nil> => 0
wait: Wait1-1 <milliseconds>ms <nil> => 108
wait: Wait1-1 <milliseconds>ms <nil> => 217
wait: Wait1-1 <milliseconds>ms <nil> => 325
wait: Wait1-1 <milliseconds>ms <nil> => 433
```
63 changes: 63 additions & 0 deletions examples/testwork/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
从原生工程文件加载
*/
package main

import (
"fmt"
b3 "github.com/magicsea/behavior3go"
. "github.com/magicsea/behavior3go/config"
. "github.com/magicsea/behavior3go/core"
. "github.com/magicsea/behavior3go/examples/share"
. "github.com/magicsea/behavior3go/loader"
"sync"
"time"
)

// 所有的树管理
var mapTreesByID = sync.Map{}
var maps = b3.NewRegisterStructMaps()

func init() {
//自定义节点注册
maps.Register("Log", new(LogTest))
maps.Register("SetValue", new(SetValue))
maps.Register("IsValue", new(IsValue))

//获取子树的方法
SetSubTreeLoadFunc(func(id string) *BehaviorTree {
//println("==>load subtree:",id)
t, ok := mapTreesByID.Load(id)
if ok {
return t.(*BehaviorTree)
}
return nil
})
}

func main() {
projectConfig, ok := LoadRawProjectCfg("testwork.b3")
if !ok {
fmt.Println("LoadRawProjectCfg err")
return
}

var firstTree *BehaviorTree
//载入
for _, v := range projectConfig.Data.Trees {
tree := CreateBevTreeFromConfig(&v, maps)
tree.Print()
mapTreesByID.Store(v.ID, tree)
if firstTree == nil {
firstTree = tree
}
}
time.Sleep(time.Second)
//输入板
board := NewBlackboard()
//循环每一帧
for i := 0; i < 40; i++ {
firstTree.Tick(i, board)
time.Sleep(time.Millisecond * 100)
}
}
Loading

0 comments on commit 4012d7c

Please sign in to comment.