home · contact · privacy
Replaced dummy function by just passing NULL and checking for it.
[plomrogue] / src / misc.c
index 692198d2d6a581e3cb6109bdfbf771fd1e665770..cff42644ca1abe26ff8f9d4997a03d542aa46168 100644 (file)
@@ -4,11 +4,33 @@
 #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 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)
@@ -16,7 +38,7 @@ uint16_t rrand(char use_seed, uint32_t new_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)
@@ -39,7 +61,7 @@ void update_log (struct World * world, char * text) {
   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) {
@@ -50,7 +72,7 @@ uint16_t center_offset (uint16_t pos, uint16_t mapsize, uint16_t 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");
@@ -59,10 +81,10 @@ void turn_over (struct World * world, char action) {
   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);
@@ -71,24 +93,24 @@ void save_game(struct World * world) {
   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;
@@ -98,8 +120,17 @@ void growshrink_active_window (struct WinMeta * win_meta, char change) {
     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;