Guest User

Untitled

a guest
Oct 17th, 2017
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.29 KB | None | 0 0
  1. GZKM.P1MomWalkMovement = function() {
  2. var getTileWeight = function(tagId) {
  3. return 1+((tagId&0x10)>>1)
  4. }
  5.  
  6. $gameVariables._data[3] = $gameVariables._data[3] || {}
  7. var state = ($gameVariables._data[3].P1MomWalkMovement = $gameVariables._data[3].P1MomWalkMovement || {})
  8. state.goal = state.goal || null
  9.  
  10.  
  11. var findgoal = function() {
  12. var arr = []
  13. for(var x = 0; x < $gameMap.width(); x++) for(var y = 0; y < $gameMap.height(); y++) if($gameMap.regionId(x,y)&0x1) arr.push({x:x,y:y})
  14. state.goal = arr[Math.floor(Math.random()*arr.length)]
  15. }
  16.  
  17. var setMomMovement = function() {
  18. var mom = this;
  19.  
  20. var evs = []
  21. for(var x = 0; x < $gameMap.width(); x++) {
  22. evs[x] = []
  23. for(var y = 0; y < $gameMap.height(); y++) {
  24. evs[x][y] = []
  25. }
  26. }
  27. $gameMap.events().filter(function(ev){return ev.isNormalPriority() && !ev.isThrough()}).forEach(function(ev){evs[ev.x][ev.y].push(ev)})
  28.  
  29. // Optimization attempt. RPGMaker's default checks for passability seem to be linear in the number of all events on map per tile.
  30. // I tried to bring this down to O(1) per tile.
  31. // No, I didn't profile this, b/c I don't have many events yet - but
  32. // I plan to have more, so I wanted to be on a safe side.
  33. var isCollidedWithEventsOld = Game_CharacterBase.prototype.isCollidedWithEvents;
  34. Game_CharacterBase.prototype.isCollidedWithEvents = function(x, y) {
  35. return evs[x][y].length
  36. }
  37.  
  38. var h = new GZKM.Heap()
  39. var map = []
  40. for(var x = 0; x < $gameMap.width(); x++) for(var y = 0; y < $gameMap.height(); y++) if($gameMap.regionId(x,y)&0x1) if(x != mom.x || y != mom.y)
  41. map[[x,y]] = h.push(Infinity, {x:x,y:y})
  42. map[[mom.x, mom.y]] = h.push(0, {x:mom.x, y:mom.y})
  43.  
  44. var curr; while((curr = h.pop()) !== undefined && curr.priority != Infinity && !(curr.elt.x == state.goal.x && curr.elt.y == state.goal.y)) {
  45. for(var d = 2; d <= 8; d+=2) { // directions
  46. var nx = $gameMap.roundXWithDirection(curr.elt.x, d)
  47. var ny = $gameMap.roundYWithDirection(curr.elt.y, d)
  48. if([nx,ny] in map && mom.canPass(curr.elt.x, curr.elt.y, d)) {
  49. var candPriority = curr.priority + getTileWeight($gameMap.regionId(curr.elt.x,curr.elt.y))
  50. if(candPriority < map[[nx,ny]].priority) {
  51. h.reprioritize(map[[nx,ny]], candPriority)
  52. map[[nx,ny]].elt.prev = curr
  53. }
  54. }
  55. }
  56. }
  57.  
  58. Game_CharacterBase.prototype.isCollidedWithEvents = isCollidedWithEventsOld;
  59.  
  60. if(!('prev' in map[[state.goal.x, state.goal.y]].elt))
  61. state.goal = null
  62. else {
  63. var t = map[[state.goal.x, state.goal.y]]
  64. while(!(t.elt.prev.elt.x == mom.x && t.elt.prev.elt.y == mom.y))
  65. t = t.elt.prev
  66. var dir;
  67. if(t.elt.x == mom.x+1) dir = 6
  68. else if(t.elt.x == mom.x-1) dir = 4
  69. else if(t.elt.y == mom.y+1) dir = 2
  70. else if(t.elt.y == mom.y-1) dir = 8
  71. this.moveStraight(dir)
  72. }
  73.  
  74. }.bind(this)
  75.  
  76. if(state.goal === null) findgoal()
  77.  
  78. setMomMovement()
  79. }
Add Comment
Please, Sign In to add comment