From 80df0368be34bdc6062f59040d7b37e094664585 Mon Sep 17 00:00:00 2001 From: Ryan Davis Date: Mon, 20 May 2013 01:51:45 -0700 Subject: [PATCH 1/6] Added the ability for the match to be finished if all bots left have the same name. Should probably have some configuration and/or cmdline options as well but that was beyond me. --- lib/rtanque/match.rb | 5 +++-- spec/rtanque/match_spec.rb | 11 ++++++++++- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/lib/rtanque/match.rb b/lib/rtanque/match.rb index 82f221c..8fd6a3f 100644 --- a/lib/rtanque/match.rb +++ b/lib/rtanque/match.rb @@ -20,7 +20,8 @@ def max_ticks_reached? end def finished? - @stopped || self.max_ticks_reached? || self.bots.count <= 1 + @stopped || self.max_ticks_reached? || self.bots.count <= 1 || + self.bots.map(&:name).uniq.size == 1 end def add_bots(*bots) @@ -64,4 +65,4 @@ def tick @ticks += 1 end end -end \ No newline at end of file +end diff --git a/spec/rtanque/match_spec.rb b/spec/rtanque/match_spec.rb index 6767fe5..58274d5 100644 --- a/spec/rtanque/match_spec.rb +++ b/spec/rtanque/match_spec.rb @@ -21,9 +21,18 @@ end it 'should be false if two or more bots left' do - @instance.add_bots(:bot, :bot2) + bot1 = double('bot', :name => "bot1") + bot2 = double('bot', :name => "bot2") + @instance.add_bots(bot1, bot2) expect(@instance.finished?).to be_false end + + it 'should be true if two or more bots left w/ same name' do + bot1 = double('bot', :name => "bot1") + bot2 = double('bot', :name => "bot1") + @instance.add_bots(bot1, bot2) + expect(@instance.finished?).to be_true + end end describe '#tick' do From 2f3c5f4d5b853d68d68aa916899bf7049671f0c6 Mon Sep 17 00:00:00 2001 From: Ryan Davis Date: Mon, 20 May 2013 01:52:00 -0700 Subject: [PATCH 2/6] Fixed a simple uninitialized ivar warning. --- sample_bots/seek_and_destroy.rb | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/sample_bots/seek_and_destroy.rb b/sample_bots/seek_and_destroy.rb index 1c6bf07..9154e52 100644 --- a/sample_bots/seek_and_destroy.rb +++ b/sample_bots/seek_and_destroy.rb @@ -8,6 +8,8 @@ class SeekAndDestroy < RTanque::Bot::Brain TURRET_FIRE_RANGE = RTanque::Heading::ONE_DEGREE * 5.0 def tick! + @desired_heading ||= nil + if (lock = self.get_radar_lock) self.destroy_lock(lock) @desired_heading = nil @@ -48,4 +50,4 @@ def get_radar_lock @locked_on = lock.name if lock lock end -end \ No newline at end of file +end From 00a1962b3ae0a78a374c92a7d0ab1f61afa7ce9d Mon Sep 17 00:00:00 2001 From: Ryan Davis Date: Mon, 20 May 2013 01:52:56 -0700 Subject: [PATCH 3/6] Use arrays as sets and all_but basically becomes Array#-. --- lib/rtanque/match/tick_group.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/rtanque/match/tick_group.rb b/lib/rtanque/match/tick_group.rb index 90c5809..516ba24 100644 --- a/lib/rtanque/match/tick_group.rb +++ b/lib/rtanque/match/tick_group.rb @@ -14,7 +14,7 @@ def each(&block) end def all_but(*to_exclude) - self.reject { |member| to_exclude.include?(member) } + self.to_a - to_exclude end def delete_if(&block) @@ -47,4 +47,4 @@ def tick end end end -end \ No newline at end of file +end From 431c46a2ecce59afcc2c7536ced8019e38837a40 Mon Sep 17 00:00:00 2001 From: Ryan Davis Date: Mon, 20 May 2013 01:53:44 -0700 Subject: [PATCH 4/6] Fix for odd errors I was getting. Heading#delta's to was sometimes not a float. Dunno why. Couldn't properly track it back because of randomness. --- lib/rtanque/heading.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/rtanque/heading.rb b/lib/rtanque/heading.rb index a03de02..e4b220e 100644 --- a/lib/rtanque/heading.rb +++ b/lib/rtanque/heading.rb @@ -78,7 +78,7 @@ def initialize(radians = NORTH) def delta(to) diff = (to.to_f - self.to_f).abs % FULL_ANGLE diff = -(FULL_ANGLE - diff) if diff > Math::PI - to < self ? -diff : diff + to.to_f < self.to_f ? -diff : diff end # @return [RTanque::Heading] @@ -159,4 +159,4 @@ def to_degrees @memoized[:to_degrees] ||= (self.radians * 180.0) / Math::PI end end -end \ No newline at end of file +end From f157b5471701d15f94fde74a27b539b99fbb1a8c Mon Sep 17 00:00:00 2001 From: Ryan Davis Date: Mon, 20 May 2013 01:54:54 -0700 Subject: [PATCH 5/6] Simple refactoring to make shell speed calculations reusable by bot brains. Important for target prediction. --- lib/rtanque/shell.rb | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/lib/rtanque/shell.rb b/lib/rtanque/shell.rb index 38f2f44..1ec4b2c 100644 --- a/lib/rtanque/shell.rb +++ b/lib/rtanque/shell.rb @@ -5,13 +5,17 @@ class Shell SHELL_SPEED_FACTOR = Configuration.shell.speed_factor attr_reader :bot, :arena, :fire_power + def self.speed fire_power + fire_power * SHELL_SPEED_FACTOR + end + def initialize(bot, position, heading, fire_power) @bot = bot @arena = bot.arena @fire_power = fire_power self.position = position self.heading = heading - self.speed = (fire_power * SHELL_SPEED_FACTOR) # TODO: add bot's relative speed in this heading + self.speed = self.class.speed(fire_power) # TODO: add bot's relative speed in this heading @dead = false end @@ -37,4 +41,4 @@ def hits(bots, &on_hit) end end end -end \ No newline at end of file +end From bea09cee1359d524755ccc8b8c76ef02628aa66c Mon Sep 17 00:00:00 2001 From: Ryan Davis Date: Mon, 20 May 2013 01:56:05 -0700 Subject: [PATCH 6/6] Added quit_when_finished configuration and made window close itself if the game is finished. Important for automated tournaments. --- lib/rtanque/configuration.rb | 3 ++- lib/rtanque/gui/window.rb | 1 + 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/rtanque/configuration.rb b/lib/rtanque/configuration.rb index 7ae7441..e8f34ca 100644 --- a/lib/rtanque/configuration.rb +++ b/lib/rtanque/configuration.rb @@ -6,6 +6,7 @@ module RTanque # @!visibility private Configuration = ::Configuration.for('default') do raise_brain_tick_errors true + quit_when_finished true bot do radius 19 @@ -43,4 +44,4 @@ module RTanque def Configuration.config(&block) ::Configuration::DSL.evaluate(self, &block) end -end \ No newline at end of file +end diff --git a/lib/rtanque/gui/window.rb b/lib/rtanque/gui/window.rb index 1e35523..d3370a9 100644 --- a/lib/rtanque/gui/window.rb +++ b/lib/rtanque/gui/window.rb @@ -36,6 +36,7 @@ def draw self.close if button_down?(Gosu::Button::KbEscape) @background.draw(0, 0, ZOrder::BACKGROUND) if @match.finished? + self.close if Configuration.quit_when_finished self.gui_bots.each { |bot| bot.grow(2) } end self.draw_drawables