home · contact · privacy
Rewrote update_log () for a comeback of the message repition compression feature...
[plomrogue] / src / roguelike.c
index 021bfe71e4abb51283c5823fb26136604f5a5b1d..ce3f5a45e1629280c86af59b1cf169caf5fbb52a 100644 (file)
@@ -1,3 +1,4 @@
+#include "roguelike.h"
 #include <stdlib.h>
 #include <stdint.h>
 #include <ncurses.h>
@@ -6,7 +7,6 @@
 #include <unistd.h>
 #include "windows.h"
 #include "draw_wins.h"
-#include "roguelike.h"
 #include "keybindings.h"
 #include "readwrite.h"
 #include "actors.h"
@@ -20,14 +20,25 @@ uint16_t rrand(char use_seed, uint32_t new_seed) {
   return (seed / 65536); }                         // Ignore least significant 16 bits (they are less random).
 
 void update_log (struct World * world, char * text) {
-// Update log with new text to be appended.
+// 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)
+    last_msg = calloc(1, sizeof(char));
   char * new_text;
   uint16_t len_old = strlen(world->log);
-  uint16_t len_new = strlen(text);
-  uint16_t len_whole = len_old + len_new + 1;
-  new_text = calloc(len_whole, sizeof(char));
-  memcpy(new_text, world->log, len_old);
-  memcpy(new_text + len_old, text, len_new);
+  if (0 == strcmp(last_msg, text)) {
+    uint16_t len_whole = len_old + 1;
+    new_text = calloc(len_whole + 1, sizeof(char));
+    memcpy(new_text, world->log, len_old);
+    memcpy(new_text + len_old, ".", 1); }
+  else {
+    uint16_t len_new = strlen(text);
+    uint16_t len_whole = len_old + len_new + 1;
+    new_text = calloc(len_whole, sizeof(char));
+    memcpy(new_text, world->log, len_old);
+    memcpy(new_text + len_old, text, len_new);
+    last_msg = calloc(len_new + 1, sizeof(char));
+    memcpy(last_msg, text, len_new); }
   free(world->log);
   world->log = new_text; }
 
@@ -110,17 +121,12 @@ void scroll_pad (struct WinMeta * win_meta, char dir) {
 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) {
-    uint16_t height = win_meta->active->frame.size.y;
-    uint16_t width = win_meta->active->frame.size.x;
-    if      (change == '-')
-      height--;
-    else if (change == '+')
-      height++;
-    else if (change == '_')
-      width--;
-    else if (change == '*')
-      width++;
-    resize_active_win (win_meta, height, width); } }
+    struct yx_uint16 size = win_meta->active->frame.size;
+    if      (change == '-') size.y--;
+    else if (change == '+') size.y++;
+    else if (change == '_') size.x--;
+    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) {
@@ -242,12 +248,12 @@ int main (int argc, char *argv[]) {
   struct Map map = init_map();
   world.map = &map;
   if (1 == world.turn) {
-    for (player.pos.y = player.pos.x = 0; 0 == is_passable(&map, player.pos.y, player.pos.x);) {
+    for (player.pos.y = player.pos.x = 0; 0 == is_passable(&map, player.pos);) {
       player.pos.y = rrand(0, 0) % map.size.y;
       player.pos.x = rrand(0, 0) % map.size.x; }
     struct Monster * monster;
     for (monster = world.monster; monster != 0; monster = monster->next)
-      for (monster->pos.y = monster->pos.x = 0; 0 == is_passable(&map, monster->pos.y, monster->pos.x);) {
+      for (monster->pos.y = monster->pos.x = 0; 0 == is_passable(&map, monster->pos);) {
         monster->pos.y = rrand(0, 0) % map.size.y;
         monster->pos.x = rrand(0, 0) % map.size.x; } }