Skip to content

Instantly share code, notes, and snippets.

@ryzokuken
Last active April 11, 2019 19:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ryzokuken/d481dc3b4225ae4e636631100a53be6d to your computer and use it in GitHub Desktop.
Save ryzokuken/d481dc3b4225ae4e636631100a53be6d to your computer and use it in GitHub Desktop.
SkyNet
  1. Install TypeScript Compiler (npm install -g typescript)
  2. Compile (tsc simulation.ts -t "esnext" -m "commonjs")
  3. Run (node simulation.js)
  4. Profit
export class Vector2 {
constructor(public x: number, public y: number) {}
invert(): Vector2 {
return new Vector2(-this.x, -this.y);
}
add(b: Vector2): Vector2 {
return new Vector2(this.x + b.x, this.y + b.y);
}
}
const ZERO_VECTOR2: Vector2 = new Vector2(0, 0);
// export function randomNumber(pivot: number): number {
// Math.floor(Math.random() * 3 * (pivot - 1)) + 1;
// }
export function chooseDirection(): number {
return Math.random() < 0.5 ? -1 : 1;
}
function randomDirection(): Vector2 {
return new Vector2(chooseDirection(), chooseDirection());
}
export class Drone {
_map: SectorMap;
lastIncentive: number = NaN;
direction: Vector2 = ZERO_VECTOR2;
constructor(public sector: Sector, map: SectorMap) {
this._map = map;
}
process() {
// FORMULATE DATA
const data: Data = { sector: this.sector };
const incentive: number = oracle(data);
if (Number.isNaN(this.lastIncentive)) {
if (incentive - this.lastIncentive > 0) {
this.move(this.direction);
} else {
this.move(this.direction.invert());
this.direction = ZERO_VECTOR2;
}
} else {
this.direction = randomDirection();
this.move(this.direction);
}
this.lastIncentive = incentive;
}
move(direction: Vector2) {
const curPos: Vector2 = this.sector.position;
const finalPos: Sector | undefined = this._map.getSector(
curPos.add(direction)
);
if (finalPos !== undefined) {
this.sector = finalPos;
} else {
this.direction = ZERO_VECTOR2;
}
}
}
export interface Sector {
position: Vector2;
baseIncentive: number;
}
interface Data {
sector: Sector;
}
function oracle(data: Data): number {
return data.sector.baseIncentive + Math.random() * 10 - 5;
}
export class SectorMap {
sectors: Sector[][] = new Array<Array<Sector>>();
// constructor(map: Map<Vector2, Sector>) {
// map.forEach((sector, position) => {
// const { x, y } = position;
// this.sectors[x][y] = sector;
// })
// }
setSector(pos: Vector2, sector: Sector) {
const { x, y } = pos;
if (this.sectors[x] === undefined) this.sectors[x] = new Array<Sector>();
this.sectors[x][y] = sector;
}
getSector(pos: Vector2): Sector {
const { x, y } = pos;
if (this.sectors[x] === undefined) this.sectors[x] = new Array<Sector>();
return this.sectors[x][y];
}
}
import { Drone, Sector, Vector2, SectorMap } from './main';
const drones: Drone[] = [];
const map: SectorMap = new SectorMap();
function initializeMapRandomly() {
console.log('INITIALIZING MAP');
for (let x = 0; x < 4; x++) {
for (let y = 0; y < 4; y++) {
const position: Vector2 = new Vector2(x, y);
const baseIncentive: number = Math.random() * 10 + 5;
const sector: Sector = { position, baseIncentive };
map.setSector(position, sector);
}
}
console.log('MAP INITIALIZED SUCCESSFULLY');
}
function initializeDrones() {
console.log('INITIALIZING DRONES');
const origin: Sector = map.getSector(new Vector2(1, 1)) as Sector;
for (let i = 0; i < 25; i++) {
drones.push(new Drone(origin, map));
}
console.log('DRONES INITIALIZED SUCCESSFULLY');
}
function perTick() {
drones.forEach(drone => drone.process());
}
initializeMapRandomly();
initializeDrones();
console.log(map.sectors);
for (let i = 0; i < 10; i++) {
console.log(i);
perTick();
drones.forEach(drone => console.log(drone.sector.position));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment