~jpgleeson/RustyDagger

ref: dda2373a91bc24bb04e6aa9e61ff48998dc6fe16 RustyDagger/src/main.rs -rw-r--r-- 10.4 KiB
dda2373a — jpgleeson Field of View and Fog of War added. 4 months ago
                                                                                
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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
use tcod::colors::*;
use tcod::console::*;
use tcod::input::Key;
use tcod::input::KeyCode::*;
use tcod::map::{FovAlgorithm, Map as FovMap};
use rand::Rng;
use std::cmp;

const SCREENWIDTH: i32 = 80;
const SCREENHEIGHT: i32 = 50;

const MAPWIDTH:i32 = 80;
const MAPHEIGHT:i32 = 45;

const FPSCAP: i32 = 30;

const COLOUR_DARK_WALL:Color = Color {r:20, g: 20, b: 20};
const COLOUR_DARK_GROUND:Color = Color{r:80, g: 80, b: 80};
const COLOUR_LIGHT_WALL:Color = Color {r: 120, g: 120, b: 120};
const COLOUR_LIGHT_GROUND:Color = Color{r:180, g: 180, b: 180};

const MAXROOMSIZE: i32 = 10;
const MINROOMSIZE: i32 = 4;
const MAXNUMBERROOMS: i32 = 48;
const MAXCAVESTEPS:i32 = 2500;

const FOV_ALGORITHM: FovAlgorithm = FovAlgorithm::Basic;
const FOV_LIGHT_WALLS: bool = true;
const TORCH_RADIUS: i32 = 12;

struct Tcod
{
    root:Root,
    con:Offscreen,
    fov: FovMap,
}

#[derive(Clone, Copy, Debug)]
struct Tile
{
    blocked: bool,
    blockSight: bool,
    explored: bool,
}

impl Tile
{
    pub fn empty() -> Self
    {
        Tile{ blocked: false, blockSight: false, explored: false,}
    }

    pub fn wall() -> Self
    {
        Tile{ blocked: true, blockSight: true, explored: false,}
    }
}

#[derive(Copy, Clone, Debug)]
struct Rectangle
{
    startX: i32,
    startY: i32,
    endX: i32,
    endY: i32,
}

impl Rectangle
{
    pub fn new(x: i32, y: i32, width: i32, height: i32) -> Self
    {
        Rectangle{startX: x, startY: y, endX: x+width, endY: y+height}
    }

    pub fn centre(&self) -> (i32, i32)
    {
        let centreX = (self.startX + self.endX) / 2;
        let centreY = (self.startY + self.endY) / 2;

        (centreX, centreY)
    }

    pub fn intersectsWith(&self, other: Rectangle) -> bool
    {
        (self.startX <= other.endX) && (self.endX >= other.startX) && (self.startY <= other.endY) && (self.endY >= other.startY)
    }
}

#[derive(Debug)]
struct Object
{
    x: i32,
    y: i32,
    char: char,
    colour: Color,
}

impl Object
{
    pub fn new(x: i32, y: i32, char: char, colour: Color) -> Self
    {
        Object{x, y, char, colour}
    }

    pub fn moveBy(&mut self, dx: i32, dy: i32, game: &Game)
    {
        if !game.map[(self.x + dx) as usize][(self.y + dy) as usize].blocked
        {
            self.x += dx;
            self.y += dy;
        }

    }

    pub fn draw(&self, con: &mut dyn Console)
    {
        con.set_default_foreground(self.colour);
        con.put_char(self.x, self.y, self.char, BackgroundFlag::None);
    }
}

type Map = Vec<Vec<Tile>>;

struct Game
{
    map: Map,
}

fn createRoom(room: Rectangle, map: &mut Map)
{
    for x in (room.startX + 1)..room.endX
    {
        for y in (room.startY + 1)..room.endY
        {
            map[x as usize][y as usize] = Tile::empty();
        }
    }
}

