Restructed Simulator to support multiple robots, Field now draws base field without scaling

This commit is contained in:
2020-06-16 07:49:33 -05:00
parent b19826d51f
commit 12b89f9370
5 changed files with 213 additions and 183 deletions

View File

@@ -9,6 +9,9 @@ module TAC
TIMECRAFTERS_SECONDARY = Gosu::Color.new(0xff_006000) TIMECRAFTERS_SECONDARY = Gosu::Color.new(0xff_006000)
TIMECRAFTERS_TERTIARY = Gosu::Color.new(0xff_00d000) TIMECRAFTERS_TERTIARY = Gosu::Color.new(0xff_00d000)
BLUE_ALLIANCE = Gosu::Color.new(0xff_000080)
RED_ALLIANCE = Gosu::Color.new(0xff_800000)
TACNET_PRIMARY = Gosu::Color.new(0xff_003f7f) TACNET_PRIMARY = Gosu::Color.new(0xff_003f7f)
TACNET_SECONDARY = Gosu::Color.new(0xff_007f7f) TACNET_SECONDARY = Gosu::Color.new(0xff_007f7f)

View File

@@ -1,11 +1,10 @@
module TAC module TAC
class Simulator class Simulator
class Field class Field
def initialize(container:, season:, simulation:, robot:) def initialize(container:, season:, simulation:)
@container = container @container = container
@season = season @season = season
@simulation = simulation @simulation = simulation
@robot = robot
@position = CyberarmEngine::Vector.new @position = CyberarmEngine::Vector.new
@scale = 1 @scale = 1
@@ -19,42 +18,42 @@ module TAC
def draw def draw
Gosu.clip_to(@position.x, @position.y, @size, @size) do Gosu.clip_to(@position.x, @position.y, @size, @size) do
Gosu.translate(@position.x, @position.y) do Gosu.translate(@position.x, @position.y) do
draw_field
Gosu.scale(@scale) do Gosu.scale(@scale) do
draw_field
self.send(:"draw_field_#{@season}") self.send(:"draw_field_#{@season}")
@robot.draw @simulation.robots.each(&:draw)
@simulation.__queue.first.draw if @simulation.__queue.first @simulation.robots.each { |robot| robot.queue.first.draw if robot.queue.first }
end end
end end
end end
end end
def draw_field def draw_field
Gosu.draw_rect(0, 0, @field_size, @field_size, Gosu::Color::GRAY) Gosu.draw_rect(0, 0, @field_size * @scale, @field_size * @scale, Gosu::Color::GRAY)
6.times do |i| # Tile lines across 6.times do |i| # Tile lines across
next if i == 0 next if i == 0
Gosu.draw_rect(@field_size / 6 * i, 0, 1, @field_size, Gosu::Color::BLACK) Gosu.draw_rect((@field_size * @scale) / 6 * i, 0, 1, @field_size * @scale, Gosu::Color::BLACK)
end end
6.times do |i| # Tile lines down 6.times do |i| # Tile lines down
next if i == 0 next if i == 0
Gosu.draw_rect(0, @field_size / 6 * i, @field_size, 1, Gosu::Color::BLACK) Gosu.draw_rect(0, (@field_size * @scale) / 6 * i, @field_size * @scale, 1, Gosu::Color::BLACK)
end end
end end
def draw_field_skystone def draw_field_skystone
# blue bridge # blue bridge
Gosu.draw_rect(0, @field_size / 2 - 2, 49, 1, @blue) Gosu.draw_rect(0, @field_size / 2 - 2, 48, 1, @blue)
Gosu.draw_rect(0, @field_size / 2 + 2, 49, 1, @blue) Gosu.draw_rect(0, @field_size / 2 + 1, 48, 1, @blue)
# mid bridge # mid bridge
Gosu.draw_rect(@field_size / 2 - 24, @field_size / 2 - 9.25, 49, 18.5, Gosu::Color.new(0xff_222222)) Gosu.draw_rect(@field_size / 2 - 24, @field_size / 2 - 9.25, 48, 18.5, Gosu::Color.new(0xff_222222))
Gosu.draw_rect(@field_size / 2 - 24, @field_size / 2 - 2, 49, 1, Gosu::Color::YELLOW) Gosu.draw_rect(@field_size / 2 - 24, @field_size / 2 - 2, 48, 1, Gosu::Color::YELLOW)
Gosu.draw_rect(@field_size / 2 - 24, @field_size / 2 + 2, 49, 1, Gosu::Color::YELLOW) Gosu.draw_rect(@field_size / 2 - 24, @field_size / 2 + 1, 48, 1, Gosu::Color::YELLOW)
# blue bridge # blue bridge
Gosu.draw_rect(@field_size - 47, @field_size / 2 - 2, 49, 1, @red) Gosu.draw_rect(@field_size - 48, @field_size / 2 - 2, 48, 1, @red)
Gosu.draw_rect(@field_size - 47, @field_size / 2 + 2, 49, 1, @red) Gosu.draw_rect(@field_size - 48, @field_size / 2 + 1, 48, 1, @red)
# blue build site # blue build site
Gosu.draw_quad( Gosu.draw_quad(
@@ -73,25 +72,25 @@ module TAC
) )
# blue depot # blue depot
Gosu.draw_rect(@field_size - 23, @field_size - 23, 24, 2, @blue) Gosu.draw_rect(@field_size - 24, @field_size - 24, 24, 2, @blue)
Gosu.draw_rect(@field_size - 23, @field_size - 23, 2, 24, @blue) Gosu.draw_rect(@field_size - 24, @field_size - 24, 2, 24, @blue)
# red depot # red depot
Gosu.draw_rect(-1, @field_size - 23, 24, 2, @red) Gosu.draw_rect(-1, @field_size - 24, 24, 2, @red)
Gosu.draw_rect(22, @field_size - 23, 2, 24, @red) Gosu.draw_rect(22, @field_size - 24, 2, 24, @red)
# blue foundation # blue foundation
Gosu.draw_rect(48, 4, 18.5, 34.5, @blue) Gosu.draw_rect(48, 4, 18.5, 34.5, @blue)
# red foundation # red foundation
Gosu.draw_rect(@field_size - (47 + 18.5), 4, 18.5, 34.5, @red) Gosu.draw_rect(@field_size - (48 + 18.5), 4, 18.5, 34.5, @red)
# stones # stones
6.times do |i| 6.times do |i|
Gosu.draw_rect(48, @field_size - 8 * i - 8, 4, 8, Gosu::Color::YELLOW) Gosu.draw_rect(48, @field_size - 8 * i - 8, 4, 8, Gosu::Color::YELLOW)
end end
6.times do |i| 6.times do |i|
Gosu.draw_rect(@field_size - (47 + 4), @field_size - 8 * i - 8, 4, 8, Gosu::Color::YELLOW) Gosu.draw_rect(@field_size - (48 + 4), @field_size - 8 * i - 8, 4, 8, Gosu::Color::YELLOW)
end end
end end

