summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorBardur Arantsson <bardur@scientician.net>2017-06-13 18:24:42 +0200
committerBardur Arantsson <bardur@scientician.net>2017-06-13 18:24:42 +0200
commit554ff9a63d5d9bc64f668a00b6baa9b91dc41306 (patch)
tree6d25a4f53042a4f4154d07fa77921c0ba13b68bd /src
parent0595b2fa946619d29624505f8edc0f9c00146e61 (diff)
Move "messages" to Game struct
Diffstat (limited to 'src')
-rw-r--r--src/birth.cc13
-rw-r--r--src/cmd4.cc23
-rw-r--r--src/game.hpp8
-rw-r--r--src/loadsave.cc8
-rw-r--r--src/melee2.cc7
-rw-r--r--src/messages.cc53
-rw-r--r--src/messages.hpp64
-rw-r--r--src/q_hobbit.cc5
-rw-r--r--src/q_poison.cc7
-rw-r--r--src/q_rand.cc6
-rw-r--r--src/q_shroom.cc4
-rw-r--r--src/q_thrain.cc3
-rw-r--r--src/util.cc16
-rw-r--r--src/xtra1.cc4
14 files changed, 133 insertions, 88 deletions
diff --git a/src/birth.cc b/src/birth.cc
index 2f56ec28..42a004bc 100644
--- a/src/birth.cc
+++ b/src/birth.cc
@@ -2682,6 +2682,7 @@ void player_birth()
{
auto const &st_info = game->edit_data.st_info;
auto &d_info = game->edit_data.d_info;
+ auto &messages = game->messages;
/* Starting index for generated towns */
std::size_t rtown = TOWN_RANDOM;
@@ -2720,11 +2721,11 @@ void player_birth()
add_note_type(NOTE_BIRTH);
/* Note player birth in the message recall */
- message_add(" ", TERM_L_BLUE);
- message_add(" ", TERM_L_BLUE);
- message_add("====================", TERM_L_BLUE);
- message_add(" ", TERM_L_BLUE);
- message_add(" ", TERM_L_BLUE);
+ messages.add(" ", TERM_L_BLUE);
+ messages.add(" ", TERM_L_BLUE);
+ messages.add("====================", TERM_L_BLUE);
+ messages.add(" ", TERM_L_BLUE);
+ messages.add(" ", TERM_L_BLUE);
/* Hack -- outfit the player */
player_outfit();
@@ -2773,7 +2774,7 @@ void player_birth()
if (wizard)
{
- message_add(format("Random dungeon town: d_idx:%d, lev:%d", i, lev), TERM_WHITE);
+ messages.add(format("Random dungeon town: d_idx:%d, lev:%d", i, lev), TERM_WHITE);
}
/* Create the town */
diff --git a/src/cmd4.cc b/src/cmd4.cc
index 3216e339..c4adab54 100644
--- a/src/cmd4.cc
+++ b/src/cmd4.cc
@@ -255,7 +255,9 @@ void do_cmd_change_name()
*/
void do_cmd_message_one()
{
- auto message = message_at(0);
+ auto const &messages = game->messages;
+
+ auto message = messages.at(0);
cptr msg = format("> %s", message.text_with_count().c_str());
@@ -284,21 +286,19 @@ void do_cmd_message_one()
*/
void do_cmd_messages()
{
- int i, j, k, n;
- u32b q;
- int wid, hgt;
+ auto const &messages = game->messages;
/* String to highlight */
std::string shower;
/* Total messages */
- n = message_num();
+ const int n = messages.size();
/* Start on first message */
- i = 0;
+ int i = 0;
/* Start at leftmost edge */
- q = 0;
+ u32b q = 0;
/* Enter "icky" mode */
character_icky = TRUE;
@@ -313,12 +313,15 @@ void do_cmd_messages()
Term_clear();
/* Retrieve current screen size */
+ int wid;
+ int hgt;
Term_get_size(&wid, &hgt);
/* Dump up to 20 (or more in bigscreen) lines of messages */
+ int j;
for (j = 0; (j < (hgt - 4)) && (i + j < n); j++)
{
- auto message = message_at(i + j);
+ auto message = messages.at(i + j);
auto text = message.text_with_count();
auto color = message.color;
@@ -354,7 +357,7 @@ void do_cmd_messages()
prt("[Press 'p' for older, 'n' for newer, ..., or ESCAPE]", hgt - 1, 0);
/* Get a command */
- k = inkey();
+ const auto k = inkey();
/* Exit on Escape */
if (k == ESCAPE) break;
@@ -419,7 +422,7 @@ void do_cmd_messages()
/* Scan messages */
for (z = i + 1; z < n; z++)
{
- auto message = message_at(z);
+ auto message = messages.at(z);
/* Search for it */
if (message.text_with_count().find(finder) != std::string::npos)
diff --git a/src/game.hpp b/src/game.hpp
index 631e16b1..37da1cc0 100644
--- a/src/game.hpp
+++ b/src/game.hpp
@@ -6,11 +6,14 @@
#include "game_edit_data.hpp"
#include "grid.hpp"
#include "h-basic.h"
+#include "messages.hpp"
#include "player_defs.hpp"
#include "random_artifact.hpp"
#include "skill_type.hpp"
#include "wilderness_map.hpp"
+#include <boost/circular_buffer.hpp>
+
/**
* All structures for the game itself.
*/
@@ -54,6 +57,11 @@ struct Game {
std::array<s16b, PY_MAX_LEVEL> player_hp { };
/**
+ * Message buffer.
+ */
+ messages messages { 2048 };
+
+ /**
* Game edit data
*/
GameEditData edit_data;
diff --git a/src/loadsave.cc b/src/loadsave.cc
index a085283d..5968e7c5 100644
--- a/src/loadsave.cc
+++ b/src/loadsave.cc
@@ -1905,8 +1905,10 @@ static void do_message(message &msg, ls_flag_t flag)
*/
static void do_messages(ls_flag_t flag)
{
+ auto &messages = game->messages;
+
/* Save/load number of messages */
- s16b num = message_num();
+ s16b num = messages.size();
do_s16b(&num, flag);
/* Read the messages */
@@ -1916,14 +1918,14 @@ static void do_messages(ls_flag_t flag)
if (flag == ls_flag_t::SAVE)
{
- message = message_at(i);
+ message = messages.at(i);
}
do_message(message, flag);
if (flag == ls_flag_t::LOAD)
{
- message_add(message);
+ messages.add(message);
}
}
}
diff --git a/src/melee2.cc b/src/melee2.cc
index 3ab3cc1c..10807b44 100644
--- a/src/melee2.cc
+++ b/src/melee2.cc
@@ -894,6 +894,8 @@ static void monster_msg(cptr fmt, ...)
void monster_msg_simple(cptr s)
{
+ auto &messages = game->messages;
+
/* Display */
if (options->disturb_other)
{
@@ -901,7 +903,7 @@ void monster_msg_simple(cptr s)
}
else
{
- message_add(s, TERM_WHITE);
+ messages.add(s, TERM_WHITE);
p_ptr->window |= PW_MESSAGE;
}
}
@@ -910,6 +912,7 @@ void cmonster_msg(char a, cptr fmt, ...)
{
va_list vp;
+ auto &messages = game->messages;
char buf[1024];
/* Begin the Varargs Stuff */
@@ -928,7 +931,7 @@ void cmonster_msg(char a, cptr fmt, ...)
}
else
{
- message_add(buf, a);
+ messages.add(buf, a);
p_ptr->window |= PW_MESSAGE;
}
}
diff --git a/src/messages.cc b/src/messages.cc
index 637fa991..ea80451b 100644
--- a/src/messages.cc
+++ b/src/messages.cc
@@ -1,54 +1,41 @@
#include "messages.hpp"
-#include <boost/circular_buffer.hpp>
+#include "game.hpp"
+
#include <fmt/format.h>
#include <string>
-/*
- * OPTION: Maximum number of messages to remember (see "io.c")
- * Default: assume maximal memorization of 2048 total messages
- */
-#define MESSAGE_MAX 2048
-
-/**
- * Circular buffer for the messages
- */
-static boost::circular_buffer<message> *buffer()
-{
- static auto *instance = new boost::circular_buffer<message>(MESSAGE_MAX);
- return instance;
-}
-
-s16b message_num()
+s16b messages::size() const
{
- return buffer()->size();
+ return buffer.size();
}
-message const &message_at(int age)
+message const &messages::at(int age) const
{
assert(age >= 0);
- assert(age < message_num());
+ assert(age < size());
// Age indexes backward through history and is zero-based, so...
- std::size_t i = buffer()->size() - 1 - age;
+ std::size_t i = buffer.size() - 1 - age;
// Get the message
- return buffer()->at(i);
+ return buffer.at(i);
}
-void message_add(cptr str, byte color)
+void messages::add(cptr msg, byte color)
{
- assert(str != nullptr);
-
- // Shorthand to avoid syntactic clutter
- auto buf = buffer();
+ assert(msg != nullptr);
+ add(std::string(msg), color);
+}
+void messages::add(std::string const &msg, byte color)
+{
// If the message is the same as the last message,
// we just increment the counter instead of adding
// the message.
- if ((!buf->empty()) && (buf->back().text == str))
+ if ((!buffer.empty()) && (buffer.back().text == msg))
{
- buf->back().count += 1;
+ buffer.back().count += 1;
return;
}
@@ -56,11 +43,11 @@ void message_add(cptr str, byte color)
message message;
message.color = color;
message.count = 1;
- message.text = str;
- buf->push_back(message);
+ message.text = msg;
+ buffer.push_back(message);
}
-void message_add(message const &message)
+void messages::add(message const &m)
{
- buffer()->push_back(message);
+ buffer.push_back(m);
}
diff --git a/src/messages.hpp b/src/messages.hpp
index 99edd0e2..4c6fb2d3 100644
--- a/src/messages.hpp
+++ b/src/messages.hpp
@@ -3,26 +3,54 @@
#include "h-basic.h"
#include "message.hpp"
-/**
- * Get the current number of messages.
- */
-s16b message_num();
+#include <boost/circular_buffer.hpp>
/**
- * Get message of given age. Age must be
- * in the half-open interval [0, message_num).
- *
- * The reference is only valid as long as
- * no messages are added.
+ * Game message buffer
*/
-message const &message_at(int age);
+struct messages final {
-/**
- * Add a message.
- */
-void message_add(cptr msg, byte color);
+private:
+ boost::circular_buffer<message> buffer;
-/**
- * Add a message.
- */
-void message_add(message const &);
+public:
+
+ /**
+ * Create message buffer with space for given
+ * number of messages.
+ */
+ explicit messages(std::size_t n)
+ : buffer(n)
+ {
+ }
+
+ /**
+ * Get the current number of messages.
+ */
+ s16b size() const;
+
+ /**
+ * Get message of given age. Age must be
+ * in the half-open interval [0, message_num).
+ *
+ * The reference is only valid as long as
+ * no messages are added.
+ */
+ message const &at(int age) const;
+
+ /**
+ * Add a message.
+ */
+ void add(cptr msg, byte color);
+
+ /**
+ * Add a message.
+ */
+ void add(std::string const &msg, byte color);
+
+ /**
+ * Add a message.
+ */
+ void add(message const &);
+
+};
diff --git a/src/q_hobbit.cc b/src/q_hobbit.cc
index 011269eb..655755a3 100644
--- a/src/q_hobbit.cc
+++ b/src/q_hobbit.cc
@@ -1,6 +1,7 @@
#include "q_hobbit.hpp"
#include "cave.hpp"
+#include "game.hpp"
#include "hook_chardump_in.hpp"
#include "hook_chat_in.hpp"
#include "hook_give_in.hpp"
@@ -209,6 +210,8 @@ static bool quest_hobbit_dump_hook(void *, void *in_, void *)
void quest_hobbit_init_hook()
{
+ auto &messages = game->messages;
+
/* Get a level to place the hobbit */
if (!cquest.data[0])
{
@@ -216,7 +219,7 @@ void quest_hobbit_init_hook()
cquest.data[1] = turn;
if (wizard)
{
- message_add(format("Hobbit level %d", cquest.data[0]), TERM_BLUE);
+ messages.add(format("Hobbit level %d", cquest.data[0]), TERM_BLUE);
}
}
diff --git a/src/q_poison.cc b/src/q_poison.cc
index a0d9e025..e7f1c71b 100644
--- a/src/q_poison.cc
+++ b/src/q_poison.cc
@@ -312,12 +312,17 @@ static bool quest_poison_drop_hook(void *, void *in_, void *)
void quest_poison_init_hook()
{
+ auto &messages = game->messages;
+
/* Get a place to place the poison */
if (!cquest.data[1])
{
cquest.data[1] = TRUE;
cquest.data[0] = rand_int(4);
- if (wizard) message_add(format("Wilderness poison %d, %d", wild_locs[cquest.data[0]][0], wild_locs[cquest.data[0]][1]), TERM_BLUE);
+ if (wizard)
+ {
+ messages.add(format("Wilderness poison %d, %d", wild_locs[cquest.data[0]][0], wild_locs[cquest.data[0]][1]), TERM_BLUE);
+ }
}
if ((cquest.status >= QUEST_STATUS_TAKEN) && (cquest.status < QUEST_STATUS_FINISHED))
diff --git a/src/q_rand.cc b/src/q_rand.cc
index e3860dd2..b643ca39 100644
--- a/src/q_rand.cc
+++ b/src/q_rand.cc
@@ -57,6 +57,7 @@ void initialize_random_quests(int n)
{
auto const &d_info = game->edit_data.d_info;
auto &r_info = game->edit_data.r_info;
+ auto &messages = game->messages;
int step, lvl, i, k;
int old_type = dungeon_type;
@@ -173,7 +174,7 @@ void initialize_random_quests(int n)
{
if (wizard)
{
- message_add(format("Could not find quest monster on lvl %d", rl), TERM_RED);
+ messages.add(format("Could not find quest monster on lvl %d", rl), TERM_RED);
}
q_ptr->type = 0;
}
@@ -188,8 +189,7 @@ void initialize_random_quests(int n)
if (wizard)
{
- message_add(format("Quest for %d on lvl %d",
- q_ptr->r_idx, rl), TERM_RED);
+ messages.add(format("Quest for %d on lvl %d", q_ptr->r_idx, rl), TERM_RED);
}
}
diff --git a/src/q_shroom.cc b/src/q_shroom.cc
index f14c7faf..627f2b39 100644
--- a/src/q_shroom.cc
+++ b/src/q_shroom.cc
@@ -319,6 +319,8 @@ static bool quest_shroom_chat_hook(void *, void *in_, void *)
void quest_shroom_init_hook()
{
+ auto &messages = game->messages;
+
/* Get a number of 'shrooms */
if (!cquest.data[1])
{
@@ -326,7 +328,7 @@ void quest_shroom_init_hook()
cquest.data[1] = rand_range(7, 14);
if (wizard)
{
- message_add(format("Shrooms number %d", cquest.data[1]), TERM_BLUE);
+ messages.add(format("Shrooms number %d", cquest.data[1]), TERM_BLUE);
}
}
diff --git a/src/q_thrain.cc b/src/q_thrain.cc
index 025ff01f..7b895695 100644
--- a/src/q_thrain.cc
+++ b/src/q_thrain.cc
@@ -264,13 +264,14 @@ static bool quest_thrain_turn_hook(void *, void *, void *)
void quest_thrain_init_hook()
{
auto const &d_info = game->edit_data.d_info;
+ auto &messages = game->messages;
if (!cquest.data[0])
{
cquest.data[0] = rand_range(d_info[DUNGEON_DOL_GULDUR].mindepth + 1, d_info[DUNGEON_DOL_GULDUR].maxdepth - 1);
if (wizard)
{
- message_add(format("Thrain lvl %d", cquest.data[0]), TERM_BLUE);
+ messages.add(format("Thrain lvl %d", cquest.data[0]), TERM_BLUE);
}
}
if ((cquest.status >= QUEST_STATUS_TAKEN) && (cquest.status < QUEST_STATUS_FINISHED))
diff --git a/src/util.cc b/src/util.cc
index 9190f4a1..9005c04f 100644
--- a/src/util.cc
+++ b/src/util.cc
@@ -1758,15 +1758,15 @@ void display_message(int x, int y, int split, byte color, cptr t)
*/
void cmsg_print(byte color, cptr msg)
{
- static int p = 0;
+ auto &messages = game->messages;
- int n;
- int wid;
+ static int p = 0;
char *t;
char buf[1024];
+ int wid;
Term_get_size(&wid, nullptr);
int lim = wid - 8;
@@ -1774,7 +1774,7 @@ void cmsg_print(byte color, cptr msg)
if (!msg_flag) p = 0;
/* Message Length */
- n = (msg ? strlen(msg) : 0);
+ int n = (msg ? strlen(msg) : 0);
/* Hack -- flush when requested or needed */
if (p && (!msg || ((p + n) > lim)))
@@ -1798,7 +1798,10 @@ void cmsg_print(byte color, cptr msg)
/* Memorize the message */
- if (character_generated) message_add(msg, color);
+ if (character_generated)
+ {
+ messages.add(msg, color);
+ }
/* Handle "auto_more" */
if (options->auto_more)
@@ -1867,9 +1870,6 @@ void cmsg_print(byte color, cptr msg)
/* Display the tail of the message */
display_message(p, 0, n, color, t);
- /* Memorize the tail */
- /* if (character_generated) message_add(t); */
-
/* Window stuff */
if (p_ptr)
{
diff --git a/src/xtra1.cc b/src/xtra1.cc
index 9f12fa3a..7141630e 100644
--- a/src/xtra1.cc
+++ b/src/xtra1.cc
@@ -1186,6 +1186,8 @@ static void fix_player()
*/
void fix_message()
{
+ auto const &messages = game->messages;
+
int j, i;
int w, h;
int x, y;
@@ -1210,7 +1212,7 @@ void fix_message()
/* Dump messages */
for (i = 0; i < h; i++)
{
- auto message = message_at(i);
+ auto message = messages.at(i);
auto text_with_count = message.text_with_count();
/* Dump the message on the appropriate line */