var Imported = Imported || {};
Imported.YEP_SaveEventLocations = true;
var Yanfly = Yanfly || {};
Yanfly.SEL = Yanfly.SEL || {};
 /*:
 * @plugindesc 
 * events when leaving and loading them upon reentering map.
 * @author Yanfly Engine Plugins
 *
 * @help 
 */
/*:ja
 * @plugindesc 
 * 保存・維持させることができるようになります。
 * @author Yanfly Engine Plugins
 *
 * @help 
 */
DataManager.processSELNotetags1 = function() {
if (!$dataMap) return;
if (!$dataMap.note) return;
var notedata = $dataMap.note.split(/[\r\n]+/);
  $dataMap.saveEventLocations = false;
for (var i = 0; i < notedata.length; i++) {
var line = notedata[i];
if (line.match(/<(?:SAVE EVENT LOCATION|save event locations)>/i)) {
$dataMap.saveEventLocations = true;
}
}
};
DataManager.processSELNotetags2 = function(obj) {
var notedata = obj.note.split(/[\r\n]+/);
  obj.saveEventLocation = false;
for (var i = 0; i < notedata.length; i++) {
var line = notedata[i];
if (line.match(/<(?:SAVE EVENT LOCATION|save event locations)>/i)) {
obj.saveEventLocation = true;
}
}
};
Yanfly.SEL.Game_System_initialize = Game_System.prototype.initialize;
Game_System.prototype.initialize = function() {
  Yanfly.SEL.Game_System_initialize.call(this);
this.initSavedEventLocations();
};
Game_System.prototype.initSavedEventLocations = function() {
this._savedEventLocations = {};
};
Game_System.prototype.savedEventLocations = function() {
if (this._savedEventLocations === undefined) this.initSavedEventLocations();
return this._savedEventLocations;
};
Game_System.prototype.isSavedEventLocation = function(mapId, eventId) {
if (this._savedEventLocations === undefined) this.initSavedEventLocations();
return this._savedEventLocations[[mapId, eventId]] !== undefined;
};
Game_System.prototype.getSavedEventX = function(mapId, eventId) {
if (this._savedEventLocations === undefined) this.initSavedEventLocations();
return this._savedEventLocations[[mapId, eventId]][0];
};
Game_System.prototype.getSavedEventY = function(mapId, eventId) {
if (this._savedEventLocations === undefined) this.initSavedEventLocations();
return this._savedEventLocations[[mapId, eventId]][1];
};
Game_System.prototype.getSavedEventDir = function(mapId, eventId) {
if (this._savedEventLocations === undefined) this.initSavedEventLocations();
return this._savedEventLocations[[mapId, eventId]][2];
};
Game_System.prototype.saveEventLocation = function(mapId, event) {
if (this._savedEventLocations === undefined) this.initSavedEventLocations();
var eventId = event.eventId();
var eventX = event.x;
var eventY = event.y;
var eventDir = event.direction();
this._savedEventLocations[[mapId, eventId]] = [eventX, eventY, eventDir];
};
Yanfly.SEL.Game_Map_setup = Game_Map.prototype.setup;
Game_Map.prototype.setup = function(mapId) {
    if ($dataMap) DataManager.processSELNotetags1();
Yanfly.SEL.Game_Map_setup.call(this, mapId);
};
Game_Map.prototype.isSaveEventLocations = function() {
  return $dataMap.saveEventLocations;
};
Game_Map.prototype.resetAllEventLocations = function() {
for (var i = 0; i < this.events().length; ++i) {
var ev = this.events()[i];
ev.resetLocation();
}
};
Yanfly.SEL.Game_Event_locate = Game_Event.prototype.locate;
Game_Event.prototype.locate = function(x, y) {
DataManager.processSELNotetags2(this.event());
Yanfly.SEL.Game_Event_locate.call(this, x, y);
this.loadLocation();
};
Yanfly.SEL.Game_Event_updateMove = Game_Event.prototype.updateMove;
Game_Event.prototype.updateMove = function() {
    Yanfly.SEL.Game_Event_updateMove.call(this);
this.saveLocation();
};
Game_Event.prototype.isSaveLocation = function() {
if ($gameMap.isSaveEventLocations()) return true;
return this.event().saveEventLocation;
};
Game_Event.prototype.saveLocation = function() {
if (!this.isSaveLocation()) return;
$gameSystem.saveEventLocation($gameMap.mapId(), this);
};
Game_Event.prototype.isLoadLocation = function() {
if (!this.isSaveLocation()) return false;
return $gameSystem.isSavedEventLocation($gameMap.mapId(), this.eventId());
};
Game_Event.prototype.loadLocation = function() {
if (!this.isLoadLocation()) return;
var x = $gameSystem.getSavedEventX($gameMap.mapId(), this.eventId());
var y = $gameSystem.getSavedEventY($gameMap.mapId(), this.eventId());
this.setPosition(x, y);
var dir = $gameSystem.getSavedEventDir($gameMap.mapId(), this.eventId());
this.setDirection(dir);
};
Game_Event.prototype.resetLocation = function() {
Yanfly.SEL.Game_Event_locate.call(this, this.event().x, this.event().y);
this.setDirection(this._originalDirection);
};
Yanfly.SEL.Game_Interpreter_pluginCommand =
    Game_Interpreter.prototype.pluginCommand;
Game_Interpreter.prototype.pluginCommand = function(command, args) {
  Yanfly.SEL.Game_Interpreter_pluginCommand.call(this, command, args)
if (command === 'ResetAllEventLocations') $gameMap.resetAllEventLocations();
};
