forked from amethyst/bracket-lib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathex14-dwarfmap.rs
340 lines (306 loc) · 11.6 KB
/
ex14-dwarfmap.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
// This the first roguelike-ish example - a walking @. We build a very simple map,
// and you can use the cursor keys to move around a world.
//
// Comments that duplicate previous examples have been removed for brevity.
//////////////////////////////////////////////////////////////
rltk::add_wasm_support!();
use rltk::{
Algorithm3D, BaseMap, Console, DistanceAlg, FastNoise, FractalType, GameState, NoiseType,
Point3, Rltk, RGB,
};
#[derive(PartialEq, Copy, Clone)]
enum TileType {
Wall,
Floor,
Ramp,
RampDown,
OpenSpace,
}
#[derive(PartialEq, Copy, Clone)]
enum Mode {
Waiting,
Moving,
}
struct State {
map: Vec<TileType>,
player_position: usize,
enable_dive: bool,
mode: Mode,
path: rltk::NavigationPath,
}
const WIDTH: i32 = 80;
const HEIGHT: i32 = 50;
const DEPTH: i32 = 128;
const LAYER_SIZE: usize = (WIDTH * HEIGHT) as usize;
const NUM_TILES: usize = LAYER_SIZE * DEPTH as usize;
pub fn xyz_idx(x: i32, y: i32, z: i32) -> usize {
(LAYER_SIZE * z as usize) + (y as usize * WIDTH as usize) + x as usize
}
pub fn idx_xyz(idx: usize) -> (i32, i32, i32) {
let z = (idx / LAYER_SIZE) as i32;
let y = ((idx as i32 - (z * LAYER_SIZE as i32) as i32) / WIDTH as i32) as i32;
let x = ((idx as i32 - (z * LAYER_SIZE as i32) as i32) % WIDTH as i32) as i32;
(x, y, z)
}
impl State {
pub fn new() -> State {
let mut state = State {
map: vec![TileType::OpenSpace; NUM_TILES],
player_position: xyz_idx(40, 19, 127),
enable_dive: true,
mode: Mode::Waiting,
path: rltk::NavigationPath::new(),
};
// Now we noise-generate a world.
// There's nothing special about these numbers; they were picked by
// playing around until I liked the map!
let mut noise = FastNoise::seeded(2);
noise.set_noise_type(NoiseType::SimplexFractal);
noise.set_fractal_type(FractalType::FBM);
noise.set_fractal_octaves(2);
noise.set_fractal_gain(0.2);
noise.set_fractal_lacunarity(1.0);
noise.set_frequency(2.0);
for y in 0..50 {
for x in 0..80 {
let n = noise.get_noise((x as f32) / 200.0, (y as f32) / 100.0);
let altitude = (n + 1.0) * 16.0;
for z in 0..altitude as i32 {
let idx = xyz_idx(x, y, z);
state.map[idx] = TileType::Wall;
state.map[xyz_idx(x, y, z + 1)] = TileType::Floor;
}
}
}
// We look for floor tiles that can become ramps
for y in 1..HEIGHT - 1 {
for x in 1..WIDTH - 1 {
for z in 0..DEPTH - 1 {
let idx = xyz_idx(x, y, z);
if state.map[idx] == TileType::Floor {
// Look to see if we need to ramp it up
if state.map[xyz_idx(x - 1, y, z + 1)] == TileType::Floor
|| state.map[xyz_idx(x + 1, y, z + 1)] == TileType::Floor
|| state.map[xyz_idx(x, y - 1, z + 1)] == TileType::Floor
|| state.map[xyz_idx(x, y + 1, z + 1)] == TileType::Floor
{
state.map[idx] = TileType::Ramp;
state.map[idx + LAYER_SIZE] = TileType::RampDown;
}
}
}
}
}
// Fall from the sky until we hit a floor or ramp
while state.map[state.player_position] != TileType::Floor
&& state.map[state.player_position] != TileType::Ramp
{
state.player_position -= LAYER_SIZE;
}
// We'll return the state with the short-hand
state
}
pub fn is_exit_valid(&self, x: i32, y: i32, z: i32) -> bool {
if x < 1 || x > WIDTH - 1 || y < 1 || y > HEIGHT - 1 || z < 1 || z > LAYER_SIZE as i32 - 1 {
return false;
}
let idx = xyz_idx(x, y, z);
self.map[idx as usize] == TileType::Floor
|| self.map[idx as usize] == TileType::Ramp
|| self.map[idx as usize] == TileType::RampDown
}
}
// Implement the game loop
impl GameState for State {
fn tick(&mut self, ctx: &mut Rltk) {
// Clear the screen
ctx.cls();
let ppos = idx_xyz(self.player_position);
// Iterate the map array, on the current level, rendering tiles. If a tile is open
// space, "dive" downwards and show layers below darkened.
for y in 0..HEIGHT {
for x in 0..WIDTH {
let mut idx = xyz_idx(x, y, ppos.2);
let mut glyph: u8 = rltk::to_cp437('░');
let mut fg = RGB::from_f32(0.0, 0.5, 0.5);
match self.map[idx] {
TileType::Floor => {
glyph = rltk::to_cp437(';');
fg = RGB::from_f32(0.0, 1.0, 0.0);
}
TileType::Wall => {
glyph = rltk::to_cp437('█');
fg = RGB::from_f32(0.5, 0.5, 0.5);
}
TileType::Ramp => {
glyph = rltk::to_cp437('▲');
fg = RGB::from_f32(1., 1., 1.);
}
TileType::RampDown => {
glyph = rltk::to_cp437('▼');
fg = RGB::from_f32(1., 1., 1.);
}
_ => {
if self.enable_dive {
let mut dive = 1;
let mut darken = 0.2;
while dive < 10 {
idx -= LAYER_SIZE;
if idx > 0 && self.map[idx] != TileType::OpenSpace {
match self.map[idx] {
TileType::Floor => {
dive = 100;
glyph = rltk::to_cp437(';');
fg = RGB::from_f32(0.0, 1., 0.0);
}
TileType::Wall => {
dive = 100;
glyph = rltk::to_cp437('█');
fg = RGB::from_f32(0.5, 0.5, 0.5);
}
TileType::Ramp => {
dive = 100;
glyph = rltk::to_cp437('▲');
fg = RGB::from_f32(1., 1., 1.);
}
TileType::RampDown => {
glyph = rltk::to_cp437('▼');
fg = RGB::from_f32(1., 1., 1.);
}
_ => {}
}
}
dive += 1;
darken += 0.1;
}
if dive > 99 {
fg = fg - darken;
};
}
}
}
ctx.set(x, y, fg, RGB::from_f32(0., 0., 0.), glyph);
}
}
// Either render a mouse path or traverse it
if self.mode == Mode::Waiting {
// Render a mouse cursor
let mouse_pos = ctx.mouse_pos();
let mx = mouse_pos.0;
let my = mouse_pos.1;
let mut mz = 1;
for altitude in 1..DEPTH as i32 - 1 {
let idx = xyz_idx(mx, my, altitude);
if self.map[idx] == TileType::Floor {
mz = altitude;
}
}
let mouse_idx = xyz_idx(mx, my, mz);
let player_idx = xyz_idx(ppos.0, ppos.1, ppos.2);
if self.map[mouse_idx as usize] != TileType::Wall
&& self.map[mouse_idx as usize] != TileType::OpenSpace
{
let path = rltk::a_star_search(player_idx as i32, mouse_idx as i32, self);
if path.success {
for loc in path.steps.iter().skip(1) {
let (x, y, _z) = idx_xyz(*loc as usize);
ctx.print_color(
x,
y,
RGB::from_f32(1., 0., 0.),
RGB::from_f32(0., 0., 0.),
"*",
);
}
if ctx.left_click {
self.mode = Mode::Moving;
self.path = path.clone();
}
}
}
} else {
self.player_position = self.path.steps[0] as usize;
self.path.steps.remove(0);
if self.path.steps.is_empty() {
self.mode = Mode::Waiting;
}
}
// Render the player @ symbol
ctx.print_color(
ppos.0,
ppos.1,
RGB::from_f32(1.0, 1.0, 0.0),
RGB::from_f32(0., 0., 0.),
"☺",
);
}
}
impl BaseMap for State {
fn is_opaque(&self, idx: i32) -> bool {
self.map[idx as usize] == TileType::Wall
}
fn get_available_exits(&self, idx: i32) -> Vec<(i32, f32)> {
let mut exits: Vec<(i32, f32)> = Vec::new();
let (x, y, z) = idx_xyz(idx as usize);
// Cardinal directions
if self.is_exit_valid(x - 1, y, z) {
exits.push((idx - 1, 1.0))
};
if self.is_exit_valid(x + 1, y, z) {
exits.push((idx + 1, 1.0))
};
if self.is_exit_valid(x, y - 1, z) {
exits.push((idx - WIDTH, 1.0))
};
if self.is_exit_valid(x, y + 1, z) {
exits.push((idx + WIDTH, 1.0))
};
// Diagonals
if self.is_exit_valid(x - 1, y - 1, z) {
exits.push(((idx - WIDTH) - 1, 1.4));
}
if self.is_exit_valid(x + 1, y - 1, z) {
exits.push(((idx - WIDTH) + 1, 1.4));
}
if self.is_exit_valid(x - 1, y + 1, z) {
exits.push(((idx + WIDTH) - 1, 1.4));
}
if self.is_exit_valid(x + 1, y + 1, z) {
exits.push(((idx + WIDTH) + 1, 1.4));
}
// Up and down for ramps
if self.map[idx as usize] == TileType::Ramp {
exits.push((idx + LAYER_SIZE as i32, 1.4));
}
if self.map[idx as usize] == TileType::RampDown {
exits.push((idx - LAYER_SIZE as i32, 1.4));
}
exits
}
fn get_pathing_distance(&self, idx1: i32, idx2: i32) -> f32 {
let pt1 = idx_xyz(idx1 as usize);
let p1 = Point3::new(pt1.0, pt1.1, pt1.2);
let pt2 = idx_xyz(idx2 as usize);
let p2 = Point3::new(pt2.0, pt2.1, pt2.2);
DistanceAlg::Pythagoras.distance3d(p1, p2)
}
}
impl Algorithm3D for State {
fn point3d_to_index(&self, pt: Point3) -> i32 {
xyz_idx(pt.x, pt.y, pt.z) as i32
}
fn index_to_point3d(&self, idx: i32) -> Point3 {
let i = idx_xyz(idx as usize);
Point3::new(i.0, i.1, i.2)
}
}
fn main() {
let context = Rltk::init_simple8x8(
80,
50,
"RLTK Example 14 - Dwarf Fortress Map Style",
"resources",
);
let gs = State::new();
rltk::main_loop(context, gs);
}