tron.rb 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. require 'socket'
  2. require 'yaml/store'
  3. config = YAML::Store.new "config.store"
  4. config.transaction do
  5. $name = config["name"]
  6. $password = config["password"]
  7. $host = config["host"]
  8. $port = config["port"]
  9. $lookahead = config["lookahead"].to_i
  10. end
  11. $dirs = Queue.new
  12. $ticks = 0
  13. def connect(host, port)
  14. $sock = TCPSocket.new(host, port)
  15. # join($name,$password)
  16. while line = $sock.gets
  17. parse(line)
  18. end
  19. end
  20. def join(name, password)
  21. send("join", name, password)
  22. end
  23. def parse(message)
  24. parts = message.split('|')
  25. case parts[0].strip
  26. when "error"
  27. puts "error occured " + parts[1]
  28. when "game"
  29. puts "#{Time.now.strftime('%M%S%L')}: #{message}"
  30. $gameW = parts[1].to_i
  31. $gameH = parts[2].to_i
  32. $id = parts[3].to_i
  33. initmap()
  34. when "pos"
  35. $map[parts[2].to_i][parts[3].to_i] = parts[1].to_i
  36. # puts $map
  37. if parts[1].to_i === $id
  38. # store our position
  39. $myX = parts[2].to_i
  40. $myY = parts[3].to_i
  41. end
  42. when "tick"
  43. puts "#{Time.now.strftime('%M%S%L')}: #{message}"
  44. puts "tick took: #{Time.now.strftime('%M%S%L').to_i - $lasttick}"
  45. $lasttick = Time.now.strftime('%M%S%L').to_i
  46. decide()
  47. when "die"
  48. puts "#{Time.now.strftime('%M%S%L')}: #{message}"
  49. parts.drop(1).each { |player| cleanup(player) }
  50. when "message"
  51. puts "#{Time.now.strftime('%M%S%L')}: #{message}"
  52. when "win", "lose"
  53. puts "#{Time.now.strftime('%M%S%L')}: #{message}"
  54. reset()
  55. when "motd"
  56. puts "#{Time.now.strftime('%M%S%L')}: #{message}"
  57. join($name, $password)
  58. end
  59. end
  60. def cleanup(player)
  61. id = player.to_i
  62. puts "Removing tiles from player ##{id}"
  63. $dead.append(id)
  64. # $map.each { |row| row.each { |element| if element == id then element = -1 end}}
  65. end
  66. $tries = 0
  67. def check_front
  68. # puts "#{$direction} at #{$myX},#{$myY} - try: #{$tries}"
  69. case $direction
  70. when "up"
  71. value = $map[overflow($myX, $gameW)][overflow($myY - 1, $gameH)]
  72. when "down"
  73. value = $map[overflow($myX, $gameW)][overflow($myY + 1, $gameH)]
  74. when "left"
  75. value = $map[overflow($myX - 1, $gameW)][overflow($myY, $gameH)]
  76. when "right"
  77. value = $map[overflow($myX + 1, $gameW)][overflow($myY, $gameH)]
  78. end
  79. $tries = $tries + 1
  80. puts "check_front: #{value}"
  81. return value
  82. end
  83. def overflow(new, max)
  84. if new >= max
  85. return 0
  86. end
  87. if new < 0
  88. return max - 1
  89. end
  90. new
  91. end
  92. def turned_position(turn_direction)
  93. dir = 0
  94. front = 0
  95. case turn_direction
  96. when "left"
  97. dir = -1
  98. when "right"
  99. dir = 1
  100. when "front"
  101. front = 1
  102. dir = 0
  103. end
  104. case $direction
  105. when "up"
  106. return overflow($myX + dir, $gameW), overflow($myY - front, $gameH)
  107. when "down"
  108. return overflow($myX - dir, $gameW), overflow($myY + front, $gameH)
  109. when "left"
  110. return overflow($myX - front, $gameW), overflow($myY - dir, $gameH)
  111. when "right"
  112. return overflow($myX + front, $gameW), overflow($myY + dir, $gameH)
  113. end
  114. end
  115. def new_direction
  116. case $direction
  117. when "up"
  118. "right"
  119. when "down"
  120. "left"
  121. when "left"
  122. "up"
  123. when "right"
  124. "down"
  125. end
  126. end
  127. def turn_direction(dir)
  128. case $direction
  129. when "up"
  130. if dir == "right"
  131. "right"
  132. else
  133. "left"
  134. end
  135. when "down"
  136. if dir == "right"
  137. "left"
  138. else
  139. "right"
  140. end
  141. when "left"
  142. if dir == "right"
  143. "up"
  144. else
  145. "down"
  146. end
  147. when "right"
  148. if dir == "right"
  149. "down"
  150. else
  151. "up"
  152. end
  153. end
  154. end
  155. def check_area(direction)
  156. $fillmap = Array.new($gameH) { Array.new($gameW) { -1 } }
  157. (0...$gameH).each do |i|
  158. (0...$gameW).each { |j| $fillmap[i][j] = $map[i][j] }
  159. end
  160. x, y = turned_position(direction)
  161. fill(x, y, 0)
  162. end
  163. def decide()
  164. $ticks = $ticks + 1
  165. lastdirection = $direction
  166. if $dirs.length > 0
  167. puts "in queue"
  168. $direction = $dirs.pop
  169. if check_front > -1 && !$dead.include?(check_front)
  170. #chat("You crossed my Plans!")
  171. $direction = lastdirection
  172. $dirs.clear()
  173. else
  174. send("move", $direction)
  175. return
  176. end
  177. end
  178. # check area front
  179. front = check_area("front")
  180. # check area left
  181. left = check_area("left")
  182. # check area right
  183. right = check_area("right")
  184. if($ticks < 20)
  185. puts "'neue' logik"
  186. if left > front && left > right
  187. puts "#{left} > #{front} && #{left} > #{right} -> left"
  188. $direction = turn_direction("left")
  189. $dirs << "left"
  190. puts "qued left"
  191. else
  192. if right > front && right > left
  193. puts "#{right} > #{front} && #{right} > #{left} -> right"
  194. $direction = turn_direction("right")
  195. $dirs << "right"
  196. puts "qued right"
  197. end
  198. end
  199. else
  200. puts "'alte' logik"
  201. if check_front > -1 && !$dead.include?(check_front) || check_area("front") < 10
  202. if left > right
  203. $direction = turn_direction("left")
  204. else
  205. if left == right
  206. case $lastturn
  207. when "left"
  208. $direction = turn_direction("right")
  209. $lastturn = "right"
  210. $dirs << "right"
  211. when "right"
  212. $direction = turn_direction("left")
  213. $lastturn = "left"
  214. $dirs << "left"
  215. end
  216. else
  217. $direction = turn_direction("right")
  218. end
  219. end
  220. puts "#{left} > #{right}: turned #{$direction}"
  221. end
  222. end
  223. #$direction = new_direction()
  224. #if $tries > 4
  225. # break
  226. #end
  227. if check_front > -1 && !$dead.include?(check_front)
  228. chat("To honor the Fallen")
  229. $direction = lastdirection
  230. end
  231. send("move", $direction)
  232. end
  233. # $tries = 0
  234. # while check_front > -1 && !$dead.include?(check_front)
  235. def chat(message)
  236. send("chat", message)
  237. end
  238. def reset() end
  239. def send(msg, *param)
  240. if param.length > 0
  241. snd = "#{msg}|#{param.join("|")}"
  242. else
  243. snd = msg
  244. end
  245. $sock.puts(snd)
  246. puts("#{Time.now.strftime('%M%S%L')}: #{snd}")
  247. end
  248. def initmap()
  249. $map = Array.new($gameH) { Array.new($gameW) { -1 } }
  250. $direction = "up"
  251. $lastturn = "left"
  252. $dead = Array.new(0)
  253. $lasttick = 0
  254. $ticks = 0
  255. chat("Rock and Stone!")
  256. end
  257. def fill(x, y, depth)
  258. x = overflow(x, $gameW)
  259. y = overflow(y, $gameH)
  260. tile = $fillmap[x][y]
  261. if depth > $lookahead
  262. return 0
  263. end
  264. if tile > -1 && !$dead.include?(tile)
  265. return 0
  266. end
  267. $fillmap[x][y] = 42 # mark as visited
  268. sum = 1
  269. sum += fill(x - 1, y, depth + 1)
  270. sum += fill(x + 1, y, depth + 1)
  271. sum += fill(x, y - 1, depth + 1)
  272. sum += fill(x, y + 1, depth + 1)
  273. return sum
  274. end
  275. connect($host, $port)