home · contact · privacy
Made single World struct a global variable, fitted a lot of code to this change,...
[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 Player struct */
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
9
10
11 extern struct Map init_map()
12 {
13     char * f_name = "init_map()";
14     struct Map map;
15     map.size.x = 64;
16     map.size.y = 64;
17     uint32_t size = map.size.x * map.size.y;
18     map.cells = try_malloc(size, f_name);
19     uint16_t y, x;
20     for (y = 0; y < map.size.y; y++)
21     {
22         for (x = 0; x < map.size.x; x++)
23         {
24             map.cells[(y * map.size.x) + x] = '~';
25         }
26     }
27     map.cells[size / 2 + (map.size.x / 2)] = '.';
28     uint32_t curpos;
29     while (1)
30     {
31         y = rrand() % map.size.y;
32         x = rrand() % map.size.x;
33         curpos = y * map.size.x + x;
34         if ('~' == map.cells[curpos]
35             && ((curpos >= map.size.x && '.' == map.cells[curpos - map.size.x])
36                 || (curpos < map.size.x * (map.size.y-1)
37                      && '.' == map.cells[curpos + map.size.x])
38                 || (curpos > 0 && curpos % map.size.x != 0
39                     && '.' == map.cells[curpos-1])
40                 || (curpos < (map.size.x * map.size.y)
41                     && (curpos+1) % map.size.x != 0
42                     && '.' == map.cells[curpos+1])))
43         {
44             if (y == 0 || y == map.size.y - 1 || x == 0 || x == map.size.x - 1)
45             {
46                 break;
47             }
48             map.cells[y * map.size.x + x] = '.';
49         }
50     }
51     return map;
52 }
53
54
55
56 extern void map_scroll(struct Win * win, struct yx_uint16 map_size, enum dir d)
57 {
58     uint16_t offset;
59     if ((NORTH == d || SOUTH == d) && map_size.y > win->framesize.y)
60     {
61         offset = center_offset(win->center.y, map_size.y, win->framesize.y);
62         win->center.y = offset + (win->framesize.y / 2);
63         if      (NORTH == d && win->center.y > 0)
64         {
65             win->center.y--;
66         }
67         else if (SOUTH == d && win->center.y < map_size.y - 1)
68         {
69             win->center.y++;
70         }
71     }
72     else if ((WEST == d || EAST == d) && map_size.x > win->framesize.x)
73     {
74         offset = center_offset(win->center.x, map_size.x, win->framesize.x);
75         win->center.x = offset + (win->framesize.x / 2);
76         if      (WEST == d && win->center.x > 0)
77         {
78             win->center.x--;
79         }
80         else if (EAST == d && win->center.x < map_size.x - 1)
81         {
82             win->center.x++;
83         }
84     }
85 }