-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDoubleX_RMMZ_Plugin_Query.js
More file actions
384 lines (336 loc) · 16.6 KB
/
DoubleX_RMMZ_Plugin_Query.js
File metadata and controls
384 lines (336 loc) · 16.6 KB
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
/*============================================================================
* ## Plugin Info
*----------------------------------------------------------------------------
* # Plugin Name
* DoubleX_RMMZ_Plugin_Query
*----------------------------------------------------------------------------
* # Introduction
* 1. The RMMZ plugin commands are supposed to make side effects but not
* return end results
* 2. With this plugin, other plugins can declare plugin queries that
* behave like RMMV plugin commands but return end results instead of
* making side effects
* 3. Plugin queries can replace skill/item damage formulae
* 4. Plugin queries can also replace script calls in conditional branch
* and control variables event commands
*----------------------------------------------------------------------------
* # Prerequisites
* Plugins:
* 1. DoubleX RMMZ Enhanced Codebase
* https://github.com/Double-X/DoubleX-RMMZ/blob/master/DoubleX%20RMMZ%20Enhanced%20Codebase.js
* Abilities(Plugin Users):
* 1. Nothing special
* Abilities(Plugin Developers):
* 1. Little RMMZ plugin development proficiency to fully utilize this
* (Elementary Javascript exposures being able to write beginner codes
* up to 300LoC scale)
* Knowledge:
* 1. Basic knowledge on what RMMV plugin command does in general on the
* user level
*----------------------------------------------------------------------------
* # Author Notes
* 1. The plugin command name collision issues in RMMV applies to
* plugin queries registed to this RMMZ plugin as well, and in this
* case, the last registered plugin query having the same name will be
* used
* 2. DON'T MAKE SIDE EFFECTS IN THE SKILL/ITEM DAMAGE FORMULA PLUGIN
* QUERIES UNLESS YOU REALLY KNOW WHAT YOU'RE TRULY DOING
*----------------------------------------------------------------------------
* # Terms Of Use
* 1. Commercial use's always allowed and crediting me's always optional.
* 2. You shall keep this plugin's Plugin Info part's contents intact.
* 3. You shalln't claim that this plugin's written by anyone other than
* DoubleX or my aliases. I always reserve the right to deny you from
* using any of my plugins anymore if you've violated this.
* 4. If you repost this plugin directly(rather than just linking back),
* you shall inform me of these direct repostings. I always reserve
* the right to request you to edit those direct repostings.
* 5. CC BY 4.0, except those conflicting with any of the above, applies
* to this plugin, unless you've my permissions not needing follow so.
* 6. I always reserve the right to deny you from using this plugin
* anymore if you've violated any of the above.
*----------------------------------------------------------------------------
* # Links
* Video:
* 1. https://www.youtube.com/watch?v=_6L8IJDiTcI
* This Plugin:
* 1. https://github.com/Double-X/DoubleX-RMMZ/blob/master/DoubleX_RMMZ_Plugin_Query.js
* Posts:
* 1. https://forums.rpgmakerweb.com/index.php?threads/doublex_rmmz_plugin_query.128382/
* 2. https://www.rpgmakercentral.com/topic/42647-doublex_rmmz_plugin_query/
* 3. https://rpgmaker.net/engines/rmmz/utilities/300/
* 4. https://www.save-point.org/thread-8181.html
* 5. https://gdu.one/forums/topic/13664-doublex_rmmz_plugin_query/
* 6. http://www.hbgames.org/forums/viewtopic.php?f=332&t=80358
* 7. https://forum.chaos-project.com/index.php/topic,16089.new.html
* 8. https://doublexrpgmaker.wordpress.com/2020/10/07/doublex_rmmz_plugin_query/
* 9. https://www.patreon.com/posts/42482628
* 10. https://www.makerdevs.com/plugin/doublex-rmmz-plugin-query
* Mentioned Patreon Supporters:
* https://www.patreon.com/posts/71738797
*----------------------------------------------------------------------------
* # Contributors
* Authors:
* 1. DoubleX
* Plugin Development Collaborators:
* - None So Far
* Bug Reporters:
* - None So Far
* Compatibility Issue Raisers:
* - None So Far
* Feature Requesters:
* - None So Far
*----------------------------------------------------------------------------
* # Changelog
* { codebase: "1.1.0", plugin: "v1.00b" }(2020 Dec 2 GMT 0700):
* 1. You no longer have to edit the value of
* DoubleX_RMMZ.Plugin_Query.PLUGIN_NAME when changing this plugin
* file name
* { codebase: "1.0.2", plugin: "v1.00a" }(2020 Oct 6 GMT 1600):
* 1. 1st version of this plugin finished
*============================================================================*/
/*~struct~NewPluginQuery:
*
* @param name
* @desc The name of the new plugin query
* @default
*
* @param args
* @type string[]
* @desc The list of argument names of the new plugin query
* @default []
*
* @param funcContents
* @type note
* @desc The contents of the function of the new plugin query
* @default
*/
/*:
* @url https://www.patreon.com/doublex
* @target MZ
* @plugindesc Versions: { codebase: "1.1.0", plugin: "v1.00b" }
* Lets you use plugin queries in conditional branch and control variables
* @orderAfter DoubleX_RMMZ_Enhanced_Codebase
* @orderAfter DoubleX RMMZ Enhanced Codebase
* @base DoubleX RMMZ Enhanced Codebase
* @author DoubleX
*
* @param newDamageFormulaPluginQueries
* @type struct<NewPluginQuery>[]
* @desc Sets the list of new damage formula plugin queries
* This list shouldn't include those already added by other plugins
* @default []
*
* @param newEventCmdPluginQueries
* @type struct<NewPluginQuery>[]
* @desc Sets the list of new event command plugin queries
* This list shouldn't include those already added by other plugins
* @default []
*
* @help
*============================================================================
* ## Plugin Query Info
*----------------------------------------------------------------------------
* 1. General form
* queryName argName1 argName2 argName3 argNameI argNameN
* E.g.:
* - If the plugin query has its query name as abcdefg and arguments
* as h, i, j and k, then the plugin query is abcdefg h i j k
* 2. (Plugin Developers Only)Registration
* PluginManager.damageFormulaPluginQueries.set(name, func);
* - Registers a plugin query with the name being name and function
* returning the result being func
* - The first 5 arguments of the function must be the following:
* i. item - The skill/item having the damage formula
* ii. a - The subject executing the skill/item
* iii. b - The target having the damage formula applied to
* iv. v - The raw data list of the game variables
* v. sign - 1(damage)/-1(recovery)
* - The registered plugin query can be used in skill/item damage
* formulae
* E.g.:
* - PluginManager.damageFormulaPluginQueries.set("matDmg", (item, a, b, v, sign, baseDmg) => {
* return +baseDmg + a.mat * 2 - b.mdf * 2;
* });
* Will define the plugin query atkDmg in the skill/item damage
* formulae as baseDmg + a.mat * 2 - b.mdf * 2
* Where baseDmg must be a Number
* PluginManager.eventCmdPluginQueries.set(name, func);
* - Registers a plugin query with the name being name and function
* returning the result being func
* - The registered plugin query can be used in conditional branch and
* control variables event commands
* E.g.:
* - PluginManager.eventCmdPluginQueries.set("isActorAnyStateAffected", (actorId, paramIds) => {
* return $gameActors.actor(+actorId).isAnyStateAffected(paramIds.split("_").map(Number));
* });
* Will define the plugin query
* isActorAnyStateAffected actorId paramIds
* in the conditional branch/control variables event commands as
* $gameActors.actor(actorId).isAnyStateAffected(paramIds);
* If paramIds is written in the form of
* paramId1_paramId2_paramId3_paramIdI_paramIdN
*============================================================================
*/
// jshint esversion: 6
var DoubleX_RMMZ = DoubleX_RMMZ || {}; // var must be used or game will crash
(() => {
"use strict";
const src = document.currentScript.src;
const name = src.split("/").slice(-1)[0].split(".")[0].replace(/%20/g, " ");
console.info(src, name);
// Separates the version numbers with the rest to make the former more clear
DoubleX_RMMZ.Plugin_Query = {
PLUGIN_NAME: name,
VERSIONS: { codebase: "1.1.0", plugin: "v1.00b" }
}; // DoubleX_RMMZ.Plugin_Query
//
})();
(PQ => {
"use strict";
const pluginCodebaseVer = PQ.VERSIONS.codebase;
if (Utils.checkRMVersion(pluginCodebaseVer)) return;
console.warn(`Your codebase version is ${Utils.RPGMAKER_VERSION} but must be
at least ${pluginCodebaseVer} to use ${PQ.PLUGIN_NAME}`);
})(DoubleX_RMMZ.Plugin_Query);
/*============================================================================
* ## Plugin Implementations
* You need not edit this part as it's about how this plugin works
*----------------------------------------------------------------------------
* # Plugin Support Info:
* 1. Prerequisites
* - Little RMMZ plugin development proficiency to fully comprehend
* this plugin
* (Elementary Javascript exposures being able to write beginner
* codes up to 300LoC scale)
* 2. Parameter/Return value of type * means it might be of any type
* 3. Function signature with (**) means it might take any number of
* parameters of any type
* 4. Supposedly nullable variables are marked with the _ suffix in their
* names(but they can be sure to be non null in some cases)
* 5. Functions supposedly returning nullable values are marked with the
* _ suffix in their names(but their return values can be sure to be
* non null in some cases)
*----------------------------------------------------------------------------*/
if (DoubleX_RMMZ.Enhanced_Codebase) {
/*============================================================================*/
/*----------------------------------------------------------------------------
* ## Managers
*----------------------------------------------------------------------------*/
/*----------------------------------------------------------------------------
* # Edit class: PluginManager
* - Lets other plugins register their plugin queries to be used by users
*----------------------------------------------------------------------------*/
(($, PQ) => {
"use strict";
/*------------------------------------------------------------------------
* New public variable
*------------------------------------------------------------------------*/
// {Map(string, (**) -> *)} damageFormulaPluginQueries: Damage formula
// plugin queries
// {Map(string, (**) -> *)} eventCmdPluginQueries: Event cmd plugin queries
$.damageFormulaPluginQueries = new Map();
$.eventCmdPluginQueries = new Map();
const params = $.parameters(PQ.PLUGIN_NAME);
[
[
"newDamageFormulaPluginQueries",
"damageFormulaPluginQueries",
["item", "a", "b", "v", "sign"]
],
["newEventCmdPluginQueries", "eventCmdPluginQueries", []]
].forEach(([param, queryContainer, fixedArgs]) => {
JSON.parse(params[param]).map(JSON.parse).forEach(pluginQuery => {
const args = fixedArgs.concat(JSON.parse(pluginQuery.args));
args.push(JSON.parse(pluginQuery.funcContents));
$[queryContainer].set(pluginQuery.name, new Function(...args));
});
});
})(PluginManager, DoubleX_RMMZ.Plugin_Query);
/*----------------------------------------------------------------------------*/
/*----------------------------------------------------------------------------
* ## Objects
*----------------------------------------------------------------------------*/
/*----------------------------------------------------------------------------
* # Edit class: Game_Action
* - Runs plugin queries in the skill/item damage formulae
*----------------------------------------------------------------------------*/
(($, MZ_EC, PQ) => {
"use strict";
const klassName = "Game_Action", EC_GI = MZ_EC[klassName].new, {
NEW,
ORIG
} = MZ_EC.setKlassContainer(klassName, $, PQ), GI = PQ[klassName];
MZ_EC.extendFunc(EC_GI, GI, "_evalDamageFormula_", function(b, sign, damageFormula) {
// Added to return the plugin query result if it's a plugin query
const query = NEW.PLUGIN_QUERY(damageFormula);
if (NEW._IS_PLUGIN_QUERY(query)) {
return NEW._pluginQueryResult.call(this, b, sign, query);
}
//
return ORIG._evalDamageFormula_.apply(this, arguments);
}); // v1.00a - v1.00a
// The plugin query should be as forgiving to syntax error as possible
NEW.PLUGIN_QUERY = scriptLine => scriptLine.split(/\s+/);
//
NEW._IS_PLUGIN_QUERY = query => {
return PluginManager.damageFormulaPluginQueries.has(query[0]);
}; // NEW._IS_PLUGIN_QUERY
/**
* The this pointer is Game_Action.prototype
* Idempotent
* @author DoubleX @interface @since v1.00a @version v1.00a
* @param {Game_Battler} b - The target to have damage formula applied
* @enum @param {number} sign - Whether it's damage or recovery(1/-1)
* @enum @param {string[]} query - The query name and argument value strings
* @returns {number?} The evaluated applied damage of skill/item to target
*/
NEW._pluginQueryResult = function(b, sign, query) {
const item = this.item();
const [a, v] = [this.subject(), $gameVariables._data];
return NEW._PLUGIN_QUERY_RESULT(item, a, b, v, sign, query);
}; // NEW._pluginQueryResult
NEW._PLUGIN_QUERY_RESULT = (item, a, b, v, sign, query) => {
const name = query.shift();
const args = [item, a, b, v, sign].fastMerge(query);
return PluginManager.damageFormulaPluginQueries.get(name)(...args);
}; // NEW._PLUGIN_QUERY_RESULT
})(Game_Interpreter.prototype, DoubleX_RMMZ.Enhanced_Codebase,
DoubleX_RMMZ.Plugin_Query);
/*----------------------------------------------------------------------------
* # Edit class: Game_Interpreter
* - Runs plugin queries in conditional branch/control variables commands
*----------------------------------------------------------------------------*/
(($, MZ_EC, PQ) => {
"use strict";
const klassName = "Game_Interpreter", EC_GI = MZ_EC[klassName].new, {
NEW,
ORIG
} = MZ_EC.setKlassContainer(klassName, $, PQ);
const GA = PQ.Game_Action.new, GI = PQ[klassName];
MZ_EC.extendFunc(EC_GI, GI, "evalScriptLine_", function(scriptLine) {
// Added to return the plugin query result if it's a plugin query
const query = GA.PLUGIN_QUERY(scriptLine);
if (NEW._IS_PLUGIN_QUERY(query)) return NEW._PLUGIN_QUERY_RESULT(query);
//
return ORIG.evalScriptLine_.apply(this, arguments);
}); // v1.00a - v1.00a
NEW._IS_PLUGIN_QUERY = query => {
return PluginManager.eventCmdPluginQueries.has(query[0]);
}; // NEW._IS_PLUGIN_QUERY
NEW._PLUGIN_QUERY_RESULT = query => {
return PluginManager.eventCmdPluginQueries.get(query.shift())(...query);
}; // NEW._PLUGIN_QUERY_RESULT
})(Game_Interpreter.prototype, DoubleX_RMMZ.Enhanced_Codebase,
DoubleX_RMMZ.Plugin_Query);
/*============================================================================*/
const curMZECVer = DoubleX_RMMZ.Enhanced_Codebase.VERSIONS.plugin;
const minMZECVer = "v0.02b";
if (curMZECVer < minMZECVer) {
console.warn(`The version of DoubleX RMMZ Enhanced Codebase is
${curMZECVer} but should be at least ${minMZECVer}`);
}
} else {
console.warn(`DoubleX RMMZ Enhanced Codebase should be placed above
${DoubleX_RMMZ.Plugin_Query.PLUGIN_NAME}`);
} // if (DoubleX_RMMZ.Enhanced_Codebase)