home · contact · privacy
f8ff0616bc502d683fc63d8d96bc15ab0b69eef3
[plomrogue] / src / client / map.c
1 /* src/client/map.c
2  *
3  * This file is part of PlomRogue. PlomRogue is licensed under the GPL version 3
4  * or any later version. For details on its copyright, license, and warranties,
5  * see the file NOTICE in the root directory of the PlomRogue source package.
6  */
7
8 #include "map.h"
9 #include <stdint.h> /* uint8_t */
10 #include <stdlib.h> /* free() */
11 #include <stdio.h> /* sprintf() */
12 #include <string.h> /* strlen(), strncmp() */
13 #include "../common/try_malloc.h" /* try_malloc() */
14 #include "../common/rexit.h" /* exit_trouble() */
15 //#include "../common/yx_uint8.h" /* yx_uint8 */
16 #include "io.h" /* send() */
17 #include "windows.h" /* struct Win, center_offset(), get_win_by_id() */
18 #include "world.h" /* for global world */
19
20
21
22 extern void map_scroll(char d)
23 {
24     struct Win * win = get_win_by_id('m');
25     uint16_t offset;
26     if (('8' == d || '2' == d) && world.map.length > win->frame_size.y)
27     {
28         offset = center_offset(win->center.y,
29                                world.map.length, win->frame_size.y);
30         win->center.y = offset + (win->frame_size.y / 2);
31         if ('2' == d && win->center.y < world.map.length - 1)
32         {
33             win->center.y++;
34             return;
35         }
36         win->center.y = win->center.y - ('8' == d && win->center.y > 0);
37     }
38     else if (('4' == d || '6' == d) && (world.map.length*2) > win->frame_size.x)
39     {
40         offset = center_offset(win->center.x,
41                                world.map.length*2, win->frame_size.x);
42         win->center.x = offset + (win->frame_size.x / 2);
43         if ('6' == d && win->center.x < (world.map.length * 2) - 1)
44         {
45             win->center.x++;
46             return;
47         }
48         win->center.x = win->center.x - ('4' == d && win->center.x > 0);
49     }
50 }
51
52
53
54 extern void toggle_lookmode()
55 {
56     if (!world.look)
57     {
58         world.look_pos = world.player_pos;
59         world.look = 1;
60     }
61     else
62     {
63         world.look = 0;
64     }
65     query_mapcell();
66 }
67
68
69
70 extern uint8_t lookmode_nav(char * command)
71 {
72     char * prefix = "move_";
73     uint8_t len_pref = strlen(prefix);
74     if (!strncmp(prefix, command, len_pref) && strlen(command) - 1 == len_pref)
75     {
76         uint8_t open_north = world.look_pos.y > 0;
77         uint8_t open_south = world.look_pos.y < world.map.length - 1;
78         uint8_t open_west  = world.look_pos.x > 0;
79         uint8_t open_east  = world.look_pos.x < world.map.length - 1;
80         uint8_t indent     = world.look_pos.y % 2;
81         if      ('s' == command[len_pref])
82         {
83             world.look_pos.x = world.look_pos.x - open_west;
84         }
85         else if ('d' == command[len_pref])
86         {
87             world.look_pos.x = world.look_pos.x + open_east;
88         }
89         else if ('w' == command[len_pref])
90         {
91             world.look_pos.y = world.look_pos.y - open_north;
92             world.look_pos.x = world.look_pos.x - !indent * open_west;
93         }
94         else if ('e' == command[len_pref])
95         {
96             world.look_pos.y = world.look_pos.y - open_north;
97             world.look_pos.x = world.look_pos.x + indent * open_east;
98         }
99         else if ('x' == command[len_pref])
100         {
101             world.look_pos.y = world.look_pos.y + open_south;
102             world.look_pos.x = world.look_pos.x - !indent * open_west;
103         }
104         else if ('c' == command[len_pref])
105         {
106             world.look_pos.y = world.look_pos.y + open_south;
107             world.look_pos.x = world.look_pos.x + indent * open_east;
108         }
109         else
110         {
111             return 0;
112         }
113         query_mapcell();
114         return 1;
115     }
116     return 0;
117 }
118
119
120
121 extern void query_mapcell()
122 {
123     free(world.things_here);
124     world.things_here = NULL;
125     char * stack = "THINGS_HERE";
126     char * stack_query = try_malloc(strlen(stack) +1+3 +1+3 +1, __func__);
127     uint8_t y = world.look ? world.look_pos.y : world.player_pos.y;
128     uint8_t x = world.look ? world.look_pos.x : world.player_pos.x;
129     int test = sprintf(stack_query, "%s %d %d", stack, y, x);
130     exit_trouble(test < 0, __func__, "sprintf");
131     send(stack_query);
132     free(stack_query);
133 }