Spielerando

Grid Building Placement

Grid building placement lets the player pick a building, see a preview that follows the cursor and snaps to the grid, and put it down where it fits. The preview turns red outside the grid, over something else, or when coins run short. A building lands whole or not at all.

Also called: Building on a grid, Placement preview, Snap to grid building, Base building grid.

On a miniature model town, a mint-green ghost cottage snaps onto four softly lit grid squares beside two finished cottages, while a red ghost overlapping a boulder and a tree shows it cannot be placed.
Plinthwick Yard, an invented town-building game. Invented game, image generated with AI.

Paste this to your AI

Copy it into Claude, Cursor or ChatGPT. It already has good values and the usual mistakes in it.

Add "Grid Building Placement" to my game.

What it should do: Placement turns a click into a choice about space: where to put the farm, how to leave room to walk, what to squeeze in. The preview tells the player the answer before they commit.

Build it exactly as this state machine. First check that it fits my game, then build it like this:

```mermaid
stateDiagram-v2
  [*] --> idle
  idle: Nothing picked
  ready: Preview fits
  blocked: Preview blocked
  idle --> idle: cursor moves
  idle --> ready: pick, it fits
  idle --> blocked: pick, it does not fit
  ready --> ready: moves, still fits
  ready --> blocked: moves, or just placed
  ready --> idle: put away
  blocked --> blocked: moves, still blocked
  blocked --> ready: moves to a free spot
  blocked --> idle: put away
```

Use these starting values:
- gridWidth: 12 cells
- gridHeight: 8 cells
- cellSize: 32 units
- costPerCell: 10 coins
- refund: 0.5
- startCoins: 200 coins

Watch out for these common mistakes:
- The building lands half a cell off: Tell your AI: centre the building on the cursor. An odd width sits on the cell under the cursor, an even width on the nearest grid line. In maths: floor of the cursor in cells, minus half the width, plus a half.
- Left of the grid counts as the first cell: Tell your AI: always round down, never cut off the fraction, so the area left of or above the grid is cell minus one and the preview is outside.
- Only one cell is checked: Tell your AI: check every cell of the footprint, and say outside the grid before over something.
- Half a building is placed: Tell your AI: check all cells first and write them all afterwards. A refused placement changes nothing.
- The preview says yes and the click says no: Tell your AI: use one function for both, and work the preview out again after every change, including right after placing.
- Taking down a building frees one cell: Tell your AI: give each building a number, and free every cell with that number. Never let the player remove bare ground.

Do not change anything else in the game. Do not claim anything without proof: model what it takes, and show me what your solution rests on and how I can see that it works. If you cannot show something, say you do not know. When you are done, tell me in plain words how to try it.

Or skip this and connect the MCP: your AI then finds all of this by itself.Connect the MCP

What it does

Placement turns a click into a choice about space: where to put the farm, how to leave room to walk, what to squeeze in. The preview tells the player the answer before they commit.

How it runs, as a diagram

Everything it can be in, and what moves it on. Your AI builds from exactly this.

cursor movespick, it fitspick, it does not fitmoves, still fitsmoves, or just placedput awaymoves, still blockedmoves to a free spotput awayNothing pickedPreview fitsPreview blocked
How it runs, as a diagram
  • Nothing picked → Nothing picked: cursor moves
  • Nothing picked → Preview fits: pick, it fits
  • Nothing picked → Preview blocked: pick, it does not fit
  • Preview fits → Preview fits: moves, still fits
  • Preview fits → Preview blocked: moves, or just placed
  • Preview fits → Nothing picked: put away
  • Preview blocked → Preview blocked: moves, still blocked
  • Preview blocked → Preview fits: moves to a free spot
  • Preview blocked → Nothing picked: put away

Good starting values

ValueStart withWhat it changes
gridWidth12 cellsHow many cells across the building area is.
gridHeight8 cellsHow many cells down the building area is.
cellSize32 unitsHow big one cell is on screen, in the same units as the cursor. It decides how finely the cursor can aim.
costPerCell10 coinsWhat each cell of a building costs, so a big building costs more than a small one. The total is rounded to a whole number of coins.
refund0.5The share of the price paid that comes back when a building is taken down. At 1 it is free to change your mind. Lower makes placing a real decision.
startCoins200 coinsWhat the player has at the start. With the cost per cell it sets how many small buildings fit before the money runs out.

What usually goes wrong

The building lands half a cell off

The code rounds the cursor to the nearest cell and then draws the building from its corner. A building two cells wide sits next to the cursor instead of under it, and the preview jumps around when the cursor moves.

Fix: Tell your AI: centre the building on the cursor. An odd width sits on the cell under the cursor, an even width on the nearest grid line. In maths: floor of the cursor in cells, minus half the width, plus a half.

Left of the grid counts as the first cell

The cursor is just left of the grid, at minus 0.4 cells. The code cuts off the fraction instead of rounding down, gets cell 0 and shows a green preview inside the grid.

Fix: Tell your AI: always round down, never cut off the fraction, so the area left of or above the grid is cell minus one and the preview is outside.

Only one cell is checked

The check looks at the cell under the cursor and not at the whole footprint. A large building is placed half on top of a rock or hangs over the edge of the grid.

Fix: Tell your AI: check every cell of the footprint, and say outside the grid before over something.

Half a building is placed

The code writes the building cell by cell and finds the overlap partway through. Half of the building is on the grid, the money is gone or not, and the player cannot remove the leftover cells.

Fix: Tell your AI: check all cells first and write them all afterwards. A refused placement changes nothing.

The preview says yes and the click says no

The preview and the placement use two different checks, written at different times. The player sees green, clicks, and nothing happens, or the other way round.

Fix: Tell your AI: use one function for both, and work the preview out again after every change, including right after placing.

Taking down a building frees one cell

The player clicks a building to remove it. The code clears only the cell that was clicked, and the rest of the building stays as an invisible block with no way to remove it.

Fix: Tell your AI: give each building a number, and free every cell with that number. Never let the player remove bare ground.

How to check it works

Works well with

Common questions

how does grid building placement work in games

The player picks a building and a preview follows the cursor, snapping to the grid. It shows whether the building fits. A click puts the building down if every cell it covers is free and the player can pay.

how do i center a multi-tile building on the cursor

Round the cursor position down to a cell, then move back by half the building's width and add a half. An odd width then sits on the cell under the cursor and an even width on the nearest grid line.

should the player get all the money back when removing a building

Usually not. Half is a good start: it lets the player fix mistakes without making placement free. Give everything back only in a game where you want players to rearrange freely.

what do i tell my ai to add building placement

Paste this: Add building placement on a grid. A preview follows the cursor, centred on it and snapped, and turns red outside the grid, on something else or when coins run short. A click puts the whole building down or does nothing. Taking one down frees all its cells and returns half the price.

Where it comes from

We could not find a reliable origin for this one yet.

Where this mechanic comes from has not been researched yet.

Games are named only as references. Names and marks belong to their owners, and nothing here is endorsed by them.

What was checked

This is what separates the blueprint from a catalogue entry: the values on this page come from code that runs.

Your AI gets the code

Tested example code comes through the Spielerando connection, so your AI can use it right away. How to connect it