@civ-clone/base-city-lifecycle 0.0.0-stage → 0.1.0

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.
Files changed (44) hide show
  1. package/.gitattributes +2 -0
  2. package/.github/workflows/build-on-push.yml +39 -0
  3. package/.prettierrc +4 -0
  4. package/LICENSE +21 -0
  5. package/README.md +31 -2
  6. package/lib/canBeWorked.d.ts +28 -0
  7. package/lib/canBeWorked.js +27 -0
  8. package/lib/canBeWorked.js.map +1 -0
  9. package/lib/canBeWorked.ts +43 -0
  10. package/lib/claimName.d.ts +12 -0
  11. package/lib/claimName.js +9 -0
  12. package/lib/claimName.js.map +1 -0
  13. package/lib/claimName.ts +13 -0
  14. package/lib/events.d.ts +31 -0
  15. package/lib/events.js +26 -0
  16. package/lib/events.js.map +1 -0
  17. package/lib/events.ts +40 -0
  18. package/lib/famine.d.ts +41 -0
  19. package/lib/famine.js +44 -0
  20. package/lib/famine.js.map +1 -0
  21. package/lib/famine.ts +68 -0
  22. package/lib/foodStorage.d.ts +21 -0
  23. package/lib/foodStorage.js +27 -0
  24. package/lib/foodStorage.js.map +1 -0
  25. package/lib/foodStorage.ts +30 -0
  26. package/lib/occupiedTiles.d.ts +25 -0
  27. package/lib/occupiedTiles.js +35 -0
  28. package/lib/occupiedTiles.js.map +1 -0
  29. package/lib/occupiedTiles.ts +52 -0
  30. package/lib/playerActions.d.ts +72 -0
  31. package/lib/playerActions.js +68 -0
  32. package/lib/playerActions.js.map +1 -0
  33. package/lib/playerActions.ts +114 -0
  34. package/lib/processYield.d.ts +30 -0
  35. package/lib/processYield.js +54 -0
  36. package/lib/processYield.js.map +1 -0
  37. package/lib/processYield.ts +82 -0
  38. package/lib/unsupported.d.ts +6 -0
  39. package/lib/unsupported.js +9 -0
  40. package/lib/unsupported.js.map +1 -0
  41. package/lib/unsupported.ts +8 -0
  42. package/package.json +40 -4
  43. package/tests/base-city-lifecycle.test.ts +536 -0
  44. package/tsconfig.json +16 -0
