1. #-------------------------------------------------------------------------------
  2. # * [ACE] Khas Pathfinder
  3. #-------------------------------------------------------------------------------
  4. # * By Khas Arcthunder - arcthunder.site40.net
  5. # * Version: 1.0 EN
  6. # * Released on: 28/02/2012
  7. #
  8. #-------------------------------------------------------------------------------
  9. # * Terms of Use
  10. #-------------------------------------------------------------------------------
  11. # When using any Khas script, you agree with the following terms:
  12. # 1. You must give credit to Khas;
  13. # 2. All Khas scripts are licensed under a Creative Commons license;
  14. # 3. All Khas scripts are for non-commercial projects. If you need some script
  15. # for your commercial project (I accept requests for this type of project),
  16. # send an email to [email protected] with your request;
  17. # 4. All Khas scripts are for personal use, you can use or edit for your own
  18. # project, but you are not allowed to post any modified version;
  19. # 5. You can’t give credit to yourself for posting any Khas script;
  20. # 6. If you want to share a Khas script, don’t post the script or the direct
  21. # download link, please redirect the user to arcthunder.site40.net
  22. # 7. You are not allowed to convert any of Khas scripts to another engine,
  23. # such converting a RGSS3 script to RGSS2 or something of that nature.
  24. #
  25. # Check all terms at http://arcthunder.site40.net/terms/
  26. #
  27. #-------------------------------------------------------------------------------
  28. # * Features
  29. #-------------------------------------------------------------------------------
  30. # Smart pathfinding
  31. # Fast Algorithm
  32. # Easy to use
  33. # Plug'n'Play
  34. # Game_Character objects compatible
  35. # Log tool
  36. #
  37. #-------------------------------------------------------------------------------
  38. # * Instructions
  39. #-------------------------------------------------------------------------------
  40. # Use the following code inside the "Call Script" box:
  41. #
  42. # find_path(id,fx,fy)
  43. # Runs the pathfinder.
  44. # id => An integer, use -1 for game player, 0 for the event that the command
  45. # will be called and X for event ID X.
  46. # fx => X destination
  47. # fy => Y destination
  48. #
  49. # find_path(id,fx,fy,true)
  50. # Call this command if you want the game to wait the moving character.
  51. #
  52. # If you want to enable Pathfinder's logs, set "Log" constant to true.
  53. #
  54. #-------------------------------------------------------------------------------
  55. # * Register
  56. #-------------------------------------------------------------------------------
  57. $khas_awesome = [] if $khas_awesome.nil?
  58. $khas_awesome << ["Pathfinder",1.0]
  59. #-------------------------------------------------------------------------------
  60. # * Script
  61. #-------------------------------------------------------------------------------
  62. class Game_Interpreter
  63. def find_path(char,fx,fy,wait=false)
  64. $game_map.refresh if $game_map.need_refresh
  65. character = get_character(char)
  66. return if character.nil?
  67. return unless Path_Core.runnable?(character,fx,fy)
  68. path = Path_Core.run(character,fx,fy)
  69. return if path.nil?
  70. route = RPG::MoveRoute.new
  71. route.repeat = false
  72. route.wait = wait
  73. route.skippable = true
  74. route.list = []
  75. path << 0x00
  76. path.each { |code| route.list << RPG::MoveCommand.new(code)}
  77. character.force_move_route(route)
  78. @moving_character = character if wait
  79. end
  80. end
  81. class Path
  82. attr_accessor :axis
  83. attr_accessor :from
  84. attr_accessor :cost
  85. attr_accessor :dir
  86. def initialize(x,y,f,c,d)
  87. @axis = [x,y]
  88. @from = f
  89. @cost = c
  90. @dir = d
  91. end
  92. end
  93. module Path_Core
  94. Log = false
  95. Directions = {[1,0] => 3,[-1,0] => 2,[0,-1] => 4,[0,1] => 1}
  96. def self.runnable?(char,x,y)
  97. return false unless $game_map.valid?(x,y)
  98. return false if char.collide_with_characters?(x,y)
  99. $game_map.all_tiles(x,y).each { |id|
  100. flag = $game_map.tileset.flags[id]
  101. next if flag & 0x10 != 0
  102. return flag & 0x0f != 0x0f}
  103. return false
  104. end
  105. def self.run(char,fx,fy)
  106. return nil if char.x == fx && char.y == fy
  107. st = Time.now
  108. @char = char
  109. @start = Path.new(@char.x,@char.y,nil,0,nil)
  110. @finish = Path.new(fx,fy,nil,0,nil)
  111. @list = []
  112. @queue = []
  113. @preference = ((@char.x-fx).abs > (@char.y-fy).abs ? 0x0186aa : 0x01d)
  114. class << @list
  115. def new_path?(path_class)
  116. for path in self
  117. return false if path.axis == path_class.axis
  118. end
  119. return true
  120. end
  121. end
  122. class << @queue
  123. def new_path?(path_class)
  124. for path in self
  125. return false if path.axis == path_class.axis
  126. end
  127. return true
  128. end
  129. end
  130. if @preference & 0x02 == 0x02
  131. @queue << Path.new(@char.x,@char.y+1,@start,1,[0,1]) if @char.passable?(@char.x,@char.y,2)
  132. @queue << Path.new(@char.x,@char.y-1,@start,1,[0,-1]) if @char.passable?(@char.x,@char.y,8)
  133. @queue << Path.new(@char.x+1,@char.y,@start,1,[1,0]) if @char.passable?(@char.x,@char.y,6)
  134. @queue << Path.new(@char.x-1,@char.y,@start,1,[-1,0]) if @char.passable?(@char.x,@char.y,4)
  135. @list << @start
  136. loop do
  137. break if @queue.empty?
  138. @cpath = @queue[0]
  139. if @cpath.axis == @finish.axis
  140. @finish.cost = @cpath.cost
  141. @finish.from = @cpath
  142. break
  143. end
  144. @list << @cpath
  145. @path_array = []
  146. p1 = Path.new(@cpath.axis[0]+1,@cpath.axis[1],@cpath,@cpath.cost+1,[1,0])
  147. p2 = Path.new(@cpath.axis[0]-1,@cpath.axis[1],@cpath,@cpath.cost+1,[-1,0])
  148. p3 = Path.new(@cpath.axis[0],@cpath.axis[1]+1,@cpath,@cpath.cost+1,[0,1])
  149. p4 = Path.new(@cpath.axis[0],@cpath.axis[1]-1,@cpath,@cpath.cost+1,[0,-1])
  150. @path_array << p3 if @char.passable?(@cpath.axis[0],@cpath.axis[1],2) && @list.new_path?(p3) && @queue.new_path?(p3)
  151. @path_array << p4 if @char.passable?(@cpath.axis[0],@cpath.axis[1],8) && @list.new_path?(p4) && @queue.new_path?(p4)
  152. @path_array << p1 if @char.passable?(@cpath.axis[0],@cpath.axis[1],6) && @list.new_path?(p1) && @queue.new_path?(p1)
  153. @path_array << p2 if @char.passable?(@cpath.axis[0],@cpath.axis[1],4) && @list.new_path?(p2) && @queue.new_path?(p2)
  154. @path_array.each { |path| @queue << path }
  155. @queue.delete(@cpath)
  156. end
  157. else
  158. @queue << Path.new(@char.x+1,@char.y,@start,1,[1,0]) if @char.passable?(@char.x,@char.y,6)
  159. @queue << Path.new(@char.x-1,@char.y,@start,1,[-1,0]) if @char.passable?(@char.x,@char.y,4)
  160. @queue << Path.new(@char.x,@char.y+1,@start,1,[0,1]) if @char.passable?(@char.x,@char.y,2)
  161. @queue << Path.new(@char.x,@char.y-1,@start,1,[0,-1]) if @char.passable?(@char.x,@char.y,8)
  162. @list << @start
  163. loop do
  164. break if @queue.empty?
  165. @cpath = @queue[0]
  166. if @cpath.axis == @finish.axis
  167. @finish.cost = @cpath.cost
  168. @finish.from = @cpath
  169. break
  170. end
  171. @list << @cpath
  172. @path_array = []
  173. p1 = Path.new(@cpath.axis[0]+1,@cpath.axis[1],@cpath,@cpath.cost+1,[1,0])
  174. p2 = Path.new(@cpath.axis[0]-1,@cpath.axis[1],@cpath,@cpath.cost+1,[-1,0])
  175. p3 = Path.new(@cpath.axis[0],@cpath.axis[1]+1,@cpath,@cpath.cost+1,[0,1])
  176. p4 = Path.new(@cpath.axis[0],@cpath.axis[1]-1,@cpath,@cpath.cost+1,[0,-1])
  177. @path_array << p1 if @char.passable?(@cpath.axis[0],@cpath.axis[1],6) && @list.new_path?(p1) && @queue.new_path?(p1)
  178. @path_array << p2 if @char.passable?(@cpath.axis[0],@cpath.axis[1],4) && @list.new_path?(p2) && @queue.new_path?(p2)
  179. @path_array << p3 if @char.passable?(@cpath.axis[0],@cpath.axis[1],2) && @list.new_path?(p3) && @queue.new_path?(p3)
  180. @path_array << p4 if @char.passable?(@cpath.axis[0],@cpath.axis[1],8) && @list.new_path?(p4) && @queue.new_path?(p4)
  181. @path_array.each { |path| @queue << path }
  182. @queue.delete(@cpath)
  183. end
  184. end
  185. if @finish.from.nil?
  186. return nil
  187. else
  188. steps = [@finish.from]
  189. loop do
  190. cr = steps[-1]
  191. if cr.cost == 1
  192. @result = []
  193. steps.each { |s| @result << Directions[s.dir]}
  194. break
  195. else
  196. steps << cr.from
  197. end
  198. end
  199. self.print_log(Time.now-st) if Log
  200. return @result.reverse
  201. end
  202. end
  203. def self.print_log(time)
  204. print "\n--------------------\n"
  205. print "Khas Pathfinder\n"
  206. print "Time: #{time}\n"
  207. print "Size: #{@result.size}\n"
  208. print "--------------------\n"
  209. end
  210. end

Khas Awesome Light Effects Script - All credit to Khas.

Comments powered by Disqus