@aephia/atlas-kit 0.8.0-next.9 → 0.8.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.
- package/README.md +1 -1
- package/dist/claim-stakes/actions/index.d.ts +153 -0
- package/dist/claim-stakes/actions/index.d.ts.map +1 -1
- package/dist/claim-stakes/actions/index.js +560 -72
- package/dist/claim-stakes/actions/index.js.map +1 -1
- package/dist/claim-stakes/index.d.ts +1 -1
- package/dist/claim-stakes/index.d.ts.map +1 -1
- package/dist/claim-stakes/index.js +1 -1
- package/dist/claim-stakes/index.js.map +1 -1
- package/dist/client/contracts.d.ts +1 -1
- package/dist/client/contracts.d.ts.map +1 -1
- package/dist/client/deployments.d.ts +1 -1
- package/dist/client/deployments.js +1 -1
- package/dist/client/errors.d.ts +1 -1
- package/dist/client/errors.d.ts.map +1 -1
- package/dist/client/errors.js.map +1 -1
- package/dist/combat/actions/index.d.ts +137 -0
- package/dist/combat/actions/index.d.ts.map +1 -1
- package/dist/combat/actions/index.js +596 -1
- package/dist/combat/actions/index.js.map +1 -1
- package/dist/crafting/actions/habs.d.ts +366 -0
- package/dist/crafting/actions/habs.d.ts.map +1 -0
- package/dist/crafting/actions/habs.js +1030 -0
- package/dist/crafting/actions/habs.js.map +1 -0
- package/dist/crafting/actions/index.d.ts +14 -3
- package/dist/crafting/actions/index.d.ts.map +1 -1
- package/dist/crafting/actions/index.js +61 -25
- package/dist/crafting/actions/index.js.map +1 -1
- package/dist/crafting/index.d.ts +4 -1
- package/dist/crafting/index.d.ts.map +1 -1
- package/dist/crafting/index.js +1 -1
- package/dist/crafting/index.js.map +1 -1
- package/dist/fleets/actions/index.d.ts +10 -8
- package/dist/fleets/actions/index.d.ts.map +1 -1
- package/dist/fleets/actions/index.js +15 -13
- package/dist/fleets/actions/index.js.map +1 -1
- package/dist/identity/actions/daily-check-in.d.ts +57 -0
- package/dist/identity/actions/daily-check-in.d.ts.map +1 -0
- package/dist/identity/actions/daily-check-in.js +200 -0
- package/dist/identity/actions/daily-check-in.js.map +1 -0
- package/dist/identity/actions/index.d.ts +1 -0
- package/dist/identity/actions/index.d.ts.map +1 -1
- package/dist/identity/actions/index.js +1 -0
- package/dist/identity/actions/index.js.map +1 -1
- package/dist/identity/index.d.ts +169 -17
- package/dist/identity/index.d.ts.map +1 -1
- package/dist/identity/index.js +142 -9
- package/dist/identity/index.js.map +1 -1
- package/dist/internal/c4/career-xp.d.ts +113 -26
- package/dist/internal/c4/career-xp.d.ts.map +1 -1
- package/dist/internal/c4/career-xp.js +101 -38
- package/dist/internal/c4/career-xp.js.map +1 -1
- package/dist/internal/c4/character-registration.d.ts.map +1 -1
- package/dist/internal/c4/character-registration.js +3 -0
- package/dist/internal/c4/character-registration.js.map +1 -1
- package/dist/internal/c4/character.d.ts +68 -0
- package/dist/internal/c4/character.d.ts.map +1 -1
- package/dist/internal/c4/character.js +46 -2
- package/dist/internal/c4/character.js.map +1 -1
- package/dist/internal/c4/claim-stake.d.ts +8 -0
- package/dist/internal/c4/claim-stake.d.ts.map +1 -1
- package/dist/internal/c4/claim-stake.js +5 -0
- package/dist/internal/c4/claim-stake.js.map +1 -1
- package/dist/internal/c4/combat-storage.d.ts +4 -0
- package/dist/internal/c4/combat-storage.d.ts.map +1 -1
- package/dist/internal/c4/combat-storage.js +6 -1
- package/dist/internal/c4/combat-storage.js.map +1 -1
- package/dist/internal/c4/crafting-hab-storage.d.ts +42 -0
- package/dist/internal/c4/crafting-hab-storage.d.ts.map +1 -0
- package/dist/internal/c4/crafting-hab-storage.js +174 -0
- package/dist/internal/c4/crafting-hab-storage.js.map +1 -0
- package/dist/internal/c4/crafting-storage.d.ts +8 -0
- package/dist/internal/c4/crafting-storage.d.ts.map +1 -1
- package/dist/internal/c4/crafting-storage.js +18 -1
- package/dist/internal/c4/crafting-storage.js.map +1 -1
- package/dist/internal/c4/crafting.d.ts +8 -0
- package/dist/internal/c4/crafting.d.ts.map +1 -1
- package/dist/internal/c4/crafting.js +5 -0
- package/dist/internal/c4/crafting.js.map +1 -1
- package/dist/internal/c4/discriminators.d.ts +27 -0
- package/dist/internal/c4/discriminators.d.ts.map +1 -1
- package/dist/internal/c4/discriminators.js +55 -1
- package/dist/internal/c4/discriminators.js.map +1 -1
- package/dist/internal/c4/fleet-storage.d.ts +2 -1
- package/dist/internal/c4/fleet-storage.d.ts.map +1 -1
- package/dist/internal/c4/fleet-storage.js +14 -5
- package/dist/internal/c4/fleet-storage.js.map +1 -1
- package/dist/internal/c4/fleet.d.ts +33 -4
- package/dist/internal/c4/fleet.d.ts.map +1 -1
- package/dist/internal/c4/fleet.js +27 -2
- package/dist/internal/c4/fleet.js.map +1 -1
- package/dist/internal/c4/game.d.ts +33 -0
- package/dist/internal/c4/game.d.ts.map +1 -1
- package/dist/internal/c4/game.js +46 -0
- package/dist/internal/c4/game.js.map +1 -1
- package/dist/internal/c4/governance.d.ts +72 -0
- package/dist/internal/c4/governance.d.ts.map +1 -1
- package/dist/internal/c4/governance.js +108 -3
- package/dist/internal/c4/governance.js.map +1 -1
- package/dist/internal/c4/runtime-bindings.d.ts +1 -1
- package/dist/internal/c4/runtime-bindings.d.ts.map +1 -1
- package/dist/internal/c4/runtime-bindings.js +1 -1
- package/dist/internal/c4/runtime-bindings.js.map +1 -1
- package/dist/internal/c4/starbase-upgrade.d.ts +20 -0
- package/dist/internal/c4/starbase-upgrade.d.ts.map +1 -0
- package/dist/internal/c4/starbase-upgrade.js +44 -0
- package/dist/internal/c4/starbase-upgrade.js.map +1 -0
- package/dist/internal/c4/world.d.ts +3 -2
- package/dist/internal/c4/world.d.ts.map +1 -1
- package/dist/internal/c4/world.js +4 -18
- package/dist/internal/c4/world.js.map +1 -1
- package/dist/internal/planning/action-runtime.d.ts +3 -2
- package/dist/internal/planning/action-runtime.d.ts.map +1 -1
- package/dist/internal/planning/action-runtime.js +4 -3
- package/dist/internal/planning/action-runtime.js.map +1 -1
- package/dist/internal/planning/authorization-adapter.d.ts +363 -1
- package/dist/internal/planning/authorization-adapter.d.ts.map +1 -1
- package/dist/internal/planning/authorization-adapter.js +519 -1
- package/dist/internal/planning/authorization-adapter.js.map +1 -1
- package/dist/internal/planning/building-xp.d.ts +50 -0
- package/dist/internal/planning/building-xp.d.ts.map +1 -0
- package/dist/internal/planning/building-xp.js +166 -0
- package/dist/internal/planning/building-xp.js.map +1 -0
- package/dist/internal/planning/claim-stake-transfer-window.d.ts +18 -0
- package/dist/internal/planning/claim-stake-transfer-window.d.ts.map +1 -0
- package/dist/internal/planning/claim-stake-transfer-window.js +57 -0
- package/dist/internal/planning/claim-stake-transfer-window.js.map +1 -0
- package/dist/internal/planning/combat-storage-actions.d.ts +3 -2
- package/dist/internal/planning/combat-storage-actions.d.ts.map +1 -1
- package/dist/internal/planning/combat-storage-actions.js +26 -4
- package/dist/internal/planning/combat-storage-actions.js.map +1 -1
- package/dist/internal/planning/crafting-hab-storage-actions.d.ts +10 -0
- package/dist/internal/planning/crafting-hab-storage-actions.d.ts.map +1 -0
- package/dist/internal/planning/crafting-hab-storage-actions.js +46 -0
- package/dist/internal/planning/crafting-hab-storage-actions.js.map +1 -0
- package/dist/internal/planning/crafting-storage-actions.d.ts +5 -3
- package/dist/internal/planning/crafting-storage-actions.d.ts.map +1 -1
- package/dist/internal/planning/crafting-storage-actions.js +66 -4
- package/dist/internal/planning/crafting-storage-actions.js.map +1 -1
- package/dist/internal/planning/currency-cache-guard.d.ts.map +1 -1
- package/dist/internal/planning/currency-cache-guard.js +5 -1
- package/dist/internal/planning/currency-cache-guard.js.map +1 -1
- package/dist/internal/planning/daily-check-in.d.ts +71 -0
- package/dist/internal/planning/daily-check-in.d.ts.map +1 -0
- package/dist/internal/planning/daily-check-in.js +201 -0
- package/dist/internal/planning/daily-check-in.js.map +1 -0
- package/dist/internal/planning/democracy-history.d.ts +15 -0
- package/dist/internal/planning/democracy-history.d.ts.map +1 -1
- package/dist/internal/planning/democracy-history.js +23 -7
- package/dist/internal/planning/democracy-history.js.map +1 -1
- package/dist/internal/planning/democracy-storage-actions.d.ts +12 -0
- package/dist/internal/planning/democracy-storage-actions.d.ts.map +1 -0
- package/dist/internal/planning/democracy-storage-actions.js +81 -0
- package/dist/internal/planning/democracy-storage-actions.js.map +1 -0
- package/dist/internal/planning/fleet-state.js +1 -1
- package/dist/internal/planning/fleet-state.js.map +1 -1
- package/dist/internal/planning/loyalty-earning.d.ts +42 -0
- package/dist/internal/planning/loyalty-earning.d.ts.map +1 -0
- package/dist/internal/planning/loyalty-earning.js +143 -0
- package/dist/internal/planning/loyalty-earning.js.map +1 -0
- package/dist/internal/planning/placement-fee.d.ts +6 -0
- package/dist/internal/planning/placement-fee.d.ts.map +1 -0
- package/dist/internal/planning/placement-fee.js +9 -0
- package/dist/internal/planning/placement-fee.js.map +1 -0
- package/dist/internal/planning/plan-guards.d.ts.map +1 -1
- package/dist/internal/planning/plan-guards.js +11 -0
- package/dist/internal/planning/plan-guards.js.map +1 -1
- package/dist/internal/planning/profile-storage-funding-guard.d.ts +1 -1
- package/dist/internal/planning/profile-storage-funding-guard.d.ts.map +1 -1
- package/dist/internal/planning/profile-storage-funding-guard.js +53 -1
- package/dist/internal/planning/profile-storage-funding-guard.js.map +1 -1
- package/dist/internal/planning/rent-catalog.d.ts.map +1 -1
- package/dist/internal/planning/rent-catalog.js +37 -1
- package/dist/internal/planning/rent-catalog.js.map +1 -1
- package/dist/internal/planning/research-unlock.d.ts.map +1 -1
- package/dist/internal/planning/research-unlock.js.map +1 -1
- package/dist/internal/planning/reward-epoch.d.ts +44 -0
- package/dist/internal/planning/reward-epoch.d.ts.map +1 -0
- package/dist/internal/planning/reward-epoch.js +220 -0
- package/dist/internal/planning/reward-epoch.js.map +1 -0
- package/dist/internal/planning/runtime-guards.d.ts.map +1 -1
- package/dist/internal/planning/runtime-guards.js +92 -13
- package/dist/internal/planning/runtime-guards.js.map +1 -1
- package/dist/internal/planning/starbase-repair-guard.d.ts +12 -0
- package/dist/internal/planning/starbase-repair-guard.d.ts.map +1 -0
- package/dist/internal/planning/starbase-repair-guard.js +54 -0
- package/dist/internal/planning/starbase-repair-guard.js.map +1 -0
- package/dist/internal/planning/starbase-system-guard.d.ts.map +1 -1
- package/dist/internal/planning/starbase-system-guard.js +48 -3
- package/dist/internal/planning/starbase-system-guard.js.map +1 -1
- package/dist/internal/planning/starbase-upgrade-storage-actions.d.ts +7 -0
- package/dist/internal/planning/starbase-upgrade-storage-actions.d.ts.map +1 -0
- package/dist/internal/planning/starbase-upgrade-storage-actions.js +71 -0
- package/dist/internal/planning/starbase-upgrade-storage-actions.js.map +1 -0
- package/dist/internal/planning/starbase-upgrade-window.d.ts +22 -0
- package/dist/internal/planning/starbase-upgrade-window.d.ts.map +1 -0
- package/dist/internal/planning/starbase-upgrade-window.js +64 -0
- package/dist/internal/planning/starbase-upgrade-window.js.map +1 -0
- package/dist/internal/planning/storage-action-readers.d.ts.map +1 -1
- package/dist/internal/planning/storage-action-readers.js +36 -3
- package/dist/internal/planning/storage-action-readers.js.map +1 -1
- package/dist/internal/planning/storage-actions.d.ts +5 -1
- package/dist/internal/planning/storage-actions.d.ts.map +1 -1
- package/dist/internal/planning/storage-actions.js +8 -1
- package/dist/internal/planning/storage-actions.js.map +1 -1
- package/dist/mining/actions/index.js +1 -1
- package/dist/mining/actions/index.js.map +1 -1
- package/dist/planning/assembly.d.ts +2 -0
- package/dist/planning/assembly.d.ts.map +1 -1
- package/dist/planning/assembly.js +5 -1
- package/dist/planning/assembly.js.map +1 -1
- package/dist/planning/execution.d.ts.map +1 -1
- package/dist/planning/execution.js +132 -5
- package/dist/planning/execution.js.map +1 -1
- package/dist/planning/index.d.ts +75 -0
- package/dist/planning/index.d.ts.map +1 -1
- package/dist/planning/index.js +1 -0
- package/dist/planning/index.js.map +1 -1
- package/dist/planning/plan-validation.d.ts.map +1 -1
- package/dist/planning/plan-validation.js +27 -0
- package/dist/planning/plan-validation.js.map +1 -1
- package/dist/planning/selected-references.d.ts +83 -1
- package/dist/planning/selected-references.d.ts.map +1 -1
- package/dist/planning/selected-references.js +192 -2
- package/dist/planning/selected-references.js.map +1 -1
- package/dist/planning/simulation.d.ts.map +1 -1
- package/dist/planning/simulation.js +11 -3
- package/dist/planning/simulation.js.map +1 -1
- package/dist/starbases/actions/democracy.d.ts +205 -0
- package/dist/starbases/actions/democracy.d.ts.map +1 -0
- package/dist/starbases/actions/democracy.js +861 -0
- package/dist/starbases/actions/democracy.js.map +1 -0
- package/dist/starbases/actions/index.d.ts +2 -0
- package/dist/starbases/actions/index.d.ts.map +1 -1
- package/dist/starbases/actions/index.js +2 -0
- package/dist/starbases/actions/index.js.map +1 -1
- package/dist/starbases/actions/upgrades.d.ts +116 -0
- package/dist/starbases/actions/upgrades.d.ts.map +1 -0
- package/dist/starbases/actions/upgrades.js +474 -0
- package/dist/starbases/actions/upgrades.js.map +1 -0
- package/dist/starbases/governance.d.ts +75 -0
- package/dist/starbases/governance.d.ts.map +1 -1
- package/dist/starbases/governance.js +82 -1
- package/dist/starbases/governance.js.map +1 -1
- package/dist/starbases/index.d.ts +1 -1
- package/dist/starbases/index.d.ts.map +1 -1
- package/dist/starbases/index.js +1 -1
- package/dist/starbases/index.js.map +1 -1
- package/package.json +2 -2
|
@@ -0,0 +1,1030 @@
|
|
|
1
|
+
import { isAddress } from '@solana/kit';
|
|
2
|
+
import { InvalidActionInputError, SageSdkError } from '../../client/errors.js';
|
|
3
|
+
import { readContextRawAccounts } from '../../client/reconciliation.js';
|
|
4
|
+
import { characterAccountDefinition, findCharacterPda, } from '../../internal/c4/character.js';
|
|
5
|
+
import { craftingHabAccountDefinition } from '../../internal/c4/crafting.js';
|
|
6
|
+
import { habPlotAvailable } from '../../internal/c4/crafting-hab-storage.js';
|
|
7
|
+
import { deriveCurrencyCache, validateCurrencyCache, } from '../../internal/c4/currency-cache.js';
|
|
8
|
+
import { factionValue } from '../../internal/c4/faction.js';
|
|
9
|
+
import { deriveRegionBuildingPolicyAddress } from '../../internal/c4/pda.js';
|
|
10
|
+
import { deriveProfileFactionAddress, profileFactionIdentityAccountDefinition, } from '../../internal/c4/profile-faction.js';
|
|
11
|
+
import { readRegionBuildingPolicy } from '../../internal/c4/region-building-policy.js';
|
|
12
|
+
import { starbasePlayerAccountDefinition } from '../../internal/c4/starbase.js';
|
|
13
|
+
import { starSystemAccountDefinition, } from '../../internal/c4/world.js';
|
|
14
|
+
import { requireGameAddress } from '../../internal/game-context.js';
|
|
15
|
+
import { accountAbsentPrecondition, accountStatePrecondition, } from '../../internal/planning/account-state.js';
|
|
16
|
+
import { adaptCancelCraftingHabBuildingChangesInstruction, adaptCraftingHabBuildingChangesInstruction, adaptCraftingHabRentTopUpInstruction, adaptDeconstructCraftingHabInstruction, adaptFinalizeCraftingHabBuildingChangesInstruction, adaptPlaceCraftingHabInstruction, adaptSettleCraftingHabBuildingXpInstruction, readPlanAuthorization, } from '../../internal/planning/authorization-adapter.js';
|
|
17
|
+
import { readCraftingHabStorageAction } from '../../internal/planning/crafting-hab-storage-actions.js';
|
|
18
|
+
import { structurePlacementFee } from '../../internal/planning/placement-fee.js';
|
|
19
|
+
import { RENT_ADDRESS } from '../../internal/planning/rent.js';
|
|
20
|
+
import { CLOCK_ADDRESS, readClock, } from '../../internal/planning/faction-enrollment.js';
|
|
21
|
+
import { planStorageFunding } from '../../internal/planning/storage-actions.js';
|
|
22
|
+
import { resolveXpRuntime } from '../../internal/planning/xp-runtime.js';
|
|
23
|
+
import { describeBuildingXpSettlement } from '../../internal/planning/building-xp.js';
|
|
24
|
+
import { buildPlannerPlan } from '../../planning/plan-construction.js';
|
|
25
|
+
import { resolveCraftingHabDefinition } from '../../registry/index.js';
|
|
26
|
+
import { decodeRegistryGame, gameCraftingHabsRegistrySection, gameHabBuildingsRegistrySection, } from '../../internal/c4/game.js';
|
|
27
|
+
const U64_MAX = 18446744073709551615n;
|
|
28
|
+
const LEVELS = [
|
|
29
|
+
'level-0',
|
|
30
|
+
'level-1',
|
|
31
|
+
'level-2',
|
|
32
|
+
'level-3',
|
|
33
|
+
'level-4',
|
|
34
|
+
'level-5',
|
|
35
|
+
'css',
|
|
36
|
+
];
|
|
37
|
+
const TIERS = ['one', 'two', 'three', 'four', 'five'];
|
|
38
|
+
function fail(message, address) {
|
|
39
|
+
throw new SageSdkError('ACTION_PRECONDITION_FAILED', message, { address });
|
|
40
|
+
}
|
|
41
|
+
function invalid(field, value, accepted, remedy) {
|
|
42
|
+
throw new InvalidActionInputError(field, value, accepted, remedy);
|
|
43
|
+
}
|
|
44
|
+
function readOptions(value, required, optional = []) {
|
|
45
|
+
if (value === null || typeof value !== 'object' || Array.isArray(value))
|
|
46
|
+
return invalid('options', value, 'a plain object', 'Provide the documented options object before retrying.');
|
|
47
|
+
const options = value;
|
|
48
|
+
const missing = required.find((key) => !Object.hasOwn(options, key));
|
|
49
|
+
const unexpected = Object.keys(options).find((key) => !required.includes(key) && !optional.includes(key));
|
|
50
|
+
if (missing !== undefined || unexpected !== undefined)
|
|
51
|
+
invalid('options', missing === undefined ? unexpected : `missing ${missing}`, `the fields ${[...required, ...optional].join(', ')}`, 'Remove unknown fields and provide every required option before retrying.');
|
|
52
|
+
return options;
|
|
53
|
+
}
|
|
54
|
+
function present(raw, address, message) {
|
|
55
|
+
if (raw === undefined || raw.executable)
|
|
56
|
+
return fail(message, address);
|
|
57
|
+
return raw;
|
|
58
|
+
}
|
|
59
|
+
function atlasAmount(value, field, positive) {
|
|
60
|
+
if (typeof value !== 'bigint' ||
|
|
61
|
+
value < (positive ? 1n : 0n) ||
|
|
62
|
+
value > U64_MAX)
|
|
63
|
+
return invalid(field, value, positive ? 'a positive u64 bigint' : 'a u64 bigint', 'Provide the raw ATLAS amount (8 decimals) as a bigint, then retry.');
|
|
64
|
+
return value;
|
|
65
|
+
}
|
|
66
|
+
function levelIndex(starbase) {
|
|
67
|
+
return LEVELS.indexOf(starbase.level);
|
|
68
|
+
}
|
|
69
|
+
/** What placing a Crafting Hab of a definition costs at a Starbase: the
|
|
70
|
+
* placement fee, the rent rate, and the plots for its tier. Pure; the game
|
|
71
|
+
* also requires a free plot, the Hab item and the Character's research.
|
|
72
|
+
* @example
|
|
73
|
+
* ```ts
|
|
74
|
+
* declare const starbase: WorldStarbaseData;
|
|
75
|
+
* declare const definition: { tier: 'one'; placementFeeMultiplier: { raw: bigint }; rentMultiplier: { value: number } };
|
|
76
|
+
* const quote = deriveCraftingHabPlacementQuote(starbase, definition);
|
|
77
|
+
* ```
|
|
78
|
+
*/
|
|
79
|
+
export function deriveCraftingHabPlacementQuote(starbase, definition) {
|
|
80
|
+
const level = LEVELS.indexOf(starbase.level);
|
|
81
|
+
const tier = TIERS.indexOf(definition.tier);
|
|
82
|
+
const fee = starbase.baseCraftingHabPlacementFee[level][tier];
|
|
83
|
+
const rent = starbase.baseCraftingHabRentRate[level][tier];
|
|
84
|
+
return {
|
|
85
|
+
placementFeeAtlasRaw: structurePlacementFee(fee.raw, definition.placementFeeMultiplier.raw),
|
|
86
|
+
rentAtlasRawPerSecond: rent.value * definition.rentMultiplier.value,
|
|
87
|
+
plots: starbase.craftingHabPlots
|
|
88
|
+
.slice(0, level + 1)
|
|
89
|
+
.reduce((sum, row) => sum + row[tier], 0),
|
|
90
|
+
};
|
|
91
|
+
}
|
|
92
|
+
/** The authorization Profile's current Starbase Player and its Star System,
|
|
93
|
+
* read fresh. */
|
|
94
|
+
async function readPlayer(ctx, snapshot, authorization, game) {
|
|
95
|
+
if (snapshot.playerProfile.address !== authorization.profile)
|
|
96
|
+
fail(`Starbase Player ${snapshot.address} belongs to Profile ${snapshot.playerProfile.address}, not authorization Profile ${authorization.profile}. Use the authorization Profile's Starbase Player and retry.`, snapshot.address);
|
|
97
|
+
const character = await findCharacterPda(ctx.programs, authorization.profile, game);
|
|
98
|
+
const system = snapshot.system.address;
|
|
99
|
+
const [playerCandidate, systemCandidate, characterCandidate] = await readContextRawAccounts(ctx, [snapshot.address, system, character]);
|
|
100
|
+
const playerRaw = present(playerCandidate, snapshot.address, `Starbase Player ${snapshot.address} is missing. Register at the Starbase and retry.`);
|
|
101
|
+
const player = starbasePlayerAccountDefinition.decode(playerRaw, ctx.programs);
|
|
102
|
+
const systemRaw = present(systemCandidate, system, `Star System ${system} is missing. Refresh it and retry.`);
|
|
103
|
+
const systemData = starSystemAccountDefinition.decode(systemRaw, ctx.programs);
|
|
104
|
+
const characterRaw = present(characterCandidate, character, `Profile ${authorization.profile} has no Character ${character}. Register the Character and retry.`);
|
|
105
|
+
const characterData = characterAccountDefinition.decode(characterRaw, ctx.programs);
|
|
106
|
+
if (player.playerProfile !== authorization.profile ||
|
|
107
|
+
player.system !== system ||
|
|
108
|
+
player.character !== character ||
|
|
109
|
+
systemData.game !== game)
|
|
110
|
+
fail(`Starbase Player ${snapshot.address} no longer matches Profile ${authorization.profile}, its Character or Star System ${system} in the configured Game. Refresh it and retry.`, snapshot.address);
|
|
111
|
+
const starbase = systemData.starbase;
|
|
112
|
+
if (starbase === undefined)
|
|
113
|
+
return fail(`Star System ${system} has no Starbase. Choose a System with a Starbase and retry.`, system);
|
|
114
|
+
if (player.sequenceId !== systemData.sequenceId)
|
|
115
|
+
fail(`Starbase Player ${snapshot.address} belongs to an earlier Starbase at Star System ${system}. Register at the current Starbase and retry.`, snapshot.address);
|
|
116
|
+
return {
|
|
117
|
+
character,
|
|
118
|
+
characterRaw,
|
|
119
|
+
characterData,
|
|
120
|
+
playerRaw,
|
|
121
|
+
player,
|
|
122
|
+
system,
|
|
123
|
+
systemRaw,
|
|
124
|
+
systemData,
|
|
125
|
+
starbase,
|
|
126
|
+
};
|
|
127
|
+
}
|
|
128
|
+
/** Plans placing a Crafting Hab on a Starbase plot.
|
|
129
|
+
*
|
|
130
|
+
* The game takes one Hab item of the definition from the Starbase Player,
|
|
131
|
+
* charges the placement fee and any initial rent from the Character's ATLAS,
|
|
132
|
+
* and puts the Hab on the first level, up to the Starbase's own, with a free
|
|
133
|
+
* plot for its tier. The Starbase must belong to the Profile's faction, and
|
|
134
|
+
* the Character's research caps how many Habs it holds and their tier. The
|
|
135
|
+
* Profile funds the new Hab's storage and the Star System's growth.
|
|
136
|
+
*
|
|
137
|
+
* The caller generates the Hab's keypair; its address signs the placement
|
|
138
|
+
* with the authorization key. Placement races and research beyond the
|
|
139
|
+
* Character's cap remain chain-authoritative.
|
|
140
|
+
*
|
|
141
|
+
* @example
|
|
142
|
+
* ```ts
|
|
143
|
+
* declare const ctx: SageContext;
|
|
144
|
+
* declare const starbasePlayer: CraftingHabStarbasePlayer;
|
|
145
|
+
* declare const authorization: PlanAuthorization;
|
|
146
|
+
* declare const craftingHab: Address;
|
|
147
|
+
* const plan = await planPlaceCraftingHab(ctx, starbasePlayer, {
|
|
148
|
+
* authorization,
|
|
149
|
+
* craftingHab,
|
|
150
|
+
* definitionId: 0,
|
|
151
|
+
* });
|
|
152
|
+
* ```
|
|
153
|
+
*/
|
|
154
|
+
export async function planPlaceCraftingHab(ctx, starbasePlayer, input) {
|
|
155
|
+
const options = readOptions(input, ['authorization', 'craftingHab', 'definitionId'], ['initialRentAtlasRaw']);
|
|
156
|
+
const authorization = readPlanAuthorization(options.authorization);
|
|
157
|
+
if (typeof options.craftingHab !== 'string' ||
|
|
158
|
+
!isAddress(options.craftingHab))
|
|
159
|
+
invalid('craftingHab', options.craftingHab, "a new keypair's address", 'Generate a keypair for the Hab, pass its address and sign with it, then retry.');
|
|
160
|
+
const craftingHab = options.craftingHab;
|
|
161
|
+
if (!Number.isSafeInteger(options.definitionId))
|
|
162
|
+
invalid('definitionId', options.definitionId, 'a Crafting Hab definition id', 'Choose a definition from listCraftingHabDefinitions, then retry.');
|
|
163
|
+
const initialRent = options.initialRentAtlasRaw === undefined
|
|
164
|
+
? 0n
|
|
165
|
+
: atlasAmount(options.initialRentAtlasRaw, 'initialRentAtlasRaw', false);
|
|
166
|
+
const game = requireGameAddress(ctx);
|
|
167
|
+
// The fee and the Hab item come from the Game's definition; read it fresh so
|
|
168
|
+
// the Plan can guard the exact Game it priced against.
|
|
169
|
+
const [gameCandidate] = await readContextRawAccounts(ctx, [game]);
|
|
170
|
+
const gameRaw = present(gameCandidate, game, `The configured Game ${game} is missing. Verify the cluster and Game, then retry.`);
|
|
171
|
+
const definition = gameCraftingHabsRegistrySection(gameRaw, decodeRegistryGame(ctx.programs, gameRaw)).definitions.find(({ id }) => id === options.definitionId);
|
|
172
|
+
if (definition === undefined)
|
|
173
|
+
return invalid('definitionId', options.definitionId, 'a Crafting Hab definition id of the configured Game', 'Choose a definition from listCraftingHabDefinitions, then retry.');
|
|
174
|
+
const fresh = await readPlayer(ctx, starbasePlayer, authorization, game);
|
|
175
|
+
const { starbase, system, characterData } = fresh;
|
|
176
|
+
const [profileFaction, [currencyCache], regionId] = await Promise.all([
|
|
177
|
+
deriveProfileFactionAddress(ctx.programs, authorization.profile),
|
|
178
|
+
deriveCurrencyCache(ctx.programs, game),
|
|
179
|
+
Promise.resolve(fresh.systemData.regionId),
|
|
180
|
+
]);
|
|
181
|
+
const policyAddress = regionId === undefined
|
|
182
|
+
? ctx.programs.sage
|
|
183
|
+
: (await deriveRegionBuildingPolicyAddress(ctx.programs, game, regionId))[0];
|
|
184
|
+
const [factionCandidate, existing, profileCandidate, rent, ...policyCandidate] = await readContextRawAccounts(ctx, [
|
|
185
|
+
profileFaction,
|
|
186
|
+
craftingHab,
|
|
187
|
+
authorization.profile,
|
|
188
|
+
RENT_ADDRESS,
|
|
189
|
+
...(regionId === undefined ? [] : [policyAddress]),
|
|
190
|
+
]);
|
|
191
|
+
if (existing !== undefined)
|
|
192
|
+
fail(`Account ${craftingHab} already exists, so it cannot become a new Crafting Hab. Generate a fresh keypair and retry.`, craftingHab);
|
|
193
|
+
const faction = profileFactionIdentityAccountDefinition.decode(present(factionCandidate, profileFaction, `Profile ${authorization.profile} has no faction. Join a faction and retry.`), ctx.programs);
|
|
194
|
+
if (faction.profile !== authorization.profile ||
|
|
195
|
+
faction.factionId !== factionValue(starbase.owner))
|
|
196
|
+
fail(`The Starbase at Star System ${system} belongs to ${starbase.owner}, not the Profile's faction; Crafting Habs can only be placed at the Profile's own faction's Starbases.`, system);
|
|
197
|
+
const level = levelIndex(starbase);
|
|
198
|
+
const tier = TIERS.indexOf(definition.tier);
|
|
199
|
+
const provided = new Set([
|
|
200
|
+
...starbase.locationTags,
|
|
201
|
+
...starbase.buildingTags,
|
|
202
|
+
]);
|
|
203
|
+
const missingTags = definition.requiredTagIds.filter((tag) => !provided.has(tag));
|
|
204
|
+
if (missingTags.length > 0)
|
|
205
|
+
fail(`${definition.name} requires Starbase tags ${missingTags.join(', ')}, which the Starbase at Star System ${system} does not provide. Choose another Starbase or Hab, then retry.`, system);
|
|
206
|
+
const modifiers = characterData.modifiers.values;
|
|
207
|
+
if (regionId !== undefined) {
|
|
208
|
+
const policy = readRegionBuildingPolicy(ctx.programs, policyCandidate[0], policyAddress, { game, regionId });
|
|
209
|
+
const missing = policy.requiredResearchTags.filter((tag) => !modifiers.researchTags.includes(tag));
|
|
210
|
+
if (missing.length > 0)
|
|
211
|
+
fail(`Character ${fresh.character} lacks research tags ${missing.join(', ')}, which Region ${String(regionId)} requires to build in Star System ${system}. Unlock that research, or choose a System in another Region.`, policyAddress);
|
|
212
|
+
}
|
|
213
|
+
if (modifiers.maxResidentialPlotTier !== undefined &&
|
|
214
|
+
tier + 1 > modifiers.maxResidentialPlotTier)
|
|
215
|
+
fail(`Character ${fresh.character} may place Crafting Habs up to tier ${String(modifiers.maxResidentialPlotTier)}, below ${definition.name}'s tier ${definition.tier}. Research a higher residential tier or pick a lower tier.`, fresh.character);
|
|
216
|
+
if (characterData.counts.craftingHabs >= modifiers.maxResidentialPlots)
|
|
217
|
+
fail(`Character ${fresh.character} already holds ${String(characterData.counts.craftingHabs)} of its ${String(modifiers.maxResidentialPlots)} residential plots. Research more plots or deconstruct a Crafting Hab.`, fresh.character);
|
|
218
|
+
if (!habPlotAvailable(fresh.systemRaw, level, tier))
|
|
219
|
+
fail(`The Starbase at Star System ${system} has no free tier-${definition.tier} Crafting Hab plot on any level up to its own. Wait for a plot to free up or choose another Starbase.`, system);
|
|
220
|
+
const held = fresh.player.cargo.amounts.find((a) => a.cargoId === definition.cargoId)
|
|
221
|
+
?.amount ?? 0n;
|
|
222
|
+
if (held < 1n)
|
|
223
|
+
fail(`Starbase Player ${starbasePlayer.address} holds no ${definition.name} item (cargo ${String(definition.cargoId)}), which placement consumes. Bring one to the Starbase and retry.`, starbasePlayer.address);
|
|
224
|
+
const quote = deriveCraftingHabPlacementQuote(starbase, definition);
|
|
225
|
+
if (characterData.atlasRaw < quote.placementFeeAtlasRaw + initialRent)
|
|
226
|
+
fail(`Character ${fresh.character} has ${String(characterData.atlasRaw)} raw ATLAS, below the ${String(quote.placementFeeAtlasRaw)} raw placement fee plus ${String(initialRent)} raw initial rent. Add ATLAS or lower the rent, then retry.`, fresh.character);
|
|
227
|
+
const instruction = adaptPlaceCraftingHabInstruction(ctx.programs, {
|
|
228
|
+
authorization,
|
|
229
|
+
game,
|
|
230
|
+
profileFaction,
|
|
231
|
+
craftingHab,
|
|
232
|
+
system,
|
|
233
|
+
starbasePlayer: starbasePlayer.address,
|
|
234
|
+
character: fresh.character,
|
|
235
|
+
currencyCache,
|
|
236
|
+
regionBuildingPolicy: policyAddress,
|
|
237
|
+
definitionId: definition.id,
|
|
238
|
+
initialRentRaw: options.initialRentAtlasRaw === undefined ? undefined : initialRent,
|
|
239
|
+
});
|
|
240
|
+
const accounts = new Map([
|
|
241
|
+
[system, fresh.systemRaw],
|
|
242
|
+
[authorization.profile, profileCandidate],
|
|
243
|
+
[RENT_ADDRESS, rent],
|
|
244
|
+
]);
|
|
245
|
+
const storage = planStorageFunding(ctx.programs, {
|
|
246
|
+
action: readCraftingHabStorageAction(ctx.programs, instruction, accounts),
|
|
247
|
+
accounts,
|
|
248
|
+
label: 'Crafting Hab placement',
|
|
249
|
+
});
|
|
250
|
+
const description = `Place ${definition.name} ${craftingHab} at the Starbase of Star System ${system}: the game takes one Hab item from Starbase Player ${starbasePlayer.address}, a ${String(quote.placementFeeAtlasRaw)} raw ATLAS placement fee and ${String(initialRent)} raw initial rent from Character ${fresh.character}. Rent accrues at ${String(quote.rentAtlasRawPerSecond)} raw ATLAS per second. ${storage.disclosure} Placement races remain chain-authoritative.`;
|
|
251
|
+
return buildPlannerPlan(ctx.programs, {
|
|
252
|
+
kind: 'crafting.place-crafting-hab',
|
|
253
|
+
preconditions: [
|
|
254
|
+
accountAbsentPrecondition(craftingHab, `Account ${craftingHab} still does not exist.`),
|
|
255
|
+
...(await Promise.all([
|
|
256
|
+
accountStatePrecondition(gameRaw, `The configured Game keeps ${definition.name}'s item, tier and fee multiplier.`),
|
|
257
|
+
accountStatePrecondition(fresh.systemRaw, `Star System ${system} keeps its Starbase level, placement fee, faction and Hab plots.`),
|
|
258
|
+
accountStatePrecondition(fresh.playerRaw, `Starbase Player ${starbasePlayer.address} keeps its Hab item.`),
|
|
259
|
+
accountStatePrecondition(fresh.characterRaw, `Character ${fresh.character} keeps its ATLAS, research and Hab count.`),
|
|
260
|
+
...(regionId === undefined
|
|
261
|
+
? []
|
|
262
|
+
: [
|
|
263
|
+
policyCandidate[0] === undefined
|
|
264
|
+
? Promise.resolve(accountAbsentPrecondition(policyAddress, `Region ${String(regionId)} still has no building policy.`))
|
|
265
|
+
: accountStatePrecondition(policyCandidate[0], `Region ${String(regionId)} keeps its building policy.`),
|
|
266
|
+
]),
|
|
267
|
+
])),
|
|
268
|
+
storage.guard,
|
|
269
|
+
],
|
|
270
|
+
steps: [
|
|
271
|
+
{
|
|
272
|
+
describes: description,
|
|
273
|
+
instruction,
|
|
274
|
+
signers: [authorization.authority, craftingHab],
|
|
275
|
+
},
|
|
276
|
+
],
|
|
277
|
+
summary: description,
|
|
278
|
+
});
|
|
279
|
+
}
|
|
280
|
+
/** The rent a Hab owes since its rent clock. The game settles rent first and
|
|
281
|
+
* refuses a Hab whose balance cannot cover it. Rent accrues fractionally, so
|
|
282
|
+
* callers refuse only a clear shortfall. */
|
|
283
|
+
async function rentDueRaw(ctx, data, starbase, definition) {
|
|
284
|
+
const [clock] = await readContextRawAccounts(ctx, [CLOCK_ADDRESS]);
|
|
285
|
+
const now = readClock(present(clock, CLOCK_ADDRESS, 'The Clock sysvar is missing. Refresh chain time and retry.'));
|
|
286
|
+
const rate = deriveCraftingHabPlacementQuote(starbase, definition).rentAtlasRawPerSecond;
|
|
287
|
+
const elapsed = now > data.lastRentAtUnixSeconds ? now - data.lastRentAtUnixSeconds : 0n;
|
|
288
|
+
return BigInt(Math.floor(Number(elapsed) * rate));
|
|
289
|
+
}
|
|
290
|
+
/** Reads a Crafting Hab of the authorization Profile, fresh. */
|
|
291
|
+
async function readHab(ctx, hab, authorization, game) {
|
|
292
|
+
if (hab.ownerProfile.address !== authorization.profile)
|
|
293
|
+
fail(`Crafting Hab ${hab.address} belongs to Profile ${hab.ownerProfile.address}; only that Profile can manage it.`, hab.address);
|
|
294
|
+
const [candidate] = await readContextRawAccounts(ctx, [hab.address]);
|
|
295
|
+
const raw = present(candidate, hab.address, `Crafting Hab ${hab.address} is missing; it may have been deconstructed. Refresh it and retry.`);
|
|
296
|
+
const data = craftingHabAccountDefinition.decode(raw, ctx.programs);
|
|
297
|
+
if (data.ownerProfile !== authorization.profile || data.game !== game)
|
|
298
|
+
fail(`Crafting Hab ${hab.address} no longer belongs to Profile ${authorization.profile} in the configured Game. Refresh it and retry.`, hab.address);
|
|
299
|
+
return { raw, data };
|
|
300
|
+
}
|
|
301
|
+
/** Plans moving raw ATLAS from the Character into a Crafting Hab's rent.
|
|
302
|
+
*
|
|
303
|
+
* The game refuses a top-up once the Hab has been delinquent past the Game's
|
|
304
|
+
* eviction grace period. Rent accrual remains chain-authoritative.
|
|
305
|
+
*
|
|
306
|
+
* @example
|
|
307
|
+
* ```ts
|
|
308
|
+
* declare const ctx: SageContext;
|
|
309
|
+
* declare const hab: CraftingHabSnapshot;
|
|
310
|
+
* declare const starbasePlayer: CraftingHabStarbasePlayer;
|
|
311
|
+
* declare const authorization: PlanAuthorization;
|
|
312
|
+
* const plan = await planTopUpCraftingHabRent(ctx, hab, {
|
|
313
|
+
* authorization,
|
|
314
|
+
* starbasePlayer,
|
|
315
|
+
* amountAtlasRaw: 100_000_000n,
|
|
316
|
+
* });
|
|
317
|
+
* ```
|
|
318
|
+
*/
|
|
319
|
+
export async function planTopUpCraftingHabRent(ctx, hab, input) {
|
|
320
|
+
const options = readOptions(input, [
|
|
321
|
+
'authorization',
|
|
322
|
+
'starbasePlayer',
|
|
323
|
+
'amountAtlasRaw',
|
|
324
|
+
]);
|
|
325
|
+
const authorization = readPlanAuthorization(options.authorization);
|
|
326
|
+
const amount = atlasAmount(options.amountAtlasRaw, 'amountAtlasRaw', true);
|
|
327
|
+
const game = requireGameAddress(ctx);
|
|
328
|
+
const current = await readHab(ctx, hab, authorization, game);
|
|
329
|
+
const fresh = await readPlayer(ctx, options.starbasePlayer, authorization, game);
|
|
330
|
+
if (current.data.system !== fresh.system)
|
|
331
|
+
fail(`Crafting Hab ${hab.address} is in Star System ${current.data.system}, not ${fresh.system}. Use the Starbase Player in the Hab's System and retry.`, hab.address);
|
|
332
|
+
if (fresh.characterData.atlasRaw < amount)
|
|
333
|
+
fail(`Character ${fresh.character} has ${String(fresh.characterData.atlasRaw)} raw ATLAS, less than the ${String(amount)} to top up. Add ATLAS or top up less, then retry.`, fresh.character);
|
|
334
|
+
const instruction = adaptCraftingHabRentTopUpInstruction(ctx.programs, {
|
|
335
|
+
authorization,
|
|
336
|
+
character: fresh.character,
|
|
337
|
+
craftingHab: hab.address,
|
|
338
|
+
system: fresh.system,
|
|
339
|
+
starbasePlayer: fresh.playerRaw.address,
|
|
340
|
+
game,
|
|
341
|
+
amountRaw: amount,
|
|
342
|
+
});
|
|
343
|
+
const description = `Top up Crafting Hab ${hab.address}'s rent with ${String(amount)} raw ATLAS from Character ${fresh.character} (rent balance now ${String(current.data.rentBalanceRaw)} raw). The game refuses a top-up once the Hab has been delinquent past the eviction grace period; rent accrual remains chain-authoritative.`;
|
|
344
|
+
return buildPlannerPlan(ctx.programs, {
|
|
345
|
+
kind: 'crafting.top-up-crafting-hab-rent',
|
|
346
|
+
preconditions: [
|
|
347
|
+
await accountStatePrecondition(fresh.characterRaw, `Character ${fresh.character} keeps its ATLAS.`),
|
|
348
|
+
],
|
|
349
|
+
steps: [
|
|
350
|
+
{
|
|
351
|
+
describes: description,
|
|
352
|
+
instruction,
|
|
353
|
+
signers: [authorization.authority],
|
|
354
|
+
},
|
|
355
|
+
],
|
|
356
|
+
summary: description,
|
|
357
|
+
});
|
|
358
|
+
}
|
|
359
|
+
/** Plans settling the Building XP one owned Crafting Hab has earned.
|
|
360
|
+
*
|
|
361
|
+
* The game first brings the Hab's rent and production up to date, which
|
|
362
|
+
* records any further paid operational time, then pays its recorded
|
|
363
|
+
* operational work to the owner's Character as Building XP, up to the
|
|
364
|
+
* Character's remaining Building allowance. Work beyond the allowance is
|
|
365
|
+
* discarded rather than carried, so the planner refuses while the allowance is
|
|
366
|
+
* empty, and it refuses a Hab that has recorded no work and is not
|
|
367
|
+
* operational. The Plan states the recorded XP and the allowance; the exact
|
|
368
|
+
* grant remains chain-authoritative.
|
|
369
|
+
*
|
|
370
|
+
* Requires the Profile's Starbase Player in the Hab's System, from the
|
|
371
|
+
* current Starbase.
|
|
372
|
+
* Bringing a large structure up to date can take more than the default
|
|
373
|
+
* 200,000 compute units, so simulate first and raise `computeUnitLimit` when
|
|
374
|
+
* assembling if needed.
|
|
375
|
+
*
|
|
376
|
+
* @example
|
|
377
|
+
* ```ts
|
|
378
|
+
* declare const ctx: SageContext;
|
|
379
|
+
* declare const hab: CraftingHabSnapshot;
|
|
380
|
+
* declare const starbasePlayer: CraftingHabStarbasePlayer;
|
|
381
|
+
* declare const authorization: PlanAuthorization;
|
|
382
|
+
* const plan = await planSettleCraftingHabBuildingXp(ctx, hab, {
|
|
383
|
+
* authorization,
|
|
384
|
+
* starbasePlayer,
|
|
385
|
+
* });
|
|
386
|
+
* console.log(plan.describe());
|
|
387
|
+
* ```
|
|
388
|
+
*/
|
|
389
|
+
export async function planSettleCraftingHabBuildingXp(ctx, hab, input) {
|
|
390
|
+
const options = readOptions(input, ['authorization', 'starbasePlayer']);
|
|
391
|
+
const authorization = readPlanAuthorization(options.authorization);
|
|
392
|
+
const game = requireGameAddress(ctx);
|
|
393
|
+
const character = await findCharacterPda(ctx.programs, authorization.profile, game);
|
|
394
|
+
const [current, fresh, [currencyCache], xp] = await Promise.all([
|
|
395
|
+
readHab(ctx, hab, authorization, game),
|
|
396
|
+
readPlayer(ctx, options.starbasePlayer, authorization, game),
|
|
397
|
+
deriveCurrencyCache(ctx.programs, game),
|
|
398
|
+
habXpGuard(ctx, game, character),
|
|
399
|
+
]);
|
|
400
|
+
if (current.data.system !== fresh.system)
|
|
401
|
+
fail(`Crafting Hab ${hab.address} is in Star System ${current.data.system}, not ${fresh.system}. Use the Starbase Player in the Hab's System and retry.`, hab.address);
|
|
402
|
+
if (current.data.systemSequenceId !== fresh.systemData.sequenceId)
|
|
403
|
+
fail(`Crafting Hab ${hab.address} belongs to an earlier Starbase at Star System ${fresh.system}, which the game treats as destroyed. Respawn or deconstruct it instead.`, hab.address);
|
|
404
|
+
// A Character's XP runtime names both accounts or refuses the Character.
|
|
405
|
+
const xpBudget = xp.accounts.xpBudget;
|
|
406
|
+
const xpBudgetConfig = xp.accounts.xpBudgetConfig;
|
|
407
|
+
const [cacheCandidate] = await readContextRawAccounts(ctx, [currencyCache]);
|
|
408
|
+
await validateCurrencyCache(ctx.programs, present(cacheCandidate, currencyCache, `Canonical CurrencyCache ${currencyCache} is missing for the configured Game. Refresh the Game currency configuration and retry.`), game, 'planSettleCraftingHabBuildingXp');
|
|
409
|
+
const settlement = await describeBuildingXpSettlement(ctx, {
|
|
410
|
+
structure: `Crafting Hab ${hab.address}`,
|
|
411
|
+
game,
|
|
412
|
+
character: fresh.character,
|
|
413
|
+
xpBudget,
|
|
414
|
+
xpBudgetConfig,
|
|
415
|
+
recorded: current.data.operationalXp,
|
|
416
|
+
operational: current.data.state.kind === 'active' &&
|
|
417
|
+
current.data.constructionRemainingSeconds === 0n,
|
|
418
|
+
});
|
|
419
|
+
const description = `Settle Crafting Hab ${hab.address}'s Building XP after bringing its rent and production up to date. ${settlement.description}`;
|
|
420
|
+
return buildPlannerPlan(ctx.programs, {
|
|
421
|
+
kind: 'crafting.settle-crafting-hab-building-xp',
|
|
422
|
+
preconditions: [
|
|
423
|
+
...(await Promise.all([
|
|
424
|
+
accountStatePrecondition(current.raw, `Crafting Hab ${hab.address} keeps its ownership, buildings, rent, production, lifecycle state and recorded Building work.`),
|
|
425
|
+
accountStatePrecondition(fresh.systemRaw, `Star System ${fresh.system} keeps its current Starbase and the Hab on its plot.`),
|
|
426
|
+
accountStatePrecondition(fresh.playerRaw, `Starbase Player ${fresh.playerRaw.address} stays registered at the current Starbase.`),
|
|
427
|
+
])),
|
|
428
|
+
{
|
|
429
|
+
kind: 'currency-cache',
|
|
430
|
+
address: currencyCache,
|
|
431
|
+
game,
|
|
432
|
+
describes: 'The canonical CurrencyCache remains assigned to the configured Game; shared accounting may change.',
|
|
433
|
+
},
|
|
434
|
+
xp.guard,
|
|
435
|
+
settlement.guard,
|
|
436
|
+
],
|
|
437
|
+
steps: [
|
|
438
|
+
{
|
|
439
|
+
describes: description,
|
|
440
|
+
instruction: adaptSettleCraftingHabBuildingXpInstruction(ctx.programs, {
|
|
441
|
+
authorization,
|
|
442
|
+
character: fresh.character,
|
|
443
|
+
craftingHab: hab.address,
|
|
444
|
+
currencyCache,
|
|
445
|
+
game,
|
|
446
|
+
starbasePlayer: fresh.playerRaw.address,
|
|
447
|
+
system: fresh.system,
|
|
448
|
+
xpBudget,
|
|
449
|
+
xpBudgetConfig,
|
|
450
|
+
}),
|
|
451
|
+
signers: [authorization.authority],
|
|
452
|
+
},
|
|
453
|
+
],
|
|
454
|
+
summary: description,
|
|
455
|
+
});
|
|
456
|
+
}
|
|
457
|
+
/** Plans deconstructing a Crafting Hab: the game closes it, returns its Hab
|
|
458
|
+
* item to the Starbase Player and its remaining rent to the Character, and
|
|
459
|
+
* frees its plot, moving a newer Hab down into it when one is waiting.
|
|
460
|
+
*
|
|
461
|
+
* The Hab must be Active, with no buildings and no running Crafting
|
|
462
|
+
* Processes, and its rent current. The closed account's storage returns to
|
|
463
|
+
* the Profile. Rent settlement at deconstruction remains chain-authoritative.
|
|
464
|
+
*
|
|
465
|
+
* @example
|
|
466
|
+
* ```ts
|
|
467
|
+
* declare const ctx: SageContext;
|
|
468
|
+
* declare const hab: CraftingHabSnapshot;
|
|
469
|
+
* declare const starbasePlayer: CraftingHabStarbasePlayer;
|
|
470
|
+
* declare const authorization: PlanAuthorization;
|
|
471
|
+
* const plan = await planDeconstructCraftingHab(ctx, hab, {
|
|
472
|
+
* authorization,
|
|
473
|
+
* starbasePlayer,
|
|
474
|
+
* });
|
|
475
|
+
* ```
|
|
476
|
+
*/
|
|
477
|
+
export async function planDeconstructCraftingHab(ctx, hab, input) {
|
|
478
|
+
const options = readOptions(input, ['authorization', 'starbasePlayer'], ['topUpAtlasRaw']);
|
|
479
|
+
const authorization = readPlanAuthorization(options.authorization);
|
|
480
|
+
const topUp = options.topUpAtlasRaw === undefined
|
|
481
|
+
? 0n
|
|
482
|
+
: atlasAmount(options.topUpAtlasRaw, 'topUpAtlasRaw', true);
|
|
483
|
+
const game = requireGameAddress(ctx);
|
|
484
|
+
const current = await readHab(ctx, hab, authorization, game);
|
|
485
|
+
const fresh = await readPlayer(ctx, options.starbasePlayer, authorization, game);
|
|
486
|
+
const data = current.data;
|
|
487
|
+
if (data.system !== fresh.system)
|
|
488
|
+
fail(`Crafting Hab ${hab.address} is in Star System ${data.system}, not ${fresh.system}. Use the Starbase Player in the Hab's System and retry.`, hab.address);
|
|
489
|
+
if (data.state.kind !== 'active')
|
|
490
|
+
fail(`Crafting Hab ${hab.address} is ${data.state.kind}; the game deconstructs only an Active Hab. ${data.state.kind === 'design' ? 'Finalize or cancel its building changes first.' : 'Pay its rent to reactivate it first.'}`, hab.address);
|
|
491
|
+
if (data.buildings.length > 0)
|
|
492
|
+
fail(`Crafting Hab ${hab.address} still has ${String(data.buildings.length)} building types; remove every building before deconstructing.`, hab.address);
|
|
493
|
+
if (data.jobsRunning > 0)
|
|
494
|
+
fail(`Crafting Hab ${hab.address} runs ${String(data.jobsRunning)} Crafting Processes; complete or cancel them before deconstructing.`, hab.address);
|
|
495
|
+
const group = fresh.starbase.craftingHabs.find((entry) => entry.chronoAddresses.includes(hab.address));
|
|
496
|
+
if (group === undefined)
|
|
497
|
+
return fail(`Crafting Hab ${hab.address} is not on any plot of the Starbase at Star System ${fresh.system}. Refresh the System and retry.`, fresh.system);
|
|
498
|
+
const due = await rentDueRaw(ctx, data, fresh.starbase, await resolveCraftingHabDefinition(ctx, data.definitionId));
|
|
499
|
+
if (data.rentBalanceRaw + topUp < due)
|
|
500
|
+
fail(`Crafting Hab ${hab.address} owes about ${String(due)} raw ATLAS of rent but holds ${String(data.rentBalanceRaw + topUp)}${topUp > 0n ? ' with the top-up' : ''}, so the game refuses to deconstruct it. Pass topUpAtlasRaw of at least ${String(due - data.rentBalanceRaw)}, with some margin for rent accruing until the transaction lands, then retry.`, hab.address);
|
|
501
|
+
if (fresh.characterData.atlasRaw < topUp)
|
|
502
|
+
fail(`Character ${fresh.character} has ${String(fresh.characterData.atlasRaw)} raw ATLAS, less than the ${String(topUp)} to top up. Add ATLAS or top up less, then retry.`, fresh.character);
|
|
503
|
+
const [currencyCache] = await deriveCurrencyCache(ctx.programs, game);
|
|
504
|
+
const instruction = adaptDeconstructCraftingHabInstruction(ctx.programs, {
|
|
505
|
+
authorization,
|
|
506
|
+
character: fresh.character,
|
|
507
|
+
game,
|
|
508
|
+
craftingHab: hab.address,
|
|
509
|
+
system: fresh.system,
|
|
510
|
+
starbasePlayer: fresh.playerRaw.address,
|
|
511
|
+
currencyCache,
|
|
512
|
+
plotLevel: group.key,
|
|
513
|
+
plotIndex: group.chronoAddresses.indexOf(hab.address),
|
|
514
|
+
});
|
|
515
|
+
const description = `Deconstruct Crafting Hab ${hab.address}: return its Hab item to Starbase Player ${fresh.playerRaw.address} and its ${String(data.rentBalanceRaw + topUp)} raw rent balance, less rent due, to Character ${fresh.character}, and free its plot at level ${String(group.key)}. The closed account's storage returns to the Profile. The game refuses while rent is delinquent; rent settlement remains chain-authoritative.`;
|
|
516
|
+
const steps = [
|
|
517
|
+
...(topUp === 0n
|
|
518
|
+
? []
|
|
519
|
+
: [
|
|
520
|
+
{
|
|
521
|
+
describes: `Top up Crafting Hab ${hab.address}'s rent with ${String(topUp)} raw ATLAS from Character ${fresh.character}, so its rent is current when it is deconstructed.`,
|
|
522
|
+
instruction: adaptCraftingHabRentTopUpInstruction(ctx.programs, {
|
|
523
|
+
authorization,
|
|
524
|
+
character: fresh.character,
|
|
525
|
+
craftingHab: hab.address,
|
|
526
|
+
system: fresh.system,
|
|
527
|
+
starbasePlayer: fresh.playerRaw.address,
|
|
528
|
+
game,
|
|
529
|
+
amountRaw: topUp,
|
|
530
|
+
}),
|
|
531
|
+
signers: [authorization.authority],
|
|
532
|
+
},
|
|
533
|
+
]),
|
|
534
|
+
{ describes: description, instruction, signers: [authorization.authority] },
|
|
535
|
+
];
|
|
536
|
+
return buildPlannerPlan(ctx.programs, {
|
|
537
|
+
kind: 'crafting.deconstruct-crafting-hab',
|
|
538
|
+
preconditions: await Promise.all([
|
|
539
|
+
accountStatePrecondition(current.raw, `Crafting Hab ${hab.address} stays Active and empty.`),
|
|
540
|
+
accountStatePrecondition(fresh.systemRaw, `Star System ${fresh.system} keeps the Hab at plot level ${String(group.key)}, index ${String(group.chronoAddresses.indexOf(hab.address))}.`),
|
|
541
|
+
]),
|
|
542
|
+
steps,
|
|
543
|
+
summary: description,
|
|
544
|
+
});
|
|
545
|
+
}
|
|
546
|
+
const U16_MAX = 65_535;
|
|
547
|
+
const U32_MAX = 4_294_967_295;
|
|
548
|
+
const I32_MAX = 2_147_483_647;
|
|
549
|
+
function readBuildingChanges(value) {
|
|
550
|
+
if (!Array.isArray(value) || value.length === 0)
|
|
551
|
+
invalid('buildingChanges', value, 'a non-empty array of add/remove building changes', 'Provide at least one building change, then retry.');
|
|
552
|
+
const seen = new Set();
|
|
553
|
+
return Array.from(value, (entry, index) => {
|
|
554
|
+
const field = `buildingChanges[${String(index)}]`;
|
|
555
|
+
const change = readOptions(entry, ['buildingId', 'kind', 'quantity']);
|
|
556
|
+
const { buildingId, kind, quantity } = change;
|
|
557
|
+
if (!Number.isInteger(buildingId) ||
|
|
558
|
+
buildingId < 0 ||
|
|
559
|
+
buildingId > U16_MAX)
|
|
560
|
+
invalid(`${field}.buildingId`, buildingId, 'a Hab building id', 'Choose a building from listHabBuildingDefinitions, then retry.');
|
|
561
|
+
if (kind !== 'add' && kind !== 'remove')
|
|
562
|
+
invalid(`${field}.kind`, kind, 'add or remove', `Set ${field}.kind to the intended building operation, then retry.`);
|
|
563
|
+
if (!Number.isInteger(quantity) ||
|
|
564
|
+
quantity < 1 ||
|
|
565
|
+
quantity > I32_MAX)
|
|
566
|
+
invalid(`${field}.quantity`, quantity, `a positive integer up to ${String(I32_MAX)}`, `Set ${field}.quantity to the number of buildings, then retry.`);
|
|
567
|
+
if (seen.has(buildingId))
|
|
568
|
+
invalid(`${field}.buildingId`, buildingId, 'one change per building id', 'Combine duplicate changes into one entry, then retry.');
|
|
569
|
+
seen.add(buildingId);
|
|
570
|
+
return change;
|
|
571
|
+
});
|
|
572
|
+
}
|
|
573
|
+
/** A Hab of the authorization Profile, its Starbase Player and the Game's Hab
|
|
574
|
+
* catalog, read fresh and checked the way every design step checks them: the
|
|
575
|
+
* Hab is in the Player's System, on a plot of the current Starbase and not
|
|
576
|
+
* evicted, and its rent is current. */
|
|
577
|
+
async function readDesignHab(ctx, hab, options, action) {
|
|
578
|
+
const authorization = readPlanAuthorization(options.authorization);
|
|
579
|
+
const game = requireGameAddress(ctx);
|
|
580
|
+
const current = await readHab(ctx, hab, authorization, game);
|
|
581
|
+
const fresh = await readPlayer(ctx, options.starbasePlayer, authorization, game);
|
|
582
|
+
const { data } = current;
|
|
583
|
+
if (data.system !== fresh.system)
|
|
584
|
+
fail(`Crafting Hab ${hab.address} is in Star System ${data.system}, not ${fresh.system}. Use the Starbase Player in the Hab's System and retry.`, hab.address);
|
|
585
|
+
if (data.systemSequenceId !== fresh.systemData.sequenceId)
|
|
586
|
+
fail(`Crafting Hab ${hab.address} belongs to an earlier Starbase at Star System ${fresh.system}, so the game refuses ${action}. Respawn or replace the Hab.`, hab.address);
|
|
587
|
+
if (data.state.kind === 'deactivated')
|
|
588
|
+
fail(`Crafting Hab ${hab.address} is deactivated for unpaid rent, so the game refuses ${action}. Top up its rent to reactivate it, refresh it and retry.`, hab.address);
|
|
589
|
+
const level = levelIndex(fresh.starbase);
|
|
590
|
+
const plot = fresh.starbase.craftingHabs
|
|
591
|
+
.filter((group) => group.key <= level)
|
|
592
|
+
.flatMap((group) => group.structures)
|
|
593
|
+
.find((entry) => entry.address === hab.address);
|
|
594
|
+
if (plot === undefined || plot.deactivationTimeUnixSeconds !== 0n)
|
|
595
|
+
fail(`Crafting Hab ${hab.address} ${plot === undefined ? 'holds no plot the Starbase at Star System ' + fresh.system + ' can reach' : 'was evicted from its plot'}, so the game refuses ${action}. Refresh the System and Hab; only respawning reclaims an evicted Hab.`, hab.address);
|
|
596
|
+
const [gameCandidate] = await readContextRawAccounts(ctx, [game]);
|
|
597
|
+
const gameRaw = present(gameCandidate, game, `The configured Game ${game} is missing. Verify the cluster and Game, then retry.`);
|
|
598
|
+
const decodedGame = decodeRegistryGame(ctx.programs, gameRaw);
|
|
599
|
+
const definition = gameCraftingHabsRegistrySection(gameRaw, decodedGame).definitions.find(({ id }) => id === data.definitionId);
|
|
600
|
+
if (definition === undefined)
|
|
601
|
+
return fail(`Crafting Hab ${hab.address}'s definition ${String(data.definitionId)} is absent from the configured Game. Refresh the definitions and Hab, then retry.`, game);
|
|
602
|
+
const buildings = new Map(gameHabBuildingsRegistrySection(gameRaw, decodedGame).definitions.map((building) => [building.id, building]));
|
|
603
|
+
const due = await rentDueRaw(ctx, data, fresh.starbase, definition);
|
|
604
|
+
if (data.rentBalanceRaw < due)
|
|
605
|
+
fail(`Crafting Hab ${hab.address} owes about ${String(due)} raw ATLAS of rent but holds ${String(data.rentBalanceRaw)}, so the game would deactivate it and refuse ${action}. Top up its rent with planTopUpCraftingHabRent, with some margin for rent accruing until the transaction lands, then retry.`, hab.address);
|
|
606
|
+
return {
|
|
607
|
+
authorization,
|
|
608
|
+
game,
|
|
609
|
+
current,
|
|
610
|
+
fresh,
|
|
611
|
+
gameRaw,
|
|
612
|
+
definition,
|
|
613
|
+
buildings,
|
|
614
|
+
};
|
|
615
|
+
}
|
|
616
|
+
/** The building layout a Hab reaches when its design finalizes: its buildings
|
|
617
|
+
* plus each signed change. The game refuses to finalize a removal beyond the
|
|
618
|
+
* buildings placed. */
|
|
619
|
+
function habFinalLayout(hab, placed, changes, buildings) {
|
|
620
|
+
const layout = new Map(placed.map(({ buildingId, quantity }) => [buildingId, quantity]));
|
|
621
|
+
for (const [buildingId, delta] of changes) {
|
|
622
|
+
const next = (layout.get(buildingId) ?? 0) + delta;
|
|
623
|
+
if (next < 0)
|
|
624
|
+
fail(`The design removes ${String(-delta)} ${buildings.get(buildingId).name} from Crafting Hab ${hab}, which holds only ${String(layout.get(buildingId) ?? 0)}; the game refuses to finalize it. Remove fewer and retry.`, hab);
|
|
625
|
+
if (next > U32_MAX)
|
|
626
|
+
fail(`The design would hold more than ${String(U32_MAX)} of building ${String(buildingId)} on Crafting Hab ${hab}. Add fewer and retry.`, hab);
|
|
627
|
+
if (next === 0)
|
|
628
|
+
layout.delete(buildingId);
|
|
629
|
+
else
|
|
630
|
+
layout.set(buildingId, next);
|
|
631
|
+
}
|
|
632
|
+
return layout;
|
|
633
|
+
}
|
|
634
|
+
/** A warning when a design adds a copy of a hub the final layout already
|
|
635
|
+
* holds more than once of; the game accepts it, but each copy costs its
|
|
636
|
+
* construction cargo. */
|
|
637
|
+
function hubCopyWarning(hub, design) {
|
|
638
|
+
return hub !== undefined && hub.quantity > 1 && (design.get(hub.id) ?? 0) > 0
|
|
639
|
+
? `The final layout holds ${String(hub.quantity)} copies of hub ${hub.name}. The game refuses a second hub type but accepts more copies of the matching hub; each copy takes its construction cargo and adds its effects.`
|
|
640
|
+
: '';
|
|
641
|
+
}
|
|
642
|
+
/** Checks a final Hab layout against the rules the game applies at
|
|
643
|
+
* finalization: every building known, at or below the Hab's tier, hub
|
|
644
|
+
* buildings of a single type matching the Hab's hub, every required building
|
|
645
|
+
* tag provided by the Hab, the Starbase or a building, and the slots within
|
|
646
|
+
* the Hab's. The game refuses a second hub type but accepts several copies of
|
|
647
|
+
* the matching hub, so the hub's count is returned rather than checked.
|
|
648
|
+
* Returns the crew the layout needs, the hub and each building whose research
|
|
649
|
+
* the Character lacks. */
|
|
650
|
+
function checkHabLayout(hab, layout, buildings, definition, starbase, researchTags, game) {
|
|
651
|
+
const provided = new Set([
|
|
652
|
+
...definition.providedTagIds,
|
|
653
|
+
...starbase.buildingTags,
|
|
654
|
+
]);
|
|
655
|
+
const required = new Set();
|
|
656
|
+
const unresearched = [];
|
|
657
|
+
let hub;
|
|
658
|
+
let slots = 0;
|
|
659
|
+
let neededCrew = 0;
|
|
660
|
+
let jobs = 0;
|
|
661
|
+
for (const [buildingId, quantity] of layout) {
|
|
662
|
+
const building = buildings.get(buildingId);
|
|
663
|
+
if (building === undefined)
|
|
664
|
+
return fail(`Building ${String(buildingId)} has no Hab building definition in the configured Game. Refresh the definitions and choose a valid building, then retry.`, game);
|
|
665
|
+
if (TIERS.indexOf(building.minimumTier) > TIERS.indexOf(definition.tier))
|
|
666
|
+
fail(`${building.name} needs a tier-${building.minimumTier} Hab, above ${definition.name}'s tier ${definition.tier}, so the game refuses to finalize it. Remove it from the design and retry.`, hab);
|
|
667
|
+
if (building.hubValue !== 0) {
|
|
668
|
+
if (hub !== undefined || building.hubValue !== definition.hubValue)
|
|
669
|
+
fail(`${hub === undefined ? `Hub building ${building.name} does not match ${definition.name}'s hub` : `The design holds two hub types, ${hub.name} and ${building.name}`}, so the game refuses to finalize it. Keep at most one hub matching the Hab and retry.`, hab);
|
|
670
|
+
hub = { id: buildingId, name: building.name, quantity };
|
|
671
|
+
}
|
|
672
|
+
const missing = building.researchRequirementIds.filter((tag) => !researchTags.includes(tag));
|
|
673
|
+
if (missing.length > 0)
|
|
674
|
+
unresearched.push(`${building.name} (research ${missing.join(', ')})`);
|
|
675
|
+
building.providedTagIds.forEach((tag) => provided.add(tag));
|
|
676
|
+
building.requiredTagIds.forEach((tag) => required.add(tag));
|
|
677
|
+
slots += building.slots * quantity;
|
|
678
|
+
neededCrew += building.neededCrew * quantity;
|
|
679
|
+
jobs += building.jobCapacity * quantity;
|
|
680
|
+
}
|
|
681
|
+
const missingTags = [...required].filter((tag) => !provided.has(tag));
|
|
682
|
+
if (missingTags.length > 0)
|
|
683
|
+
fail(`The design needs building tags ${missingTags.join(', ')}, which neither ${definition.name}, the Starbase nor its buildings provide, so the game refuses to finalize it. Add a building that provides them, or remove the ones that need them, and retry.`, hab);
|
|
684
|
+
if (slots > definition.slots)
|
|
685
|
+
fail(`The design uses ${String(slots)} slots, more than ${definition.name}'s ${String(definition.slots)}, so the game refuses to finalize it. Remove buildings and retry.`, hab);
|
|
686
|
+
if (neededCrew > U16_MAX || jobs > U16_MAX)
|
|
687
|
+
fail(`The design needs ${String(neededCrew)} crew and ${String(jobs)} job slots, beyond what a Hab records (${String(U16_MAX)}). Remove buildings and retry.`, hab);
|
|
688
|
+
return { neededCrew, unresearched, ...(hub === undefined ? {} : { hub }) };
|
|
689
|
+
}
|
|
690
|
+
/** Construction cargo a design takes from the Starbase Player, net of the
|
|
691
|
+
* refunds its removals return, and the construction time it adds. */
|
|
692
|
+
function habConstruction(changes, buildings) {
|
|
693
|
+
const cargo = new Map();
|
|
694
|
+
let seconds = 0n;
|
|
695
|
+
for (const [buildingId, delta] of changes) {
|
|
696
|
+
const building = buildings.get(buildingId);
|
|
697
|
+
const count = BigInt(Math.abs(delta));
|
|
698
|
+
if (delta > 0)
|
|
699
|
+
seconds += building.constructionDurationSeconds * count;
|
|
700
|
+
for (const { cargoId, quantityRaw } of delta > 0
|
|
701
|
+
? building.constructionCost
|
|
702
|
+
: building.refunds)
|
|
703
|
+
cargo.set(cargoId, (cargo.get(cargoId) ?? 0n) +
|
|
704
|
+
(delta > 0 ? quantityRaw : -quantityRaw) * count);
|
|
705
|
+
}
|
|
706
|
+
return { cargo, seconds };
|
|
707
|
+
}
|
|
708
|
+
/** The crew and cargo a design's finalization needs beyond what the Starbase
|
|
709
|
+
* Player holds. A Building Legacy rank discounts construction cargo, so a
|
|
710
|
+
* shortfall against the full cost only counts when the Character holds no
|
|
711
|
+
* such rank. */
|
|
712
|
+
function habFinalizeShortfalls(fresh, hab, neededCrew, cargo) {
|
|
713
|
+
const certain = [];
|
|
714
|
+
const possible = [];
|
|
715
|
+
const crew = neededCrew - hab.neededCrew;
|
|
716
|
+
const available = fresh.player.totalCrew - fresh.player.busyCrew;
|
|
717
|
+
if (crew > available)
|
|
718
|
+
certain.push(`${String(crew)} more crew, while Starbase Player ${fresh.playerRaw.address} has ${String(available)} available`);
|
|
719
|
+
const discounted = fresh.characterData.xp.progression.legacy.ranks.building > 0;
|
|
720
|
+
for (const [cargoId, amount] of cargo) {
|
|
721
|
+
if (amount <= 0n)
|
|
722
|
+
continue;
|
|
723
|
+
const held = fresh.player.cargo.amounts.find((entry) => entry.cargoId === cargoId)
|
|
724
|
+
?.amount ?? 0n;
|
|
725
|
+
if (held < amount)
|
|
726
|
+
(discounted ? possible : certain).push(`${String(amount)} raw of cargo ${String(cargoId)}${discounted ? ' before the Building Legacy discount' : ''}, while the Starbase Player holds ${String(held)}`);
|
|
727
|
+
}
|
|
728
|
+
return { certain, possible };
|
|
729
|
+
}
|
|
730
|
+
async function habXpGuard(ctx, game, character) {
|
|
731
|
+
const xp = await resolveXpRuntime(ctx, {
|
|
732
|
+
game,
|
|
733
|
+
...(character === undefined ? {} : { character }),
|
|
734
|
+
});
|
|
735
|
+
return {
|
|
736
|
+
accounts: xp.accounts,
|
|
737
|
+
guard: {
|
|
738
|
+
kind: 'xp-runtime',
|
|
739
|
+
address: game,
|
|
740
|
+
intent: xp.intent,
|
|
741
|
+
accounts: xp.accounts,
|
|
742
|
+
describes: character === undefined
|
|
743
|
+
? 'The Game runtime remains supported. Wait for the phase change or update Atlas Kit, then recreate the Plan.'
|
|
744
|
+
: 'The Game runtime and the Character Career XP accounts remain supported and reconciled. Repair missing XP state using the game client, then recreate the Plan.',
|
|
745
|
+
},
|
|
746
|
+
};
|
|
747
|
+
}
|
|
748
|
+
/** Plans recording building additions and removals in a Crafting Hab's design.
|
|
749
|
+
*
|
|
750
|
+
* An Active Hab enters Design; a Hab already in Design adds these changes to
|
|
751
|
+
* the ones it records. The game records the changes without checking them and
|
|
752
|
+
* applies its building rules only at finalization, so the planner refuses a
|
|
753
|
+
* design that could never finalize: an unknown building, a removal beyond the
|
|
754
|
+
* buildings placed, a building above the Hab's tier, a second hub type or one
|
|
755
|
+
* that does not match the Hab's, a building tag nothing provides, or more
|
|
756
|
+
* slots than the Hab has. It warns instead about what the Profile can still
|
|
757
|
+
* fix before finalizing: research the Character lacks, and the crew and
|
|
758
|
+
* construction cargo the Starbase Player must hold. The game accepts several
|
|
759
|
+
* copies of the matching hub, so adding one warns rather than refuses.
|
|
760
|
+
*
|
|
761
|
+
* The Hab must not be under construction or running Crafting Processes, and
|
|
762
|
+
* its rent must be current. The Profile funds the design's storage growth.
|
|
763
|
+
* Concurrent design changes remain chain-authoritative.
|
|
764
|
+
*
|
|
765
|
+
* @example
|
|
766
|
+
* ```ts
|
|
767
|
+
* declare const ctx: SageContext;
|
|
768
|
+
* declare const hab: CraftingHabSnapshot;
|
|
769
|
+
* declare const options: PlanCraftingHabBuildingChangesOptions;
|
|
770
|
+
* const plan = await planCraftingHabBuildingChanges(ctx, hab, options);
|
|
771
|
+
* console.log(plan.describe());
|
|
772
|
+
* ```
|
|
773
|
+
*/
|
|
774
|
+
export async function planCraftingHabBuildingChanges(ctx, hab, input) {
|
|
775
|
+
const options = readOptions(input, [
|
|
776
|
+
'authorization',
|
|
777
|
+
'starbasePlayer',
|
|
778
|
+
'buildingChanges',
|
|
779
|
+
]);
|
|
780
|
+
const changes = readBuildingChanges(options.buildingChanges);
|
|
781
|
+
const action = 'building changes';
|
|
782
|
+
const { authorization, game, current, fresh, gameRaw, definition, buildings, } = await readDesignHab(ctx, hab, options, action);
|
|
783
|
+
const { data } = current;
|
|
784
|
+
if (data.version !== 1)
|
|
785
|
+
fail(`Crafting Hab ${hab.address} is a version ${String(data.version)} Hab, which records no committed crew, so the game can never finalize its design. Only version-1 Habs support building changes.`, hab.address);
|
|
786
|
+
if (data.constructionRemainingSeconds > 0n)
|
|
787
|
+
fail(`Crafting Hab ${hab.address} records ${String(data.constructionRemainingSeconds)} seconds of construction remaining, and the game refuses building changes while a Hab is under construction. The stored time advances only when the Hab is processed: wait, refresh the Hab and retry.`, hab.address);
|
|
788
|
+
if (data.jobsRunning > 0)
|
|
789
|
+
fail(`Crafting Hab ${hab.address} runs ${String(data.jobsRunning)} Crafting Processes; complete or cancel them before changing its buildings.`, hab.address);
|
|
790
|
+
const profileFaction = await deriveProfileFactionAddress(ctx.programs, authorization.profile);
|
|
791
|
+
const [factionCandidate, profileCandidate, rent] = await readContextRawAccounts(ctx, [
|
|
792
|
+
profileFaction,
|
|
793
|
+
authorization.profile,
|
|
794
|
+
RENT_ADDRESS,
|
|
795
|
+
]);
|
|
796
|
+
const factionRaw = present(factionCandidate, profileFaction, `Profile ${authorization.profile} has no faction. Join a faction and retry.`);
|
|
797
|
+
if (profileFactionIdentityAccountDefinition.decode(factionRaw, ctx.programs)
|
|
798
|
+
.profile !== authorization.profile)
|
|
799
|
+
fail(`ProfileFaction ${profileFaction} does not belong to Profile ${authorization.profile}. Refresh or repair the Profile faction and retry.`, profileFaction);
|
|
800
|
+
const design = new Map(data.state.kind === 'design'
|
|
801
|
+
? data.state.buildings.map(({ buildingId, quantity }) => [
|
|
802
|
+
buildingId,
|
|
803
|
+
quantity,
|
|
804
|
+
])
|
|
805
|
+
: []);
|
|
806
|
+
for (const change of changes) {
|
|
807
|
+
if (!buildings.has(change.buildingId))
|
|
808
|
+
invalid('buildingChanges', change.buildingId, 'a Hab building id of the configured Game', 'Choose a building from listHabBuildingDefinitions, then retry.');
|
|
809
|
+
const next = (design.get(change.buildingId) ?? 0) +
|
|
810
|
+
(change.kind === 'add' ? change.quantity : -change.quantity);
|
|
811
|
+
if (Math.abs(next) > I32_MAX)
|
|
812
|
+
fail(`The design would record a change of ${String(next)} ${buildings.get(change.buildingId).name}, beyond what the game can record. Change fewer buildings and retry.`, hab.address);
|
|
813
|
+
design.set(change.buildingId, next);
|
|
814
|
+
}
|
|
815
|
+
const layout = habFinalLayout(hab.address, data.buildings, design, buildings);
|
|
816
|
+
const { neededCrew, unresearched, hub } = checkHabLayout(hab.address, layout, buildings, definition, fresh.starbase, fresh.characterData.modifiers.values.researchTags, game);
|
|
817
|
+
const { certain, possible } = habFinalizeShortfalls(fresh, data, neededCrew, habConstruction(design, buildings).cargo);
|
|
818
|
+
const hubCopies = hubCopyWarning(hub, design);
|
|
819
|
+
const warnings = [
|
|
820
|
+
...(unresearched.length === 0
|
|
821
|
+
? []
|
|
822
|
+
: [
|
|
823
|
+
`Character ${fresh.character} lacks the research for ${unresearched.join('; ')}; the game records the design but refuses to finalize it until that research is unlocked.`,
|
|
824
|
+
]),
|
|
825
|
+
...(certain.length + possible.length === 0
|
|
826
|
+
? []
|
|
827
|
+
: [
|
|
828
|
+
`Finalizing needs ${[...certain, ...possible].join('; ')}; bring them to the Starbase before finalizing.`,
|
|
829
|
+
]),
|
|
830
|
+
...(hubCopies === '' ? [] : [hubCopies]),
|
|
831
|
+
];
|
|
832
|
+
const [currencyCache] = await deriveCurrencyCache(ctx.programs, game);
|
|
833
|
+
const xp = await habXpGuard(ctx, game);
|
|
834
|
+
const sequenceId = data.state.kind === 'design' ? data.state.sequenceId : 0;
|
|
835
|
+
const instruction = adaptCraftingHabBuildingChangesInstruction(ctx.programs, {
|
|
836
|
+
authorization,
|
|
837
|
+
game,
|
|
838
|
+
profileFaction,
|
|
839
|
+
craftingHab: hab.address,
|
|
840
|
+
system: fresh.system,
|
|
841
|
+
starbasePlayer: fresh.playerRaw.address,
|
|
842
|
+
currencyCache,
|
|
843
|
+
sequenceId,
|
|
844
|
+
buildingChanges: changes.map(({ buildingId, kind, quantity }) => [
|
|
845
|
+
buildingId,
|
|
846
|
+
kind === 'add' ? quantity : -quantity,
|
|
847
|
+
]),
|
|
848
|
+
});
|
|
849
|
+
const accounts = new Map([
|
|
850
|
+
[hab.address, current.raw],
|
|
851
|
+
[authorization.profile, profileCandidate],
|
|
852
|
+
[RENT_ADDRESS, rent],
|
|
853
|
+
]);
|
|
854
|
+
const storage = planStorageFunding(ctx.programs, {
|
|
855
|
+
action: readCraftingHabStorageAction(ctx.programs, instruction, accounts),
|
|
856
|
+
accounts,
|
|
857
|
+
label: 'Crafting Hab building changes',
|
|
858
|
+
});
|
|
859
|
+
const description = `Record building changes in Crafting Hab ${hab.address}'s design: ${changes
|
|
860
|
+
.map(({ buildingId, kind, quantity }) => `${kind} ${String(quantity)} ${buildings.get(buildingId).name}`)
|
|
861
|
+
.join('; ')}. Nothing is built until the design is finalized.${warnings.map((w) => ` Warning: ${w}`).join('')} ${storage.disclosure}`;
|
|
862
|
+
return buildPlannerPlan(ctx.programs, {
|
|
863
|
+
kind: 'crafting.crafting-hab-building-changes',
|
|
864
|
+
preconditions: [
|
|
865
|
+
...(await Promise.all([
|
|
866
|
+
accountStatePrecondition(gameRaw, `The configured Game keeps ${definition.name}'s and the Hab buildings' definitions.`),
|
|
867
|
+
accountStatePrecondition(current.raw, `Crafting Hab ${hab.address} keeps its buildings, design, rent and state.`),
|
|
868
|
+
accountStatePrecondition(fresh.systemRaw, `Star System ${fresh.system} keeps the Hab on its plot.`),
|
|
869
|
+
accountStatePrecondition(factionRaw, `ProfileFaction ${profileFaction} stays assigned to Profile ${authorization.profile}.`),
|
|
870
|
+
])),
|
|
871
|
+
xp.guard,
|
|
872
|
+
storage.guard,
|
|
873
|
+
],
|
|
874
|
+
steps: [
|
|
875
|
+
{
|
|
876
|
+
describes: description,
|
|
877
|
+
instruction,
|
|
878
|
+
signers: [authorization.authority],
|
|
879
|
+
},
|
|
880
|
+
],
|
|
881
|
+
summary: description,
|
|
882
|
+
});
|
|
883
|
+
}
|
|
884
|
+
/** Plans finalizing a Crafting Hab's building design.
|
|
885
|
+
*
|
|
886
|
+
* The game applies the design: it checks the final layout's tier, hub type,
|
|
887
|
+
* building tags and slots and the Character's research for every building in
|
|
888
|
+
* it, takes construction cargo from the Starbase Player and returns removed
|
|
889
|
+
* buildings' refunds to it, reserves or releases the crew the layout needs,
|
|
890
|
+
* and starts construction. The planner
|
|
891
|
+
* refuses any of those the game would refuse, and a crew or cargo shortfall
|
|
892
|
+
* it can see; a Building Legacy rank discounts construction cargo, so a
|
|
893
|
+
* shortfall against the full cost only warns when the Character holds one.
|
|
894
|
+
*
|
|
895
|
+
* The Hab must be in Design, running no Crafting Processes, and its rent
|
|
896
|
+
* current. Finalization can draw Profile storage this Plan does not bound: a
|
|
897
|
+
* refund can open a Starbase Player cargo line, and new buildings grow the
|
|
898
|
+
* Hab, which the program funds from the Profile vault, so simulate before
|
|
899
|
+
* signing.
|
|
900
|
+
*
|
|
901
|
+
* @example
|
|
902
|
+
* ```ts
|
|
903
|
+
* declare const ctx: SageContext;
|
|
904
|
+
* declare const hab: CraftingHabSnapshot;
|
|
905
|
+
* declare const options: PlanFinalizeCraftingHabBuildingChangesOptions;
|
|
906
|
+
* const plan = await planFinalizeCraftingHabBuildingChanges(ctx, hab, options);
|
|
907
|
+
* ```
|
|
908
|
+
*/
|
|
909
|
+
export async function planFinalizeCraftingHabBuildingChanges(ctx, hab, input) {
|
|
910
|
+
const options = readOptions(input, ['authorization', 'starbasePlayer']);
|
|
911
|
+
const action = 'finalization';
|
|
912
|
+
const { authorization, game, current, fresh, gameRaw, definition, buildings, } = await readDesignHab(ctx, hab, options, action);
|
|
913
|
+
const { data } = current;
|
|
914
|
+
if (data.state.kind !== 'design')
|
|
915
|
+
return fail(`Crafting Hab ${hab.address} has no pending building design to finalize. Record building changes first, refresh the Hab and retry.`, hab.address);
|
|
916
|
+
if (data.version !== 1)
|
|
917
|
+
fail(`Crafting Hab ${hab.address} is a version ${String(data.version)} Hab, which records no committed crew, so the game refuses to finalize its design. Cancel the design instead.`, hab.address);
|
|
918
|
+
if (data.jobsRunning > 0)
|
|
919
|
+
fail(`Crafting Hab ${hab.address} runs ${String(data.jobsRunning)} Crafting Processes; complete or cancel them before finalizing.`, hab.address);
|
|
920
|
+
const design = new Map(data.state.buildings.map(({ buildingId, quantity }) => [
|
|
921
|
+
buildingId,
|
|
922
|
+
quantity,
|
|
923
|
+
]));
|
|
924
|
+
const unknown = [...design.keys()].find((id) => !buildings.has(id));
|
|
925
|
+
if (unknown !== undefined)
|
|
926
|
+
fail(`The design names building ${String(unknown)}, which has no Hab building definition in the configured Game, so the game refuses to finalize it. Cancel the design and record a new one.`, hab.address);
|
|
927
|
+
const layout = habFinalLayout(hab.address, data.buildings, design, buildings);
|
|
928
|
+
const { neededCrew, unresearched, hub } = checkHabLayout(hab.address, layout, buildings, definition, fresh.starbase, fresh.characterData.modifiers.values.researchTags, game);
|
|
929
|
+
if (unresearched.length > 0)
|
|
930
|
+
fail(`Character ${fresh.character} lacks the research for ${unresearched.join('; ')}, so the game refuses to finalize the design. Unlock that research, or cancel the design and record one without those buildings.`, fresh.character);
|
|
931
|
+
const { cargo, seconds } = habConstruction(design, buildings);
|
|
932
|
+
const { certain, possible } = habFinalizeShortfalls(fresh, data, neededCrew, cargo);
|
|
933
|
+
if (certain.length > 0)
|
|
934
|
+
fail(`Finalizing Crafting Hab ${hab.address}'s design needs ${certain.join('; ')}. Bring them to the Starbase, refresh the Starbase Player and retry.`, fresh.playerRaw.address);
|
|
935
|
+
const [currencyCache] = await deriveCurrencyCache(ctx.programs, game);
|
|
936
|
+
const xp = await habXpGuard(ctx, game, fresh.character);
|
|
937
|
+
const instruction = adaptFinalizeCraftingHabBuildingChangesInstruction(ctx.programs, {
|
|
938
|
+
authorization,
|
|
939
|
+
game,
|
|
940
|
+
character: fresh.character,
|
|
941
|
+
craftingHab: hab.address,
|
|
942
|
+
system: fresh.system,
|
|
943
|
+
starbasePlayer: fresh.playerRaw.address,
|
|
944
|
+
currencyCache,
|
|
945
|
+
...xp.accounts,
|
|
946
|
+
});
|
|
947
|
+
const crew = neededCrew - data.neededCrew;
|
|
948
|
+
const amounts = (sign) => [...cargo]
|
|
949
|
+
.filter(([, amount]) => amount * sign > 0n)
|
|
950
|
+
.map(([cargoId, amount]) => `${String(amount * sign)} raw of cargo ${String(cargoId)}`)
|
|
951
|
+
.join(', ');
|
|
952
|
+
const hubCopy = hubCopyWarning(hub, design);
|
|
953
|
+
const takes = amounts(1n);
|
|
954
|
+
const returns = amounts(-1n);
|
|
955
|
+
const movement = takes === '' && returns === ''
|
|
956
|
+
? 'moves no cargo'
|
|
957
|
+
: `${[
|
|
958
|
+
...(takes === '' ? [] : [`takes ${takes} from`]),
|
|
959
|
+
...(returns === '' ? [] : [`returns ${returns} to`]),
|
|
960
|
+
].join(' and ')} Starbase Player ${fresh.playerRaw.address}`;
|
|
961
|
+
const description = `Finalize Crafting Hab ${hab.address}'s building design: the game ${movement}, ${crew >= 0 ? `reserves ${String(crew)} more crew` : `releases ${String(-crew)} crew`}, and starts ${String(seconds)} seconds of construction before Building Legacy and Council Rank adjustments.${hubCopy === '' ? '' : ` Warning: ${hubCopy}`}${possible.length === 0 ? '' : ` Warning: the Starbase Player may lack ${possible.join('; ')}; the game refuses if the discounted cost still exceeds it.`} Finalization can draw Profile storage this Plan does not bound, for a new cargo line or the Hab's growth; simulate before signing.`;
|
|
962
|
+
return buildPlannerPlan(ctx.programs, {
|
|
963
|
+
kind: 'crafting.finalize-crafting-hab-building-changes',
|
|
964
|
+
preconditions: [
|
|
965
|
+
...(await Promise.all([
|
|
966
|
+
accountStatePrecondition(gameRaw, `The configured Game keeps ${definition.name}'s and the Hab buildings' definitions.`),
|
|
967
|
+
accountStatePrecondition(current.raw, `Crafting Hab ${hab.address} keeps its design, buildings, crew and rent.`),
|
|
968
|
+
accountStatePrecondition(fresh.systemRaw, `Star System ${fresh.system} keeps the Hab on its plot and its building tags.`),
|
|
969
|
+
accountStatePrecondition(fresh.playerRaw, `Starbase Player ${fresh.playerRaw.address} keeps its crew and cargo.`),
|
|
970
|
+
accountStatePrecondition(fresh.characterRaw, `Character ${fresh.character} keeps its research and Building Legacy rank.`),
|
|
971
|
+
])),
|
|
972
|
+
xp.guard,
|
|
973
|
+
],
|
|
974
|
+
steps: [
|
|
975
|
+
{
|
|
976
|
+
describes: description,
|
|
977
|
+
instruction,
|
|
978
|
+
signers: [authorization.authority],
|
|
979
|
+
},
|
|
980
|
+
],
|
|
981
|
+
summary: description,
|
|
982
|
+
});
|
|
983
|
+
}
|
|
984
|
+
/** Plans cancelling a Crafting Hab's pending building design, returning the
|
|
985
|
+
* Hab to Active with its buildings unchanged. Its rent must be current.
|
|
986
|
+
*
|
|
987
|
+
* @example
|
|
988
|
+
* ```ts
|
|
989
|
+
* declare const ctx: SageContext;
|
|
990
|
+
* declare const hab: CraftingHabSnapshot;
|
|
991
|
+
* declare const options: PlanCancelCraftingHabBuildingChangesOptions;
|
|
992
|
+
* const plan = await planCancelCraftingHabBuildingChanges(ctx, hab, options);
|
|
993
|
+
* ```
|
|
994
|
+
*/
|
|
995
|
+
export async function planCancelCraftingHabBuildingChanges(ctx, hab, input) {
|
|
996
|
+
const options = readOptions(input, ['authorization', 'starbasePlayer']);
|
|
997
|
+
const { authorization, game, current, fresh } = await readDesignHab(ctx, hab, options, 'cancellation');
|
|
998
|
+
if (current.data.state.kind !== 'design')
|
|
999
|
+
fail(`Crafting Hab ${hab.address} has no pending building design to cancel. Refresh the Hab and retry.`, hab.address);
|
|
1000
|
+
const [currencyCache] = await deriveCurrencyCache(ctx.programs, game);
|
|
1001
|
+
const xp = await habXpGuard(ctx, game);
|
|
1002
|
+
const instruction = adaptCancelCraftingHabBuildingChangesInstruction(ctx.programs, {
|
|
1003
|
+
authorization,
|
|
1004
|
+
game,
|
|
1005
|
+
craftingHab: hab.address,
|
|
1006
|
+
system: fresh.system,
|
|
1007
|
+
starbasePlayer: fresh.playerRaw.address,
|
|
1008
|
+
currencyCache,
|
|
1009
|
+
});
|
|
1010
|
+
const description = `Cancel Crafting Hab ${hab.address}'s pending building design; the Hab returns to Active with its buildings unchanged. The game settles rent first; rent accrual remains chain-authoritative.`;
|
|
1011
|
+
return buildPlannerPlan(ctx.programs, {
|
|
1012
|
+
kind: 'crafting.cancel-crafting-hab-building-changes',
|
|
1013
|
+
preconditions: [
|
|
1014
|
+
...(await Promise.all([
|
|
1015
|
+
accountStatePrecondition(current.raw, `Crafting Hab ${hab.address} keeps its pending design and rent.`),
|
|
1016
|
+
accountStatePrecondition(fresh.systemRaw, `Star System ${fresh.system} keeps the Hab on its plot.`),
|
|
1017
|
+
])),
|
|
1018
|
+
xp.guard,
|
|
1019
|
+
],
|
|
1020
|
+
steps: [
|
|
1021
|
+
{
|
|
1022
|
+
describes: description,
|
|
1023
|
+
instruction,
|
|
1024
|
+
signers: [authorization.authority],
|
|
1025
|
+
},
|
|
1026
|
+
],
|
|
1027
|
+
summary: description,
|
|
1028
|
+
});
|
|
1029
|
+
}
|
|
1030
|
+
//# sourceMappingURL=habs.js.map
|