View File

@@ -2,12 +2,19 @@ module TAC
class Simulator class Simulator
class Robot class Robot
attr_accessor :position, :angle attr_accessor :position, :angle
attr_reader :width, :depth attr_reader :alliance, :width, :depth
def initialize(width:, depth:) def initialize(alliance:, width:, depth:)
@alliance = alliance
@width, @depth = width, depth @width, @depth = width, depth
@position = CyberarmEngine::Vector.new @position = CyberarmEngine::Vector.new
@angle = 0 @angle = 0
@queue = []
@unit = :ticks
@ticks_per_revolution = 240
@gear_ratio = 1
end end
def draw def draw
@@ -15,14 +22,155 @@ module TAC
Gosu.rotate(@angle, @position.x, @position.y) do Gosu.rotate(@angle, @position.x, @position.y) do
Gosu.draw_rect(@position.x - @width / 2, @position.y - @depth / 2, @width, @depth, Gosu::Color::BLACK) Gosu.draw_rect(@position.x - @width / 2, @position.y - @depth / 2, @width, @depth, Gosu::Color::BLACK)
Gosu.draw_rect(@position.x - @width / 2 + 1, @position.y - @depth / 2 + 1, @width - 2, @depth - 2, Gosu::Color.new(0xff_808022)) Gosu.draw_rect(@position.x - @width / 2 + 1, @position.y - @depth / 2 + 1, @width - 2, @depth - 2, Gosu::Color.new(0xff_808022))
Gosu.draw_arc(@position.x, @position.y, 6, 1.0, 32, 2, TAC::Palette::TIMECRAFTERS_PRIMARY)
if @alliance == :blue
Gosu.draw_arc(@position.x, @position.y, 6, 1.0, 32, 2, TAC::Palette::BLUE_ALLIANCE)
elsif @alliance == :red
Gosu.draw_arc(@position.x, @position.y, 6, 1.0, 32, 2, TAC::Palette::RED_ALLIANCE)
else
Gosu.draw_arc(@position.x, @position.y, 6, 1.0, 32, 2, @alliance)
end
Gosu.draw_circle(@position.x, @position.y - @depth * 0.25, 2, 3, TAC::Palette::TIMECRAFTERS_TERTIARY) Gosu.draw_circle(@position.x, @position.y - @depth * 0.25, 2, 3, TAC::Palette::TIMECRAFTERS_TERTIARY)
end end
end end
end end
def update def update(dt)
@angle %= 360.0 @angle %= 360.0
if state = @queue.first
state.update(dt)
if state.complete?
@queue.delete(state)
@queue.first.start unless @queue.empty?
end
end
end
def set_unit(unit, units_per_revolution = unit)
case unit
when :ticks, :inches, :centimeters
@unit = unit
else
raise "Unsupported unit '#{unit.inspect}' exected :ticks, :inches or :centimeters"
end
@units_per_revolution = units_per_revolution
end
def set_gear_ratio(from, to)
@gear_ratio = Float(from) / Float(to)
end
def set_ticks_per_revolution(ticks)
@ticks_per_revolution = Integer(ticks)
end
def forward(distance, power = 0.5)
@queue << Move.new(robot: self, distance: distance, power: power)
end
def backward(distance, power = 1.0)
@queue << Move.new(robot: self, distance: -distance, power: power)
end
def turn(relative_angle, power = 1.0)
@queue << Turn.new(robot: self, relative_angle: relative_angle, power: power)
end
def speed
@ticks_per_revolution / @gear_ratio
end
def queue
@queue
end
class State
def start
end
def draw
end
def update(dt)
end
def complete?
@complete
end
end
class Move < State
def initialize(robot:, distance:, power:)
@robot = robot
@distance = distance
@power = power
end
def start
@starting_position = @robot.position.clone
@goal = @starting_position.clone
@goal.x += Math.cos(@robot.angle.gosu_to_radians) * @distance
@goal.y += Math.sin(@robot.angle.gosu_to_radians) * @distance
@complete = false
@allowable_error = 3.0
end
def draw
Gosu.draw_line(@robot.position.x + @robot.width / 2, @robot.position.y + @robot.depth / 2, Gosu::Color::GREEN, @goal.x + @robot.width / 2, @goal.y + @robot.depth / 2, Gosu::Color::GREEN)
Gosu.draw_rect(@goal.x + (@robot.width / 2 - 2), @goal.y + (@robot.depth / 2 - 2), 4, 4, Gosu::Color::RED)
end
def update(dt)
speed = (@distance > 0 ? @power * dt : -@power * dt) * @robot.speed
if @robot.position.distance(@goal) <= @allowable_error
@complete = true
@robot.position = @goal
else
if speed > 0
@robot.position -= (@robot.position - @goal).normalized * speed
else
@robot.position += (@robot.position - @goal).normalized * speed
end
end
end
end
class Turn < State
def initialize(robot:, relative_angle:, power:)
@robot = robot
@relative_angle = relative_angle
@power = power
end
def start
@starting_angle = @robot.angle
@last_angle = @starting_angle
@complete = false
@allowable_error = 3.0
end
def update(dt)
target_angle = (@starting_angle + @relative_angle) % 360.0
if @robot.angle.between?(target_angle - @allowable_error, target_angle + @allowable_error)
@complete = true
@robot.angle = target_angle
elsif (@robot.angle - @last_angle).between?(target_angle - @allowable_error, target_angle + @allowable_error)
@complete = true
@robot.angle = target_angle
elsif target_angle > @starting_angle
@robot.angle += @power * dt * @robot.speed
elsif target_angle < @starting_angle
@robot.angle -= @power * dt * @robot.speed
end
@last_angle = @robot.angle
end
end end
end end
end end

