home · contact · privacy
Server: Remove log_help(), this should be serverd by the client.
[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 "io.h" /* send() */
16 #include "world.h" /* for world */
17
18
19
20 extern void toggle_lookmode()
21 {
22     if (!world.look)
23     {
24         world.look_pos = world.player_pos;
25         world.look = 1;
26     }
27     else
28     {
29         world.look = 0;
30     }
31     query_mapcell();
32 }
33
34
35
36 extern uint8_t lookmode_nav(char * command)
37 {
38     char * prefix = "move_";
39     uint8_t len_pref = strlen(prefix);
40     if (!strncmp(prefix, command, len_pref) && strlen(command) - 1 == len_pref)
41     {
42         uint8_t open_north = world.look_pos.y > 0;
43         uint8_t open_south = world.look_pos.y < world.map.length - 1;
44         uint8_t open_west  = world.look_pos.x > 0;
45         uint8_t open_east  = world.look_pos.x < world.map.length - 1;
46         uint8_t indent     = world.look_pos.y % 2;
47         if      ('s' == command[len_pref])
48         {
49             world.look_pos.x = world.look_pos.x - open_west;
50         }
51         else if ('d' == command[len_pref])
52         {
53             world.look_pos.x = world.look_pos.x + open_east;
54         }
55         else if ('w' == command[len_pref])
56         {
57             world.look_pos.y = world.look_pos.y - open_north;
58             world.look_pos.x = world.look_pos.x - !indent * open_west;
59         }
60         else if ('e' == command[len_pref])
61         {
62             world.look_pos.y = world.look_pos.y - open_north;
63             world.look_pos.x = world.look_pos.x + indent * open_east;
64         }
65         else if ('x' == command[len_pref])
66         {
67             world.look_pos.y = world.look_pos.y + open_south;
68             world.look_pos.x = world.look_pos.x - !indent * open_west;
69         }
70         else if ('c' == command[len_pref])
71         {
72             world.look_pos.y = world.look_pos.y + open_south;
73             world.look_pos.x = world.look_pos.x + indent * open_east;
74         }
75         else
76         {
77             return 0;
78         }
79         query_mapcell();
80         return 1;
81     }
82     return 0;
83 }
84
85
86
87 extern void query_mapcell()
88 {
89     free(world.things_here);
90     world.things_here = NULL;
91     char * stack = "THINGS_HERE";
92     char * stack_query = try_malloc(strlen(stack) +1+3 +1+3 +1, __func__);
93     uint8_t y = world.look ? world.look_pos.y : world.player_pos.y;
94     uint8_t x = world.look ? world.look_pos.x : world.player_pos.x;
95     int test = sprintf(stack_query, "%s %d %d", stack, y, x);
96     exit_trouble(test < 0, __func__, "sprintf");
97     send(stack_query);
98     free(stack_query);
99 }