1. #--------------------------------------------------------------------------
2. # Enemy Detection System
3. # by Gubid - 8/12/07
4. # v 1.2.2 Updated 10/2/07
5. #--------------------------------------------------------------------------
6. #Description:
7. #This system allows events that are named something including "ENEMY" then
8. #that event will be told to approach Game_Player, until Game_Player has gotten
9. #a specified distance away. At that time event will return to its original
10. #position if desired. If you happen to get close enough to the returning event
11. #it will begin to follow you again.
12. #
13. #Thanks to Near Fantasia for supplying the slightly modified path finding script.
14. #--------------------------------------------------------------------------
15. #if there are any questions as to the use of this script or
16. #modifications/additions/etc, please post it to the thread on CreationAyslum.net
17. #--------------------------------------------------------------------------
18.
19. #--------------------------------------------------------------------------
20. #Update_Detect - How to Stop Detection if desired
21. #--------------------------------------------------------------------------
22. #1. Within a event, add a SCRIPT (3rd page)
23. #2. type "update_detect(EVENT_ID, TRUE/FALSE)" -Note: it defaults to false
24.
25. #--------------------------------------------------------------------------
26. #RETURN TO POSITION
27. #--------------------------------------------------------------------------
28. #Use this to determine if you would like the event to return to its original
29. #position or not.
30. #--------------------------------------------------------------------------
31. RETURN_TO_POSITION = true
32.
33. #--------------------------------------------------------------------------
34. #CHASE_SPEED and ENABLE_SPEED_CHANGE
35. #--------------------------------------------------------------------------
36. #Use this to set the speed in which enemies will approach after spotting you
37. #and whether or not their speed will change.
38. #--------------------------------------------------------------------------
39. CHASE_SPEED = 3
40. ENABLE_SPEED_CHANGE = true
41.
42. #--------------------------------------------------------------------------
43. #VIEW_RANGE
44. #--------------------------------------------------------------------------
45. #Use this to set the distance in which you must be in order to start the detection
46. #process, or escape to stop.
47. #--------------------------------------------------------------------------
48. VIEW_RANGE = 5
49.
50. #--------------------------------------------------------------------------
51. #ESCAPE_RANGE
52. #--------------------------------------------------------------------------
53. #Use this to set the distance in which you must escape to stop the detection process
54. #--------------------------------------------------------------------------
55. ESCAPE_RANGE = 9
56.
57. #--------------------------------------------------------------------------
58. #Other Notes
59. #--------------------------------------------------------------------------
60. #If the event has a custom move route, they will resume that route when finished
61. #chasing you, but if you dont set RETURN_TO_POSITION, then they will resume
62. #their custom route at the location they stopped chasing you.
63.
64. class Game_Map
65. #--------------------------------------------------------------------------
66. # * Frame Update
67. #--------------------------------------------------------------------------
68. alias en_detect_gm_update update
69. def update
70. en_detect_gm_update
71.
72. # Update map event
73. for event in @events.values
74. if !event.name.include?("enemy")
75. next
76. else
77. if event.character_name != ""
78. if event.stop_detect?
79. next
80. end
81. if check_distance(event.id) == true
82. if event.exclamation == false
83. event.old_type = event.move_type
84. event.move_type = 2
85. if ENABLE_SPEED_CHANGE
86. event.old_speed = event.move_speed
87. event.move_speed = CHASE_SPEED
88. end
89. event.exclamation = true
90. event.animation_id = 98 #98 happens to be the exclamation animation
91. event.f_route.clear
92. end
93. else
94. if event.exclamation == true
95. event.exclamation = false
96. if RETURN_TO_POSITION == true
97. if event.x != @map.events[event.id].x or event.y != @map.events[event.id].y
98. route = get_route(event, @map.events[event.id].x, @map.events[event.id].y)
99. event.run_route(route)
100. end
101. else
102. event.restore_route
103. end
104. end
105. end
106. end
107. end
108. end
109. end
110.
111. def check_distance(id = nil)
112. unless id == nil
113. event = $game_map.events[id]
114. range = ($game_player.x - event.x).abs + ($game_player.y - event.y).abs
115.
116. if range <= VIEW_RANGE and range > 0
117. return true
118. elsif range <= ESCAPE_RANGE and range > 0 and event.exclamation
119. return true
120. end
121. end
122. end
123.
124. def get_route(event, x=0, y=0)
125. target_x = x
126. target_y = y
127. max = (target_x - event.x).abs + (target_y - event.y).abs + 5
128. position = [[event.x, event.y]]
129. route = [[]]
130. more_step = [0]
131. for i in more_step
132. x = position[i][0]
133. y = position[i][1]
134. if !position.include?([x, y + 1]) and event.passable?(x, y, 2)
135. position.push([x, y + 1])
136. route.push(route[i] + [2])
137. if route[i].size + 1 < max
138. more_step.push(route.index(route[i] + [2]))
139. end
140. end
141.
142. if !position.include?([x - 1, y]) and event.passable?(x, y, 4)
143. position.push([x - 1, y])
144. route.push(route[i] + [4])
145. if route[i].size + 1 < max
146. more_step.push(route.index(route[i] + [4]))
147. end
148. end
149.
150. if !position.include?([x + 1, y]) and event.passable?(x, y, 6)
151. position.push([x + 1, y])
152. route.push(route[i] + [6])
153. if route[i].size + 1 < max
154. more_step.push(route.index(route[i] + [6]))
155. end
156. end
157.
158. if !position.include?([x, y - 1]) and event.passable?(x, y, 8)
159. position.push([x, y - 1])
160. route.push(route[i] + [8])
161. if route[i].size + 1 < max
162. more_step.push(route.index(route[i] + [8]))
163. end
164. end
165. end
166. if position.index([target_x, target_y]) == false
167. return [2]
168. else
169. return route[position.index([target_x, target_y])]
170. end
171. end
172. end
173.
174.
175. class Game_Event
176. attr_accessor :move_type
177. attr_accessor :move_speed
178. attr_accessor :old_speed
179. attr_accessor :exclamation
180. attr_accessor :old_type
181. attr_accessor :stop_trigger
182. attr_reader :f_route
183.
184. def initialize(map_id, event)
185. super()
186. @map_id = map_id
187. @event = event
188. @id = @event.id
189. @erased = false
190. @starting = false
191. @through = true
192. @name = @event.name
193. @exclamation = false
194. @f_route = []
195. @step = 0
196. @old_type = @move_type
197. @old_move_route = @move_route
198. @old_speed = @move_speed
199. @stop_trigger = false
200. moveto(@event.x, @event.y)
201. refresh
202. end
203.
204. def stop_detect?
205. return @stop_trigger
206. end
207.
208. def restore_route
209. @move_type = @old_type
210. @move_route_index = @original_move_route_index
211. @move_speed = @old_speed
212. refresh
213. return
214. end
215.
216. def name
217. return @name
218. end
219.
220. alias en_det_update update
221. def update
222. if @f_route.size > 0
223. run_path unless self.moving?
224. if @f_route.size == 0
225. restore_route
226. end
227. end
228. en_det_update
229. end
230.
231. def run_route(route)
232. @f_route = route
233. @step = 0
234. end
235.
236. def run_path
237. if @f_route.size > 0
238. if @f_route[@step] == nil
239. @f_route = []
240. end
241. action = @f_route[@step]
242. @step += 1
243. case action
244. when 2
245. self.move_down
246. when 4
247. self.move_left
248. when 6
249. self.move_right
250. when 8
251. self.move_up
252. end
253. end
254. end
255. end
256.
257. class Interpreter
258. def update_detect(id, set = false)
259. $game_map.events[id].stop_trigger = set
260. end
261.
262. def spotted?
263. is_true = 0
264. for event in $game_map.events.values
265. if event.exclamation
266. is_true += 1
267. end
268. end
269. if is_true > 0
270. return true
271. else
272. return false
273. end
274. end
275.
276. def change_switch(id = 0, value = false, type = 0, switch = "A")
277. if id > 0
278. if type == 0 #for use of main switches
279. #id is switch_id and value should set to true or false
280. $game_switches[id] = value
281. elsif type == 1 #For use of self switches
282. #id is event.id and switch is the desired selfswitch. It must be text!
283. #value is true or false
284. key = [$game_map.map_id, id, switch]
285. # Updates self switch
286. $game_self_switches[key] = value
287. end
288. # Refresh map
289. $game_map.need_refresh = true
290. return true
291. end
292. end
293. end
294.