Advertisement
RefresherTowel

Statistics System

Aug 22nd, 2023
1,248
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. enum statistic_types {
  2.     REAL,
  3.     PERCENTAGE,
  4.     FLAT_PERCENT,
  5.     MULTIPLIER,
  6.     TIMER
  7. }
  8.  
  9. ///@func    Statistic(base_value, owner_id, name, statistic_type)
  10. ///@param   {real}                  _base_value
  11. ///@param   {struct|ID.Instance}    _owner_id
  12. ///@param   {string}                _name
  13. ///@param   {real}                  _type
  14. function Statistic(_base_value, _owner_id, _name = "Unknown", _type = statistic_types.REAL) constructor {
  15.     modifiers               = [];
  16.     single_shot_modifiers   = [];
  17.     modifier_size           = 0;
  18.     base_value              = _base_value;
  19.     current_value           = _base_value;
  20.     altered                 = false;
  21.     owner                   = _owner_id;
  22.     has_count               = false;
  23.     has_duration            = false;
  24.     name                    = _name;
  25.     type                    = _type;
  26.     if (is_instanceof(owner, Wand)) {
  27.         tooltip_label           = "wand_" +string_lower(string_replace_all(_name, " ", "_"));
  28.     }
  29.     else {
  30.         tooltip_label       = string_lower(string_replace_all(_name, " ", "_"));
  31.     }
  32.    
  33.     ///@func    AddModifier(mod);
  34.     ///@param   {Struct.Modifier}   _mod
  35.     static AddModifier = function(_mod) {
  36.         array_push(modifiers,_mod);
  37.         array_sort(modifiers,function(a,b) {
  38.             return a.type-b.type;
  39.         });
  40.         modifier_size++;
  41.         for (var i=0;i<modifier_size;i++) {
  42.             var __mod = modifiers[i];
  43.             var _mod_type = __mod.type;
  44.             if (_mod_type == modifier_types.DURATION) {
  45.                 has_duration = true;
  46.                 break;
  47.             }
  48.         }
  49.         altered = true;
  50.     }
  51.    
  52.     ///@func    FindModifierBySource(source)
  53.     ///@param   {Struct.Modifier}   _source
  54.     ///@returns {Array<Struct.Modifier>}
  55.     static FindModifierBySource = function(_source) {
  56.         var _mods = [];
  57.         for (var i=0;i<modifier_size;i++) {
  58.             if (modifiers[i].source == _source) {
  59.                 array_push(_mods,modifiers[i]);
  60.             }
  61.         }
  62.         return _mods;
  63.     }
  64.    
  65.     ///@func    RemoveModifierBySource(source);
  66.     ///@param   {Struct.Modifier}   _source
  67.     ///@returns {bool}
  68.     static RemoveModifierBySource = function(_source) {
  69.         var _deleted = false;
  70.         for (var i=modifier_size-1;i>=0;i--) {
  71.             if (modifiers[i].source == _source) {
  72.                 delete modifiers[i];
  73.                 array_delete(modifiers,i,1);
  74.                 _deleted = true;
  75.                 modifier_size--;
  76.             }
  77.         }
  78.         if (_deleted) {
  79.             altered = true;
  80.             return true;
  81.         }
  82.         return false;
  83.     }
  84.     // There are other things you might want to remove or find by, such as ID or position in the modifiers array, but I'll leave it at just removing and finding using the source for brevity's sake
  85.    
  86.     ///@func    GetValue()
  87.     ///@returns {real}
  88.     static GetValue = function() {
  89.         if (altered) {
  90.             var _value = base_value;
  91.             var _add = 0;
  92.             var _multi_flat = 1;
  93.             var _multi_pct = 1;
  94.             for (var i=0;i<modifier_size;i++) {
  95.                 var _mod = modifiers[i];
  96.                 switch (_mod.math_type) {
  97.                     case modifier_math.ADD:
  98.                         _add += _mod.value;
  99.                     break;
  100.                     case modifier_math.MULTIPLY_FLAT:
  101.                         _multi_flat += _mod.value;
  102.                     break;
  103.                     case modifier_math.MULTIPLY_PCT:
  104.                         _multi_pct *= _mod.value;
  105.                     break;
  106.                 }
  107.             }
  108.             _value += _add;
  109.             _value *= _multi_flat;
  110.             _value *= _multi_pct;
  111.             current_value = _value;
  112.             altered = false;
  113.         }
  114.         return current_value;
  115.     }
  116.    
  117.     ///@func    GetBaseValue()
  118.     ///@returns {real}
  119.     static GetBaseValue = function() {
  120.         return base_value;
  121.     }
  122.    
  123.     ///@func    GetValueBySource(source)
  124.     ///@param   {struct}    _source
  125.     ///@returns {real}
  126.     static GetValueBySource = function(_source) {
  127.         var _value = 0;
  128.         var _add = 0;
  129.         var _multi_flat = 1;
  130.         var _multi_pct = 1;
  131.         for (var i=0;i<modifier_size;i++) {
  132.             var _mod = modifiers[i];
  133.             if (_mod.source == _source) {
  134.                 switch (_mod.math_type) {
  135.                     case modifier_math.ADD:
  136.                         _add += _mod.value;
  137.                     break;
  138.                     case modifier_math.MULTIPLY_FLAT:
  139.                         _multi_flat += _mod.value;
  140.                     break;
  141.                     case modifier_math.MULTIPLY_PCT:
  142.                         _multi_pct *= _mod.value;
  143.                     break;
  144.                 }
  145.             }
  146.         }
  147.         _value += _add;
  148.         _value *= _multi_flat;
  149.         _value *= _multi_pct;
  150.         return _value;
  151.                    
  152.     }
  153.    
  154.     ///@func    AlterValue(amount)
  155.     ///@param   {real}  _amount
  156.     static AlterValue = function(_amount) {
  157.         base_value += _amount;
  158.         altered = true;
  159.     }
  160.    
  161.     ///@func    SetBaseValue(amount)
  162.     ///@param   {real}  _amount
  163.     static SetBaseValue = function(_amount) {
  164.         base_value = _amount;
  165.         altered = true;
  166.     }
  167.    
  168.     ///@func    PrintValue(value)
  169.     ///@param   {real}  _value
  170.     ///@returns {string}
  171.     static PrintValue = function(_value) {
  172.         switch (type) {
  173.             case statistic_types.PERCENTAGE:
  174.                 return $"{_value * 100}%";
  175.             case statistic_types.MULTIPLIER:
  176.                 return $"{_value}x";
  177.             case statistic_types.FLAT_PERCENT:
  178.                 return $"{_value}%";
  179.             case statistic_types.TIMER:
  180.                 return $"{_value / SECOND}s";
  181.             default:
  182.                 return $"{_value}";
  183.         }
  184.     }
  185. }
  186.  
  187. enum modifier_math {
  188.     ADD,
  189.     MULTIPLY_FLAT,
  190.     MULTIPLY_PCT,
  191.     NUM
  192. }
  193.  
  194. enum modifier_types {
  195.     DURATION,
  196.     PERSISTENT,
  197.     NUM
  198. }
  199.  
  200. ///@func    Modifier(source_id, mod_type, math_op, value, duration, stat_type)
  201. ///@param   {Id.Instance|Struct.SpellBoost|Struct.Wand} _source_id
  202. ///@param   {real}                                      _mod_type
  203. ///@param   {real}                                      _math_op
  204. ///@param   {real}                                      _value
  205. ///@param   {real}                                      _duration
  206. ///@param   {real}                                      _stat_type
  207. function Modifier(_source_id,_mod_type,_math_op,_value,_duration = undefined, _stat_type = statistic_types.REAL) constructor {
  208.     source = _source_id;
  209.     type = _mod_type;
  210.     math_type = _math_op;
  211.     value = _value;
  212.     duration = _duration;
  213.     stat_type = _stat_type;
  214.     try {
  215.         source_name = source.name;
  216.     }
  217.     catch(_err) {
  218.         source_name = "Unknown";
  219.     }
  220. }
  221.  
  222.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement