home · contact · privacy
Moved game exiting into its own function.
[plomrogue] / src / misc.c
1 #include "misc.h"
2 #include <stdlib.h>
3 #include <string.h>
4 #include "windows.h"
5 #include "keybindings.h"
6 #include "readwrite.h"
7 #include "map_objects.h"
8 #include "map_object_actions.h"
9 #include "map.h"
10 #include "main.h"
11 #include "yx_uint16.h"
12
13 extern void exit_game(struct World * world, struct Map * map) {
14 // Clean up and exit.
15   endwin();
16   free(map->cells);
17   uint16_t key;
18   for (key = 0; key <= world->keyswindata->max; key++)
19     free(world->keybindings[key].name);
20   free(world->keybindings);
21   free(world->keyswindata);
22   free(world->log);
23   exit (EXIT_SUCCESS); }
24
25 extern void textfile_sizes (FILE * file, uint16_t * linemax_p, uint16_t * n_lines_p) {
26 // Learn largest line length (linemax_p) and (n_lines_p if not set to NULL) number of lines.
27   uint16_t n_lines = 0;
28   int c = 0;
29   uint16_t linemax = 0;
30   uint16_t c_count = 0;
31   while (EOF != c) {
32     c_count++;
33     c = getc(file);
34     if ('\n' == c) {
35       if (c_count > linemax)
36         linemax = c_count + 1;
37       c_count = 0;
38       if (n_lines_p)
39         n_lines++; } }
40   fseek(file, 0, SEEK_SET);
41   * linemax_p = linemax;
42   if (n_lines_p)
43     * n_lines_p = n_lines; }
44
45 extern uint16_t rrand(char use_seed, uint32_t new_seed) {
46 // Pseudo-random number generator (LGC algorithm). Use instead of rand() to ensure portable predictability.
47   static uint32_t seed;
48   if (0 != use_seed)
49     seed = new_seed;
50   seed = ((seed * 1103515245) + 12345) % 2147483648;   // Values as recommended by POSIX.1-2001 (see rand(3)).
51   return (seed / 65536); }                         // Ignore least significant 16 bits (they are less random).
52
53 extern void update_log (struct World * world, char * text) {
54 // Update log by appending text, or by appending a "." if text is the same as the last one.
55   static char * last_msg;
56   if (0 == last_msg)
57     last_msg = calloc(1, sizeof(char));
58   char * new_text;
59   uint16_t len_old = strlen(world->log);
60   if (0 == strcmp(last_msg, text)) {
61     uint16_t len_whole = len_old + 1;
62     new_text = calloc(len_whole + 1, sizeof(char));
63     memcpy(new_text, world->log, len_old);
64     memcpy(new_text + len_old, ".", 1); }
65   else {
66     uint16_t len_new = strlen(text);
67     uint16_t len_whole = len_old + len_new + 1;
68     new_text = calloc(len_whole, sizeof(char));
69     memcpy(new_text, world->log, len_old);
70     memcpy(new_text + len_old, text, len_new);
71     last_msg = calloc(len_new + 1, sizeof(char));
72     memcpy(last_msg, text, len_new); }
73   free(world->log);
74   world->log = new_text; }
75
76 extern uint16_t center_offset (uint16_t pos, uint16_t mapsize, uint16_t framesize) {
77 // Return the offset for display of a map inside a frame centered on pos.
78   uint16_t offset = 0;
79   if (mapsize > framesize) {
80     if (pos > framesize / 2) {
81       if (pos < mapsize - (framesize / 2))
82         offset = pos - (framesize / 2);
83       else
84         offset = mapsize - framesize; } }
85   return offset; }
86
87 extern void turn_over (struct World * world, char action) {
88 // Record action in game record file, increment turn and move enemy.
89   if (1 == world->interactive) {
90     FILE * file = fopen("record", "a");
91     fputc(action, file);
92     fclose(file); }
93   world->turn++;
94   rrand(1, world->seed * world->turn);
95   struct Monster * monster;
96   for (monster = world->monster; monster != 0; monster = monster->map_obj.next)
97     move_monster(world, monster); }
98
99 extern void save_game(struct World * world) {
100 // Save game data to game file.
101   FILE * file = fopen("savefile", "w");
102   write_uint32_bigendian(world->seed, file);
103   write_uint32_bigendian(world->turn, file);
104   write_uint16_bigendian(world->player->pos.y + 1, file);
105   write_uint16_bigendian(world->player->pos.x + 1, file);
106   fputc(world->player->hitpoints, file);
107   write_map_objects (world->monster, file, write_map_objects_monsterdata);
108   write_map_objects (world->item, file, NULL);
109   fclose(file); }
110
111 extern void toggle_window (struct WinMeta * win_meta, struct Win * win) {
112 // Toggle display of window win.
113   if (0 != win->frame.curses_win)
114     suspend_win(win_meta, win);
115   else
116     append_win(win_meta, win); }
117
118 extern void scroll_pad (struct WinMeta * win_meta, char dir) {
119 // Try to scroll pad left or right.
120   if      ('+' == dir)
121     reset_pad_offset(win_meta, win_meta->pad_offset + 1);
122   else if ('-' == dir)
123     reset_pad_offset(win_meta, win_meta->pad_offset - 1); }
124
125 extern void growshrink_active_window (struct WinMeta * win_meta, char change) {
126 // Grow or shrink active window horizontally or vertically by one cell size.
127   if (0 != win_meta->active) {
128     struct yx_uint16 size = win_meta->active->frame.size;
129     if      (change == '-') size.y--;
130     else if (change == '+') size.y++;
131     else if (change == '_') size.x--;
132     else if (change == '*') size.x++;
133     resize_active_win (win_meta, size); } }
134
135 extern struct yx_uint16 find_passable_pos (struct Map * map) {
136 // Return a random passable position on map.
137   struct yx_uint16 pos;
138   for (pos.y = pos.x = 0; 0 == is_passable(map, pos);) {
139       pos.y = rrand(0, 0) % map->size.y;
140       pos.x = rrand(0, 0) % map->size.x; }
141   return pos; }
142
143 extern unsigned char meta_keys(int key, struct World * world, struct WinMeta * win_meta,
144                                struct Win * win_keys, struct Win * win_map, struct Win * win_info,
145                                struct Win * win_log) {
146 // Call some meta program / window management actions dependent on key. Return 1 to signal quitting.
147   if (key == get_action_key(world->keybindings, "quit"))
148     return 1;
149   else if (key == get_action_key(world->keybindings, "scroll pad right"))
150     scroll_pad (win_meta, '+');
151   else if (key == get_action_key(world->keybindings, "scroll pad left"))
152     scroll_pad (win_meta, '-');
153   else if (key == get_action_key(world->keybindings, "toggle keys window"))
154     toggle_window(win_meta, win_keys);
155   else if (key == get_action_key(world->keybindings, "toggle map window"))
156     toggle_window(win_meta, win_map);
157   else if (key == get_action_key(world->keybindings, "toggle info window"))
158     toggle_window(win_meta, win_info);
159   else if (key == get_action_key(world->keybindings, "toggle log window"))
160     toggle_window(win_meta, win_log);
161   else if (key == get_action_key(world->keybindings, "cycle forwards"))
162     cycle_active_win(win_meta, 'n');
163   else if (key == get_action_key(world->keybindings, "cycle backwards"))
164     cycle_active_win(win_meta, 'p');
165   else if (key == get_action_key(world->keybindings, "shift forwards"))
166     shift_active_win(win_meta, 'f');
167   else if (key == get_action_key(world->keybindings, "shift backwards"))
168     shift_active_win(win_meta, 'b');
169   else if (key == get_action_key(world->keybindings, "grow horizontally"))
170     growshrink_active_window(win_meta, '*');
171   else if (key == get_action_key(world->keybindings, "shrink horizontally"))
172     growshrink_active_window(win_meta, '_');
173   else if (key == get_action_key(world->keybindings, "grow vertically"))
174     growshrink_active_window(win_meta, '+');
175   else if (key == get_action_key(world->keybindings, "shrink vertically"))
176     growshrink_active_window(win_meta, '-');
177   else if (key == get_action_key(world->keybindings, "save keys"))
178     save_keybindings(world);
179   else if (key == get_action_key(world->keybindings, "keys nav up"))
180     keyswin_move_selection (world, 'u');
181   else if (key == get_action_key(world->keybindings, "keys nav down"))
182     keyswin_move_selection (world, 'd');
183   else if (key == get_action_key(world->keybindings, "keys mod"))
184     keyswin_mod_key (world, win_meta);
185   else if (key == get_action_key(world->keybindings, "map up"))
186     map_scroll (world->map, NORTH, win_map->frame.size);
187   else if (key == get_action_key(world->keybindings, "map down"))
188     map_scroll (world->map, SOUTH, win_map->frame.size);
189   else if (key == get_action_key(world->keybindings, "map right"))
190     map_scroll (world->map, EAST, win_map->frame.size);
191   else if (key == get_action_key(world->keybindings, "map left"))
192     map_scroll (world->map, WEST, win_map->frame.size);
193   else if (key == get_action_key(world->keybindings, "map center player"))
194     map_center_player (world->map, world->player, win_map->frame.size);
195   return 0; }