summaryrefslogtreecommitdiff
path: root/src/messages.cc
blob: 071dc66e6a56d4a9de35108c5084c1257ccb6108 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include "messages.hpp"

#include "game.hpp"

#include <fmt/format.h>
#include <string>

s16b Messages::size() const
{
	return buffer.size();
}

message const &Messages::at(int age) const
{
	assert(age >= 0);
	assert(age < size());

	// Age indexes backward through history and is zero-based, so...
	std::size_t i = buffer.size() - 1 - age;

	// Get the message
	return buffer.at(i);
}

void Messages::add(cptr msg, byte color)
{
	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 ((!buffer.empty()) && (buffer.back().text == msg))
	{
		buffer.back().count += 1;
		return;
	}

	// Push onto the end of the buffer.
	message message;
	message.color = color;
	message.count = 1;
	message.text = msg;
	buffer.push_back(message);
}

void Messages::add(message const &m)
{
	buffer.push_back(m);
}