package/.gitattributes ADDED
@@ -0,0 +1,2 @@
1
+ **/*.js linguist-generated=true
2
+
@@ -0,0 +1,39 @@
1
+ name: Build and test application on push to remote
2
+
3
+ on: [push]
4
+
5
+ jobs:
6
+ build:
7
+ runs-on: ubuntu-latest
8
+ steps:
9
+ - uses: actions/checkout@v4
10
+ - uses: pnpm/action-setup@v4
11
+ with:
12
+ version: 11
13
+ - uses: actions/setup-node@v4
14
+ with:
15
+ node-version: 22
16
+ # No lock file, so this tests what a consumer resolves. The settings match
17
+ # web-renderer's pnpm-workspace.yaml: several @civ-clone packages depend on
18
+ # each other as `github:` URLs, this project's own releases are often only
19
+ # minutes old, and nothing here needs a dependency's build script.
20
+ - run: >-
21
+ pnpm install --no-frozen-lockfile
22
+ --config.block-exotic-subdeps=false
23
+ --config.minimum-release-age=0
24
+ --config.strict-dep-builds=false
25
+ - run: npm run --if-present prettier:check
26
+ # The tests load the committed `.js`, so compile to prove the source
27
+ # type-checks, and fail if the committed output is stale. `--force`
28
+ # because a fresh checkout's timestamps can make `tsc --build` treat the
29
+ # committed output as up to date and skip the compile entirely.
30
+ - run: npm run --if-present ts:compile -- --force
31
+ - run: npm run --if-present prettier:format
32
+ # Stale *or never committed* output fails: `git diff` alone misses a new
33
+ # module whose `.js` was never added, which the published package would
34
+ # then omit. Only the compiled extensions, so the lock file the install
35
+ # writes cannot fail it; `node_modules` is ignored, so it stays out.
36
+ - run: |
37
+ git status --short --untracked-files=all -- '*.js' '*.d.ts' '*.js.map'
38
+ test -z "$(git status --porcelain --untracked-files=all -- '*.js' '*.d.ts' '*.js.map')"
39
+ - run: npm run --if-present test
package/.prettierrc ADDED
@@ -0,0 +1,4 @@
1
+ {
2
+ "semi": true,
3
+ "singleQuote": true
4
+ }
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2020 Dom Hastings
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -1,3 +1,32 @@
1
- # Temporary Holding Version
1
+ # base-city-lifecycle
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ A city's turn and what goes with it, as a library for a ruleset's city rules:
4
+
5
+ - `lib/processYield`: `storeFood(food, city, cityGrowthRegistry)` puts a city's food for the turn into its store and
6
+ checks it. `buildWithProduction(production, city, cityYields, ProductionSupportType, cityBuildRegistry, ruleRegistry)`
7
+ puts its production into its build and checks it: while production net of unit support is below 0, the units it
8
+ can't support go to the `Unsupported` rules, furthest from the city first, and nothing is added while a
9
+ `CivilDisorder` rule holds.
10
+ - `lib/foodStorage`: `hasFoodToGrow(cityGrowth)` and `grow(cityGrowth)`; `isFoodExhausted(cityGrowth)` (the store is
11
+ below 0) and `exhaustFood(cityGrowth, ruleRegistry)`, which runs the `FoodExhausted` rules.
12
+ - `lib/famine`: `foodSupportedUnit(cityGrowth, FoodSupportType)`, the first unit a city feeds;
13
+ `emitFoodStorageExhausted(cityGrowth, unit, engine)`; `loseFoodSupportedUnit(cityGrowth, FoodSupportType)`,
14
+ `shrinkFromFamine(cityGrowth)` and `emptyFoodStore(cityGrowth)`.
15
+ - `lib/canBeWorked`: `isNotWorked(tile, workedTileRegistry)`, `isNotOccupiedByEnemy(tile, city, unitRegistry)` and
16
+ `isNotAnotherCity(tile, city, cityRegistry)`.
17
+ - `lib/occupiedTiles`: `enemyWorkedTile(unit, tile, workedTileRegistry)`, a tile another player's city works (not its
18
+ centre), and `releaseEnemyWorkedTile(unit, tile, workedTileRegistry, ruleRegistry)`, which takes it from that city
19
+ and runs its `TileReassigned` rules.
20
+ - `lib/playerActions`: the city actions a player is offered, each as a check and a list: `hasCityToChooseProduction`
21
+ and `chooseProductionActions`, `hasCityToChangeProduction` and `changeProductionActions`, `hasCity` and
22
+ `changeWorkedTileActions`, `hasSpecialist` and `changeSpecialistActions`.
23
+ - `lib/unsupported`: `disbandUnsupported(city, unit)`.
24
+ - `lib/claimName`: `claimName(city, cityNames)` and the `CityNames` type.
25
+ - `lib/events`: `emitTileWorked(city, tile, engine)`, `emitTileReleased(city, tile, engine)` and
26
+ `emitBuildingHeld(cityBuild, item, held, engine)`, for clients.
27
+
28
+ The yield types a ruleset pays unit support with are passed in (`FoodSupportType`, `ProductionSupportType`: a yield
29
+ that knows its `unit()`).
30
+
31
+ A library: it registers no rules. The ruleset decides the rules, their ids and order, and when each runs. See
32
+ `civ1-city` for Civilization's.
@@ -0,0 +1,28 @@
1
+ import City from '@civ-clone/core-city/City';
2
+ import { CityRegistry } from '@civ-clone/core-city/CityRegistry';
3
+ import Tile from '@civ-clone/core-world/Tile';
4
+ import { UnitRegistry } from '@civ-clone/core-unit/UnitRegistry';
5
+ import { WorkedTileRegistry } from '@civ-clone/core-city/WorkedTileRegistry';
6
+ /**
7
+ * Whether no city works `tile` yet.
8
+ */
9
+ export declare const isNotWorked: (
10
+ tile: Tile,
11
+ workedTileRegistry: WorkedTileRegistry
12
+ ) => boolean;
13
+ /**
14
+ * Whether no other player's unit stands on `tile`.
15
+ */
16
+ export declare const isNotOccupiedByEnemy: (
17
+ tile: Tile,
18
+ city: City,
19
+ unitRegistry: UnitRegistry
20
+ ) => boolean;
21
+ /**
22
+ * Whether `tile` is not the centre of a city other than `city`.
23
+ */
24
+ export declare const isNotAnotherCity: (
25
+ tile: Tile,
26
+ city: City,
27
+ cityRegistry: CityRegistry
28
+ ) => boolean;
@@ -0,0 +1,27 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.isNotAnotherCity = exports.isNotOccupiedByEnemy = exports.isNotWorked = void 0;
4
+ /**
5
+ * Whether no city works `tile` yet.
6
+ */
7
+ const isNotWorked = (tile, workedTileRegistry) => !workedTileRegistry.tileIsWorked(tile);
8
+ exports.isNotWorked = isNotWorked;
9
+ /**
10
+ * Whether no other player's unit stands on `tile`.
11
+ */
12
+ const isNotOccupiedByEnemy = (tile, city, unitRegistry) => !unitRegistry
13
+ .getByTile(tile)
14
+ .some((unit) => unit.player() !== city.player());
15
+ exports.isNotOccupiedByEnemy = isNotOccupiedByEnemy;
16
+ /**
17
+ * Whether `tile` is not the centre of a city other than `city`.
18
+ */
19
+ const isNotAnotherCity = (tile, city, cityRegistry) => {
20
+ const otherTileCity = cityRegistry.getByTile(tile);
21
+ if (otherTileCity === null) {
22
+ return true;
23
+ }
24
+ return otherTileCity === city;
25
+ };
26
+ exports.isNotAnotherCity = isNotAnotherCity;
27
+ //# sourceMappingURL=canBeWorked.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"canBeWorked.js","sourceRoot":"","sources":["canBeWorked.ts"],"names":[],"mappings":";;;AAOA;;GAEG;AACI,MAAM,WAAW,GAAG,CACzB,IAAU,EACV,kBAAsC,EAC7B,EAAE,CAAC,CAAC,kBAAkB,CAAC,YAAY,CAAC,IAAI,CAAC,CAAC;AAHxC,QAAA,WAAW,eAG6B;AAErD;;GAEG;AACI,MAAM,oBAAoB,GAAG,CAClC,IAAU,EACV,IAAU,EACV,YAA0B,EACjB,EAAE,CACX,CAAC,YAAY;KACV,SAAS,CAAC,IAAI,CAAC;KACf,IAAI,CAAC,CAAC,IAAU,EAAE,EAAE,CAAC,IAAI,CAAC,MAAM,EAAE,KAAK,IAAI,CAAC,MAAM,EAAE,CAAC,CAAC;AAP9C,QAAA,oBAAoB,wBAO0B;AAE3D;;GAEG;AACI,MAAM,gBAAgB,GAAG,CAC9B,IAAU,EACV,IAAU,EACV,YAA0B,EACjB,EAAE;IACX,MAAM,aAAa,GAAG,YAAY,CAAC,SAAS,CAAC,IAAI,CAAC,CAAC;IAEnD,IAAI,aAAa,KAAK,IAAI,EAAE;QAC1B,OAAO,IAAI,CAAC;KACb;IAED,OAAO,aAAa,KAAK,IAAI,CAAC;AAChC,CAAC,CAAC;AAZW,QAAA,gBAAgB,oBAY3B"}
@@ -0,0 +1,43 @@
1
+ import City from '@civ-clone/core-city/City';
2
+ import { CityRegistry } from '@civ-clone/core-city/CityRegistry';
3
+ import Tile from '@civ-clone/core-world/Tile';
4
+ import Unit from '@civ-clone/core-unit/Unit';
5
+ import { UnitRegistry } from '@civ-clone/core-unit/UnitRegistry';
6
+ import { WorkedTileRegistry } from '@civ-clone/core-city/WorkedTileRegistry';
7
+
8
+ /**
9
+ * Whether no city works `tile` yet.
10
+ */
11
+ export const isNotWorked = (
12
+ tile: Tile,
13
+ workedTileRegistry: WorkedTileRegistry
14
+ ): boolean => !workedTileRegistry.tileIsWorked(tile);
15
+
16
+ /**
17
+ * Whether no other player's unit stands on `tile`.
18
+ */
19
+ export const isNotOccupiedByEnemy = (
20
+ tile: Tile,
21
+ city: City,
22
+ unitRegistry: UnitRegistry
23
+ ): boolean =>
24
+ !unitRegistry
25
+ .getByTile(tile)
26
+ .some((unit: Unit) => unit.player() !== city.player());
27
+
28
+ /**
29
+ * Whether `tile` is not the centre of a city other than `city`.
30
+ */
31
+ export const isNotAnotherCity = (
32
+ tile: Tile,
33
+ city: City,
34
+ cityRegistry: CityRegistry
35
+ ): boolean => {
36
+ const otherTileCity = cityRegistry.getByTile(tile);
37
+
38
+ if (otherTileCity === null) {
39
+ return true;
40
+ }
41
+
42
+ return otherTileCity === city;
43
+ };
@@ -0,0 +1,12 @@
1
+ import City from '@civ-clone/core-city/City';
2
+ /**
3
+ * What keeps the names cities have been given: a game's `cityNames` (core-civilization's `CityNameRegistry`), of which
4
+ * only this much is asked.
5
+ */
6
+ export type CityNames = {
7
+ claim(name: string): void;
8
+ };
9
+ /**
10
+ * `city`'s name is claimed, so that no city founded later is given it too.
11
+ */
12
+ export declare const claimName: (city: City, cityNames: CityNames) => void;
@@ -0,0 +1,9 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.claimName = void 0;
4
+ /**
5
+ * `city`'s name is claimed, so that no city founded later is given it too.
6
+ */
7
+ const claimName = (city, cityNames) => cityNames.claim(city.name());
8
+ exports.claimName = claimName;
9
+ //# sourceMappingURL=claimName.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"claimName.js","sourceRoot":"","sources":["claimName.ts"],"names":[],"mappings":";;;AAQA;;GAEG;AACI,MAAM,SAAS,GAAG,CAAC,IAAU,EAAE,SAAoB,EAAQ,EAAE,CAClE,SAAS,CAAC,KAAK,CAAC,IAAI,CAAC,IAAI,EAAE,CAAC,CAAC;AADlB,QAAA,SAAS,aACS"}
@@ -0,0 +1,13 @@
1
+ import City from '@civ-clone/core-city/City';
2
+
3
+ /**
4
+ * What keeps the names cities have been given: a game's `cityNames` (core-civilization's `CityNameRegistry`), of which
5
+ * only this much is asked.
6
+ */
7
+ export type CityNames = { claim(name: string): void };
8
+
9
+ /**
10
+ * `city`'s name is claimed, so that no city founded later is given it too.
11
+ */
12
+ export const claimName = (city: City, cityNames: CityNames): void =>
13
+ cityNames.claim(city.name());
@@ -0,0 +1,31 @@
1
+ import { IBuildable as Buildable } from '@civ-clone/core-city-build/Buildable';
2
+ import City from '@civ-clone/core-city/City';
3
+ import CityBuild from '@civ-clone/core-city-build/CityBuild';
4
+ import { Engine } from '@civ-clone/core-engine/Engine';
5
+ import Tile from '@civ-clone/core-world/Tile';
6
+ /**
7
+ * Tells clients that `city` now works `tile`, so they can redraw it and show who works it.
8
+ */
9
+ export declare const emitTileWorked: (
10
+ city: City,
11
+ tile: Tile,
12
+ engine: Engine
13
+ ) => void;
14
+ /**
15
+ * Tells clients that `city` no longer works `tile`, so they can redraw it.
16
+ */
17
+ export declare const emitTileReleased: (
18
+ city: City,
19
+ tile: Tile,
20
+ engine: Engine
21
+ ) => void;
22
+ /**
23
+ * Tells clients that `cityBuild`'s `item` is paid for but held back, with the ids of the `Hold` rules holding it (`null`
24
+ * for a rule without one), for a client to word.
25
+ */
26
+ export declare const emitBuildingHeld: (
27
+ cityBuild: CityBuild,
28
+ item: Buildable,
29
+ held: (string | null)[],
30
+ engine: Engine
31
+ ) => void;
package/lib/events.js ADDED
@@ -0,0 +1,26 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.emitBuildingHeld = exports.emitTileReleased = exports.emitTileWorked = void 0;
4
+ /**
5
+ * Tells clients that `city` now works `tile`, so they can redraw it and show who works it.
6
+ */
7
+ const emitTileWorked = (city, tile, engine) => {
8
+ engine.emit('city:tile-worked', city, tile);
9
+ };
10
+ exports.emitTileWorked = emitTileWorked;
11
+ /**
12
+ * Tells clients that `city` no longer works `tile`, so they can redraw it.
13
+ */
14
+ const emitTileReleased = (city, tile, engine) => {
15
+ engine.emit('city:tile-released', city, tile);
16
+ };
17
+ exports.emitTileReleased = emitTileReleased;
18
+ /**
19
+ * Tells clients that `cityBuild`'s `item` is paid for but held back, with the ids of the `Hold` rules holding it (`null`
20
+ * for a rule without one), for a client to word.
21
+ */
22
+ const emitBuildingHeld = (cityBuild, item, held, engine) => {
23
+ engine.emit('city:building-held', cityBuild, item, held);
24
+ };
25
+ exports.emitBuildingHeld = emitBuildingHeld;
26
+ //# sourceMappingURL=events.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"events.js","sourceRoot":"","sources":["events.ts"],"names":[],"mappings":";;;AAMA;;GAEG;AACI,MAAM,cAAc,GAAG,CAC5B,IAAU,EACV,IAAU,EACV,MAAc,EACR,EAAE;IACR,MAAM,CAAC,IAAI,CAAC,kBAAkB,EAAE,IAAI,EAAE,IAAI,CAAC,CAAC;AAC9C,CAAC,CAAC;AANW,QAAA,cAAc,kBAMzB;AAEF;;GAEG;AACI,MAAM,gBAAgB,GAAG,CAC9B,IAAU,EACV,IAAU,EACV,MAAc,EACR,EAAE;IACR,MAAM,CAAC,IAAI,CAAC,oBAAoB,EAAE,IAAI,EAAE,IAAI,CAAC,CAAC;AAChD,CAAC,CAAC;AANW,QAAA,gBAAgB,oBAM3B;AAEF;;;GAGG;AACI,MAAM,gBAAgB,GAAG,CAC9B,SAAoB,EACpB,IAAe,EACf,IAAuB,EACvB,MAAc,EACR,EAAE;IACR,MAAM,CAAC,IAAI,CAAC,oBAAoB,EAAE,SAAS,EAAE,IAAI,EAAE,IAAI,CAAC,CAAC;AAC3D,CAAC,CAAC;AAPW,QAAA,gBAAgB,oBAO3B"}
package/lib/events.ts ADDED
@@ -0,0 +1,40 @@
1
+ import { IBuildable as Buildable } from '@civ-clone/core-city-build/Buildable';
2
+ import City from '@civ-clone/core-city/City';
3
+ import CityBuild from '@civ-clone/core-city-build/CityBuild';
4
+ import { Engine } from '@civ-clone/core-engine/Engine';
5
+ import Tile from '@civ-clone/core-world/Tile';
6
+
7
+ /**
8
+ * Tells clients that `city` now works `tile`, so they can redraw it and show who works it.
9
+ */
10
+ export const emitTileWorked = (
11
+ city: City,
12
+ tile: Tile,
13
+ engine: Engine
14
+ ): void => {
15
+ engine.emit('city:tile-worked', city, tile);
16
+ };
17
+
18
+ /**
19
+ * Tells clients that `city` no longer works `tile`, so they can redraw it.
20
+ */
21
+ export const emitTileReleased = (
22
+ city: City,
23
+ tile: Tile,
24
+ engine: Engine
25
+ ): void => {
26
+ engine.emit('city:tile-released', city, tile);
27
+ };
28
+
29
+ /**
30
+ * Tells clients that `cityBuild`'s `item` is paid for but held back, with the ids of the `Hold` rules holding it (`null`
31
+ * for a rule without one), for a client to word.
32
+ */
33
+ export const emitBuildingHeld = (
34
+ cityBuild: CityBuild,
35
+ item: Buildable,
36
+ held: (string | null)[],
37
+ engine: Engine
38
+ ): void => {
39
+ engine.emit('city:building-held', cityBuild, item, held);
40
+ };
@@ -0,0 +1,41 @@
1
+ import CityGrowth from '@civ-clone/core-city-growth/CityGrowth';
2
+ import { Engine } from '@civ-clone/core-engine/Engine';
3
+ import Unit from '@civ-clone/core-unit/Unit';
4
+ import Yield from '@civ-clone/core-yield/Yield';
5
+ /**
6
+ * A yield a city pays for one of its units, which knows that unit (or `null`).
7
+ */
8
+ export type UnitYieldType = abstract new (...args: any[]) => Yield & {
9
+ unit(): Unit | null;
10
+ };
11
+ /**
12
+ * The first unit `cityGrowth`'s city feeds, as a `FoodSupportType` yield, in the order of the city's yields (the order
13
+ * its units were built), or `null`.
14
+ */
15
+ export declare const foodSupportedUnit: (
16
+ cityGrowth: CityGrowth,
17
+ FoodSupportType: UnitYieldType
18
+ ) => Unit | null;
19
+ /**
20
+ * Tells clients that `cityGrowth`'s city has run out of food, with the unit it will lose for it, when there is one.
21
+ */
22
+ export declare const emitFoodStorageExhausted: (
23
+ cityGrowth: CityGrowth,
24
+ unit: Unit | null,
25
+ engine: Engine
26
+ ) => void;
27
+ /**
28
+ * The city loses the first unit it feeds (see `foodSupportedUnit`), which must exist.
29
+ */
30
+ export declare const loseFoodSupportedUnit: (
31
+ cityGrowth: CityGrowth,
32
+ FoodSupportType: UnitYieldType
33
+ ) => void;
34
+ /**
35
+ * The city shrinks, because its food ran out.
36
+ */
37
+ export declare const shrinkFromFamine: (cityGrowth: CityGrowth) => void;
38
+ /**
39
+ * The city's food store is emptied.
40
+ */
41
+ export declare const emptyFoodStore: (cityGrowth: CityGrowth) => void;
package/lib/famine.js ADDED
@@ -0,0 +1,44 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.emptyFoodStore = exports.shrinkFromFamine = exports.loseFoodSupportedUnit = exports.emitFoodStorageExhausted = exports.foodSupportedUnit = void 0;
4
+ const FoodExhausted_1 = require("@civ-clone/core-city-growth/Rules/FoodExhausted");
5
+ /**
6
+ * The first unit `cityGrowth`'s city feeds, as a `FoodSupportType` yield, in the order of the city's yields (the order
7
+ * its units were built), or `null`.
8
+ */
9
+ const foodSupportedUnit = (cityGrowth, FoodSupportType) => {
10
+ var _a, _b;
11
+ return (_b = (_a = cityGrowth
12
+ .city()
13
+ .yields()
14
+ .find((cityYield) => cityYield instanceof FoodSupportType &&
15
+ cityYield.unit() !== null)) === null || _a === void 0 ? void 0 : _a.unit()) !== null && _b !== void 0 ? _b : null;
16
+ };
17
+ exports.foodSupportedUnit = foodSupportedUnit;
18
+ /**
19
+ * Tells clients that `cityGrowth`'s city has run out of food, with the unit it will lose for it, when there is one.
20
+ */
21
+ const emitFoodStorageExhausted = (cityGrowth, unit, engine) => {
22
+ if (unit === null) {
23
+ engine.emit('city:food-storage-exhausted', cityGrowth.city());
24
+ return;
25
+ }
26
+ engine.emit('city:food-storage-exhausted', cityGrowth.city(), unit);
27
+ };
28
+ exports.emitFoodStorageExhausted = emitFoodStorageExhausted;
29
+ /**
30
+ * The city loses the first unit it feeds (see `foodSupportedUnit`), which must exist.
31
+ */
32
+ const loseFoodSupportedUnit = (cityGrowth, FoodSupportType) => (0, exports.foodSupportedUnit)(cityGrowth, FoodSupportType).destroy();
33
+ exports.loseFoodSupportedUnit = loseFoodSupportedUnit;
34
+ /**
35
+ * The city shrinks, because its food ran out.
36
+ */
37
+ const shrinkFromFamine = (cityGrowth) => cityGrowth.shrink(FoodExhausted_1.default);
38
+ exports.shrinkFromFamine = shrinkFromFamine;
39
+ /**
40
+ * The city's food store is emptied.
41
+ */
42
+ const emptyFoodStore = (cityGrowth) => cityGrowth.empty();
43
+ exports.emptyFoodStore = emptyFoodStore;
44
+ //# sourceMappingURL=famine.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"famine.js","sourceRoot":"","sources":["famine.ts"],"names":[],"mappings":";;;AAEA,mFAA4E;AAW5E;;;GAGG;AACI,MAAM,iBAAiB,GAAG,CAC/B,UAAsB,EACtB,eAA8B,EACjB,EAAE;;IACf,OAAA,MAAA,MACE,UAAU;SACP,IAAI,EAAE;SACN,MAAM,EAAE;SACR,IAAI,CACH,CAAC,SAAgB,EAAW,EAAE,CAC5B,SAAS,YAAY,eAAe;QACnC,SAAyC,CAAC,IAAI,EAAE,KAAK,IAAI,CAEjE,0CAAE,IAAI,EAAE,mCAAI,IAAI,CAAA;CAAA,CAAC;AAbP,QAAA,iBAAiB,qBAaV;AAEpB;;GAEG;AACI,MAAM,wBAAwB,GAAG,CACtC,UAAsB,EACtB,IAAiB,EACjB,MAAc,EACR,EAAE;IACR,IAAI,IAAI,KAAK,IAAI,EAAE;QACjB,MAAM,CAAC,IAAI,CAAC,6BAA6B,EAAE,UAAU,CAAC,IAAI,EAAE,CAAC,CAAC;QAE9D,OAAO;KACR;IAED,MAAM,CAAC,IAAI,CAAC,6BAA6B,EAAE,UAAU,CAAC,IAAI,EAAE,EAAE,IAAI,CAAC,CAAC;AACtE,CAAC,CAAC;AAZW,QAAA,wBAAwB,4BAYnC;AAEF;;GAEG;AACI,MAAM,qBAAqB,GAAG,CACnC,UAAsB,EACtB,eAA8B,EACxB,EAAE,CAAC,IAAA,yBAAiB,EAAC,UAAU,EAAE,eAAe,CAAE,CAAC,OAAO,EAAE,CAAC;AAHxD,QAAA,qBAAqB,yBAGmC;AAErE;;GAEG;AACI,MAAM,gBAAgB,GAAG,CAAC,UAAsB,EAAQ,EAAE,CAC/D,UAAU,CAAC,MAAM,CAAC,uBAAa,CAAC,CAAC;AADtB,QAAA,gBAAgB,oBACM;AAEnC;;GAEG;AACI,MAAM,cAAc,GAAG,CAAC,UAAsB,EAAQ,EAAE,CAC7D,UAAU,CAAC,KAAK,EAAE,CAAC;AADR,QAAA,cAAc,kBACN"}
package/lib/famine.ts ADDED
@@ -0,0 +1,68 @@
1
+ import CityGrowth from '@civ-clone/core-city-growth/CityGrowth';
2
+ import { Engine } from '@civ-clone/core-engine/Engine';
3
+ import FoodExhausted from '@civ-clone/core-city-growth/Rules/FoodExhausted';
4
+ import Unit from '@civ-clone/core-unit/Unit';
5
+ import Yield from '@civ-clone/core-yield/Yield';
6
+
7
+ /**
8
+ * A yield a city pays for one of its units, which knows that unit (or `null`).
9
+ */
10
+ export type UnitYieldType = abstract new (...args: any[]) => Yield & {
11
+ unit(): Unit | null;
12
+ };
13
+
14
+ /**
15
+ * The first unit `cityGrowth`'s city feeds, as a `FoodSupportType` yield, in the order of the city's yields (the order
16
+ * its units were built), or `null`.
17
+ */
18
+ export const foodSupportedUnit = (
19
+ cityGrowth: CityGrowth,
20
+ FoodSupportType: UnitYieldType
21
+ ): Unit | null =>
22
+ (
23
+ cityGrowth
24
+ .city()
25
+ .yields()
26
+ .find(
27
+ (cityYield: Yield): boolean =>
28
+ cityYield instanceof FoodSupportType &&
29
+ (cityYield as InstanceType<UnitYieldType>).unit() !== null
30
+ ) as InstanceType<UnitYieldType> | undefined
31
+ )?.unit() ?? null;
32
+
33
+ /**
34
+ * Tells clients that `cityGrowth`'s city has run out of food, with the unit it will lose for it, when there is one.
35
+ */
36
+ export const emitFoodStorageExhausted = (
37
+ cityGrowth: CityGrowth,
38
+ unit: Unit | null,
39
+ engine: Engine
40
+ ): void => {
41
+ if (unit === null) {
42
+ engine.emit('city:food-storage-exhausted', cityGrowth.city());
43
+
44
+ return;
45
+ }
46
+
47
+ engine.emit('city:food-storage-exhausted', cityGrowth.city(), unit);
48
+ };
49
+
50
+ /**
51
+ * The city loses the first unit it feeds (see `foodSupportedUnit`), which must exist.
52
+ */
53
+ export const loseFoodSupportedUnit = (
54
+ cityGrowth: CityGrowth,
55
+ FoodSupportType: UnitYieldType
56
+ ): void => foodSupportedUnit(cityGrowth, FoodSupportType)!.destroy();
57
+
58
+ /**
59
+ * The city shrinks, because its food ran out.
60
+ */
61
+ export const shrinkFromFamine = (cityGrowth: CityGrowth): void =>
62
+ cityGrowth.shrink(FoodExhausted);
63
+
64
+ /**
65
+ * The city's food store is emptied.
66
+ */
67
+ export const emptyFoodStore = (cityGrowth: CityGrowth): void =>
68
+ cityGrowth.empty();
@@ -0,0 +1,21 @@
1
+ import CityGrowth from '@civ-clone/core-city-growth/CityGrowth';
2
+ import { RuleRegistry } from '@civ-clone/core-rule/RuleRegistry';
3
+ /**
4
+ * Whether a city's food store holds enough for it to grow.
5
+ */
6
+ export declare const hasFoodToGrow: (cityGrowth: CityGrowth) => boolean;
7
+ /**
8
+ * The city grows.
9
+ */
10
+ export declare const grow: (cityGrowth: CityGrowth) => void;
11
+ /**
12
+ * Whether a city's food store has run out: it is below 0 once the turn's food is added.
13
+ */
14
+ export declare const isFoodExhausted: (cityGrowth: CityGrowth) => boolean;
15
+ /**
16
+ * The city's food has run out: its `FoodExhausted` rules decide what that costs it.
17
+ */
18
+ export declare const exhaustFood: (
19
+ cityGrowth: CityGrowth,
20
+ ruleRegistry: RuleRegistry
21
+ ) => void;
@@ -0,0 +1,27 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.exhaustFood = exports.isFoodExhausted = exports.grow = exports.hasFoodToGrow = void 0;
4
+ const FoodExhausted_1 = require("@civ-clone/core-city-growth/Rules/FoodExhausted");
5
+ /**
6
+ * Whether a city's food store holds enough for it to grow.
7
+ */
8
+ const hasFoodToGrow = (cityGrowth) => cityGrowth.progress().value() >= cityGrowth.cost().value();
9
+ exports.hasFoodToGrow = hasFoodToGrow;
10
+ /**
11
+ * The city grows.
12
+ */
13
+ const grow = (cityGrowth) => cityGrowth.grow();
14
+ exports.grow = grow;
15
+ /**
16
+ * Whether a city's food store has run out: it is below 0 once the turn's food is added.
17
+ */
18
+ const isFoodExhausted = (cityGrowth) => cityGrowth.progress().value() < 0;
19
+ exports.isFoodExhausted = isFoodExhausted;
20
+ /**
21
+ * The city's food has run out: its `FoodExhausted` rules decide what that costs it.
22
+ */
23
+ const exhaustFood = (cityGrowth, ruleRegistry) => {
24
+ ruleRegistry.process(FoodExhausted_1.default, cityGrowth);
25
+ };
26
+ exports.exhaustFood = exhaustFood;
27
+ //# sourceMappingURL=foodStorage.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"foodStorage.js","sourceRoot":"","sources":["foodStorage.ts"],"names":[],"mappings":";;;AACA,mFAA4E;AAG5E;;GAEG;AACI,MAAM,aAAa,GAAG,CAAC,UAAsB,EAAW,EAAE,CAC/D,UAAU,CAAC,QAAQ,EAAE,CAAC,KAAK,EAAE,IAAI,UAAU,CAAC,IAAI,EAAE,CAAC,KAAK,EAAE,CAAC;AADhD,QAAA,aAAa,iBACmC;AAE7D;;GAEG;AACI,MAAM,IAAI,GAAG,CAAC,UAAsB,EAAQ,EAAE,CAAC,UAAU,CAAC,IAAI,EAAE,CAAC;AAA3D,QAAA,IAAI,QAAuD;AAExE;;GAEG;AACI,MAAM,eAAe,GAAG,CAAC,UAAsB,EAAW,EAAE,CACjE,UAAU,CAAC,QAAQ,EAAE,CAAC,KAAK,EAAE,GAAG,CAAC,CAAC;AADvB,QAAA,eAAe,mBACQ;AAEpC;;GAEG;AACI,MAAM,WAAW,GAAG,CACzB,UAAsB,EACtB,YAA0B,EACpB,EAAE;IACR,YAAY,CAAC,OAAO,CAAC,uBAAa,EAAE,UAAU,CAAC,CAAC;AAClD,CAAC,CAAC;AALW,QAAA,WAAW,eAKtB"}
@@ -0,0 +1,30 @@
1
+ import CityGrowth from '@civ-clone/core-city-growth/CityGrowth';
2
+ import FoodExhausted from '@civ-clone/core-city-growth/Rules/FoodExhausted';
3
+ import { RuleRegistry } from '@civ-clone/core-rule/RuleRegistry';
4
+
5
+ /**
6
+ * Whether a city's food store holds enough for it to grow.
7
+ */
8
+ export const hasFoodToGrow = (cityGrowth: CityGrowth): boolean =>
9
+ cityGrowth.progress().value() >= cityGrowth.cost().value();
10
+
11
+ /**
12
+ * The city grows.
13
+ */
14
+ export const grow = (cityGrowth: CityGrowth): void => cityGrowth.grow();
15
+
16
+ /**
17
+ * Whether a city's food store has run out: it is below 0 once the turn's food is added.
18
+ */
19
+ export const isFoodExhausted = (cityGrowth: CityGrowth): boolean =>
20
+ cityGrowth.progress().value() < 0;
21
+
22
+ /**
23
+ * The city's food has run out: its `FoodExhausted` rules decide what that costs it.
24
+ */
25
+ export const exhaustFood = (
26
+ cityGrowth: CityGrowth,
27
+ ruleRegistry: RuleRegistry
28
+ ): void => {
29
+ ruleRegistry.process(FoodExhausted, cityGrowth);
30
+ };
@@ -0,0 +1,25 @@
1
+ import { RuleRegistry } from '@civ-clone/core-rule/RuleRegistry';
2
+ import Tile from '@civ-clone/core-world/Tile';
3
+ import Unit from '@civ-clone/core-unit/Unit';
4
+ import WorkedTile from '@civ-clone/core-city/WorkedTile';
5
+ import { WorkedTileRegistry } from '@civ-clone/core-city/WorkedTileRegistry';
6
+ /**
7
+ * The `WorkedTile` for `tile` if a city of a player other than `unit`'s works it, other than as its centre (a city always
8
+ * works its own centre, even while a unit capturing it is moving in), or `null`.
9
+ */
10
+ export declare const enemyWorkedTile: (
11
+ unit: Unit,
12
+ tile: Tile,
13
+ workedTileRegistry: WorkedTileRegistry
14
+ ) => WorkedTile | null;
15
+ /**
16
+ * `unit` has moved onto `tile`: if another player's city works it (see `enemyWorkedTile`), the city stops working it,
17
+ * and its `TileReassigned` rules choose what it works instead. The tile is looked up again here rather than in a
18
+ * `Criterion`, because an earlier `Moved` effect (cargo moving with its transport, say) may already have released it.
19
+ */
20
+ export declare const releaseEnemyWorkedTile: (
21
+ unit: Unit,
22
+ tile: Tile,
23
+ workedTileRegistry: WorkedTileRegistry,
24
+ ruleRegistry: RuleRegistry
25
+ ) => void;