diff --git a/app/components/village/Grid.vue b/app/components/village/Grid.vue deleted file mode 100644 index 101ffd9..0000000 --- a/app/components/village/Grid.vue +++ /dev/null @@ -1,120 +0,0 @@ - - - - - - {{ $t('village.grid.title') }} - {{ $t('village.grid.hint') }} - - - - - - - - - - - - {{ $t(`buildings.${tile.type}`) }} - - - {{ tile.planned ? '…' : tile.level }} - - - - - - - diff --git a/app/composables/useVillage.ts b/app/composables/useVillage.ts index 16b83bb..48a7727 100644 --- a/app/composables/useVillage.ts +++ b/app/composables/useVillage.ts @@ -75,7 +75,7 @@ export function useVillage(villageId: number) { canAfford, secondsUntil, refresh, - build: (type: BuildingType, at: { x: number; y: number } | null) => run('village.build', { type, ...at }), + build: (type: BuildingType) => run('village.build', { type }), upgrade: (buildingId: number) => run('village.upgrade', { building_id: buildingId }), demolish: (buildingId: number) => run('village.demolish', { building_id: buildingId }), cancel: (orderId: number) => run('village.cancel', { order_id: orderId }), diff --git a/app/mock/catalog.ts b/app/mock/catalog.ts index b30fea7..49dbc30 100644 --- a/app/mock/catalog.ts +++ b/app/mock/catalog.ts @@ -5,7 +5,8 @@ import type { BuildingType, Cost, Resource, Resources } from '~/types/game' // (config/game/buildings.php and app/Game/), not to the client. export interface CatalogEntry { - footprint: [number, number] + /** Building space an instance takes (0 for the wall). */ + space: number max_per_village: number | null /** Level 0 values. Level n costs, takes, needs and produces (n + 1) times as much. */ cost: Cost @@ -21,19 +22,19 @@ export const BASE_TRICKLE = 5 export const VILLAGER_UPKEEP = 1 export const DEMOLISH_REFUND = 0.5 export const DEFAULT_PRIORITY = 5 -export const GRID = { w: 10, h: 5 } +export const MAX_SPACE = 50 export const START_RESOURCES: Resources = { wood: 500, clay: 500, iron: 500, food: 100 } export const CATALOG: Record = { - headquarters: { footprint: [3, 3], max_per_village: 1, cost: { wood: 90, clay: 80, iron: 70 }, seconds: 20, workers: 2, production: null }, - storage: { footprint: [2, 2], max_per_village: null, cost: { wood: 60, clay: 50, iron: 40 }, seconds: 15, workers: 1, production: null, capacity: 1000 }, - farm: { footprint: [2, 2], max_per_village: null, cost: { wood: 45, clay: 40, iron: 30 }, seconds: 15, workers: 2, production: { resource: 'food', per_hour: 20 } }, - timber_camp: { footprint: [2, 2], max_per_village: null, cost: { wood: 50, clay: 60, iron: 40 }, seconds: 12, workers: 2, production: { resource: 'wood', per_hour: 30 } }, - clay_pit: { footprint: [2, 2], max_per_village: null, cost: { wood: 65, clay: 50, iron: 40 }, seconds: 12, workers: 2, production: { resource: 'clay', per_hour: 30 } }, - iron_mine: { footprint: [2, 2], max_per_village: null, cost: { wood: 75, clay: 65, iron: 70 }, seconds: 14, workers: 2, production: { resource: 'iron', per_hour: 30 } }, - hiding_place: { footprint: [1, 1], max_per_village: null, cost: { wood: 50, clay: 60, iron: 50 }, seconds: 10, workers: 0, production: null }, - wall: { footprint: [0, 0], max_per_village: 1, cost: { wood: 50, clay: 100, iron: 20 }, seconds: 20, workers: 1, production: null }, - rally_point: { footprint: [1, 1], max_per_village: 1, cost: { wood: 10, clay: 40, iron: 30 }, seconds: 10, workers: 0, production: null }, + headquarters: { space: 9, max_per_village: 1, cost: { wood: 90, clay: 80, iron: 70 }, seconds: 20, workers: 2, production: null }, + storage: { space: 4, max_per_village: null, cost: { wood: 60, clay: 50, iron: 40 }, seconds: 15, workers: 1, production: null, capacity: 1000 }, + farm: { space: 4, max_per_village: null, cost: { wood: 45, clay: 40, iron: 30 }, seconds: 15, workers: 2, production: { resource: 'food', per_hour: 20 } }, + timber_camp: { space: 4, max_per_village: null, cost: { wood: 50, clay: 60, iron: 40 }, seconds: 12, workers: 2, production: { resource: 'wood', per_hour: 30 } }, + clay_pit: { space: 4, max_per_village: null, cost: { wood: 65, clay: 50, iron: 40 }, seconds: 12, workers: 2, production: { resource: 'clay', per_hour: 30 } }, + iron_mine: { space: 4, max_per_village: null, cost: { wood: 75, clay: 65, iron: 70 }, seconds: 14, workers: 2, production: { resource: 'iron', per_hour: 30 } }, + hiding_place: { space: 1, max_per_village: null, cost: { wood: 50, clay: 60, iron: 50 }, seconds: 10, workers: 0, production: null }, + wall: { space: 0, max_per_village: 1, cost: { wood: 50, clay: 100, iron: 20 }, seconds: 20, workers: 1, production: null }, + rally_point: { space: 1, max_per_village: 1, cost: { wood: 10, clay: 40, iron: 30 }, seconds: 10, workers: 0, production: null }, } export const BUILDING_TYPES = Object.keys(CATALOG) as BuildingType[] diff --git a/app/mock/server.ts b/app/mock/server.ts index 2ac9459..1621e54 100644 --- a/app/mock/server.ts +++ b/app/mock/server.ts @@ -27,7 +27,7 @@ import { CATALOG, DEFAULT_PRIORITY, DEMOLISH_REFUND, - GRID, + MAX_SPACE, MAX_LEVEL, START_RESOURCES, VILLAGER_UPKEEP, @@ -38,8 +38,6 @@ interface MockBuilding { id: number type: BuildingType level: number - x: number - y: number priority: number } @@ -49,8 +47,6 @@ interface MockOrder { type: BuildingType action: BuildOrder['action'] target_level: number - x: number - y: number cost: Cost seconds: number builder: number | null @@ -65,7 +61,7 @@ interface MockVillage { name: string x: number y: number - grid: { w: number; h: number } + space_max: number resources: Resources resources_at: number buildings: MockBuilding[] @@ -79,7 +75,7 @@ interface MockState { villages: MockVillage[] } -const STORAGE_KEY = 'vp.mock.v1' +const STORAGE_KEY = 'vp.mock.v2' const LATENCY_MS = 60 const WORLDS = [ { id: 1, name: 'World 1', slug: 'w1', speed: 1 }, @@ -212,31 +208,15 @@ function refund(v: MockVillage, amount: Cost) { v.resources.iron = Math.min(cap, v.resources.iron + amount.iron) } -/** Cells taken by buildings and by queued new buildings. */ -function occupied(v: MockVillage) { - const cells = new Set() - const mark = (type: BuildingType, x: number, y: number) => { - const [w, h] = CATALOG[type].footprint - for (let dy = 0; dy < h; dy++) for (let dx = 0; dx < w; dx++) cells.add(`${x + dx},${y + dy}`) - } - v.buildings.forEach((b) => mark(b.type, b.x, b.y)) - v.orders.filter((o) => o.action === 'build').forEach((o) => mark(o.type, o.x, o.y)) - return cells +/** Building space taken by buildings and by queued new buildings. */ +function spaceUsed(v: MockVillage) { + return ( + v.buildings.reduce((sum, b) => sum + CATALOG[b.type].space, 0) + + v.orders.filter((o) => o.action === 'build').reduce((sum, o) => sum + CATALOG[o.type].space, 0) + ) } -function fits(v: MockVillage, type: BuildingType, x: number, y: number) { - const [w, h] = CATALOG[type].footprint - if (w === 0 || h === 0) return true - if (x < 0 || y < 0 || x + w > v.grid.w || y + h > v.grid.h) return false - const cells = occupied(v) - for (let dy = 0; dy < h; dy++) for (let dx = 0; dx < w; dx++) if (cells.has(`${x + dx},${y + dy}`)) return false - return true -} - -function freeSlot(v: MockVillage, type: BuildingType) { - for (let y = 0; y < v.grid.h; y++) for (let x = 0; x < v.grid.w; x++) if (fits(v, type, x, y)) return { x, y } - return null -} +const spaceFits = (v: MockVillage, type: BuildingType) => spaceUsed(v) + CATALOG[type].space <= v.space_max /** Level after everything already queued for this building; -1 if it is being removed. */ function queuedLevel(v: MockVillage, b: MockBuilding) { @@ -277,10 +257,10 @@ function buildOptions(v: MockVillage): BuildOption[] { const blocked = entry.max_per_village !== null && instances(v, type) >= entry.max_per_village ? 'max_per_village' - : freeSlot(v, type) === null + : !spaceFits(v, type) ? 'no_space' : null - return { type, footprint: entry.footprint, target_level: 0, cost, seconds: levelSeconds(type, 0), blocked } + return { type, space: entry.space, target_level: 0, cost, seconds: levelSeconds(type, 0), blocked } }) } @@ -307,7 +287,7 @@ function startWaiting(v: MockVillage, at: number) { function finish(state: MockState, v: MockVillage, o: MockOrder) { if (o.action === 'build') { - v.buildings.push({ id: nextId(state), type: o.type, level: 0, x: o.x, y: o.y, priority: DEFAULT_PRIORITY }) + v.buildings.push({ id: nextId(state), type: o.type, level: 0, priority: DEFAULT_PRIORITY }) return } const b = v.buildings.find((b) => b.id === o.building_id) @@ -347,7 +327,7 @@ function view(v: MockVillage, now: number): VillageState { name: v.name, x: v.x, y: v.y, - grid: { ...v.grid }, + space: { used: spaceUsed(v), max: v.space_max }, resources: { ...v.resources }, resources_at: iso(v.resources_at), rates: rates(v), @@ -367,9 +347,7 @@ function view(v: MockVillage, now: number): VillageState { id: b.id, type: b.type, level: b.level, - x: b.x, - y: b.y, - footprint: CATALOG[b.type].footprint, + space: CATALOG[b.type].space, priority: b.priority, workers: { assigned: assigned.get(b.id) ?? 0, required: requiredWorkers(b) }, staffing: staffing(b, assigned), @@ -385,9 +363,7 @@ function view(v: MockVillage, now: number): VillageState { type: o.type, action: o.action, target_level: o.target_level, - x: o.x, - y: o.y, - footprint: CATALOG[o.type].footprint, + space: CATALOG[o.type].space, cost: o.cost, position: i + 1, builder: o.builder, @@ -414,15 +390,14 @@ function createVillage(state: MockState, user: AuthUser, worldId: number, now: n name: `${user.name}'s village`, x, y, - grid: { ...GRID }, + space_max: MAX_SPACE, resources: { ...START_RESOURCES }, resources_at: now, buildings: [], orders: [], } for (const type of ['headquarters', 'storage', 'farm'] as const) { - const at = freeSlot(v, type)! - v.buildings.push({ id: nextId(state), type, level: 0, x: at.x, y: at.y, priority: DEFAULT_PRIORITY }) + v.buildings.push({ id: nextId(state), type, level: 0, priority: DEFAULT_PRIORITY }) } return v } @@ -466,15 +441,10 @@ const handlers: Record = { const type = data.type as BuildingType const entry = CATALOG[type] ?? fail('not_found') if (entry.max_per_village !== null && instances(v, type) >= entry.max_per_village) fail('max_per_village') - const at = - typeof data.x === 'number' && typeof data.y === 'number' - ? fits(v, type, data.x, data.y) - ? { x: data.x, y: data.y } - : fail('no_space') - : (freeSlot(v, type) ?? fail('no_space')) + if (!spaceFits(v, type)) fail('no_space') const cost = levelCost(type, 0) pay(v, cost) - enqueue(state, v, { building_id: null, type, action: 'build', target_level: 0, x: at.x, y: at.y, cost, seconds: levelSeconds(type, 0) }, now) + enqueue(state, v, { building_id: null, type, action: 'build', target_level: 0, cost, seconds: levelSeconds(type, 0) }, now) return view(v, now) }, @@ -483,7 +453,7 @@ const handlers: Record = { const b = ownBuilding(v, data.building_id) const offer = upgradeOffer(v, b) ?? fail('max_level') pay(v, offer.cost) - enqueue(state, v, { building_id: b.id, type: b.type, action: 'upgrade', target_level: offer.target_level, x: b.x, y: b.y, cost: offer.cost, seconds: offer.seconds }, now) + enqueue(state, v, { building_id: b.id, type: b.type, action: 'upgrade', target_level: offer.target_level, cost: offer.cost, seconds: offer.seconds }, now) return view(v, now) }, @@ -491,7 +461,7 @@ const handlers: Record = { const v = ownVillage(state, user, data.village_id, now) const b = ownBuilding(v, data.building_id) const offer = demolishOffer(v, b) ?? fail('core_building') - enqueue(state, v, { building_id: b.id, type: b.type, action: 'demolish', target_level: offer.target_level, x: b.x, y: b.y, cost: { wood: 0, clay: 0, iron: 0 }, seconds: offer.seconds }, now) + enqueue(state, v, { building_id: b.id, type: b.type, action: 'demolish', target_level: offer.target_level, cost: { wood: 0, clay: 0, iron: 0 }, seconds: offer.seconds }, now) return view(v, now) }, diff --git a/app/types/game.ts b/app/types/game.ts index efe7008..0102e7d 100644 --- a/app/types/game.ts +++ b/app/types/game.ts @@ -16,7 +16,7 @@ export type BuildingType = | 'wall' | 'rally_point' -/** Why the server refuses an offer. The client shows `village.blocked.`. */ +/** Why the server refuses an offer. The client shows `village.blocked.`; no_space means not enough free building space. */ export type BlockedReason = 'max_per_village' | 'no_space' | 'max_level' | 'requires' export interface AuthUser { @@ -46,7 +46,8 @@ export interface VillageState { name: string x: number y: number - grid: { w: number; h: number } + /** Building space: what buildings and queued new buildings take, out of the village maximum. */ + space: { used: number; max: number } /** Amounts stored at `resources_at`. */ resources: Resources resources_at: string @@ -77,9 +78,8 @@ export interface Building { id: number type: BuildingType level: number - x: number - y: number - footprint: [number, number] + /** Building space this instance takes, fixed per type (0 for the wall). */ + space: number priority: number workers: { assigned: number; required: number } /** assigned / required, 0..1. */ @@ -101,7 +101,7 @@ export interface Offer { export interface BuildOption extends Offer { type: BuildingType - footprint: [number, number] + space: number } export interface BuildOrder { @@ -112,9 +112,7 @@ export interface BuildOrder { action: 'build' | 'upgrade' | 'demolish' /** -1 means the instance is removed. */ target_level: number - x: number - y: number - footprint: [number, number] + space: number cost: Cost position: number /** Builder slot working on it, or null while it waits. */
{{ $t('village.grid.hint') }}