Prefabricating Map Sections

Hands-on Rust — by Herbert Wolverson (84 / 120)

The Pragmatic Programmers
The Pragmatic Programmers

--

👈 Creating Drunkard’s Walk Maps | TOC | Wrap-Up 👉

Random dungeons are great, but sometimes you want to represent something specific. You might have a great idea for part — or even all — of a map. This has been used to great effect in many games. Games as varied as Diablo and Dungeon Crawl Stone Soup procedurally generate a large portion of their content — and intersperse pre-made sections called vaults.

Handmade Dungeons

Create a new file named map_builder/prefab.rs. Add the usual use crate::prelude::* and define your first prefabricated map section:

MoreInterestingDungeons/prefab/src/map_builder/prefab.rs

​ ​use​ ​crate​::​prelude​::*;

​ ​const​ FORTRESS : (&str, i32, i32) = (​"​
​ ​------------​
​ ​---######---​
​ ​---#----#---​
​ ​---#-M--#---​
​ ​-###----###-​
​ ​--M------M--​
​ ​-###----###-​
​ ​---#----#---​
​ ​---#----#---​
​ ​---######---​
​ ​------------​
​ ​"​, 12, 11);

The two variables represent the size of your map section. The real magic is in the FORTRESS string. You’re representing your desired map as characters within the string constant. — represents an open space, and # represents a wall. M represents a monster spawning location. You can design any map section you want — but be sure to set the size appropriately.

Don’t Use Spaces for Floors

--

--

The Pragmatic Programmers
The Pragmatic Programmers

We create timely, practical books and learning resources on classic and cutting-edge topics to help you practice your craft and accelerate your career.