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