fn createCollonadeRoom(room: Rectangle, map: &mut Map)
{
    if (rand::random())
    {
        for x in (room.startX + 1)..room.endX
        {
            for y in (room.startY + 1)..room.endY
            {
                map[x as usize][y as usize] = Tile::empty();
                if y == room.startY + 2
                {
                    if x % 2 == 0
                    {
                        map[x as usize][y as usize] = Tile::wall();
                    }
                } else if y == room.endY - 2
                {
                    if x % 2 == 0
                    {
                        map[x as usize][y as usize] = Tile::wall();
                    }
                }
            }
        }
    }

    else
    {
        for x in (room.startX + 1)..room.endX
        {
            for y in (room.startY + 1)..room.endY
            {
                map[x as usize][y as usize] = Tile::empty();
                if x == room.startX + 2
                {
                    if y % 2 == 0
                    {
                        map[x as usize][y as usize] = Tile::wall();
                    }
                }
                else if x == room.endX - 2
                {
                    if y % 2 == 0
                    {
                        map[x as usize][y as usize] = Tile::wall();
                    }
                }
            }
        }
    }
}

fn createHorizontalTunnel(startX: i32, endX: i32, y: i32, map: &mut Map)
{
    for x in cmp::min(startX, endX)..(cmp::max(startX, endX) + 1)
    {
        map[x as usize][y as usize] = Tile::empty();
    }
}

fn createVerticalTunnel(startY: i32, endY: i32, x: i32, map: &mut Map)
{
    for y in cmp::min(startY, endY)..(cmp::max(startY, endY) + 1)
    {
        map[x as usize][y as usize] = Tile::empty();
    }
}

fn buildCaveMap(player: &mut Object) -> Map
{
    let mut map = vec![vec![Tile::wall(); MAPHEIGHT as usize]; MAPWIDTH as usize];

    let startX:i32 = rand::thread_rng().gen_range((MAPWIDTH/2) - 10, (MAPWIDTH / 2) + 10);
    let startY:i32 = rand::thread_rng().gen_range((MAPHEIGHT/2) - 10, (MAPHEIGHT / 2) + 10);

    let mut x = startX;
    let mut y = startY;

    let npcStep:i32 = rand::thread_rng().gen_range(0, MAXCAVESTEPS);

    for a in 0..MAXCAVESTEPS
    {
        map[x as usize][y as usize] = Tile::empty();

        if a == npcStep
        {
            let NPC:Object = Object::new(x, y, '@', BLUE);
        }

        let nextDirection = rand::thread_rng().gen_range(0, 4);
        if (nextDirection == 0 && x < (MAPWIDTH-1))
        {
            x += 1;
        }
        else if (nextDirection == 1 && x > 1)
        {
            x -= 1;
        }
        else if (nextDirection == 2 && y < (MAPHEIGHT -1))
        {
            y += 1;
        }
        else if (y > 1)
        {
            y -= 1;
        }
        else
        {
            x = startX;
            y = startY;
        }
    }

    player.x = startX;
    player.y = startY;

    map
}

fn buildMap(player: &mut Object) -> Map
{
    let mut map = vec![vec![Tile::wall(); MAPHEIGHT as usize]; MAPWIDTH as usize];
    let mut rooms = vec![];

    for _ in 0..MAXNUMBERROOMS
    {
        let width = rand::thread_rng().gen_range(MINROOMSIZE, MAXROOMSIZE + 1);
        let height = rand::thread_rng().gen_range(MINROOMSIZE, MAXROOMSIZE + 1);

        let x = rand::thread_rng().gen_range(0, MAPWIDTH - width);
        let y = rand::thread_rng().gen_range(0, MAPHEIGHT - height);

        let newRoom = Rectangle::new(x, y, width, height);

        let failed = rooms.iter().any(|otherRoom| newRoom.intersectsWith(*otherRoom));

        if !failed
        {
            if rand::random()
            {
                createRoom(newRoom, &mut map);
            }
            else
            {
                createCollonadeRoom(newRoom, &mut map);
            }

            let (newX, newY) = newRoom.centre();

            if (rooms.is_empty())
            {
                player.x = newX;
                player.y = newY;
            }
            else
            {
                let (previousX, previousY) = rooms[rooms.len() - 1].centre();

                if rand::random()
                {
                    createHorizontalTunnel(previousX, newX, previousY, &mut map);
                    createVerticalTunnel(previousY, newY, newX, &mut map);
                }
                else
                {
                    createVerticalTunnel(previousY, newY, previousX, &mut map);
                    createHorizontalTunnel(previousX, newX, newY, &mut map);
                }
            }


            rooms.push(newRoom);
        }
    }

    map
}

fn main() {
    let root:Root = Root::initializer().font("arial10x10.png", FontLayout::Tcod).font_type(FontType::Greyscale).size(SCREENWIDTH, SCREENHEIGHT).title("Rusty Dagger").init();
    let con = Offscreen::new(MAPWIDTH, MAPHEIGHT);

    let mut tcod = Tcod {root, con: Offscreen::new(MAPWIDTH, MAPHEIGHT), fov: FovMap::new(MAPWIDTH, MAPHEIGHT),};

    tcod::system::set_fps(FPSCAP);

    let player:Object = Object::new(0, 0, '@', RED);

    let NPC:Object = Object::new(10, 15, '@', BLUE);

    let mut objects = [player, NPC];

    let mut game;

    if (rand::random())
    {
        game = Game{ map: buildMap(&mut objects[0]),};
    }
    else
    {
        game = Game{map: buildCaveMap(&mut objects[0]),};
    }

    for y in 0..MAPHEIGHT
    {
        for x in 0..MAPWIDTH
        {
            tcod.fov.set(x, y, !game.map[x as usize][y as usize].blockSight, !game.map[x as usize][y as usize].blocked,);
        }
    }

    let mut lastPlayerPosition = (-1, -1);

    while(!tcod.root.window_closed())
    {
        tcod.con.clear();
        let fov_recompute = lastPlayerPosition != (objects[0].x, objects[0].y);
        render(&mut tcod, &mut game, &objects, fov_recompute);
        tcod.root.flush();

        let player = &mut objects[0];

        lastPlayerPosition = (player.x, player.y);

        let exit = handleInput(&mut tcod, &mut game, player);
        
        if (exit)
        {
            break;
        }
    }
}

fn handleInput(tcod: &mut Tcod, game: &Game, player: &mut Object) -> bool
{
    let key = tcod.root.wait_for_keypress(true);
    match key
    {
        Key {code: Up, ..} => player.moveBy(0, -1, game),
        Key {code: Down, ..} => player.moveBy(0, 1, game),
        Key {code: Right, ..} => player.moveBy(1, 0, game),
        Key {code: Left, ..} => player.moveBy(-1, 0, game),

        _ => {}
    }

    false
}

fn render(tcod: &mut Tcod, game: &mut Game, objects: &[Object], fovRecompute: bool)
{
    if (fovRecompute)
    {
        let player = &objects[0];
        tcod.fov.compute_fov(player.x, player.y, TORCH_RADIUS, FOV_LIGHT_WALLS, FOV_ALGORITHM);
    }

    for y in 0..MAPHEIGHT
    {
        for x in 0..MAPWIDTH
        {
            let visible = tcod.fov.is_in_fov(x, y);
            let wall = game.map[x as usize][y as usize].blockSight;
            let colour = match(visible, wall)
            {
                (false, true) => COLOUR_DARK_WALL,
                (false, false) => COLOUR_LIGHT_WALL,
                (true, true) => COLOUR_DARK_GROUND,
                (true, false) => COLOUR_LIGHT_GROUND,
            };

            let explored = &mut game.map[x as usize][y as usize].explored;
            if visible
            {
                *explored = true;
            }
            if *explored
            {
                tcod.con.set_char_background(x, y, colour, BackgroundFlag::Set);
            }
        }
    }

    for Object in objects
    {
        if (tcod.fov.is_in_fov(Object.x, Object.y))
        {
            Object.draw(&mut tcod.con)
        }
    }

    blit(&tcod.con, (0, 0), (MAPWIDTH, MAPHEIGHT), &mut tcod.root, (0, 0), 1.0, 1.0);
}