Skip to content

Commit

Permalink
feat: tower effectiveness at range
Browse files Browse the repository at this point in the history
* feat: tower effectiveness at range

* fix: supply the tower power in the function
  • Loading branch information
Arcath authored and RiftLurker committed Jan 22, 2018
1 parent 9a01ad6 commit ad42ed7
Show file tree
Hide file tree
Showing 5 changed files with 113 additions and 0 deletions.
15 changes: 15 additions & 0 deletions src/tower-effectiveness-at-range/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
export function towerEffectivenessAtRange(
range: number,
max: number,
): number {
if (range <= TOWER_OPTIMAL_RANGE) {
return max;
}
if (range >= TOWER_FALLOFF_RANGE) {
return max * (1 - TOWER_FALLOFF);
}

const towerFalloffPerTile = TOWER_FALLOFF / (TOWER_FALLOFF_RANGE - TOWER_OPTIMAL_RANGE);

return max * (1 - (range - TOWER_OPTIMAL_RANGE) * towerFalloffPerTile);
}
48 changes: 48 additions & 0 deletions src/tower-effectiveness-at-range/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
{
"name": "@open-screeps/tower-effectiveness-at-range",
"version": "0.0.0-development",
"description": "Calculate the effectiveness of tower actions at the given range",
"main": "index.js",
"scripts": {
"lint": "tslint -p ./tsconfig.json",
"test": "tsc && nyc ava",
"prepare": "npm test",
"release": "semantic-release -e semantic-release-monorepo"
},
"repository": {
"type": "git",
"url": "git+https://github.com/postcrafter/open-screeps.git"
},
"files": [
"index.{d.ts,js,js.map}"
],
"keywords": [
"screeps",
"open-screeps"
],
"author": "Adam Laycock &lt;[email protected]&gt;",
"license": "MIT",
"bugs": {
"url": "https://github.com/postcrafter/open-screeps/issues"
},
"homepage": "https://github.com/postcrafter/open-screeps/src/tower-effectiveness-at-range#readme",
"dependencies": {
"@types/screeps": "^0.0.0"
},
"devDependencies": {
"ava": "^0.24.0",
"condition-circle": "^2.0.1",
"nyc": "^11.3.0",
"semantic-release": "^11.0.2",
"semantic-release-monorepo": "^4.0.0",
"tslint": "^5.9.1",
"tslint-config-airbnb": "^5.4.2",
"typescript": "^2.6.2"
},
"publishConfig": {
"access": "public"
},
"release": {
"verifyConditions": "condition-circle"
}
}
23 changes: 23 additions & 0 deletions src/tower-effectiveness-at-range/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# tower-effectiveness-at-range
> Calculate the effectiveness of tower actions at the given range
## Install
```sh
$ npm install @open-screeps/tower-effectiveness-at-range
```

## Usage
```typescript
import { towerEffectivenessAtRange } from '@open-screeps/tower-effectiveness-at-range';

let damage = towerEffectivenessAtRange(creep.pos.getRangeTo(tower));

let heal = towerEffectivenessAtRange(creep.pos.getRangeTo(tower), 'heal');

let repair = towerEffectivenessAtRange(container.pos.getRangeTo(tower), 'repair');
```

## Related

## License
[MIT](../../license.md)
24 changes: 24 additions & 0 deletions src/tower-effectiveness-at-range/test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import ava from 'ava';

import { towerEffectivenessAtRange } from './index';

declare const global: any;

global.TOWER_HITS = 3000;
global.TOWER_CAPACITY = 1000;
global.TOWER_ENERGY_COST = 10;
global.TOWER_POWER_ATTACK = 600;
global.TOWER_POWER_HEAL = 400;
global.TOWER_POWER_REPAIR = 800;
global.TOWER_OPTIMAL_RANGE = 5;
global.TOWER_FALLOFF_RANGE = 20;
global.TOWER_FALLOFF = 0.75;

ava('Should calculate the tower effectiveness for the given ranges', (t) => {
t.is(towerEffectivenessAtRange(2, TOWER_POWER_ATTACK), 600);
t.is(towerEffectivenessAtRange(21, TOWER_POWER_ATTACK), 150);
t.is(towerEffectivenessAtRange(10, TOWER_POWER_ATTACK), 450);
t.is(towerEffectivenessAtRange(15, TOWER_POWER_ATTACK), 300);
t.is(towerEffectivenessAtRange(2, TOWER_POWER_HEAL), 400);
t.is(towerEffectivenessAtRange(2, TOWER_POWER_REPAIR), 800);
});
3 changes: 3 additions & 0 deletions src/tower-effectiveness-at-range/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"extends": "../../tsconfig.base.json"
}

0 comments on commit ad42ed7

Please sign in to comment.