#include "windows.h"
#include "keybindings.h"
#include "readwrite.h"
-#include "objects_on_map.h"
+#include "map_objects.h"
+#include "map_object_actions.h"
#include "map.h"
#include "main.h"
+#include "yx_uint16.h"
-uint16_t rrand(char use_seed, uint32_t new_seed) {
+extern void exit_game(struct World * world, struct Map * map) {
+// Clean up and exit.
+ endwin();
+ free(map->cells);
+ uint16_t key;
+ for (key = 0; key <= world->keyswindata->max; key++)
+ free(world->keybindings[key].name);
+ free(world->keybindings);
+ free(world->keyswindata);
+ free(world->log);
+ exit (EXIT_SUCCESS); }
+
+extern void textfile_sizes (FILE * file, uint16_t * linemax_p, uint16_t * n_lines_p) {
+// Learn largest line length (linemax_p) and (n_lines_p if not set to NULL) number of lines.
+ uint16_t n_lines = 0;
+ int c = 0;
+ uint16_t linemax = 0;
+ uint16_t c_count = 0;
+ while (EOF != c) {
+ c_count++;
+ c = getc(file);
+ if ('\n' == c) {
+ if (c_count > linemax)
+ linemax = c_count + 1;
+ c_count = 0;
+ if (n_lines_p)
+ n_lines++; } }
+ fseek(file, 0, SEEK_SET);
+ * linemax_p = linemax;
+ if (n_lines_p)
+ * n_lines_p = n_lines; }
+
+extern uint16_t rrand(char use_seed, uint32_t new_seed) {
// Pseudo-random number generator (LGC algorithm). Use instead of rand() to ensure portable predictability.
static uint32_t seed;
if (0 != use_seed)
seed = ((seed * 1103515245) + 12345) % 2147483648; // Values as recommended by POSIX.1-2001 (see rand(3)).
return (seed / 65536); } // Ignore least significant 16 bits (they are less random).
-void update_log (struct World * world, char * text) {
+extern void update_log (struct World * world, char * text) {
// Update log by appending text, or by appending a "." if text is the same as the last one.
static char * last_msg;
if (0 == last_msg)
free(world->log);
world->log = new_text; }
-uint16_t center_offset (uint16_t pos, uint16_t mapsize, uint16_t framesize) {
+extern uint16_t center_offset (uint16_t pos, uint16_t mapsize, uint16_t framesize) {
// Return the offset for display of a map inside a frame centered on pos.
uint16_t offset = 0;
if (mapsize > framesize) {
offset = mapsize - framesize; } }
return offset; }
-void turn_over (struct World * world, char action) {
+extern void turn_over (struct World * world, char action) {
// Record action in game record file, increment turn and move enemy.
if (1 == world->interactive) {
FILE * file = fopen("record", "a");
world->turn++;
rrand(1, world->seed * world->turn);
struct Monster * monster;
- for (monster = world->monster; monster != 0; monster = monster->cmo.next)
+ for (monster = world->monster; monster != 0; monster = monster->map_obj.next)
move_monster(world, monster); }
-void save_game(struct World * world) {
+extern void save_game(struct World * world) {
// Save game data to game file.
FILE * file = fopen("savefile", "w");
write_uint32_bigendian(world->seed, file);
write_uint16_bigendian(world->player->pos.x + 1, file);
fputc(world->player->hitpoints, file);
write_map_objects (world->monster, file, write_map_objects_monsterdata);
- write_map_objects (world->item, file, readwrite_map_objects_dummy);
+ write_map_objects (world->item, file, NULL);
fclose(file); }
-void toggle_window (struct WinMeta * win_meta, struct Win * win) {
+extern void toggle_window (struct WinMeta * win_meta, struct Win * win) {
// Toggle display of window win.
if (0 != win->frame.curses_win)
suspend_win(win_meta, win);
else
append_win(win_meta, win); }
-void scroll_pad (struct WinMeta * win_meta, char dir) {
+extern void scroll_pad (struct WinMeta * win_meta, char dir) {
// Try to scroll pad left or right.
if ('+' == dir)
reset_pad_offset(win_meta, win_meta->pad_offset + 1);
else if ('-' == dir)
reset_pad_offset(win_meta, win_meta->pad_offset - 1); }
-void growshrink_active_window (struct WinMeta * win_meta, char change) {
+extern void growshrink_active_window (struct WinMeta * win_meta, char change) {
// Grow or shrink active window horizontally or vertically by one cell size.
if (0 != win_meta->active) {
struct yx_uint16 size = win_meta->active->frame.size;
else if (change == '*') size.x++;
resize_active_win (win_meta, size); } }
-unsigned char meta_keys(int key, struct World * world, struct WinMeta * win_meta, struct Win * win_keys,
- struct Win * win_map, struct Win * win_info, struct Win * win_log) {
+extern struct yx_uint16 find_passable_pos (struct Map * map) {
+// Return a random passable position on map.
+ struct yx_uint16 pos;
+ for (pos.y = pos.x = 0; 0 == is_passable(map, pos);) {
+ pos.y = rrand(0, 0) % map->size.y;
+ pos.x = rrand(0, 0) % map->size.x; }
+ return pos; }
+
+extern unsigned char meta_keys(int key, struct World * world, struct WinMeta * win_meta,
+ struct Win * win_keys, struct Win * win_map, struct Win * win_info,
+ struct Win * win_log) {
// Call some meta program / window management actions dependent on key. Return 1 to signal quitting.
if (key == get_action_key(world->keybindings, "quit"))
return 1;