forked from aburch/simutrans
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimmesg.cc
258 lines (224 loc) · 5.71 KB
/
simmesg.cc
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
/*
* Manages all gameplay-related messages of the games
*
* Copyright (c) 2005 Markus Pristovsek
*
* This file is part of the Simutrans project under the artistic license.
* (see license.txt)
*/
#include "macros.h"
#include "simdebug.h"
#include "simmesg.h"
#include "simticker.h"
#include "display/simgraph.h"
#include "simcolor.h"
#include "gui/simwin.h"
#include "simworld.h"
#include "dataobj/loadsave.h"
#include "dataobj/environment.h"
#include "player/simplay.h"
#include "utils/simstring.h"
#include "tpl/slist_tpl.h"
#include "gui/messagebox.h"
#include <string.h>
void message_t::node::rdwr(loadsave_t *file)
{
file->rdwr_str( msg, lengthof(msg) );
file->rdwr_long( type );
pos.rdwr( file );
file->rdwr_short( color );
file->rdwr_long( time );
if( file->is_loading() ) {
bild = IMG_LEER;
}
}
PLAYER_COLOR_VAL message_t::node::get_player_color(karte_t *welt) const
{
// correct for player color
PLAYER_COLOR_VAL colorval = color;
if( color&PLAYER_FLAG ) {
player_t *player = welt->get_player(color&(~PLAYER_FLAG));
colorval = player ? PLAYER_FLAG+player->get_player_color1()+1 : MN_GREY0;
}
return colorval;
}
message_t::message_t(karte_t *w)
{
welt = w;
ticker_flags = 0xFF7F; // everything on the ticker only
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()
{
clear();
}
void message_t::clear()
{
while (!list.empty()) {
delete list.remove_first();
}
ticker::clear_ticker();
}
/* get flags for message routing */
void message_t::get_message_flags( sint32 *t, sint32 *w, sint32 *a, sint32 *i)
{
*t = ticker_flags;
*w = win_flags;
*a = auto_win_flags;
*i = ignore_flags;
}
/* set flags for message routing */
void message_t::set_message_flags( sint32 t, sint32 w, sint32 a, sint32 i)
{
ticker_flags = t;
win_flags = w;
auto_win_flags = a;
ignore_flags = i;
}
/**
* Add a message to the message list
* @param pos position of the event
* @param color message color
* @param where type of message
* @param bild image associated with message (will be ignored if pos!=koord::invalid)
* @author prissi
*/
void message_t::add_message(const char *text, koord pos, uint16 what_flags, PLAYER_COLOR_VAL color, image_id bild )
{
DBG_MESSAGE("message_t::add_msg()","%40s (at %i,%i)", text, pos.x, pos.y );
sint32 what = what_flags & ~local_flag;
sint32 art = (1<<what);
if( art&ignore_flags ) {
// wants us to ignore this completely
return;
}
/* we will not add traffic jam messages two times to the list
* if it was within the last 20 messages
* or within last months
* and is not a general (BLACK) message
*/
if( what == traffic_jams ) {
sint32 now = welt->get_current_month()-2;
uint32 i = 0;
FOR(slist_tpl<node*>, const iter, list) {
node const& n = *iter;
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;
}
if (++i == 20) break;
}
}
// if no coordinate is provided, there is maybe one in the text message?
// syntax: either @x,y or (x,y)
if (pos == koord::invalid) {
const char *str = text;
// scan until either @ or ( are found
while( *(str += strcspn(str, "@(")) ) {
str += 1;
int x=-1, y=-1;
if (sscanf(str, "%d,%d", &x, &y) == 2) {
if (welt->is_within_limits(x,y)) {
pos.x = x;
pos.y = y;
break; // success
}
}
}
}
// we do not allow messages larger than 256 bytes
node *const n = new node();
tstrncpy(n->msg, text, lengthof(n->msg));
n->type = what_flags;
n->pos = pos;
n->color = color;
n->time = welt->get_current_month();
n->bild = bild;
PLAYER_COLOR_VAL colorval = n->get_player_color(welt);
// should we send this message to a ticker?
if( art&ticker_flags ) {
ticker::add_msg(text, pos, colorval);
}
// insert at the top
list.insert(n);
char* p = list.front()->msg;
// if local flag is set and we are not current player, do not open windows
if( (art&(1<<ai))==0 && (color & PLAYER_FLAG) != 0 && welt->get_active_player_nr() != (color&(~PLAYER_FLAG)) ) {
return;
}
// check if some window has focus
gui_frame_t *old_top = win_get_top();
gui_component_t *focus = win_get_focus();
// should we open a window?
if ( art & (auto_win_flags | win_flags) ) {
news_window* news;
if (pos == koord::invalid) {
news = new news_img(p, bild, colorval);
} else {
news = new news_loc(p, pos, colorval);
}
wintype w_t = art & win_flags ? w_info /* normal window */ : w_time_delete /* autoclose window */;
create_win( news, w_t, magic_none );
}
// restore focus
if( old_top && focus ) {
top_win( old_top, true );
}
}
void message_t::rotate90( sint16 size_w )
{
FOR(slist_tpl<node*>, const i, list) {
i->pos.rotate90(size_w);
}
}
void message_t::rdwr( loadsave_t *file )
{
uint16 msg_count;
if( file->is_saving() ) {
if( env_t::server ) {
// on server: do not save local messages
msg_count = 0;
FOR(slist_tpl<node*>, const i, list) {
if (!(i->type & local_flag)) {
if (++msg_count == 2000) break;
}
}
file->rdwr_short( msg_count );
FOR(slist_tpl<node*>, const i, list) {
if (msg_count == 0) break;
if (!(i->type & local_flag)) {
i->rdwr(file);
msg_count --;
}
}
assert( msg_count == 0 );
}
else {
msg_count = min( 2000u, list.get_count() );
file->rdwr_short( msg_count );
FOR(slist_tpl<node*>, const i, list) {
i->rdwr(file);
if (--msg_count == 0) break;
}
}
}
else {
// loading
clear();
file->rdwr_short(msg_count);
while( (msg_count--)>0 ) {
node *n = new node();
n->rdwr(file);
list.append(n);
}
}
}