Make a melee RTS
A classic real-time strategy match: workers gather gold and lumber, buildings train an army, and the last side with buildings standing wins, against the computer.
Quick start
npm create @v12-engine my-game -- --starter melee
cd my-game && npm run devYou get the example's whole melee game: two races, the Bay map, random maps from its generator, and a computer at every other start. Its rules are in src/ as apps/example/src/games/melee has them.
Build it yourself
1. Resources and upkeep
resources: [
{ id: 'gold', name: 'Gold', start: 500, upkeep: true },
{ id: 'lumber', name: 'Lumber', start: 300 }
],
currency: 'gold',
upkeep: [
{ above: 50, name: 'Low upkeep', income: 0.7 },
{ above: 80, name: 'High upkeep', income: 0.4 }
]2. A mine and a worker
export const goldmine: UnitTypeDef = {
id: 'goldmine',
name: 'Gold Mine',
kind: 'resource',
radius: 2,
footprint: { w: 4, d: 4 },
stats: { health: 1 },
sight: 0,
invulnerable: true,
resource: { id: 'gold', amount: 12500, slots: 1, entered: true }
}
export const peasant: UnitTypeDef = {
id: 'peasant',
name: 'Peasant',
kind: 'unit',
radius: 0.4,
stats: { health: 250, attackDamage: 8, attackSpeed: 0.6, attackRange: 0.4, moveSpeed: 4.05 },
sight: 8,
attacks: [{ damagePoint: 0.4, acquisitionRange: 0, targets: ['unit', 'building'] }],
cost: { gold: 75 },
supply: 1,
buildTime: 10,
harvester: { capacity: 10, gatherTime: 1.5 },
builds: ['townhall', 'farm', 'barracks', 'altar', 'lumbermill', 'tower', 'blacksmith']
}Workers carry loads to a building with dropOff (true for everything, ['lumber'] for a lumber mill).
3. Buildings and the tech tree
| Field | Example |
|---|---|
trains | ['footman', 'rifleman'] on the barracks |
supplyGiven | 6 on a farm |
requires | ['keep'] on what needs a keep first |
upgradesTo | 'keep' on the town hall (a list for a choice) |
researches | ['swords'] on the blacksmith (UpgradeDefs) |
The command card, build menu, queues, rally points and greyed-out reasons all follow from this. More in Economy.
4. The start of a match
startingBase: { hall: 'townhall', workers: { type: 'peasant', count: 5 } }For each player the map places nothing for, this puts a hall at the start and the workers between it and the closest mine. A race can have its own (RaceDef.startingBase), or write it in code: startingBase: { spawn(world, player, at) { /* … */ } }.
5. Computer players
import { launch } from '@v12-engine/client'
launch({
target: '#game',
game: meleeGame,
map: myMap,
onStart(world, { map, player }) {
const players = (map?.players ?? []).filter((other) => other.start)
for (const other of players) {
if (other.id === player) continue
const enemyBases = players.filter((rival) => rival.team !== other.team).map((rival) => rival.start!)
world.computers.seat(other.id, { enemyBases, settings: { army: { firstAttack: 360, waveSize: 6 } } })
}
}
})List meleeBrain in Rules.brains. Difficulty, personality, build orders and waves: Computer players.
6. Who wins
Name no victory and the engine's lastTeamStanding decides: a player with no building left is out. Use objectiveVictory for missions.
What else melee shows
Each has a test in apps/example/tests/meleeShows.test.ts.
| Feature | How |
|---|---|
| A tavern | UnitTypeDef.hires, neutral: heroes hired once each |
| Walls and gates | wallsPack, UnitTypeDef.wall, wall.gate; drag a line (buildLineCommand), open and shut (wallGateCommand, wallGate) |
| Blight on the minimap | A MinimapLayer reading the layer's cells (client.cellLayers.terrain) |
| Boats | naval, transport; board, load, unload |
| Patrol | On every attacker's card (P) |
| Veterancy | veterancyPack (Combat) |
| Ruins | remainsPack, rubbleType (Economy) |
| A battle line | Rules.formations (battleLine, Movement) |
| Trading | give, RuleSettings.economy.tradeTax, from the Allies panel |
| Shipped maps | Rules.maps: saves carry only the ground's changes |
| A title menu | apps/example/src/scenes/title.ts |
In the example
apps/example/src/games/melee: rules (index.ts,units.ts,abilities.ts,items.ts) andview/.apps/example/src/scenes/dev.ts(the sandbox),scenes/map.ts(a map from the editor).- A map's
check(checkMeleeMapinapps/example/src/games/melee/maps.ts) flags a map with no town hall or no second team. - Next: Heroes, Testing your rules, Online.