behavior_tree 0.1.6 → 0.1.10

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,7 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require_relative './decorator_base'
4
- require_relative '../concerns/validations/proc_or_block'
5
4
 
6
5
  module BehaviorTree
7
6
  module Decorators
@@ -1,7 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require_relative './decorator_base'
4
-
5
3
  module BehaviorTree
6
4
  module Decorators
7
5
  # Returns the inverted child status.
@@ -29,10 +29,6 @@ module BehaviorTree
29
29
  end
30
30
  end
31
31
 
32
- def ensure_after_tick
33
- status_map
34
- end
35
-
36
32
  protected
37
33
 
38
34
  def repeat_while
@@ -1,7 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require_relative './repeat_times_base'
4
-
5
3
  module BehaviorTree
6
4
  module Decorators
7
5
  # Repeat N times while child has success status.
@@ -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
@@ -1,20 +1,20 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require_relative './concerns/tree_structure/algorithms'
4
-
5
3
  module BehaviorTree
6
4
  # A node (abstract class).
7
5
  class NodeBase
8
6
  include TreeStructure::Algorithms
9
- attr_reader :status, :tick_count, :ticks_running, :arbitrary_storage
7
+ attr_reader :status, :prev_status, :tick_count, :ticks_running, :arbitrary_storage
10
8
 
11
9
  def initialize
12
10
  @status = NodeStatus.new NodeStatus::SUCCESS
11
+ @prev_status = NodeStatus.new NodeStatus::SUCCESS
12
+
13
13
  @tick_count = 0
14
14
  @ticks_running = 0
15
15
  @context = nil
16
16
 
17
- @status.subscribe { |prev, curr| on_status_change(prev, curr) }
17
+ @status.subscribe { |prev| __on_status_change__(prev) }
18
18
 
19
19
  @arbitrary_storage = {}
20
20
  end
@@ -34,9 +34,10 @@ module BehaviorTree
34
34
 
35
35
  def tick!
36
36
  @tick_count += 1
37
- @tick_prevented = !should_tick?
37
+ should_tick = should_tick?
38
+ raise ShouldTickNotBooleanError, should_tick unless [true, false].include? should_tick
38
39
 
39
- unless @tick_prevented
40
+ unless (@tick_prevented = !should_tick)
40
41
  status.running!
41
42
  on_tick
42
43
  @ticks_running += 1
@@ -49,6 +50,9 @@ module BehaviorTree
49
50
  #
50
51
  # This error can be replicated by pasting a valid status object in IRB, such as by doing:
51
52
  # BehaviorTree.const_get(:NodeStatus).new(:__running__) # Valid, but IRB crashes.
53
+ #
54
+ # Ruby 3.0.0 -> Crash
55
+ # Ruby 2.7.0 -> OK
52
56
  nil
53
57
  end
54
58
 
@@ -100,18 +104,26 @@ module BehaviorTree
100
104
 
101
105
  def on_finished_running; end
102
106
 
107
+ def on_status_change; end
108
+
103
109
  private
104
110
 
105
- # 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).
106
112
  # The fact that it's set to 0 means that setting to running must be done before
107
113
  # increasing the counts (so that @ticks_running becomes 1 after the whole tick lifecycle).
108
- def on_status_change(prev, curr)
109
- 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?
110
120
  on_finished_running
111
- elsif curr == NodeStatus::RUNNING
121
+ elsif status.running?
112
122
  @ticks_running = 0
113
123
  on_started_running
114
124
  end
125
+
126
+ on_status_change
115
127
  end
116
128
  end
117
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,8 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require_relative './node_base'
4
- require_relative './concerns/validations/single_child'
5
-
6
3
  module BehaviorTree
7
4
  # A node that has a single child (abstract class).
8
5
  class SingleChildNodeBase < NodeBase
@@ -1,8 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require_relative '../node_base'
4
- require_relative '../concerns/validations/proc_or_block'
5
-
6
3
  module BehaviorTree
7
4
  # A task (leaf) node.
8
5
  class TaskBase < NodeBase
@@ -1,8 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require_relative './concerns/tree_structure/printer'
4
- require_relative './tasks/task_base'
5
-
6
3
  module BehaviorTree
7
4
  # Root node of the tree.
8
5
  # This is the class that must be instantiated by the user.
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module BehaviorTree
4
- VERSION = '0.1.6'
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.6
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: []