behavior_tree 0.1.9 → 0.1.10

Sign up to get free protection for your applications and to get access to all the features.
@@ -62,4 +62,12 @@ module BehaviorTree
62
62
  super "Cannot register node '#{node_type}', it already exists."
63
63
  end
64
64
  end
65
+
66
+ # Exception for when the returned value in should_tick? is not a boolean.
67
+ class ShouldTickNotBooleanError < StandardError
68
+ def initialize(returned_value)
69
+ err = "Return of 'should_tick?' must be a boolean value. It returned #{returned_value} (#{returned_value.class})."
70
+ super err
71
+ end
72
+ end
65
73
  end
@@ -4,15 +4,17 @@ module BehaviorTree
4
4
  # A node (abstract class).
5
5
  class NodeBase
6
6
  include TreeStructure::Algorithms
7
- attr_reader :status, :tick_count, :ticks_running, :arbitrary_storage
7
+ attr_reader :status, :prev_status, :tick_count, :ticks_running, :arbitrary_storage
8
8
 
9
9
  def initialize
10
10
  @status = NodeStatus.new NodeStatus::SUCCESS
11
+ @prev_status = NodeStatus.new NodeStatus::SUCCESS
12
+
11
13
  @tick_count = 0
12
14
  @ticks_running = 0
13
15
  @context = nil
14
16
 
15
- @status.subscribe { |prev, curr| on_status_change(prev, curr) }
17
+ @status.subscribe { |prev| __on_status_change__(prev) }
16
18
 
17
19
  @arbitrary_storage = {}
18
20
  end
@@ -32,9 +34,10 @@ module BehaviorTree
32
34
 
33
35
  def tick!
34
36
  @tick_count += 1
35
- @tick_prevented = !should_tick?
37
+ should_tick = should_tick?
38
+ raise ShouldTickNotBooleanError, should_tick unless [true, false].include? should_tick
36
39
 
37
- unless @tick_prevented
40
+ unless (@tick_prevented = !should_tick)
38
41
  status.running!
39
42
  on_tick
40
43
  @ticks_running += 1
@@ -47,6 +50,9 @@ module BehaviorTree
47
50
  #
48
51
  # This error can be replicated by pasting a valid status object in IRB, such as by doing:
49
52
  # BehaviorTree.const_get(:NodeStatus).new(:__running__) # Valid, but IRB crashes.
53
+ #
54
+ # Ruby 3.0.0 -> Crash
55
+ # Ruby 2.7.0 -> OK
50
56
  nil
51
57
  end
52
58
 
@@ -98,18 +104,26 @@ module BehaviorTree
98
104
 
99
105
  def on_finished_running; end
100
106
 
107
+ def on_status_change; end
108
+
101
109
  private
102
110
 
103
- # Always prev != curr (states that are set to the same aren't notified).
111
+ # Always prev != current (states that are set to the same aren't notified).
104
112
  # The fact that it's set to 0 means that setting to running must be done before
105
113
  # increasing the counts (so that @ticks_running becomes 1 after the whole tick lifecycle).
106
- def on_status_change(prev, curr)
107
- if prev == NodeStatus::RUNNING
114
+ # This is the non custom on_status_change. Users are expected to override the one without
115
+ # double underscore if they want to execute custom logic.
116
+ def __on_status_change__(prev)
117
+ prev_status.set(prev)
118
+
119
+ if prev_status.running?
108
120
  on_finished_running
109
- elsif curr == NodeStatus::RUNNING
121
+ elsif status.running?
110
122
  @ticks_running = 0
111
123
  on_started_running
112
124
  end
125
+
126
+ on_status_change
113
127
  end
114
128
  end
115
129
 
@@ -20,7 +20,7 @@ module BehaviorTree
20
20
  @value = value
21
21
 
22
22
  # NOTE: Make sure to notify after having set the @value above, so that the new status is already set.
23
- @subscriber&.(prev, value)
23
+ @subscriber&.(prev)
24
24
  end
25
25
 
26
26
  def subscribe(&subscriber)
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module BehaviorTree
4
- VERSION = '0.1.9'
4
+ VERSION = '0.1.10'
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: behavior_tree
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.9
4
+ version: 0.1.10
5
5
  platform: ruby
6
6
  authors:
7
7
  - Felo Vilches
8
- autorequire:
8
+ autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2021-07-15 00:00:00.000000000 Z
11
+ date: 2021-07-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: colorize
@@ -24,7 +24,7 @@ dependencies:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
26
  version: 0.8.1
27
- description:
27
+ description:
28
28
  email:
29
29
  - felovilches@gmail.com
30
30
  executables: []
@@ -48,7 +48,10 @@ files:
48
48
  - lib/behavior_tree/builder.rb
49
49
  - lib/behavior_tree/concerns/dsl/dsl.yml
50
50
  - lib/behavior_tree/concerns/dsl/initial_config.rb
51
+ - lib/behavior_tree/concerns/dsl/randomizer.rb
52
+ - lib/behavior_tree/concerns/dsl/registration.rb
51
53
  - lib/behavior_tree/concerns/dsl/spell_checker.rb
54
+ - lib/behavior_tree/concerns/dsl/utils.rb
52
55
  - lib/behavior_tree/concerns/node_iterators/all_nodes.rb
53
56
  - lib/behavior_tree/concerns/node_iterators/prioritize_running.rb
54
57
  - lib/behavior_tree/concerns/tree_structure/algorithms.rb
@@ -79,7 +82,7 @@ homepage: https://github.com/FeloVilches/Ruby-Behavior-Tree
79
82
  licenses:
80
83
  - MIT
81
84
  metadata: {}
82
- post_install_message:
85
+ post_install_message:
83
86
  rdoc_options: []
84
87
  require_paths:
85
88
  - lib
@@ -94,8 +97,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
94
97
  - !ruby/object:Gem::Version
95
98
  version: '0'
96
99
  requirements: []
97
- rubygems_version: 3.1.2
98
- signing_key:
100
+ rubygems_version: 3.2.3
101
+ signing_key:
99
102
  specification_version: 4
100
- summary: Behavior Tree (AI) library for Ruby.
103
+ summary: A robust and customizable Ruby gem for creating Behavior Trees.
101
104
  test_files: []