2017-02-09 00:00:35 +00:00
|
|
|
module TS.SpaceTac {
|
2017-05-10 21:29:20 +00:00
|
|
|
/**
|
|
|
|
* Equipment generator from loot templates
|
|
|
|
*
|
|
|
|
* Loot templates are automatically populated from the "SpaceTac.Equipments" namespace
|
|
|
|
*/
|
2015-01-14 00:00:00 +00:00
|
|
|
export class LootGenerator {
|
|
|
|
// List of available templates
|
2017-05-10 21:29:20 +00:00
|
|
|
templates: LootTemplate[]
|
2015-01-14 00:00:00 +00:00
|
|
|
|
|
|
|
// Random generator that will be used
|
2017-05-10 21:29:20 +00:00
|
|
|
random: RandomGenerator
|
|
|
|
|
|
|
|
// Filter to select a subset of templates
|
|
|
|
templatefilter: (template: LootTemplate) => boolean
|
2015-01-14 00:00:00 +00:00
|
|
|
|
2017-02-26 17:44:15 +00:00
|
|
|
constructor(random = RandomGenerator.global, populate: boolean = true) {
|
2015-01-14 00:00:00 +00:00
|
|
|
this.templates = [];
|
2015-02-13 00:00:00 +00:00
|
|
|
this.random = random;
|
2017-05-10 21:29:20 +00:00
|
|
|
this.templatefilter = () => true;
|
2015-01-14 00:00:00 +00:00
|
|
|
|
2015-02-13 00:00:00 +00:00
|
|
|
if (populate) {
|
|
|
|
this.populate();
|
|
|
|
}
|
2015-01-14 00:00:00 +00:00
|
|
|
}
|
|
|
|
|
2017-05-10 21:29:20 +00:00
|
|
|
/**
|
|
|
|
* Set the template filter for next generations
|
|
|
|
*/
|
|
|
|
setTemplateFilter(filter: (template: LootTemplate) => boolean) {
|
|
|
|
this.templatefilter = filter;
|
|
|
|
}
|
|
|
|
|
2015-01-14 00:00:00 +00:00
|
|
|
// Fill the list of templates
|
|
|
|
populate(): void {
|
2017-05-02 21:33:58 +00:00
|
|
|
let templates: LootTemplate[] = [];
|
|
|
|
let namespace: any = TS.SpaceTac.Equipments;
|
|
|
|
for (var template_name in namespace) {
|
2017-02-07 00:08:07 +00:00
|
|
|
if (template_name && template_name.indexOf("Abstract") != 0) {
|
2017-05-02 21:33:58 +00:00
|
|
|
let template_class = namespace[template_name];
|
|
|
|
let template: LootTemplate = new template_class();
|
2015-01-14 00:00:00 +00:00
|
|
|
templates.push(template);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
this.templates = templates;
|
2015-01-14 00:00:00 +00:00
|
|
|
}
|
|
|
|
|
2017-04-18 19:51:23 +00:00
|
|
|
// Generate a random equipment for a specific level
|
2015-01-14 00:00:00 +00:00
|
|
|
// If slot is specified, it will generate an equipment for this slot type specifically
|
|
|
|
// If no equipment could be generated from available templates, null is returned
|
2017-04-20 21:20:50 +00:00
|
|
|
generate(level: number, quality = EquipmentQuality.COMMON, slot: SlotType | null = null): Equipment | null {
|
2015-01-14 00:00:00 +00:00
|
|
|
// Generate equipments matching conditions, with each template
|
2017-05-10 21:29:20 +00:00
|
|
|
let templates = this.templates.filter(this.templatefilter).filter(template => slot == null || slot == template.slot);
|
|
|
|
let equipments = templates.map(template => template.generate(level, quality, this.random));
|
2015-01-14 00:00:00 +00:00
|
|
|
|
|
|
|
// No equipment could be generated with given conditions
|
|
|
|
if (equipments.length === 0) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Pick a random equipment
|
|
|
|
return this.random.choice(equipments);
|
|
|
|
}
|
2017-04-25 23:27:42 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Generate a random equipment of highest level, from a given set of skills
|
|
|
|
*/
|
|
|
|
generateHighest(skills: ShipSkills, quality = EquipmentQuality.COMMON, slot: SlotType | null = null): Equipment | null {
|
2017-05-10 21:29:20 +00:00
|
|
|
let templates = this.templates.filter(this.templatefilter).filter(template => slot == null || slot == template.slot);
|
2017-04-25 23:27:42 +00:00
|
|
|
let candidates: Equipment[] = [];
|
|
|
|
let level = 1;
|
|
|
|
|
|
|
|
templates.forEach(template => {
|
|
|
|
let equipment = template.generateHighest(skills, quality, this.random);
|
|
|
|
if (equipment && equipment.level >= level) {
|
|
|
|
if (equipment.level > level) {
|
|
|
|
candidates.splice(0);
|
|
|
|
level = equipment.level;
|
|
|
|
}
|
|
|
|
candidates.push(equipment);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return (candidates.length == 0) ? null : this.random.choice(candidates);
|
|
|
|
}
|
2015-01-14 00:00:00 +00:00
|
|
|
}
|
|
|
|
}
|