Add distance condition for auto resource sending

This commit is contained in:
Anton Vakhrushev 2020-10-14 11:58:42 +03:00
parent 33964dc99b
commit 901bea6b4e
3 changed files with 32 additions and 1 deletions

View File

@ -19,6 +19,10 @@ export class Coordinates implements CoordinatesInterface {
eq(other: CoordinatesInterface): boolean {
return this.x === other.x && this.y === other.y;
}
dist(other: CoordinatesInterface): number {
return Math.sqrt(Math.pow(this.x - other.x, 2) + Math.pow(this.y - other.y, 2));
}
}
export class Village {

View File

@ -12,6 +12,8 @@ import { MARKET_ID } from '../../Core/Buildings';
import { registerAction } from '../ActionMap';
import { Task } from '../../Queue/Task';
const MAX_VILLAGE_DISTANCE = 25;
@registerAction
export class FindSendResourcesPathAction extends BaseAction {
async run(args: Args, task: Task): Promise<any> {
@ -21,7 +23,8 @@ export class FindSendResourcesPathAction extends BaseAction {
const villages = this.villageFactory.getAllVillages();
for (let fromVillage of villages) {
for (let toVillage of villages) {
if (fromVillage.id !== toVillage.id) {
const dist = fromVillage.crd.dist(toVillage.crd);
if (fromVillage.id !== toVillage.id && dist < MAX_VILLAGE_DISTANCE) {
reports.push(calculator.calc(fromVillage.id, toVillage.id));
}
}

View File

@ -0,0 +1,24 @@
import { it, describe } from 'mocha';
import { expect } from 'chai';
import { Coordinates } from '../../src/Core/Village';
describe('Coordinates', function () {
it('Can compare same', function () {
const a = new Coordinates(1, 1);
const b = new Coordinates(1, 1);
expect(a.eq(b)).is.true;
});
it('Can compare different', function () {
const a = new Coordinates(1, 1);
const b = new Coordinates(2, 1);
expect(a.eq(b)).is.false;
});
it('Can calc distance', function () {
const a = new Coordinates(0, 0);
const b = new Coordinates(-3, -4);
expect(a.dist(b)).is.equals(5);
});
});