home · contact · privacy
Each map object action now take different numbers of turns to complete. Re-wrote...
[plomrogue] / src / map.c
1 #include "map.h"
2 #include <stdint.h>      /* for uint16_t, uint32_t */
3 #include "misc.h"        /* for try_malloc(), center_offset() */
4 #include "map_objects.h" /* for get_player() */
5 #include "yx_uint16.h"   /* for yx_uint16 and dir enums */
6 #include "rrand.h"       /* for rrand() */
7 #include "windows.h"     /* for struct Win */
8 #include "main.h"        /* for world global */
9 #include "wincontrol.h"  /* for get_win_by_id() */
10
11
12
13 extern struct Map init_map()
14 {
15     char * f_name = "init_map()";
16     struct Map map;
17     map.size.x = 64;
18     map.size.y = 64;
19     uint32_t size = map.size.x * map.size.y;
20     map.cells = try_malloc(size, f_name);
21     uint16_t y, x;
22     for (y = 0; y < map.size.y; y++)
23     {
24         for (x = 0; x < map.size.x; x++)
25         {
26             map.cells[(y * map.size.x) + x] = '~';
27         }
28     }
29     map.cells[size / 2 + (map.size.x / 2)] = '.';
30     uint32_t curpos;
31     while (1)
32     {
33         y = rrand() % map.size.y;
34         x = rrand() % map.size.x;
35         curpos = y * map.size.x + x;
36         if ('~' == map.cells[curpos]
37             && ((curpos >= map.size.x && '.' == map.cells[curpos - map.size.x])
38                 || (curpos < map.size.x * (map.size.y-1)
39                      && '.' == map.cells[curpos + map.size.x])
40                 || (curpos > 0 && curpos % map.size.x != 0
41                     && '.' == map.cells[curpos-1])
42                 || (curpos < (map.size.x * map.size.y)
43                     && (curpos+1) % map.size.x != 0
44                     && '.' == map.cells[curpos+1])))
45         {
46             if (y == 0 || y == map.size.y - 1 || x == 0 || x == map.size.x - 1)
47             {
48                 break;
49             }
50             map.cells[y * map.size.x + x] = '.';
51         }
52     }
53     return map;
54 }
55
56
57
58 extern void map_scroll(char d)
59 {
60     struct Win * win = get_win_by_id('m');
61     uint16_t offset;
62     if (('N' == d || 'S' == d) && world.map->size.y > win->framesize.y)
63     {
64         offset = center_offset(win->center.y,
65                                world.map->size.y, win->framesize.y);
66         win->center.y = offset + (win->framesize.y / 2);
67         if      ('N' == d && win->center.y > 0)
68         {
69             win->center.y--;
70         }
71         else if ('S' == d && win->center.y < world.map->size.y - 1)
72         {
73             win->center.y++;
74         }
75     }
76     else if (('W' == d || 'E' == d) && world.map->size.x > win->framesize.x)
77     {
78         offset = center_offset(win->center.x,
79                                world.map->size.x, win->framesize.x);
80         win->center.x = offset + (win->framesize.x / 2);
81         if      ('W' == d && win->center.x > 0)
82         {
83             win->center.x--;
84         }
85         else if ('E' == d && win->center.x < world.map->size.x - 1)
86         {
87             win->center.x++;
88         }
89     }
90 }
91
92
93
94 extern void map_center()
95 {
96     struct MapObj * player = get_player();
97     struct Win * win_map   = get_win_by_id('m');
98     win_map->center = player->pos;
99 }
100
101
102
103 extern uint8_t is_passable(struct Map * map, struct yx_uint16 pos)
104 {
105     uint8_t passable = 0;
106     if (0 <= pos.x && pos.x < map->size.x && 0 <= pos.y && pos.y < map->size.y)
107     {
108         passable = (('.' == map->cells[pos.y * map->size.x + pos.x]));
109     }
110     return passable;
111 }