@civ-clone/base-unit-fuel 0.1.0 → 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +5 -3
- package/lib/fuelRules.d.ts +13 -2
- package/lib/fuelRules.js +13 -2
- package/lib/fuelRules.js.map +1 -1
- package/lib/fuelRules.ts +20 -1
- package/package.json +2 -1
- package/tests/fuel.test.ts +33 -2
package/README.md
CHANGED
|
@@ -10,14 +10,16 @@ Deep-import the modules; there is no index.
|
|
|
10
10
|
- `lib/Fuel`: `Fuel`, the unit yield that gives a unit's range: how many of its player's turns it can end away from a
|
|
11
11
|
refuelling point, counting the one it set out in (1: it must refuel every turn; 2: it can stay out one turn). 0, as
|
|
12
12
|
with no `Yield` rule for it, needs no fuel.
|
|
13
|
-
- `lib/fuelRules`: `fuelRules({ ruleRegistry, unitImprovementRegistry, unitRegistry }, { prefix?, range?, countOn?,
|
|
13
|
+
- `lib/fuelRules`: `fuelRules({ engine?, ruleRegistry, unitImprovementRegistry, unitRegistry }, { prefix?, range?, countOn?,
|
|
14
14
|
countsAsMove?, isAttack? })`:
|
|
15
15
|
- a rule that counts turns out, as `countOn` says (see below);
|
|
16
|
-
- an `OutOfFuel` rule that destroys the unit. A game adds its own `OutOfFuel` rules for anything else
|
|
16
|
+
- an `OutOfFuel` rule that destroys the unit. A game adds its own `OutOfFuel` rules for anything else;
|
|
17
|
+
- given an `engine`, a `High` `OutOfFuel` rule that emits `unit:out-of-fuel` with the unit, before it is destroyed, so
|
|
18
|
+
the client can say the unit ran out of fuel (not that it was lost at sea).
|
|
17
19
|
- `range(unit)` is the unit's `Fuel` yield by default (`fuelRange`); a game can read its own table instead.
|
|
18
20
|
- helpers: `refuels(ruleRegistry, unit, tile?)`, `turnsAloft(unitImprovementRegistry, unit)`, `aloftOf(...)`,
|
|
19
21
|
`checkFuel(...)` and `checkFuelAfterMove(...)`.
|
|
20
|
-
- Rule ids `<prefix>:player/turn-end/fuel` (or `<prefix>:unit/moved/fuel`) and `<prefix>:unit/out-of-fuel/destroy
|
|
22
|
+
- Rule ids `<prefix>:player/turn-end/fuel` (or `<prefix>:unit/moved/fuel`) and `<prefix>:unit/out-of-fuel/destroy`, plus `<prefix>:unit/out-of-fuel/emit` with an engine.
|
|
21
23
|
- `lib/Rules`: `Refuel(unit, tile) → boolean` (a unit refuels on a square if any rule's criteria match and its effect
|
|
22
24
|
answers `true`) and `OutOfFuel(unit)`.
|
|
23
25
|
- `lib/refuel`, `Refuel` rules a game registers, each with an optional `applies(unit)` filter and id:
|
package/lib/fuelRules.d.ts
CHANGED
|
@@ -4,12 +4,15 @@ import { UnitImprovementRegistry } from '@civ-clone/core-unit-improvement/UnitIm
|
|
|
4
4
|
import { UnitRegistry } from '@civ-clone/core-unit/UnitRegistry';
|
|
5
5
|
import Action from '@civ-clone/core-unit/Action';
|
|
6
6
|
import Aloft from './Aloft';
|
|
7
|
+
import Engine from '@civ-clone/core-engine/Engine';
|
|
7
8
|
import Moved from '@civ-clone/core-unit/Rules/Moved';
|
|
8
9
|
import Player from '@civ-clone/core-player/Player';
|
|
9
10
|
import Tile from '@civ-clone/core-world/Tile';
|
|
10
11
|
import TurnEnd from '@civ-clone/core-player/Rules/TurnEnd';
|
|
11
12
|
import Unit from '@civ-clone/core-unit/Unit';
|
|
12
13
|
export interface FuelDependencies {
|
|
14
|
+
/** When given, the client is told when a unit runs out of fuel: `unit:out-of-fuel`, with the unit. */
|
|
15
|
+
engine?: Engine;
|
|
13
16
|
ruleRegistry?: RuleRegistry;
|
|
14
17
|
unitImprovementRegistry?: UnitImprovementRegistry;
|
|
15
18
|
unitRegistry?: UnitRegistry;
|
|
@@ -83,13 +86,21 @@ export declare const checkFuelAfterMove: (
|
|
|
83
86
|
/**
|
|
84
87
|
* A ruleset's fuel rules: one that counts the turns each unit with a range spends away from a refuelling point, and an
|
|
85
88
|
* `OutOfFuel` rule that destroys a unit that runs out. A game adds its own `Refuel` rules (see `lib/refuel`), and its
|
|
86
|
-
* own `OutOfFuel` rules for anything else that should happen
|
|
89
|
+
* own `OutOfFuel` rules for anything else that should happen.
|
|
90
|
+
*
|
|
91
|
+
* Given an `engine`, it also emits `unit:out-of-fuel` with the unit, before the unit is destroyed (so it's still on its
|
|
92
|
+
* square), for the client's notice.
|
|
87
93
|
*
|
|
88
94
|
* With `countOn: 'turn-end'` (the default) the count is a `TurnEnd` rule (`checkFuel`): it doesn't matter whether a
|
|
89
95
|
* unit spent its moves. With `'out-of-moves'` it is a `Moved` rule (`checkFuelAfterMove`), as the original counts it.
|
|
90
96
|
*/
|
|
91
97
|
export declare const fuelRules: (
|
|
92
|
-
{
|
|
98
|
+
{
|
|
99
|
+
engine,
|
|
100
|
+
ruleRegistry,
|
|
101
|
+
unitImprovementRegistry,
|
|
102
|
+
unitRegistry,
|
|
103
|
+
}: FuelDependencies,
|
|
93
104
|
{ prefix, range, countOn, countsAsMove, isAttack }?: FuelOptions
|
|
94
105
|
) => (TurnEnd | Moved | OutOfFuel)[];
|
|
95
106
|
export default fuelRules;
|
package/lib/fuelRules.js
CHANGED
|
@@ -7,6 +7,7 @@ const UnitImprovementRegistry_1 = require("@civ-clone/core-unit-improvement/Unit
|
|
|
7
7
|
const UnitRegistry_1 = require("@civ-clone/core-unit/UnitRegistry");
|
|
8
8
|
const Aloft_1 = require("./Aloft");
|
|
9
9
|
const Criterion_1 = require("@civ-clone/core-rule/Criterion");
|
|
10
|
+
const High_1 = require("@civ-clone/core-rule/Priorities/High");
|
|
10
11
|
const Effect_1 = require("@civ-clone/core-rule/Effect");
|
|
11
12
|
const Fuel_1 = require("./Fuel");
|
|
12
13
|
const Moved_1 = require("@civ-clone/core-unit/Rules/Moved");
|
|
@@ -114,17 +115,27 @@ exports.checkFuelAfterMove = checkFuelAfterMove;
|
|
|
114
115
|
/**
|
|
115
116
|
* A ruleset's fuel rules: one that counts the turns each unit with a range spends away from a refuelling point, and an
|
|
116
117
|
* `OutOfFuel` rule that destroys a unit that runs out. A game adds its own `Refuel` rules (see `lib/refuel`), and its
|
|
117
|
-
* own `OutOfFuel` rules for anything else that should happen
|
|
118
|
+
* own `OutOfFuel` rules for anything else that should happen.
|
|
119
|
+
*
|
|
120
|
+
* Given an `engine`, it also emits `unit:out-of-fuel` with the unit, before the unit is destroyed (so it's still on its
|
|
121
|
+
* square), for the client's notice.
|
|
118
122
|
*
|
|
119
123
|
* With `countOn: 'turn-end'` (the default) the count is a `TurnEnd` rule (`checkFuel`): it doesn't matter whether a
|
|
120
124
|
* unit spent its moves. With `'out-of-moves'` it is a `Moved` rule (`checkFuelAfterMove`), as the original counts it.
|
|
121
125
|
*/
|
|
122
|
-
const fuelRules = ({ ruleRegistry = RuleRegistry_1.instance, unitImprovementRegistry = UnitImprovementRegistry_1.instance, unitRegistry = UnitRegistry_1.instance, }, { prefix, range = exports.fuelRange, countOn = 'turn-end', countsAsMove = (unit, action) => action.from() !== action.to(), isAttack = (unit, action) => unit.tile() !== action.to(), } = {}) => {
|
|
126
|
+
const fuelRules = ({ engine, ruleRegistry = RuleRegistry_1.instance, unitImprovementRegistry = UnitImprovementRegistry_1.instance, unitRegistry = UnitRegistry_1.instance, }, { prefix, range = exports.fuelRange, countOn = 'turn-end', countsAsMove = (unit, action) => action.from() !== action.to(), isAttack = (unit, action) => unit.tile() !== action.to(), } = {}) => {
|
|
123
127
|
const id = (name) => prefix === undefined ? [] : [`${prefix}:${name}`];
|
|
124
128
|
return [
|
|
125
129
|
countOn === 'out-of-moves'
|
|
126
130
|
? new Moved_1.default(...id('unit/moved/fuel'), new Criterion_1.default(countsAsMove), new Effect_1.default((0, exports.checkFuelAfterMove)(ruleRegistry, unitImprovementRegistry, range, isAttack)))
|
|
127
131
|
: new TurnEnd_1.default(...id('player/turn-end/fuel'), new Effect_1.default((0, exports.checkFuel)(ruleRegistry, unitImprovementRegistry, unitRegistry, range))),
|
|
132
|
+
...(engine === undefined
|
|
133
|
+
? []
|
|
134
|
+
: [
|
|
135
|
+
new Rules_1.OutOfFuel(...id('unit/out-of-fuel/emit'), new High_1.default(), new Effect_1.default((unit) => {
|
|
136
|
+
engine.emit('unit:out-of-fuel', unit);
|
|
137
|
+
})),
|
|
138
|
+
]),
|
|
128
139
|
new Rules_1.OutOfFuel(...id('unit/out-of-fuel/destroy'), new Effect_1.default((unit) => unit.destroy())),
|
|
129
140
|
];
|
|
130
141
|
};
|
package/lib/fuelRules.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"fuelRules.js","sourceRoot":"","sources":["fuelRules.ts"],"names":[],"mappings":";;;AAAA,mCAA4C;AAC5C,oEAG2C;AAC3C,sGAGkE;AAClE,oEAG2C;AAE3C,mCAA4B;AAC5B,8DAAuD;
|
|
1
|
+
{"version":3,"file":"fuelRules.js","sourceRoot":"","sources":["fuelRules.ts"],"names":[],"mappings":";;;AAAA,mCAA4C;AAC5C,oEAG2C;AAC3C,sGAGkE;AAClE,oEAG2C;AAE3C,mCAA4B;AAC5B,8DAAuD;AAEvD,+DAAwD;AACxD,wDAAiD;AACjD,iCAA0B;AAC1B,4DAAqD;AAGrD,kEAA2D;AAuC3D,6BAA6B;AACtB,MAAM,SAAS,GAAU,CAAC,IAAU,EAAU,EAAE;IACrD,MAAM,CAAC,KAAK,CAAC,GAAG,IAAI,CAAC,KAAK,CAAC,IAAI,cAAI,EAAE,CAAC,CAAC;IAEvC,OAAO,KAAK,CAAC,KAAK,EAAE,CAAC;AACvB,CAAC,CAAC;AAJW,QAAA,SAAS,aAIpB;AAEF,4CAA4C;AACrC,MAAM,OAAO,GAAG,CACrB,uBAAgD,EAChD,IAAU,EACS,EAAE,CACrB,uBAAuB;KACpB,SAAS,CAAC,IAAI,CAAC;KACf,IAAI,CACH,CAAC,eAAgC,EAAW,EAAE,CAC5C,eAAe,YAAY,eAAK,CACd,CAAC;AATd,QAAA,OAAO,WASO;AAE3B,4FAA4F;AACrF,MAAM,UAAU,GAAG,CACxB,uBAAgD,EAChD,IAAU,EACF,EA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CAAC,CAAC,CAAC,GAAG,MAAM,IAAI,IAAI,EAAE,CAAC,CAAC;IAEpD,OAAO;QACL,OAAO,KAAK,cAAc;YACxB,CAAC,CAAC,IAAI,eAAK,CACP,GAAG,EAAE,CAAC,iBAAiB,CAAC,EACxB,IAAI,mBAAS,CAAC,YAAY,CAAC,EAC3B,IAAI,gBAAM,CACR,IAAA,0BAAkB,EAChB,YAAY,EACZ,uBAAuB,EACvB,KAAK,EACL,QAAQ,CACT,CACF,CACF;YACH,CAAC,CAAC,IAAI,iBAAO,CACT,GAAG,EAAE,CAAC,sBAAsB,CAAC,EAC7B,IAAI,gBAAM,CACR,IAAA,iBAAS,EACP,YAAY,EACZ,uBAAuB,EACvB,YAAY,EACZ,KAAK,CACN,CACF,CACF;QACL,GAAG,CAAC,MAAM,KAAK,SAAS;YACtB,CAAC,CAAC,EAAE;YACJ,CAAC,CAAC;gBACE,IAAI,iBAAS,CACX,GAAG,EAAE,CAAC,uBAAuB,CAAC,EAC9B,IAAI,cAAI,EAAE,EACV,IAAI,gBAAM,CAAC,CAAC,IAAU,EAAQ,EAAE;oBAC9B,MAAM,CAAC,IAAI,CAAC,kBAAkB,EAAE,IAAI,CAAC,CAAC;gBACxC,CAAC,CAAC,CACH;aACF,CAAC;QACN,IAAI,iBAAS,CACX,GAAG,EAAE,CAAC,0BAA0B,CAAC,EACjC,IAAI,gBAAM,CAAC,CAAC,IAAU,EAAQ,EAAE,CAAC,IAAI,CAAC,OAAO,EAAE,CAAC,CACjD;KACF,CAAC;AACJ,CAAC,CAAC;AA7DW,QAAA,SAAS,aA6DpB;AAEF,kBAAe,iBAAS,CAAC"}
|
package/lib/fuelRules.ts
CHANGED
|
@@ -14,6 +14,8 @@ import {
|
|
|
14
14
|
import Action from '@civ-clone/core-unit/Action';
|
|
15
15
|
import Aloft from './Aloft';
|
|
16
16
|
import Criterion from '@civ-clone/core-rule/Criterion';
|
|
17
|
+
import Engine from '@civ-clone/core-engine/Engine';
|
|
18
|
+
import High from '@civ-clone/core-rule/Priorities/High';
|
|
17
19
|
import Effect from '@civ-clone/core-rule/Effect';
|
|
18
20
|
import Fuel from './Fuel';
|
|
19
21
|
import Moved from '@civ-clone/core-unit/Rules/Moved';
|
|
@@ -24,6 +26,8 @@ import Unit from '@civ-clone/core-unit/Unit';
|
|
|
24
26
|
import UnitImprovement from '@civ-clone/core-unit-improvement/UnitImprovement';
|
|
25
27
|
|
|
26
28
|
export interface FuelDependencies {
|
|
29
|
+
/** When given, the client is told when a unit runs out of fuel: `unit:out-of-fuel`, with the unit. */
|
|
30
|
+
engine?: Engine;
|
|
27
31
|
ruleRegistry?: RuleRegistry;
|
|
28
32
|
unitImprovementRegistry?: UnitImprovementRegistry;
|
|
29
33
|
unitRegistry?: UnitRegistry;
|
|
@@ -218,13 +222,17 @@ export const checkFuelAfterMove =
|
|
|
218
222
|
/**
|
|
219
223
|
* A ruleset's fuel rules: one that counts the turns each unit with a range spends away from a refuelling point, and an
|
|
220
224
|
* `OutOfFuel` rule that destroys a unit that runs out. A game adds its own `Refuel` rules (see `lib/refuel`), and its
|
|
221
|
-
* own `OutOfFuel` rules for anything else that should happen
|
|
225
|
+
* own `OutOfFuel` rules for anything else that should happen.
|
|
226
|
+
*
|
|
227
|
+
* Given an `engine`, it also emits `unit:out-of-fuel` with the unit, before the unit is destroyed (so it's still on its
|
|
228
|
+
* square), for the client's notice.
|
|
222
229
|
*
|
|
223
230
|
* With `countOn: 'turn-end'` (the default) the count is a `TurnEnd` rule (`checkFuel`): it doesn't matter whether a
|
|
224
231
|
* unit spent its moves. With `'out-of-moves'` it is a `Moved` rule (`checkFuelAfterMove`), as the original counts it.
|
|
225
232
|
*/
|
|
226
233
|
export const fuelRules = (
|
|
227
234
|
{
|
|
235
|
+
engine,
|
|
228
236
|
ruleRegistry = ruleRegistryInstance,
|
|
229
237
|
unitImprovementRegistry = unitImprovementRegistryInstance,
|
|
230
238
|
unitRegistry = unitRegistryInstance,
|
|
@@ -267,6 +275,17 @@ export const fuelRules = (
|
|
|
267
275
|
)
|
|
268
276
|
)
|
|
269
277
|
),
|
|
278
|
+
...(engine === undefined
|
|
279
|
+
? []
|
|
280
|
+
: [
|
|
281
|
+
new OutOfFuel(
|
|
282
|
+
...id('unit/out-of-fuel/emit'),
|
|
283
|
+
new High(),
|
|
284
|
+
new Effect((unit: Unit): void => {
|
|
285
|
+
engine.emit('unit:out-of-fuel', unit);
|
|
286
|
+
})
|
|
287
|
+
),
|
|
288
|
+
]),
|
|
270
289
|
new OutOfFuel(
|
|
271
290
|
...id('unit/out-of-fuel/destroy'),
|
|
272
291
|
new Effect((unit: Unit): void => unit.destroy())
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@civ-clone/base-unit-fuel",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.1",
|
|
4
4
|
"description": "Fuel for units that must return to base: a range, the turns a unit has spent out, the places it refuels, and running out of fuel.",
|
|
5
5
|
"repository": "git@github.com:civ-clone/base-unit-fuel.git",
|
|
6
6
|
"keywords": [
|
|
@@ -33,6 +33,7 @@
|
|
|
33
33
|
},
|
|
34
34
|
"dependencies": {
|
|
35
35
|
"@civ-clone/core-city": "^0.1.19",
|
|
36
|
+
"@civ-clone/core-engine": "^0.1.6",
|
|
36
37
|
"@civ-clone/core-player": "^0.1.10",
|
|
37
38
|
"@civ-clone/core-rule": "^0.1.9",
|
|
38
39
|
"@civ-clone/core-tile-improvement": "^0.1.6",
|
package/tests/fuel.test.ts
CHANGED
|
@@ -14,6 +14,7 @@ import CityRegistry from '@civ-clone/core-city/CityRegistry';
|
|
|
14
14
|
import Criterion from '@civ-clone/core-rule/Criterion';
|
|
15
15
|
import Destroyed from '@civ-clone/core-unit/Rules/Destroyed';
|
|
16
16
|
import Effect from '@civ-clone/core-rule/Effect';
|
|
17
|
+
import Engine from '@civ-clone/core-engine/Engine';
|
|
17
18
|
import Fuel from '../lib/Fuel';
|
|
18
19
|
import Generator from '@civ-clone/core-world-generator/Generator';
|
|
19
20
|
import { Movement } from '@civ-clone/core-unit/Yields';
|
|
@@ -44,7 +45,7 @@ class Airfield extends TileImprovement {}
|
|
|
44
45
|
|
|
45
46
|
class Step extends Action {}
|
|
46
47
|
|
|
47
|
-
const setUp = (countOn: CountOn = 'turn-end') => {
|
|
48
|
+
const setUp = (countOn: CountOn = 'turn-end', engine?: Engine) => {
|
|
48
49
|
const ruleRegistry = new RuleRegistry(),
|
|
49
50
|
unitRegistry = new UnitRegistry(),
|
|
50
51
|
cityRegistry = new CityRegistry(),
|
|
@@ -106,7 +107,7 @@ const setUp = (countOn: CountOn = 'turn-end') => {
|
|
|
106
107
|
})
|
|
107
108
|
),
|
|
108
109
|
...fuelRules(
|
|
109
|
-
{ ruleRegistry, unitImprovementRegistry, unitRegistry },
|
|
110
|
+
{ engine, ruleRegistry, unitImprovementRegistry, unitRegistry },
|
|
110
111
|
{ prefix: 'test', countOn }
|
|
111
112
|
),
|
|
112
113
|
refuelInOwnCity(cityRegistry),
|
|
@@ -246,6 +247,36 @@ describe('fuelRules', (): void => {
|
|
|
246
247
|
expect(Object.keys(out)).not.to.include('_turns');
|
|
247
248
|
});
|
|
248
249
|
|
|
250
|
+
it('should tell the client, given an engine, before the unit is destroyed', (): void => {
|
|
251
|
+
const emitted: [string, Unit, Tile | null][] = [],
|
|
252
|
+
engine = {
|
|
253
|
+
emit: (event: string, unit: Unit): void => {
|
|
254
|
+
emitted.push([event, unit, unit.destroyed() ? null : unit.tile()]);
|
|
255
|
+
},
|
|
256
|
+
} as unknown as Engine,
|
|
257
|
+
{ endTurn, events, tiles, unit } = setUp('turn-end', engine),
|
|
258
|
+
out = unit(tiles[3], 1);
|
|
259
|
+
|
|
260
|
+
endTurn();
|
|
261
|
+
|
|
262
|
+
expect(emitted).to.deep.equal([['unit:out-of-fuel', out, tiles[3]]]);
|
|
263
|
+
expect(events).to.deep.equal([
|
|
264
|
+
['out-of-fuel', out],
|
|
265
|
+
['destroyed', out],
|
|
266
|
+
]);
|
|
267
|
+
});
|
|
268
|
+
|
|
269
|
+
it('should emit nothing without an engine', (): void => {
|
|
270
|
+
const { ruleRegistry } = setUp();
|
|
271
|
+
|
|
272
|
+
expect(
|
|
273
|
+
ruleRegistry
|
|
274
|
+
.get(OutOfFuel)
|
|
275
|
+
.map((rule) => rule.id())
|
|
276
|
+
.filter((id) => (id ?? '').endsWith('/emit'))
|
|
277
|
+
).to.deep.equal([]);
|
|
278
|
+
});
|
|
279
|
+
|
|
249
280
|
it('should name its rules with the prefix', (): void => {
|
|
250
281
|
expect(
|
|
251
282
|
fuelRules({}, { prefix: 'test' }).map((rule) => rule.id())
|