-
Notifications
You must be signed in to change notification settings - Fork 5
/
Kru_MapMerge.js
148 lines (127 loc) · 4 KB
/
Kru_MapMerge.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
//=============================================================================
// Map Merge
// Version: 1.0.0
//=============================================================================
/*:
* @plugindesc v1.0.0 Merges maps if conditions are met.
*
* @author Krusynth
*
* @help
* ============================================================================
* Information
* ============================================================================
*
* This plugin allows authors to replace a section of a map with a new map
* when a given set of conditions are met.
*
* Usage
* Add a line to the parent map's notes field with a JSON object in the
* following format. The id is the id number of the child map to use. You can
* find this number at the bottom of the map window in RPG Maker MV.
*
* {"mapmerge":[{"condition":"$gameActors._data[1]._level > 10","mapId":40,"offset":{"x":4,"y":4}}]}
*
* In this example, map 40 will be inserted into the current map at [4,4], if
* the first actor is above level 10.
*
* Terms & Conditions
* This plugin is free for non-commercial and commercial use.
*/
var Imported = Imported || {};
Imported.Kru_MapMerge = "1.0.0";
var Kru = Kru || {};
Kru.MapMerge = {
config: {}
};
if(!Imported.Kru_Core) {
alert('Kru_StatOverhaul requires Kru_Core.');
throw new Error('Kru_StatOverhaul requires Kru_Core.');
}
Kru.MapMerge.DataManager_loadMapData = DataManager.loadMapData;
DataManager.loadMapData = function(mapId) {
if (mapId > 0) {
$dataMap = Kru.helpers.getMapData(mapId, Kru.MapMerge.processMap);
Kru.MapMerge.processMap($dataMap);
} else {
this.makeEmptyMap();
}
}
Kru.MapMerge.processMap = function(parentMap) {
let notes = Kru.helpers.parseNoteTags(parentMap.note);
if(typeof(notes.mapmerge) !== 'undefined') {
for(gms_i = 0; gms_i < notes.mapmerge.length; gms_i++) {
let map = notes.mapmerge[gms_i];
if(
map.mapId &&
(typeof(map.condition) === 'undefined' ||
eval(map.condition))
) {
let mapData = Kru.helpers.getMapData(map.mapId);
let offset = {x:0, y:0};
if(map.offset) {
offset = map.offset;
}
Kru.MapMerge.mapMergeTiles(parentMap, mapData, offset);
Kru.MapMerge.mapMergeEvents(parentMap, mapData, offset);
}
}
}
};
Kru.MapMerge.mapMergeTiles = function(map1, map2, offset) {
offset = offset || {};
offset.x = offset.x || 0;
offset.y = offset.y || 0;
// Depth (Z) is generally the same for all maps.
let depth = map1.data.length / (map1.width * map1.height);
let height = map2.height;
if(height > map1.height) {
height = map1.height;
}
let width = map2.width;
if(width > map1.width) {
width = map1.width;
}
// Depth
for(let z = 0; z < depth; z++) {
// Rows
for(let y = 0; y < height; y++) {
// Columns
for(let x = 0; x < width; x++) {
let idx1 = ((z * map1.height) + (y + offset.y)) * map1.width + offset.x + x;
let idx2 = (z * map2.height + y) * map2.width + x
// Remap our tile.
map1.data[idx1] = map2.data[idx2];
}
}
}
}
Kru.MapMerge.mapMergeEvents = function(map1, map2, offset) {
offset = offset || {};
offset.x = offset.x || 0;
offset.y = offset.y || 0;
for(let i = 0; i < map2.events.length; i++) {
let event = map2.events[i];
if(event) {
// Increment our id.
event.id = map1.events.length;
event.x += offset.x;
event.y += offset.y;
if(typeof(event.meta) === 'undefined') {
event.meta = {};
}
map1.events.push(event);
}
}
}
// Remove extra map load when the map hasn't changed on scene load.
// This fixes the issue where exiting the menu resets the map.
Kru.MapMerge.Scene_Map_create = Scene_Map.prototype.create;
Scene_Map.prototype.create = function() {
Scene_Base.prototype.create.call(this);
this._transfer = $gamePlayer.isTransferring();
// Here's what we're changing.
if(this._transfer) {
DataManager.loadMapData($gamePlayer.newMapId());
}
};