Skip to content

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 ​

sh
npm create @v12-engine my-game -- --starter melee
cd my-game && npm run dev

You 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 ​

ts
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 ​

ts
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 ​

FieldExample
trains['footman', 'rifleman'] on the barracks
supplyGiven6 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 ​

ts
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 ​

ts
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.

FeatureHow
A tavernUnitTypeDef.hires, neutral: heroes hired once each
Walls and gateswallsPack, UnitTypeDef.wall, wall.gate; drag a line (buildLineCommand), open and shut (wallGateCommand, wallGate)
Blight on the minimapA MinimapLayer reading the layer's cells (client.cellLayers.terrain)
Boatsnaval, transport; board, load, unload
PatrolOn every attacker's card (P)
VeterancyveterancyPack (Combat)
RuinsremainsPack, rubbleType (Economy)
A battle lineRules.formations (battleLine, Movement)
Tradinggive, RuleSettings.economy.tradeTax, from the Allies panel
Shipped mapsRules.maps: saves carry only the ground's changes
A title menuapps/example/src/scenes/title.ts

In the example ​

  • apps/example/src/games/melee: rules (index.ts, units.ts, abilities.ts, items.ts) and view/.
  • apps/example/src/scenes/dev.ts (the sandbox), scenes/map.ts (a map from the editor).
  • A map's check (checkMeleeMap in apps/example/src/games/melee/maps.ts) flags a map with no town hall or no second team.
  • Next: Heroes, Testing your rules, Online.