File: simmesg.cc

package info (click to toggle)
simutrans 100.0%2Bds1-4
  • links: PTS, VCS
  • area: main
  • in suites: lenny
  • size: 9,776 kB
  • ctags: 9,485
  • sloc: cpp: 72,459; ansic: 5,646; makefile: 450
file content (170 lines) | stat: -rw-r--r-- 3,787 bytes parent folder | download
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
/*
 * Manages all gameplay-related messages of the games
 *
 * Copyright (c) 2005 Markus Pristovsek
 *
 * This file is part of the Simutrans project under the artistic licence.
 * (see licence.txt)
 */

#include "macros.h"
#include "simdebug.h"
#include "simmesg.h"
#include "simticker.h"
#include "simgraph.h"
#include "simcolor.h"
#include "simmem.h"
#include "simwin.h"
#include "utils/simstring.h"
#include "tpl/slist_tpl.h"
#include "gui/messagebox.h"



message_t * message_t::single_instance= NULL;



message_t * message_t::get_instance()
{
	if(single_instance==NULL) {
dbg->fatal("message_t::message_t()","Init without world!");
		single_instance = new message_t(NULL);
	}
	return single_instance;
}



message_t::message_t(karte_t *w)
{
	// only a singe instance of this messenger
	if(single_instance!=NULL) {
DBG_MESSAGE("message_t::message_t()","previous instance %p");
		return;
	}
	single_instance = this;
	welt = w;

	ticker_flags = 0xFF7F;	// everything on the ticker
	win_flags = 0;
	auto_win_flags = 0;
	ignore_flags = 0;
	if(w) {
		win_flags = 256+8;
		auto_win_flags = 128+512;
	}
}


message_t::~message_t()
{
DBG_MESSAGE("message_t::~message_t()","previous instance %p");
	single_instance = NULL;
}


/* get flags for message routing */
void message_t::get_flags( int *t, int *w, int *a, int  *i)
{
	*t = ticker_flags;
	*w = win_flags;
	*a = auto_win_flags;
	*i = ignore_flags;
}



/* set flags for message routing */
void message_t::set_flags( int t, int w, int a, int i)
{
	ticker_flags = t;
	win_flags = w;
	auto_win_flags = a;
	ignore_flags = i;
}


message_t::node* message_t::get_node(unsigned i)
{
	return i < list.count() ? &list.at(i) : NULL;
}


/**
 * Add a message to the message list
 * @param pos    position of the event
 * @param color  message color
 * @param where type of message
 * @param bild images assosiated with message
 * @author prissi
 */
void message_t::add_message(const char *text, koord pos, msg_typ what, uint8 color, image_id bild )
{
DBG_MESSAGE("message_t::add_msg()","%40s (at %i,%i)", text, pos.x, pos.y );

	int art = (1<<what);
	if(art&ignore_flags) {
		// wants us to ignore this completely
		return;
	}

	// correct for player color
	PLAYER_COLOR_VAL colorval=color;
	if(color<8) {
		colorval = PLAYER_FLAG|(welt->gib_spieler(color)->get_player_color1()+0);
	}

	// should we send this message to a ticker? (always done)
	if(art&ticker_flags) {
		ticker::add_msg(text, pos, colorval);
	}

	// we will not add messages two times to the list if it was within the last 20 messages or within last three months
	sint32 now = welt->get_current_month()-2;
	size_t k = 0;
	for (slist_iterator_tpl<node> i(list); k++ < 20 && i.next();) {
		const node& n = i.get_current();
		if (n.time >= now &&
				strcmp(n.msg, text) == 0 &&
				(n.pos.x & 0xFFF0) == (pos.x & 0xFFF0) && // positions need not 100% match ...
				(n.pos.y & 0xFFF0) == (pos.y & 0xFFF0)) {
			// we had exactly this message already
			return;
		}
	}

	// we do not allow messages larger than 256 bytes
	node n;

	tstrncpy(n.msg, text, lengthof(n.msg));
	n.pos = pos;
	n.color = colorval;
	n.time = welt->get_current_month();
	n.bild = bild;

	// insert at the top
	list.insert(n);
	char* p = list.front().msg;
	// should we open an autoclose windows?
	if(art & auto_win_flags) {
		news_window* news;
		if (pos == koord::invalid) {
			news = new news_img(p, bild, colorval);
		} else {
			news = new news_loc(welt, p, pos, colorval);
		}
		create_win(  news, w_time_delete, magic_none );
	}

	// should we open a normal windows?
	if (art & win_flags) {
		news_window* news;
		if (pos == koord::invalid) {
			news = new news_img(p, bild, colorval);
		} else {
			news = new news_loc(welt, p, pos, colorval);
		}
		create_win(-1, -1, news, w_info, magic_none);
	}
}