sc2ai 0.0.2 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
@@ -179,6 +179,26 @@ module Api
179
179
  action(ability_id: Api::AbilityId::SMART, target:, queue_command:)
180
180
  end
181
181
 
182
+ # Shorthand for performing action MOVE
183
+ # @param target [Api::Unit, Integer, Api::Point2D] is a unit, unit tag or a Api::Point2D
184
+ # @param queue_command [Boolean] shift+command
185
+ def move(target:, queue_command: false)
186
+ action(ability_id: Api::AbilityId::MOVE, target:, queue_command:)
187
+ end
188
+
189
+ # Shorthand for performing action STOP
190
+ # @param queue_command [Boolean] shift+command
191
+ def stop(queue_command: false)
192
+ action(ability_id: Api::AbilityId::STOP, queue_command:)
193
+ end
194
+
195
+ # Shorthand for performing action HOLDPOSITION
196
+ # @param queue_command [Boolean] shift+command
197
+ def hold(queue_command: false)
198
+ action(ability_id: Api::AbilityId::HOLDPOSITION, queue_command:)
199
+ end
200
+ alias_method :hold_position, :hold
201
+
182
202
  # Shorthand for performing action ATTACK
183
203
  # @param target [Api::Unit, Integer, Api::Point2D] is a unit, unit tag or a Api::Point2D
184
204
  # @param queue_command [Boolean] shift+command
@@ -224,6 +244,7 @@ module Api
224
244
  # Draws a placement outline
225
245
  # @param color [Api::Color] optional api color, default white
226
246
  # @return [void]
247
+ # noinspection RubyArgCount
227
248
  def debug_draw_placement(color = nil)
228
249
  # Slightly elevate the Z position so that the line doesn't clip into the terrain at same Z level
229
250
  z_elevated = pos.z + 0.01
@@ -432,6 +453,12 @@ module Api
432
453
  build_progress == 1.0 # standard:disable Lint/FloatComparison
433
454
  end
434
455
 
456
+ # Returns true if build progress is < 100%
457
+ # @return [Boolean]
458
+ def in_progress?
459
+ !is_completed?
460
+ end
461
+
435
462
  # Convenience functions ---
436
463
 
437
464
  # TERRAN Convenience functions ---
@@ -444,7 +471,7 @@ module Api
444
471
 
445
472
  # Returns whether the structure has a reactor add-on
446
473
  # @return [Boolean] if the unit has a reactor attached
447
- def has_reactor
474
+ def has_reactor?
448
475
  Sc2::UnitGroup::TYPE_REACTOR.include?(add_on&.unit_type)
449
476
  end
450
477
 
@@ -455,26 +482,46 @@ module Api
455
482
  # # Get the actual tech-lab with #add_on
456
483
  # sp.add_on.research ...
457
484
  # @return [Boolean] if the unit has a tech lab attached
458
- def has_tech_lab
485
+ def has_tech_lab?
459
486
  Sc2::UnitGroup::TYPE_TECHLAB.include?(add_on&.unit_type)
460
487
  end
461
488
 
462
489
  # For Terran builds a tech lab add-on on the current structure
463
490
  # @return [void]
464
- def build_reactor
465
- build(unit_type_id: Api::UnitTypeId::REACTOR)
491
+ def build_reactor(queue_command: false)
492
+ unit_type_id = case unit_type
493
+ when Api::UnitTypeId::BARRACKS, Api::UnitTypeId::BARRACKSFLYING
494
+ Api::UnitTypeId::BARRACKSREACTOR
495
+ when Api::UnitTypeId::FACTORY, Api::UnitTypeId::FACTORYFLYING
496
+ Api::UnitTypeId::FACTORYREACTOR
497
+ when Api::UnitTypeId::STARPORT, Api::UnitTypeId::STARPORTFLYING
498
+ Api::UnitTypeId::STARPORTREACTOR
499
+ end
500
+ build(unit_type_id: unit_type_id, queue_command:)
466
501
  end
467
502
 
468
503
  # For Terran builds a tech lab add-on on the current structure
469
504
  # @return [void]
470
- def build_tech_lab
471
- build(unit_type_id: Api::UnitTypeId::TECHLAB)
505
+ def build_tech_lab(queue_command: false)
506
+ unit_type_id = case unit_type
507
+ when Api::UnitTypeId::BARRACKS, Api::UnitTypeId::BARRACKSFLYING
508
+ Api::UnitTypeId::BARRACKSTECHLAB
509
+ when Api::UnitTypeId::FACTORY, Api::UnitTypeId::FACTORYFLYING
510
+ Api::UnitTypeId::FACTORYTECHLAB
511
+ when Api::UnitTypeId::STARPORT, Api::UnitTypeId::STARPORTFLYING
512
+ Api::UnitTypeId::STARPORTTECHLAB
513
+ end
514
+ build(unit_type_id: unit_type_id, queue_command:)
472
515
  end
473
516
 
517
+ # GENERAL Convenience functions ---
518
+
519
+ # ...
520
+
474
521
  private
475
522
 
476
523
  # @private
477
- # Reduces repitition in the is_*action*?(target:) methods
524
+ # Reduces repetition in the is_*action*?(target:) methods
478
525
  def is_performing_ability_on_target?(abilities, target: nil)
479
526
  # Exit if not actioning the ability
480
527
  return false unless is_performing_ability?(abilities)
@@ -46,7 +46,7 @@ module Sc2
46
46
  # @param queue_command [Boolean] shift+command
47
47
  def warp(unit_type_id:, target: nil, queue_command: false)
