Advertisement
Guest User

Untitled

a guest
Jul 30th, 2015
217
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.81 KB | None | 0 0
  1. # Total Score: 917
  2. # Your average grade for this tower is: A
  3. #
  4. # Level 1: S
  5. # Level 2: A
  6. # Level 3: B
  7. # Level 4: A
  8. # Level 5: B
  9. # Level 6: A
  10. # Level 7: A
  11.  
  12. class Player
  13. @@dirs = [:forward, :left, :backward, :right]
  14. def play_turn(w)
  15. # If surrounded, bind a warrior.
  16. adjacentMobs = []
  17. adjacentCaptives = []
  18. tickingCaptives = []
  19. @@dirs.each { |x|
  20. if w.feel(x).enemy?
  21. adjacentMobs.push(x)
  22. end
  23. if w.feel(x).captive?
  24. adjacentCaptives.push(x)
  25. end
  26. }
  27.  
  28. remaining = w.listen
  29. remaining.each { |x|
  30. if x.ticking?
  31. tickingCaptives.push(x)
  32. end
  33. }
  34.  
  35. if not tickingCaptives.empty?
  36. dir = w.direction_of(tickingCaptives.first)
  37. ahead = w.look(dir)
  38. count = 0
  39. if ahead[0].enemy? and ahead[1].enemy?
  40. safe = true
  41. if w.health <= 4
  42. safe = false
  43. else
  44. tickingCaptives.each { |c|
  45. if w.distance_of(c) <= 2
  46. safe = false
  47. break
  48. end
  49. }
  50. end
  51. if safe
  52. acted = false
  53. adjacentMobs.each { |e|
  54. if e == :forward
  55. next
  56. end
  57. w.bind!(e)
  58. acted = true
  59. break
  60. }
  61. if not acted
  62. w.detonate!(dir)
  63. end
  64. return
  65. end
  66. end
  67. end
  68.  
  69. if not remaining.empty? and adjacentMobs.empty? and w.health < 11
  70. if tickingCaptives.empty?
  71. w.rest!
  72. return
  73. else
  74. spaces = w.look(w.direction_of(tickingCaptives.first))
  75. if spaces[0].captive?
  76. w.rescue!(w.direction_of(spaces[0]))
  77. return
  78. elsif spaces[1].enemy?
  79. w.rest!
  80. return
  81. end
  82. end
  83. end
  84.  
  85. if tickingCaptives.length > 0
  86. next_dir = w.direction_of(tickingCaptives.first)
  87. if w.feel(next_dir).captive?
  88. w.rescue!(next_dir)
  89. elsif not w.feel(next_dir).empty? or w.feel(next_dir).stairs?
  90. avoid_dir = avoid(w, next_dir)
  91. if avoid_dir and avoid_dir != opposite(@last_movement)
  92. w.walk!(avoid_dir)
  93. @last_movement = avoid_dir
  94. else
  95. push_thru = next_dir
  96. end
  97. else
  98. w.walk!(next_dir)
  99. @last_movement = next_dir
  100. end
  101. end
  102.  
  103. if tickingCaptives.length > 0
  104. if push_thru
  105. deal_with(w, push_thru)
  106. end
  107. else
  108. if adjacentMobs.length > 0
  109. deal_with(w, adjacentMobs.first)
  110. elsif not remaining.empty? and w.health < 16
  111. w.rest!
  112. elsif adjacentCaptives.length > 0
  113. w.rescue!(adjacentCaptives.first)
  114. else
  115. if remaining.empty?
  116. w.walk!(w.direction_of_stairs)
  117. @last_movement = w.direction_of_stairs
  118. else
  119. next_dir = w.direction_of(remaining.first)
  120. if w.feel(next_dir).stairs?
  121. next_dir = avoid(w, next_dir)
  122. end
  123. w.walk!(next_dir)
  124. @last_movement = next_dir
  125. end
  126. end
  127. end
  128. end
  129.  
  130. def deal_with(w, dir)
  131. # Bind any other enemies.
  132. for i in 0..@@dirs.length-1
  133. d = @@dirs[i]
  134. if d == dir
  135. next
  136. end
  137. unit = w.feel(d)
  138. if unit.enemy?
  139. w.bind!(d)
  140. return
  141. end
  142. end
  143.  
  144. unit = w.feel(dir)
  145. if unit.enemy?
  146. w.attack!(dir)
  147. elsif unit.captive?
  148. w.rescue!(dir)
  149. end
  150. end
  151.  
  152. def avoid(w, dir)
  153. index = @@dirs.find_index(dir)
  154. try_dir = @@dirs[(index + 1) % @@dirs.length]
  155. if w.feel(try_dir).empty?
  156. next_dir = try_dir
  157. else
  158. try_dir = @@dirs[(index + 3) % @@dirs.length]
  159. if w.feel(try_dir).empty?
  160. next_dir = try_dir
  161. end
  162. end
  163. return next_dir
  164. end
  165.  
  166. def opposite(dir)
  167. if dir == :forward
  168. return :backward
  169. elsif dir == :backward
  170. return :forward
  171. elsif dir == :right
  172. return :left
  173. end
  174. return :right
  175. end
  176. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement