02409tt

Warper
Member
Joined
Jul 23, 2017
Messages
3
Reaction score
0
First Language
English
Primarily Uses
RMMV
Hey all! This is my first post on these forums so I hope I'm in the right place.

In my game, you have to learn each of 6 elements as you progress through the game. In the style of Pokémon or Rock Paper Scissors, each element has elements that it's effective or ineffective against. As the player learns new elements, I'd like the effectiveness (Element Rate) across each element to be averaged out between all the elements the player knows.

For example, the player knows Air, Earth, and Fire. Lightning is effective against Air, ineffective against Earth, and normal against Fire. I want these to average out to be normal, or a 100% Lightning element rate for the player.

I have some knowledge of programming, but not too much with JavaScript specifically. Below is my code, and for the life of me, I can't figure out how to make it work the way I want it to.
If anyone with more experience could help me figure this out, I'd greatly appreciate it! Thanks!

Code:
//=============================================================================
// Element Rate Calculator
// by 02409tt
// Date: 5/29/18
 
//=============================================================================
 
/*:
 * @plugindesc Element Rate Calculator
 * @author 02409tt
 *
 * @param ---Global Rates---
 * @default
 *
 * @param Normal
 * @desc The element rate of normal effectiveness.
 * @parent ---Global Rates---
 * @default 1
 *
 * @param Strong
 * @desc The element rate of strong effectiveness.
 * @parent ---Global Rates---
 * @default 1.25
 *
 * @param Weak
 * @desc The element rate of weak effectiveness.
 * @parent ---Global Rates---
 * @default .75
 *
 * @param ---Elements---
 * @default
 *
 * @param Earth
 * @type struct<Elements>
 * @parent ---Elements---
 * @desc Adjust the effectiveness of elements against the Earth element here.
 * @default {"Earth":"Weak","Fire":"Strong","Air":"Weak","Water":"Strong","Poison":"Weak","Lightning":"Weak"}
 *
 * @param Fire
 * @type struct<Elements>
 * @parent ---Elements---
 * @desc Adjust the effectiveness of elements against the Fire element here.
 * @default {"Earth":"Weak","Fire":"Weak","Air":"Strong","Water":"Strong","Poison":"Normal","Lightning":"Normal"}
 *
 * @param Air
 * @type struct<Elements>
 * @parent ---Elements---
 * @desc Adjust the effectiveness of elements against the Air element here.
 * @default {"Earth":"Weak","Fire":"Weak","Air":"Weak","Water":"Normal","Poison":"Normal","Lightning":"Strong"}
 *
 * @param Water
 * @type struct<Elements>
 * @parent ---Elements---
 * @desc Adjust the effectiveness of elements against the Water element here.
 * @default {"Earth":"Weak","Fire":"Weak","Air":"Normal","Water":"Weak","Poison":"Strong","Lightning":"Strong"}
 *
 * @param Poison
 * @type struct<Elements>
 * @parent ---Elements---
 * @desc Adjust the effectiveness of elements against the Poison element here.
 * @default {"Earth":"Strong","Fire":"Normal","Air":"Normal","Water":"Weak","Poison":"Weak","Lightning":"Normal"}
 *
 * @param Lightning
 * @type struct<Elements>
 * @parent ---Elements---
 * @desc Adjust the effectiveness of elements against the Lightning element here.
 * @default {"Earth":"Strong","Fire":"Normal","Air":"Weak","Water":"Weak","Poison":"Normal","Lightning":"Weak"}
 *
 * @param ---Switches---
 * @default
 *
 * @param Earth Learned ID
 * @desc The ID of the switch that is ON when Earth is learned.
 * @parent ---Switches---
 * @default 28
 *
 * @param Fire Learned ID
 * @desc The ID of the switch that is ON when Fire is learned.
 * @parent ---Switches---
 * @default 29
 *
 * @param Air Learned ID
 * @desc The ID of the switch that is ON when Air is learned.
 * @parent ---Switches---
 * @default 30
 *
 * @param Water Learned ID
 * @desc The ID of the switch that is ON when Water is learned.
 * @parent ---Switches---
 * @default 31
 *
 * @param Poison Learned ID
 * @desc The ID of the switch that is ON when Poison is learned.
 * @parent ---Switches---
 * @default 32
 *
 * @param Lightning Learned ID
 * @desc The ID of the switch that is ON when Lightning is learned.
 * @parent ---Switches---
 * @default 33
*/
/*~struct~Elements:
 *
 * @param Earth
 * @type text
 * @desc Effectiveness of Earth against other elements.
 * @default Normal
 *
 * @param Fire
 * @type text
 * @desc Effectiveness of Fire against other elements.
 * @default Normal
 *
 * @param Air
 * @type text
 * @desc Effectiveness of Air against other elements.
 * @default Normal
 *
 * @param Water
 * @type text
 * @desc Effectiveness of Water against other elements.
 * @default Normal
 *
 * @param Poison
 * @type text
 * @desc Effectiveness of Poison against other elements.
 * @default Normal
 *
 * @param Lightning
 * @type text
 * @desc Effectiveness of Lightning against other elements.
 * @default Normal
 */
 
 
(function() {
  var params = PluginManager.parameters('ElementRateCalculator');
 
  function elementRateUpdater(earthValue, fireValue, airValue, waterValue, poisonValue, lightningValue) {
    $gameParty.leader().currentClass().traits.push({
      code: 11,
      dataId: 1,
      value: earthValue});
    $gameParty.leader().currentClass().traits.push({
      code: 11,
      dataId: 2,
      value: fireValue});
    $gameParty.leader().currentClass().traits.push({
      code: 11,
      dataId: 3,
      value: airValue});
    $gameParty.leader().currentClass().traits.push({
      code: 11,
      dataId: 4,
      value: waterValue});
    $gameParty.leader().currentClass().traits.push({
      code: 11,
      dataId: 5,
      value: poisonValue});
    $gameParty.leader().currentClass().traits.push({
      code: 11,
      dataId: 6,
      value: lightningValue});
  };
 
  function elementRateInitialize() {
    elementRateUpdater(1, 1, 1, 1, 1, 1);
  };
 
  function elementRateManager() {
 
    var stringToValue = {
      'Normal': Number(params['Normal']),
      'Strong': Number(params['Strong']),
      'Weak': Number(params['Weak'])
    };
 
    var possibleLearnedElements = {
      'earthLearned': $gameSwitches.value(Number(params['Earth Learned ID'])),
      'fireLearned': $gameSwitches.value(Number(params['Fire Learned ID'])),
      'airLearned': $gameSwitches.value(Number(params['Air Learned ID'])),
      'waterLearned': $gameSwitches.value(Number(params['Water Learned ID'])),
      'poisonLearned': $gameSwitches.value(Number(params['Poison Learned ID'])),
      'lightningLearned': $gameSwitches.value(Number(params['Lightning Learned ID']))
    };
 
    var learnedElements = [];
 
    if (possibleLearnedElements['earthLearned'] == true) {
      learnedElements.push('Earth');
    };
    if (possibleLearnedElements['fireLearned'] == true) {
      learnedElements.push('Fire');
    };
    if (possibleLearnedElements['airLearned'] == true) {
      learnedElements.push('Air');
    };
    if (possibleLearnedElements['waterLearned'] == true) {
      learnedElements.push('Water');
    };
    if (possibleLearnedElements['poisonLearned'] == true) {
      learnedElements.push('Poison');
    };
    if (possibleLearnedElements['lightningLearned'] == true) {
      learnedElements.push('Lightning');
    };
 
    var numberLearned = learnedElements.length;
 
    var earthAddition = 0;
    var fireAddition = 0;
    var airAddition = 0;
    var waterAddition = 0;
    var poisonAddition = 0;
    var lightningAddition = 0;
 
    for (var i = 0; i < numberLearned; i++) {
      var interactions = JSON.parse(params[learnedElements[i]]);
      earthAddition = stringToValue[interactions.Earth] + earthAddition;
      fireAddition = stringToValue[interactions.Fire] + fireAddition;
      airAddition = stringToValue[interactions.Air] + airAddition;
      waterAddition = stringToValue[interactions.Water] + waterAddition;
      poisonAddition = stringToValue[interactions.Poison] + poisonAddition;
      lightningAddition = stringToValue[interactions.Lightning] + lightningAddition;
    };
 
    overallEarthRate = earthAddition/numberLearned;
    overallFireRate = fireAddition/numberLearned;
    overallAirRate = airAddition/numberLearned;
    overallWaterRate = waterAddition/numberLearned;
    overallPoisonRate = poisonAddition/numberLearned;
    overallLightningRate = lightningAddition/numberLearned;
 
  };
 
  // Plugin Commands //
 
  var _Game_Interpreter_pluginCommand = Game_Interpreter.prototype.pluginCommand;
 
  Game_Interpreter.prototype.pluginCommand = function(command, args) {
      _Game_Interpreter_pluginCommand.call(this, command, args);
      if (command == "ElementRate") {
 
        var subCommand = args[0];
 
        if (subCommand == "update") {
          elementRateUpdater(1,1,1,1,1,1);
          elementRateManager();
          elementRateUpdater(overallEarthRate, overallFireRate, overallAirRate, overallWaterRate, overallPoisonRate, overallLightningRate);
        };
        if (subCommand == "set") {
          earthValue = Number(args[1]);
          fireValue = Number(args[2]);
          airValue = Number(args[3]);
          waterValue = Number(args[4]);
          poisonValue = Number(args[5]);
          lightningValue = Number(args[6]);
          elementRateUpdater(earthValue, fireValue, airValue, waterValue, poisonValue, lightningValue);
        };
 
      };
  };
 
  })();
 