View File

@@ -1,176 +1,38 @@
module TAC module TAC
class Simulator class Simulator
class Simulation class Simulation
attr_reader :robot attr_reader :robots
def initialize(source_code:, field_container:) def initialize(source_code:, field_container:)
@source_code = source_code @source_code = source_code
@field_container = field_container @field_container = field_container
@robot = Simulator::Robot.new(width: 18, depth: 18) @robots = []
@field = Field.new(simulation: self, robot: @robot, season: :skystone, container: @field_container) @field = Field.new(simulation: self, season: :skystone, container: @field_container)
@queue = []
@unit = :ticks
@ticks_per_revolution = 240
@gear_ratio = 1
@last_milliseconds = Gosu.milliseconds @last_milliseconds = Gosu.milliseconds
end end
def __start def start
self.instance_eval(@source_code) self.instance_eval(@source_code)
@queue.first.start unless @queue.empty? @robots.each { |robot| robot.queue.first.start unless robot.queue.empty? }
end end
def __queue def draw
@queue
end
def __draw
@field.draw @field.draw
end end
def __update def update
@field.update @field.update
@robot.update @robots.each { |robot| robot.update((Gosu.milliseconds - @last_milliseconds) / 1000.0) }
if state = @queue.first
state.update((Gosu.milliseconds - @last_milliseconds) / 1000.0)
if state.complete?
@queue.delete(state)
@queue.first.start unless @queue.empty?
end
end
@last_milliseconds = Gosu.milliseconds @last_milliseconds = Gosu.milliseconds
end end
def create_robot(width:, height:) def create_robot(alliance:, width:, depth:)
@robot = Simulator::Robot.new(width: width, height: height) robot = Simulator::Robot.new(alliance: alliance, width: width, depth: depth)
end @robots << robot
def set_unit(unit, units_per_revolution = unit) return robot
case unit
when :ticks, :inches, :centimeters
@unit = unit
else
raise "Unsupported unit '#{unit.inspect}' exected :ticks, :inches or :centimeters"
end
@units_per_revolution = units_per_revolution
end
def set_gear_ratio(from, to)
@gear_ratio = Float(from) / Float(to)
end
def set_ticks_per_revolution(ticks)
@ticks_per_revolution = Integer(ticks)
end
def forward(distance, power = 0.5)
@queue << Move.new(simulation: self, distance: distance, power: power)
end
def backward(distance, power = 1.0)
@queue << Move.new(simulation: self, distance: -distance, power: power)
end
def turn(relative_angle, power = 1.0)
@queue << Turn.new(simulation: self, relative_angle: relative_angle, power: power)
end
def __speed
@ticks_per_revolution / @gear_ratio
end
class State
def start
end
def draw
end
def update(dt)
end
def complete?
@complete
end
end
class Move < State
def initialize(simulation:, distance:, power:)
@simulation = simulation
@robot = @simulation.robot
@distance = distance
@power = power
end
def start
@starting_position = @robot.position.clone
@goal = @starting_position.clone
@goal.x += Math.cos(@robot.angle.gosu_to_radians) * @distance
@goal.y += Math.sin(@robot.angle.gosu_to_radians) * @distance
@complete = false
@allowable_error = 3.0
end
def draw
Gosu.draw_line(@robot.position.x + @robot.width / 2, @robot.position.y + @robot.depth / 2, Gosu::Color::GREEN, @goal.x + @robot.width / 2, @goal.y + @robot.depth / 2, Gosu::Color::GREEN)
Gosu.draw_rect(@goal.x + (@robot.width / 2 - 2), @goal.y + (@robot.depth / 2 - 2), 4, 4, Gosu::Color::RED)
end
def update(dt)
speed = (@distance > 0 ? @power * dt : -@power * dt) * @simulation.__speed
if @robot.position.distance(@goal) <= @allowable_error
@complete = true
@robot.position = @goal
else
if speed > 0
@robot.position -= (@robot.position - @goal).normalized * speed
else
@robot.position += (@robot.position - @goal).normalized * speed
end
end
end
end
class Turn < State
def initialize(simulation:, relative_angle:, power:)
@simulation = simulation
@robot = @simulation.robot
@relative_angle = relative_angle
@power = power
end
def start
@starting_angle = @robot.angle
@last_angle = @starting_angle
@complete = false
@allowable_error = 3.0
end
def update(dt)
target_angle = (@starting_angle + @relative_angle) % 360.0
if @robot.angle.between?(target_angle - @allowable_error, target_angle + @allowable_error)
@complete = true
@robot.angle = target_angle
elsif (@robot.angle - @last_angle).between?(target_angle - @allowable_error, target_angle + @allowable_error)
@complete = true
@robot.angle = target_angle
elsif target_angle > @starting_angle
@robot.angle += @power * dt * @simulation.__speed
elsif target_angle < @starting_angle
@robot.angle -= @power * dt * @simulation.__speed
end
@last_angle = @robot.angle
end
end end
end end
end end

