2024-10-21 15:35:32 +00:00
|
|
|
import { writable } from "svelte/store";
|
2024-10-23 08:36:03 +00:00
|
|
|
|
2024-10-23 08:14:19 +00:00
|
|
|
import { createBuilding } from "./create";
|
2024-10-24 13:31:40 +00:00
|
|
|
import { getTilesAtDistance, Hex } from "./hexgrid";
|
2024-10-25 17:03:32 +00:00
|
|
|
import type { BuildingType } from "./types";
|
2024-10-24 13:31:40 +00:00
|
|
|
import { getKeysAsNumbers, shuffle } from "./utils";
|
2024-10-24 09:32:31 +00:00
|
|
|
|
|
|
|
|
|
|
|
type Board = {
|
|
|
|
[key: number]: {
|
|
|
|
[key: number]: number;
|
|
|
|
}
|
|
|
|
}
|
2024-10-21 15:35:32 +00:00
|
|
|
|
|
|
|
|
2024-10-24 17:27:33 +00:00
|
|
|
interface QueuedBuilding {
|
|
|
|
id: number;
|
|
|
|
remainingTime: number;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-10-21 15:35:32 +00:00
|
|
|
export interface VillageState {
|
2024-10-25 17:03:32 +00:00
|
|
|
buildings: BuildingType[];
|
|
|
|
units: {
|
|
|
|
[key: string]: number;
|
|
|
|
},
|
2024-10-21 15:35:32 +00:00
|
|
|
resources: {
|
|
|
|
wood: number;
|
|
|
|
stone: number;
|
|
|
|
iron: number;
|
|
|
|
food: number;
|
|
|
|
culture: number;
|
2024-10-22 15:15:35 +00:00
|
|
|
};
|
2024-10-24 09:32:31 +00:00
|
|
|
villageTiles: Board;
|
|
|
|
outsideTiles: Board;
|
2024-10-24 17:27:33 +00:00
|
|
|
queue: QueuedBuilding[];
|
2024-10-21 15:35:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-10-24 09:32:31 +00:00
|
|
|
export const DEFAULT_TILE = -1;
|
|
|
|
export const VILLAGE_TILE = -2;
|
|
|
|
|
|
|
|
|
|
|
|
function getInitialVillageBoard() {
|
|
|
|
const board: Board = {
|
|
|
|
0: { 0: DEFAULT_TILE },
|
|
|
|
};
|
|
|
|
for (let i = 1; i <= 2; i++) {
|
|
|
|
getTilesAtDistance(i).forEach(tile => {
|
|
|
|
if (board[tile.y] === undefined) {
|
|
|
|
board[tile.y] = {};
|
|
|
|
}
|
|
|
|
|
|
|
|
board[tile.y][tile.x] = DEFAULT_TILE;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return board;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function getInitialOutsideBoard() {
|
|
|
|
const board: Board = {
|
|
|
|
0: { 0: VILLAGE_TILE },
|
|
|
|
};
|
|
|
|
for (let i = 1; i <= 2; i++) {
|
|
|
|
getTilesAtDistance(i).forEach(tile => {
|
|
|
|
if (board[tile.y] === undefined) {
|
|
|
|
board[tile.y] = {};
|
|
|
|
}
|
|
|
|
|
|
|
|
board[tile.y][tile.x] = DEFAULT_TILE;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return board;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function getInitialState() {
|
2024-10-24 13:31:40 +00:00
|
|
|
const state: VillageState = {
|
|
|
|
buildings: [],
|
2024-10-25 17:03:32 +00:00
|
|
|
units: {},
|
2024-10-24 09:32:31 +00:00
|
|
|
resources: {
|
|
|
|
wood: 60,
|
|
|
|
stone: 60,
|
|
|
|
iron: 60,
|
|
|
|
food: 50,
|
|
|
|
culture: 0,
|
|
|
|
},
|
|
|
|
villageTiles: getInitialVillageBoard(),
|
|
|
|
outsideTiles: getInitialOutsideBoard(),
|
2024-10-24 17:27:33 +00:00
|
|
|
queue: [],
|
2024-10-24 09:32:31 +00:00
|
|
|
};
|
|
|
|
|
2024-10-24 13:31:40 +00:00
|
|
|
// Create the Town hall.
|
2024-10-24 14:24:39 +00:00
|
|
|
const townhall = createBuilding('townhall');
|
2024-10-24 09:32:31 +00:00
|
|
|
state.villageTiles[0][0] = townhall.id;
|
2024-10-24 13:31:40 +00:00
|
|
|
state.buildings.push(townhall);
|
|
|
|
|
|
|
|
// Create all the resource buildings.
|
2024-10-24 14:24:39 +00:00
|
|
|
const resourceBuildingTypes: Array<string> = shuffle([
|
2024-10-24 13:31:40 +00:00
|
|
|
'woodcutter', 'woodcutter', 'woodcutter', 'woodcutter',
|
|
|
|
'mine', 'mine', 'mine', 'mine',
|
|
|
|
'pit', 'pit', 'pit', 'pit',
|
|
|
|
'field', 'field', 'field', 'field', 'field', 'field',
|
|
|
|
]);
|
|
|
|
getKeysAsNumbers(state.outsideTiles).forEach(y => {
|
|
|
|
getKeysAsNumbers(state.outsideTiles[y]).forEach(x => {
|
|
|
|
if (state.outsideTiles[y][x] !== DEFAULT_TILE) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const type = resourceBuildingTypes.pop();
|
|
|
|
if (type === undefined) {
|
|
|
|
throw new Error("Not enough building types for outside resource buildings");
|
|
|
|
}
|
2024-10-24 14:24:39 +00:00
|
|
|
const newBuilding = createBuilding(type);
|
2024-10-24 13:31:40 +00:00
|
|
|
newBuilding.tile = new Hex(x, y);
|
2024-10-25 17:03:32 +00:00
|
|
|
newBuilding.level = 10;
|
2024-10-24 13:31:40 +00:00
|
|
|
state.outsideTiles[y][x] = newBuilding.id;
|
|
|
|
state.buildings.push(newBuilding);
|
|
|
|
});
|
|
|
|
});
|
2024-10-24 09:32:31 +00:00
|
|
|
|
|
|
|
return state;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const village = writable<VillageState>(getInitialState());
|
2024-10-21 15:35:32 +00:00
|
|
|
|
|
|
|
|
|
|
|
export default village;
|