Jachan

Veteran
Veteran
Joined
May 23, 2015
Messages
94
Reaction score
4
First Language
Sign Language
Primarily Uses
RMMV
Have you tried Yanfly's "Element Core" plugin?
 

02409tt

Warper
Member
Joined
Jul 23, 2017
Messages
3
Reaction score
0
First Language
English
Primarily Uses
RMMV
Have you tried Yanfly's "Element Core" plugin?
Yes, unless I'm missing something. I need a bit more customization since every element rate changes depending on which elements are learned in which order, etc.
 

Jachan

Veteran
Veteran
Joined
May 23, 2015
Messages
94
Reaction score
4
First Language
Sign Language
Primarily Uses
RMMV
Okay, just a quick asking is all.

Now I have read the whole... And I think I see your point... It would be pretty hard to do that without direct same-element (thunder magic to thunder resistance), for sure. Interesting idea though. :)

Sorry, I can't help you this out because I don't know the real solution to this. :(
 

caethyril

^_^
Global Mod
Joined
Feb 21, 2018
Messages
2,855
Reaction score
2,192
First Language
EN
Primarily Uses
RMMZ
There's a scope error: overallEarthRate, overallFireRate, etc are local to the elementRateManager method and cannot be "seen" outside it. Thus, you're feeding undefined variables into your elementRateUpdater method, which get implicitly converted to zeroes. :kaoback:

I think one way around this would be to simply define the "overall" variables at the plugin scope, e.g. immediately after the params variable declaration:
Code:
var params = PluginManager.parameters('ElementRateCalculator');
var overallEarthRate = 0;
var overallFireRate = 0;
etc...

I don't know which languages you've prior experience with, but if you're more used to explicit declaration then you may be interested in JavaScript's strict mode. It helps to catch little slip-ups like this, at the expense of having to be a little more careful with how you code. :kaoluv:
 

Latest Threads

Latest Profile Posts

I'm so annoyed with myself...I can't get this code to work so it looks like I'm going the plugin route for a problem I should be able to do myself :kaolivid:Great. Now I get to murder save file compatibility. Again.
Can't believe my code from four years ago is still working! Did some expansions, and...
1635194553179.png
In related news, I may have an update for my old Extra status window plugin ready. That is, once I manage to sit down and update the documentation... and the demo project... :kaoswt:
How does one pronounce Godot? I've heard it pronounced a couple of different ways, and I genuinely don't know the proper way.
March 2020 ...
1635188823939.jpeg
... vs October 2021
You look like you had a rough day...Here, have a Baby Panda!

Forum statistics

Threads
116,077
Messages
1,095,547
Members
151,442
Latest member
Sasha_If
Top