View File

@@ -21,24 +21,42 @@ module TAC
background Gosu::Color.new(0x88_ff8800) background Gosu::Color.new(0x88_ff8800)
flow width: 1.0, height: 0.05 do flow width: 1.0, height: 0.05 do
button get_image("#{TAC::ROOT_PATH}/media/icons/right.png"), image_width: THEME_ICON_SIZE, width: 0.49 do button get_image("#{TAC::ROOT_PATH}/media/icons/right.png"), image_width: THEME_ICON_SIZE, width: 0.49, tip: "Run simulation" do
begin begin
@simulation = TAC::Simulator::Simulation.new(source_code: @source_code.value, field_container: @field_container) @simulation = TAC::Simulator::Simulation.new(source_code: @source_code.value, field_container: @field_container)
@simulation.__start @simulation.start
rescue SyntaxError, NameError, NoMethodError, TypeError, ArgumentError => e rescue SyntaxError, NameError, NoMethodError, TypeError, ArgumentError => e
puts e.backtrace.reverse.join("\n")
puts e
push_state(Dialog::AlertDialog, title: "#{e.class}", message: e) push_state(Dialog::AlertDialog, title: "#{e.class}", message: e)
end end
end end
button get_image("#{TAC::ROOT_PATH}/media/icons/stop.png"), image_width: THEME_ICON_SIZE, width: 0.49 do button get_image("#{TAC::ROOT_PATH}/media/icons/stop.png"), image_width: THEME_ICON_SIZE, width: 0.49, tip: "Stop simulation" do
@simulation.__queue.clear if @simulation @simulation.queue.clear if @simulation
end
button get_image("#{TAC::ROOT_PATH}/media/icons/save.png"), image_width: THEME_ICON_SIZE, width: 0.49, tip: "Save source code" do
File.open("#{TAC::ROOT_PATH}/data/simulator.rb", "w") { |f| f.write @source_code.value }
end end
button get_image("#{TAC::ROOT_PATH}/media/icons/save.png"), image_width: THEME_ICON_SIZE, width: 0.49
@simulation_status = label "" @simulation_status = label ""
end end
stack width: 1.0, height: 0.95 do stack width: 1.0, height: 0.95 do
@source_code = edit_box "backward 100\nturn 90\nforward 100\nturn -90\nforward 100\nturn -90\nforward 100", width: 1.0, height: 1.0 source_code = ""
if File.exist?("#{TAC::ROOT_PATH}/data/simulator.rb")
source_code = File.read("#{TAC::ROOT_PATH}/data/simulator.rb")
else
source_code =
"robot = create_robot(alliance: :blue, width: 18, depth: 18)
robot.backward 100
robot.turn 90
robot.forward 100
robot.turn -90
robot.forward 100
robot.turn -90
robot.forward 100"
end
@source_code = edit_box source_code, width: 1.0, height: 1.0
end end
end end
end end
@@ -48,13 +66,13 @@ module TAC
super super
Gosu.flush Gosu.flush
@simulation.__draw if @simulation @simulation.draw if @simulation
end end
def update def update
super super
@simulation.__update if @simulation @simulation.update if @simulation
end end
end end
end end