Widget:Monster: Difference between revisions

From Path of Exile Wiki
Jump to navigation Jump to search
>OmegaK2
(WIP)
 
>OmegaK2
(Working out the correct stat should work now)
Line 7: Line 7:
     static added(values, value) {
     static added(values, value) {
         values.forEach(function (other) {
         values.forEach(function (other) {
             value = value + other;
             value.add(other);
         });
         });
        return value;
     }
     }
     static increased(values, value) {
     static increased(values, value) {
         var multi
         var multi = new Stat('', 0);
         values.forEach(function (inc) {
         values.forEach(function (inc) {
             multi = multi + inc
             multi.add(inc);
         });
         });
         return value * (1 + multi/100);
         multi.div(100);
        multi.add(1);
        value.mult(multi);
     }
     }
     static more(values, value) {
     static more(values, value) {
         values.forEach(function (multi) {
         values.forEach(function (multi) {
             value = value * (1 + multi/100);
             var temp = multi.copy();
            temp.div(100);
            temp.add(1);
            value.mult(temp);
         });
         });
        return value;
     }
     }
     static less(values, value) {
     static less(values, value) {
         values.forEach(function (multi) {
         values.forEach(function (multi) {
             value = value * (1 - multi/100);
             var temp = multi.copy();
            temp.div(100);
            // since it's less it's negative and subtracted from 1
            temp.mult(-1);
            temp.add(1);
            value.mult(temp);
         });
         });
         return value;
    }
}
 
class Stat {
    constructor(id, min, max) {
        this.id = id;
        this.min = min;
        if (typeof max === 'undefined') {
            max = min;
        }
        this.max = max;
    }
   
    copy() {
         return new Stat(this.id, this.min, this.max);
    }
   
    _type_check (other, func_number, func_stat) {
        var t = typeof other;
        if (t === 'number') {
            func_number(this);
        } else if (t === 'object' && other.constructor.name == 'Stat') {
            func_stat(this);
        } else {
            throw 'Stat arithmetric requires a stat object or number, got type "' + t + '"';
        }
    }
   
    add(other) {
        this._type_check(other,
            function(stat) {
                stat.min += other;
                stat.max += other;
            },
            function(stat) {
                stat.min += other.min;
                stat.max += other.max;
            },
        )
    }
    sub(other) {
        this._type_check(other,
            function(stat) {
                stat.min -= other;
                stat.max -= other;
            },
            function(stat) {
                stat.min -= other.min;
                stat.max -= other.max;
            },
        )
    }
    mult(other) {
        this._type_check(other,
            function(stat) {
                stat.min *= other;
                stat.max *= other;
            },
            function(stat) {
                stat.min *= other.min;
                stat.max *= other.max;
            },
        )
    }
    div(other) {
        this._type_check(other,
            function(stat) {
                stat.min /= other;
                stat.max /= other;
            },
            function(stat) {
                stat.min /= other.min;
                stat.max /= other.max;
            },
        )
     }
     }
}
}
Line 100: Line 182:
         var v = this.stats[stat_id];
         var v = this.stats[stat_id];
         if (typeof v === 'undefined') {
         if (typeof v === 'undefined') {
            if (typeof value === 'number') {
                value = new Stat(stat_id, value);
            }
             this.stats[stat_id] = value;
             this.stats[stat_id] = value;
         } else {
         } else {
             this.stats[stat_id] = v + value;
             this.stats[stat_id].add(value);
         }
         }
     }
     }
      
      
     calcuate_stat(type) {
     calculate_property(type) {
         var value = Monster.level_data[this.level][type];
         var value = new Stat(type, Monster.level_data[this.level][type]);
         var calc = Monster.calculation_params[type];
         var calc = Monster.calculation_params[type];
          
          
Line 138: Line 223:
                     }
                     }
                 });
                 });
                 value = StatCalculation[stat_calc_type](values, value);
                 StatCalculation[stat_calc_type](values, value);
             }
             }
         }
         }
Line 168: Line 253:
};
};


// List of things affected by each stat to calcuate a final value
// List of things affected by each stat to calculate a final value
Monster.calculation_params = {
Monster.calculation_params = {
     damage: {
     damage: {
Line 351: Line 436:
             var v = value.title;
             var v = value.title;
             var rarity = v.mod_id.replace(/Monster(Magic|Rare|Unique)[0-9]*$/, '$1');
             var rarity = v.mod_id.replace(/Monster(Magic|Rare|Unique)[0-9]*$/, '$1');
             Monster.rarity_data[rarity][v.stat_id] = {
             Monster.rarity_data[rarity][v.stat_id] = new Stat(v.stat_id, Number(v.min), Number(v.max));
                min: Number(v.min),
                max: Number(v.max),
            };
         });
         });
         _run_final_init();
         _run_final_init();
Line 365: Line 447:
     console.log('Data loaded');
     console.log('Data loaded');
     var m = new Monster(86, 'Rare', is_map_monster=true, is_boss=true);
     var m = new Monster(86, 'Rare', is_map_monster=true, is_boss=true);
     console.log(m.calcuate_stat('damage'));
     console.log(m.calculate_property('damage'));
}
}


monster_init();
monster_init();
//
// Test functions
//
function test_stat() {
    a = new Stat('id', 1, 1);
    b = new Stat('id', 5, 5);
    a.add(b);
    console.log('+ 6?', a.min, a.max);
    a.sub(b);
    console.log('- 1?', a.min, a.max);
    a.mult(b);
    console.log('* 5?', a.min, a.max);
    a.div(b);
    console.log('/ 1?', a.min, a.max);
    a.add(10);
    console.log('+ 11?', a.min, a.max);
    a.sub(10);
    console.log('- 1?', a.min, a.max);
    a.mult(10);
    console.log('* 10?', a.min, a.max);
    a.div(10);
    console.log('/ 1?', a.min, a.max);
}
//test_stat();


//window.addEventListener('load', monster_init);
//window.addEventListener('load', monster_init);
}
}
</script></includeonly>
</script></includeonly>

Revision as of 19:32, 7 February 2020