| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273 |
- require 'socket'
- require 'yaml/store'
- config = YAML::Store.new "config.store"
- config.transaction do
- $name = config["name"]
- $password = config["password"]
- $host = config["host"]
- $port = config["port"]
- $lookahead = config["lookahead"].to_i
- end
- def connect(host, port)
- $sock = TCPSocket.new(host, port)
- # join($name,$password)
- while line = $sock.gets
- parse(line)
- end
- end
- def join(name, password)
- send("join", name, password)
- end
- def parse(message)
- parts = message.split('|')
- case parts[0].strip
- when "error"
- puts "error occured " + parts[1]
- when "game"
- puts message
- $gameW = parts[1].to_i
- $gameH = parts[2].to_i
- $id = parts[3].to_i
- initmap()
- when "pos"
- $map[parts[2].to_i][parts[3].to_i] = parts[1].to_i
- # puts $map
- if parts[1].to_i === $id
- # store our position
- $myX = parts[2].to_i
- $myY = parts[3].to_i
- end
- when "tick"
- puts message
- decide()
- when "die"
- puts message
- parts.drop(1).each { |player| cleanup(player) }
- when "message"
- puts message
- when "win", "lose"
- puts message
- reset()
- when "motd"
- puts message
- join($name, $password)
- end
- end
- def cleanup(player)
- id = player.to_i
- puts "Removing tiles from player ##{id}"
- $dead.append(id)
- # $map.each { |row| row.each { |element| if element == id then element = -1 end}}
- end
- $tries = 0
- def check_front
- # puts "#{$direction} at #{$myX},#{$myY} - try: #{$tries}"
- case $direction
- when "up"
- value = $map[overflow($myX, $gameW)][overflow($myY - 1, $gameH)]
- when "down"
- value = $map[overflow($myX, $gameW)][overflow($myY + 1, $gameH)]
- when "left"
- value = $map[overflow($myX - 1, $gameW)][overflow($myY, $gameH)]
- when "right"
- value = $map[overflow($myX + 1, $gameW)][overflow($myY, $gameH)]
- end
- $tries = $tries + 1
- puts "check_front: #{value}"
- return value
- end
- def overflow(new, max)
- if new >= max
- return 0
- end
- if new < 0
- return max - 1
- end
- new
- end
- def turned_position(turn_direction)
- dir = 0
- front = 0
- case turn_direction
- when "left"
- dir = -1
- when "right"
- dir = 1
- when "front"
- front = 1
- dir = 0
- end
- case $direction
- when "up"
- return overflow($myX + dir, $gameW), overflow($myY - front, $gameH)
- when "down"
- return overflow($myX - dir, $gameW), overflow($myY + front, $gameH)
- when "left"
- return overflow($myX - front, $gameW), overflow($myY - dir, $gameH)
- when "right"
- return overflow($myX + front, $gameW), overflow($myY + dir, $gameH)
- end
- end
- def new_direction
- case $direction
- when "up"
- "right"
- when "down"
- "left"
- when "left"
- "up"
- when "right"
- "down"
- end
- end
- def turn_direction(dir)
- case $direction
- when "up"
- if dir == "right"
- "right"
- else
- "left"
- end
- when "down"
- if dir == "right"
- "left"
- else
- "right"
- end
- when "left"
- if dir == "right"
- "up"
- else
- "down"
- end
- when "right"
- if dir == "right"
- "down"
- else
- "up"
- end
- end
- end
- def check_area(direction)
- $fillmap = Array.new($gameH) { Array.new($gameW) { -1 } }
- (0...$gameH).each do |i|
- (0...$gameW).each { |j| $fillmap[i][j] = $map[i][j] }
- end
- x, y = turned_position(direction)
- fill(x, y, 0)
- end
- def decide()
- lastdirection = $direction
- # check area left
- left = check_area("left")
- # check area right
- right = check_area("right")
- if check_front > -1 && !$dead.include?(check_front) || check_area("front") < 10
- if left > right
- $direction = turn_direction("left")
- else
- if left == right
- case $lastturn
- when "left"
- $direction = turn_direction("right")
- $lastturn = "right"
- when "right"
- $direction = turn_direction("left")
- $lastturn = "left"
- end
- else
- $direction = turn_direction("right")
- end
- end
- puts "#{left} > #{right}: turned #{$direction}"
- end
- # $direction = new_direction()
- # if $tries > 4
- # break
- # end
- # end
- if check_front > -1 && !$dead.include?(check_front)
- chat("To honor the Fallen")
- $direction = lastdirection
- end
- send("move", $direction)
- end
- # $tries = 0
- # while check_front > -1 && !$dead.include?(check_front)
- def chat(message)
- send("chat", message)
- end
- def reset() end
- def send(msg, *param)
- if param.length > 0
- snd = "#{msg}|#{param.join("|")}"
- else
- snd = msg
- end
- $sock.puts(snd)
- puts(snd)
- end
- def initmap()
- $map = Array.new($gameH) { Array.new($gameW) { -1 } }
- $direction = "up"
- $lastturn = "left"
- $dead = Array.new(0)
- chat("Rock and Stone!")
- end
- def fill(x, y, depth)
- x = overflow(x, $gameW)
- y = overflow(y, $gameH)
- tile = $fillmap[x][y]
- if depth > $lookahead
- return 0
- end
- if tile > -1 && !$dead.include?(tile)
- return 0
- end
- $fillmap[x][y] = 42 # mark as visited
- sum = 1
- sum += fill(x - 1, y, depth + 1)
- sum += fill(x + 1, y, depth + 1)
- sum += fill(x, y - 1, depth + 1)
- sum += fill(x, y + 1, depth + 1)
- return sum
- end
- connect($host, $port)
|