1
0
Fork 0

Use RandomGenerator from tscommon

This commit is contained in:
Michaël Lemaire 2017-02-26 18:44:15 +01:00
parent db194b4bf6
commit 135123e7f1
16 changed files with 39 additions and 89 deletions

View file

@ -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]);

View file

@ -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);

View file

@ -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);

View file

@ -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));

View file

@ -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;

View file

@ -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;

View file

@ -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");

View file

@ -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;

View file

@ -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);
}
}
}

View file

@ -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);

View file

@ -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) => {

View file

@ -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));

View file

@ -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();

View file

@ -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);

View file

@ -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);

View file

@ -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;