From 135123e7f16e9b83e6660bb15780284dc9c25cd0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C3=ABl=20Lemaire?= Date: Sun, 26 Feb 2017 18:44:15 +0100 Subject: [PATCH] Use RandomGenerator from tscommon --- src/core/Battle.spec.ts | 8 +++--- src/core/BattleOutcome.spec.ts | 8 +++--- src/core/BattleOutcome.ts | 4 +-- src/core/LootGenerator.spec.ts | 2 +- src/core/LootGenerator.ts | 2 +- src/core/LootTemplate.ts | 9 +++---- src/core/NameGenerator.spec.ts | 2 +- src/core/NameGenerator.ts | 2 +- src/core/RandomGenerator.ts | 49 ---------------------------------- src/core/Ship.spec.ts | 4 +-- src/core/Ship.ts | 6 ++--- src/core/Star.ts | 12 ++++----- src/core/StarLocation.spec.ts | 4 +-- src/core/StarLocation.ts | 8 +++--- src/core/Universe.ts | 4 +-- src/core/ai/BullyAI.spec.ts | 4 +-- 16 files changed, 39 insertions(+), 89 deletions(-) delete mode 100644 src/core/RandomGenerator.ts diff --git a/src/core/Battle.spec.ts b/src/core/Battle.spec.ts index 1a9102f..2e50b63 100644 --- a/src/core/Battle.spec.ts +++ b/src/core/Battle.spec.ts @@ -18,7 +18,7 @@ module TS.SpaceTac { var battle = new Battle(fleet1, fleet2); expect(battle.play_order.length).toBe(0); - var gen = new RandomGenerator(1.0, 0.1, 1.0, 0.2, 0.6); + var gen = new SkewedRandomGenerator([1.0, 0.1, 1.0, 0.2, 0.6]); battle.throwInitiative(gen); expect(battle.play_order.length).toBe(5); @@ -77,7 +77,7 @@ module TS.SpaceTac { expect(battle.playing_ship_index).toBeNull(); // Force play order - var gen = new RandomGenerator(0.1, 0.2, 0.0); + var gen = new SkewedRandomGenerator([0.1, 0.2, 0.0]); battle.throwInitiative(gen); expect(battle.playing_ship).toBeNull(); @@ -127,7 +127,7 @@ module TS.SpaceTac { spyOn(ship3, "startTurn").and.callThrough(); // Force play order - var gen = new RandomGenerator(0.3, 0.2, 0.1); + var gen = new SkewedRandomGenerator([0.3, 0.2, 0.1]); battle.throwInitiative(gen); battle.advanceToNextShip(); @@ -207,7 +207,7 @@ module TS.SpaceTac { ship4.setArenaPosition(12, 12); var battle = new Battle(fleet1); - battle.throwInitiative(new RandomGenerator(5, 4, 3, 2)); + battle.throwInitiative(new SkewedRandomGenerator([5, 4, 3, 2])); var result = battle.collectShipsInCircle(Target.newFromLocation(5, 8), 3); expect(result).toEqual([ship2, ship3]); diff --git a/src/core/BattleOutcome.spec.ts b/src/core/BattleOutcome.spec.ts index 2fd7df4..e1ee263 100644 --- a/src/core/BattleOutcome.spec.ts +++ b/src/core/BattleOutcome.spec.ts @@ -33,10 +33,10 @@ module TS.SpaceTac.Specs { var battle = new Battle(fleet1, fleet2); var outcome = new BattleOutcome(fleet1); - var random = new RandomGenerator( + var random = new SkewedRandomGenerator([ 0, // leave first ship alone 0.45, // take 2 equipments from the 4 of second ship - 1, // - take last equipment + 0.999, // - take last equipment 0, // - take first equipment 0.6, // standard loot on first ship of second fleet 0, // - take first equipment @@ -45,8 +45,8 @@ module TS.SpaceTac.Specs { 0, // - lower end of level range (dead ship has 5, so range is 4-6) 0, // - take first generated equipment (there is only one anyway) 0.96, // lucky loot on fourth ship - 1 // - higher end of level range - ); + 0.999 // - higher end of level range + ]); // Force lucky finds with one template var looter = new LootGenerator(random, false); diff --git a/src/core/BattleOutcome.ts b/src/core/BattleOutcome.ts index a941ba2..7749f23 100644 --- a/src/core/BattleOutcome.ts +++ b/src/core/BattleOutcome.ts @@ -24,7 +24,7 @@ module TS.SpaceTac { if (!ship.alive) { if (ship.fleet === this.winner) { // Member of the winner fleet, salvage a number of equipments - var count = random.throwInt(0, ship.getEquipmentCount()); + var count = random.randInt(0, ship.getEquipmentCount()); while (count > 0) { var salvaged = ship.getRandomEquipment(random); salvaged.detach(); @@ -33,7 +33,7 @@ module TS.SpaceTac { } } else { - var luck = random.throw(); + var luck = random.random(); if (luck > 0.9) { // Salvage a supposedly transported item var transported = this.generateLootItem(random, ship.level); diff --git a/src/core/LootGenerator.spec.ts b/src/core/LootGenerator.spec.ts index 03bf898..f5e55a6 100644 --- a/src/core/LootGenerator.spec.ts +++ b/src/core/LootGenerator.spec.ts @@ -14,7 +14,7 @@ module TS.SpaceTac.Specs { it("generates items within a given level range", () => { var generator = new LootGenerator(); generator.templates = [new TestTemplate()]; - generator.random.forceNextValue(0.5); + generator.random = new SkewedRandomGenerator([0.5]); var equipment = generator.generate(new IntegerRange(3, 6)); diff --git a/src/core/LootGenerator.ts b/src/core/LootGenerator.ts index c854ff4..d99fcba 100644 --- a/src/core/LootGenerator.ts +++ b/src/core/LootGenerator.ts @@ -9,7 +9,7 @@ module TS.SpaceTac { // Construct a basic loot generator // The list of templates will be automatically populated - constructor(random: RandomGenerator = new RandomGenerator(), populate: boolean = true) { + constructor(random = RandomGenerator.global, populate: boolean = true) { this.templates = []; this.random = random; diff --git a/src/core/LootTemplate.ts b/src/core/LootTemplate.ts index 0d9b119..7e34d5e 100644 --- a/src/core/LootTemplate.ts +++ b/src/core/LootTemplate.ts @@ -51,9 +51,8 @@ module TS.SpaceTac { } // Generate a random equipment with this template - generate(random: RandomGenerator = null): Equipment { - random = random || new RandomGenerator(); - var power = random.throw(); + generate(random = RandomGenerator.global): Equipment { + var power = random.random(); return this.generateFixed(power); } @@ -114,10 +113,10 @@ module TS.SpaceTac { // Generate an equipment that will have its level requirement in the given range // May return null if level range is not compatible with the template - generateInLevelRange(level: IntegerRange, random: RandomGenerator = new RandomGenerator()): Equipment { + generateInLevelRange(level: IntegerRange, random = RandomGenerator.global): Equipment { var random_range = this.getPowerRangeForLevel(level); if (random_range) { - var power = random.throw() * (random_range.max - random_range.min) + random_range.min; + var power = random.random() * (random_range.max - random_range.min) + random_range.min; return this.generateFixed(power); } else { return null; diff --git a/src/core/NameGenerator.spec.ts b/src/core/NameGenerator.spec.ts index 8f3e20d..e9405ae 100644 --- a/src/core/NameGenerator.spec.ts +++ b/src/core/NameGenerator.spec.ts @@ -1,7 +1,7 @@ module TS.SpaceTac.Specs { describe("NameGenerator", () => { it("generates unique names", () => { - var random = new RandomGenerator(0.48, 0.9, 0.1); + var random = new SkewedRandomGenerator([0.48, 0.9, 0.1]); var gen = new NameGenerator(["a", "b", "c"], random); expect(gen.getName()).toEqual("b"); diff --git a/src/core/NameGenerator.ts b/src/core/NameGenerator.ts index 156ecf9..791a55b 100644 --- a/src/core/NameGenerator.ts +++ b/src/core/NameGenerator.ts @@ -18,7 +18,7 @@ module TS.SpaceTac { return null; } - var index = this.random.throwInt(0, this.choices.length - 1); + var index = this.random.randInt(0, this.choices.length - 1); var result = this.choices[index]; this.choices.splice(index, 1); return result; diff --git a/src/core/RandomGenerator.ts b/src/core/RandomGenerator.ts deleted file mode 100644 index dbe978c..0000000 --- a/src/core/RandomGenerator.ts +++ /dev/null @@ -1,49 +0,0 @@ -module TS.SpaceTac { - // Random generator, used in all throws - export class RandomGenerator { - // Array of next values, empty for a correct generator - private fake_values: number[]; - - // Basic constructor (can specify fake values as arguments) - constructor(...values: number[]) { - this.fake_values = []; - - values.forEach((value: number) => { - this.forceNextValue(value); - }); - } - - // Generate a value, based on an attribute level - throw(level: number = 1): number { - if (this.fake_values.length > 0) { - return this.fake_values.shift() * level; - } else { - return Math.random() * level; - } - } - - // Generate a random integer value in a range - throwInt(min: number, max: number): number { - var value = this.throw(max - min + 1); - return Math.floor(value) + min; - } - - // Choose a random item from an array - choice(items: any[]): any { - var index = this.throwInt(0, items.length - 1); - return items[index]; - } - - // Fake the generator, by forcing the next value - // Call it several times to set future successive values - // This value will replace the 0.0-1.0 random value, not the final one - forceNextValue(value: number): void { - if (value < 0.0) { - value = 0.0; - } else if (value >= 1.0) { - value = 0.999999999; - } - this.fake_values.push(value); - } - } -} diff --git a/src/core/Ship.spec.ts b/src/core/Ship.spec.ts index bb524a8..ddcb545 100644 --- a/src/core/Ship.spec.ts +++ b/src/core/Ship.spec.ts @@ -227,12 +227,12 @@ module TS.SpaceTac.Specs { ship.addSlot(SlotType.Shield); ship.addSlot(SlotType.Weapon).attach(new Equipment(SlotType.Weapon)); - var random = new RandomGenerator(0.2); + var random = new SkewedRandomGenerator([0.2]); var picked = ship.getRandomEquipment(random); expect(picked).not.toBeNull(); expect(picked).toBe(ship.slots[0].attached); - random.forceNextValue(1); + random = new SkewedRandomGenerator([0.999999]); picked = ship.getRandomEquipment(random); expect(picked).not.toBeNull(); expect(picked).toBe(ship.slots[2].attached); diff --git a/src/core/Ship.ts b/src/core/Ship.ts index 87723a8..5d1e8b2 100644 --- a/src/core/Ship.ts +++ b/src/core/Ship.ts @@ -134,7 +134,7 @@ module TS.SpaceTac { // Make an initiative throw, to resolve play order in a battle throwInitiative(gen: RandomGenerator): void { - this.play_priority = gen.throw(this.attributes.initiative.get()); + this.play_priority = gen.random() * this.attributes.initiative.get(); } // Return the player owning this ship @@ -463,12 +463,12 @@ module TS.SpaceTac { } // Get a random attached equipment, null if no equipment is attached - getRandomEquipment(random: RandomGenerator = new RandomGenerator()): Equipment { + getRandomEquipment(random = RandomGenerator.global): Equipment { var count = this.getEquipmentCount(); if (count === 0) { return null; } else { - var picked = random.throwInt(0, count - 1); + var picked = random.randInt(0, count - 1); var result: Equipment = null; var index = 0; this.slots.forEach((slot: Slot) => { diff --git a/src/core/Star.ts b/src/core/Star.ts index 01699f1..bca6b3d 100644 --- a/src/core/Star.ts +++ b/src/core/Star.ts @@ -98,21 +98,21 @@ module TS.SpaceTac { } // Generate the contents of this star system - generate(random: RandomGenerator = new RandomGenerator()): void { - var location_count = random.throwInt(2, 10); + generate(random = RandomGenerator.global): void { + var location_count = random.randInt(2, 10); this.name = random.choice(Star.NAMES_POOL); this.generateLocations(location_count, random); } // Generate points of interest (*count* doesn't include the star and warp locations) - generateLocations(count: number, random = new RandomGenerator()): void { + generateLocations(count: number, random = RandomGenerator.global): void { while (count--) { this.generateOneLocation(StarLocationType.PLANET, this.locations, this.radius * 0.2, this.radius * 0.7, random); } } // Generate a warp location to another star (to be bound later) - generateWarpLocationTo(other: Star, random = new RandomGenerator()): StarLocation { + generateWarpLocationTo(other: Star, random = RandomGenerator.global): StarLocation { let fav_phi = Math.atan2(other.y - this.y, other.x - this.x); var warp = this.generateOneLocation(StarLocationType.WARP, this.locations, this.radius * 0.8, this.radius * 1, random, fav_phi); return warp; @@ -167,8 +167,8 @@ module TS.SpaceTac { // Generate a single location private generateOneLocation(type: StarLocationType, others: StarLocation[], radius_min: number, radius_max: number, random: RandomGenerator, fav_phi: number | null = null): StarLocation { do { - var phi = fav_phi ? (fav_phi + random.throw(0.4) - 0.2) : random.throw(Math.PI * 2); - var r = random.throw(radius_max - radius_min) + radius_min; + var phi = fav_phi ? (fav_phi + random.random() * 0.4 - 0.2) : (random.random() * Math.PI * 2); + var r = random.random() * (radius_max - radius_min) + radius_min; var result = new StarLocation(this, type, r * Math.cos(phi), r * Math.sin(phi)); } while (!this.checkMinDistance(result, others)); diff --git a/src/core/StarLocation.spec.ts b/src/core/StarLocation.spec.ts index 5ce4061..a07b5e1 100644 --- a/src/core/StarLocation.spec.ts +++ b/src/core/StarLocation.spec.ts @@ -3,7 +3,7 @@ module TS.SpaceTac.Specs { it("removes generated encounters that lose", function () { var location = new StarLocation(null, StarLocationType.PLANET, 0, 0); var fleet = new Fleet(); - var random = new RandomGenerator(0); + var random = new SkewedRandomGenerator([0]); var battle = location.enterLocation(fleet, random); expect(location.encounter).not.toBeNull(); @@ -17,7 +17,7 @@ module TS.SpaceTac.Specs { it("leaves generated encounters that win", function () { var location = new StarLocation(null, StarLocationType.PLANET, 0, 0); var fleet = new Fleet(); - var random = new RandomGenerator(0); + var random = new SkewedRandomGenerator([0]); var battle = location.enterLocation(fleet, random); expect(location.encounter).not.toBeNull(); diff --git a/src/core/StarLocation.ts b/src/core/StarLocation.ts index 5078762..d85a280 100644 --- a/src/core/StarLocation.ts +++ b/src/core/StarLocation.ts @@ -52,13 +52,13 @@ module TS.SpaceTac { // Call this when first probing a location to generate the possible encounter // Returns the encountered fleet, null if no encounter happens - tryGenerateEncounter(random: RandomGenerator = new RandomGenerator()): Fleet { + tryGenerateEncounter(random = RandomGenerator.global): Fleet { if (!this.encounter_gen) { this.encounter_gen = true; - if (random.throw() < 0.8) { + if (random.random() < 0.8) { var fleet_generator = new FleetGenerator(random); - var ship_count = random.throwInt(1, 5); + var ship_count = random.randInt(1, 5); this.encounter = fleet_generator.generate(this.star.level, null, ship_count); } } @@ -69,7 +69,7 @@ module TS.SpaceTac { // Call this when entering a location to generate the possible encounter // *fleet* is the player fleet, entering the location // Returns the engaged battle, null if no encounter happens - enterLocation(fleet: Fleet, random: RandomGenerator = new RandomGenerator()): Battle { + enterLocation(fleet: Fleet, random = RandomGenerator.global): Battle { var encounter = this.tryGenerateEncounter(random); if (encounter) { var battle = new Battle(fleet, encounter); diff --git a/src/core/Universe.ts b/src/core/Universe.ts index 6720847..b9a00e2 100644 --- a/src/core/Universe.ts +++ b/src/core/Universe.ts @@ -37,8 +37,8 @@ module TS.SpaceTac { var names = new NameGenerator(Star.NAMES_POOL); while (count) { - var x = random.throw() * this.radius * 2.0 - this.radius; - var y = random.throw() * this.radius * 2.0 - this.radius; + var x = random.random() * this.radius * 2.0 - this.radius; + var y = random.random() * this.radius * 2.0 - this.radius; var star = new Star(this, x, y); var nearest = this.getNearestTo(star, result); diff --git a/src/core/ai/BullyAI.spec.ts b/src/core/ai/BullyAI.spec.ts index 2e029dc..37fab9a 100644 --- a/src/core/ai/BullyAI.spec.ts +++ b/src/core/ai/BullyAI.spec.ts @@ -6,7 +6,7 @@ module TS.SpaceTac.Specs { battle.fleets[1].addShip(new Ship(null, "1-0")); battle.fleets[1].addShip(new Ship(null, "1-1")); - var random = new RandomGenerator(0, 0.5, 1); + var random = new SkewedRandomGenerator([0, 0.5, 1]); battle.throwInitiative(random); var ai = new BullyAI(battle.fleets[0].ships[0], Timer.synchronous); @@ -137,7 +137,7 @@ module TS.SpaceTac.Specs { var ship3 = new Ship(); ship3.setArenaPosition(11, 15); battle.fleets[1].addShip(ship3); - battle.throwInitiative(new RandomGenerator(1, 0.5, 0)); + battle.throwInitiative(new SkewedRandomGenerator([1, 0.5, 0])); var ai = new BullyAI(ship1, Timer.synchronous); ai.ship = ship1;