48
48
  return if size.zero?
49
- bot&.warp(units: self, unit_type_id:, target: target, queue_command:)
49
+ bot&.warp(units: self, unit_type_id:, target:, queue_command:)
50
50
  end
51
51
 
52
52
  # Shorthand for performing action SMART (right-click)
@@ -56,11 +56,31 @@ module Sc2
56
56
  action(ability_id: Api::AbilityId::SMART, target:, queue_command:)
57
57
  end
58
58
 
59
+ # Shorthand for performing action MOVE
60
+ # @param target [Api::Unit, Integer, Api::Point2D] is a unit, unit tag or a Api::Point2D
61
+ # @param queue_command [Boolean] shift+command
62
+ def move(target:, queue_command: false)
63
+ action(ability_id: Api::AbilityId::MOVE, target:, queue_command:)
64
+ end
65
+
66
+ # Shorthand for performing action STOP
67
+ # @param queue_command [Boolean] shift+command
68
+ def stop(queue_command: false)
69
+ action(ability_id: Api::AbilityId::STOP, queue_command:)
70
+ end
71
+
72
+ # Shorthand for performing action HOLDPOSITION
73
+ # @param queue_command [Boolean] shift+command
74
+ def hold(queue_command: false)
75
+ action(ability_id: Api::AbilityId::HOLDPOSITION, queue_command:)
76
+ end
77
+ alias_method :hold_position, :hold
78
+
59
79
  # Shorthand for performing action ATTACK
60
80
  # @param target [Api::Unit, Integer, Api::Point2D] is a unit, unit tag or a Api::Point2D
61
81
  # @param queue_command [Boolean] shift+command
62
82
  def attack(target:, queue_command: false)
63
- action(ability_id: Api::AbilityId::ATTACK, target: target, queue_command: queue_command)
83
+ action(ability_id: Api::AbilityId::ATTACK, target:, queue_command:)
64
84
  end
65
85
 
66
86
  # Issues repair command on target
@@ -89,27 +89,32 @@ module Sc2
89
89
  @units[tags.at(index)]
90
90
  end
91
91
 
92
+ # Meta documentation methods
93
+ # @!method first
94
+ # @return [Api::Unit]
95
+ # @!method last
96
+ # @return [Api::Unit]
97
+
92
98
  # Calls the given block with each Api::Unit value
93
99
  # @example
94
- # unit_group.each {|unit| puts unit.tag } #=> 1234 ...
95
- #
96
100
  # unit_group.each do |unit|
97
101
  # puts unit.tag #=> 1234 ...
98
102
  # end
99
- def each(&block)
100
- @units.each_value(&block)
103
+ # @yieldparam unit [Api::Unit]
104
+ def each(&)
105
+ @units.each_value(&)
101
106
  end
102
107
 
103
108
  # Calls the given block with each key-value pair
104
109
  # @return [self] a new unit group with items merged
105
110
  # @example
106
- # unit_group.each {|tag, unit| puts "#{tag}: #{unit}"} #=> "12345: #<Api::Unit ...>"
107
- #
108
- # unit_group.each do |tag, unit|
111
+ # unit_group.each_with_tag do |tag, unit|
109
112
  # puts "#{tag}: #{unit}"} #=> "12345: #<Api::Unit ...>"
110
113
  # end
111
- def each_with_tag(&block)
112
- @units.each(&block)
114
+ # @yieldparam tag [Integer]
115
+ # @yieldparam unit [Api::Unit]
116
+ def each_with_tag(&)
117
+ @units.each(&)
113
118
  self
114
119
  end
115
120
 
@@ -229,12 +234,19 @@ module Sc2
229
234
  end
230
235
 
231
236
  # Selects a single random Unit without a parameter or an array of Units with a param, i.e. self.random(2)
232
- # @return []
237
+ # @return [Api::Unit]
233
238
  def sample(...)
234
239
  @units.values.sample(...)
235
240
  end
236
241
  alias_method :random, :sample
237
242
 
243
+ # Returns the first Unit for which the block returns a truthy value
244
+ # @return [Api::Unit]
245
+ def detect(...)
246
+ @units.values.find(...)
247
+ end
248
+ alias_method :find, :detect
249
+
238
250
  # def select_or(*procs)
239
251
  # result = UnitGroup.new
240
252
  # procs.each do |proc|
data/lib/sc2ai/version.rb CHANGED
@@ -2,5 +2,5 @@
2
2
 
3
3
  module Sc2
4
4
  # gem version
5
- VERSION = "0.0.2"
5
+ VERSION = "0.0.4"
6
6
  end
@@ -4,14 +4,13 @@ class <%= @classname %> < Sc2::Player::Bot
4
4
 
5
5
  def configure
6
6
  @realtime = false # Step-mode vs Bot, Realtime vs Humans
7
- @step_size = 1 # Gives 22.4ms, typically compete at 2 (44.8ms) or 4 (179.2ms).
7
+ @step_count = 2 # 2s/22.4 = ~89.29ms step cycle time allowance
8
8
  @enable_feature_layer = false; # Enables ui_ and spatial_ actions. Advanced, and has performance cost.
9
9
  end
10
10
 
11
11
  def on_step
12
12
  if game_loop == 0
13
- enemy_start_pos = game_info.start_raw.start_locations.first
14
- units.workers.attack(target: enemy_start_pos)
13
+ units.workers.attack(target: geo.enemy_start_position)
15
14
  end
16
15
 
17
16
  # If your attack fails, "good game" and exit