home · contact · privacy
The player is now a map object like any other. All actor contacts now lead to violenc...
[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 struct World;
8
9
10
11 struct Map init_map(struct World * world)
12 {
13     char * f_name = "init_map()";
14     struct Map map;
15     map.size.x = 64;
16     map.size.y = 64;
17     map.offset.x = 0;
18     map.offset.y = 0;
19     uint32_t size = map.size.x * map.size.y;
20     map.cells = try_malloc(size, world, 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 void map_scroll (struct Map * map, enum dir d, struct yx_uint16 win_size)
59 {
60     if      (NORTH == d && map->offset.y > 0)
61     {
62         map->offset.y--;
63     }
64     else if (WEST  == d && map->offset.x > 0)
65     {
66         map->offset.x--;
67     }
68     else if (SOUTH == d && map->offset.y + win_size.y < map->size.y)
69     {
70         map->offset.y++;
71     }
72     else if (EAST  == d && map->offset.x + win_size.x < map->size.x)
73     {
74         map->offset.x++;
75     }
76 }
77
78
79
80 void map_center_object(struct Map * map, struct MapObj * object,
81                        struct yx_uint16 win_size)
82 {
83     map->offset.y = center_offset(object->pos.y, map->size.y, win_size.y);
84     map->offset.x = center_offset(object->pos.x, map->size.x, win_size.x);
85 }