forked from aburch/simutrans
/
simintr.cc
312 lines (266 loc) · 7.87 KB
/
simintr.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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
/*
* This file is part of the Simutrans-Extended project under the Artistic License.
* (see LICENSE.txt)
*/
#include <stdio.h>
#include <string.h>
#include "simdebug.h"
#include "sys/simsys.h"
#include "simintr.h"
#include "gui/simwin.h"
#include "player/simplay.h"
#include "player/finance.h"
#include "dataobj/environment.h"
#include "dataobj/translator.h"
#include "simworld.h"
#include "display/simview.h"
#include "boden/wasser.h"
static main_view_t *main_view = NULL;
static long last_time;
static bool enabled = false;
#define FRAME_TIME_MULTI (16)
// pause between two frames
static long frame_time = 36*FRAME_TIME_MULTI;
bool reduce_frame_time()
{
if( frame_time > 150*FRAME_TIME_MULTI ) { // < ~6.6fps
frame_time -= 8;
return true;
}
else if(frame_time > (FRAME_TIME_MULTI*1000)/env_t::max_fps) {
frame_time -= 1;
return true;
}
else {
frame_time = (FRAME_TIME_MULTI*1000)/env_t::max_fps;
return false;
}
}
bool increase_frame_time()
{
if(frame_time > (FRAME_TIME_MULTI*1000)/5) { // < 5 fps
return false;
}
else {
frame_time ++;
return true;
}
}
sint32 get_frame_time()
{
return frame_time/FRAME_TIME_MULTI;
}
void set_frame_time(uint32 time)
{
frame_time = clamp( time, 1000/env_t::max_fps, 1000/env_t::min_fps )*FRAME_TIME_MULTI;
}
void intr_refresh_display(bool dirty)
{
wasser_t::prepare_for_refresh();
dr_prepare_flush();
main_view->display( dirty );
if( env_t::player_finance_display_account ) {
win_display_flush( (double)world()->get_active_player()->get_finance()->get_account_balance()/100.0 );
}
else {
win_display_flush( (double)world()->get_active_player()->get_finance()->get_netwealth()/100.0 );
}
// with a switch statement more types could be supported ...
dr_flush();
}
// debug version with caller information
void interrupt_check(const char* caller_info)
{
DBG_DEBUG4("interrupt_check", "called from (%s)", caller_info);
(void)caller_info;
if( !enabled ) {
return;
}
static uint32 last_ms = 0;
if( !world()->is_fast_forward() || world()->get_ticks() != last_ms ) {
const uint32 now = dr_time();
if((now-last_time)*FRAME_TIME_MULTI < frame_time) {
return;
}
const sint32 diff = (( (sint32)now - (sint32)last_time)*world()->get_time_multiplier())/16;
if( diff>0 ) {
enabled = false;
last_time = now;
world()->sync_step( diff, !world()->is_fast_forward(), true );
// since pause may have been activated in this sync_step
enabled = !world()->is_paused();
}
}
last_ms = world()->get_ticks();
}
void intr_set_view(main_view_t *view)
{
main_view = view;
last_time = dr_time();
enabled = true;
}
void intr_set_last_time(sint32 time)
{
last_time = time;
}
void intr_disable()
{
enabled = false;
}
void intr_enable()
{
enabled = true;
}
char const *tick_to_string( sint64 ticks, bool show_full )
{
static sint32 tage_per_month[12]={31,28,31,30,31,30,31,31,30,31,30,31};
static char const* const seasons[] = { "q2", "q3", "q4", "q1" };
static char time [128];
time[0] = 0;
if (world()== NULL) {
return time;
}
sint32 month = world()->get_last_month();
sint32 year = world()->get_last_year();
// calculate right month first
const uint32 ticks_this_month = ticks % world()->ticks_per_world_month;
month += ticks_this_month / world()->ticks_per_world_month;
while( month>11 ) {
month -= 12;
year ++;
}
while( month<0 ) {
month += 12;
year --;
}
uint32 tage, hours, minuten;
char ticks_as_clock[32], month_as_clock[32];
if (env_t::show_month > env_t::DATE_FMT_MONTH && env_t::show_month < env_t::DATE_FMT_INTERNAL_MINUTE) {
tage = (((sint64)ticks_this_month*tage_per_month[month]) >> world()->ticks_per_world_month_shift) + 1;
hours = (((sint64)ticks_this_month*tage_per_month[month]) >> (world()->ticks_per_world_month_shift-16));
minuten = (((hours*3) % 8192)*60)/8192;
hours = ((hours*3) / 8192)%24;
}
else if (env_t::show_month == env_t::DATE_FMT_INTERNAL_MINUTE || env_t::show_month == env_t::DATE_FMT_JAPANESE_INTERNAL_MINUTE) {
world()->sprintf_ticks(ticks_as_clock, sizeof(ticks_as_clock), ticks_this_month);
world()->sprintf_ticks(month_as_clock, sizeof(month_as_clock), world()->ticks_per_world_month);
tage = hours = minuten = 0;
}
else {
tage = 0;
hours = (ticks_this_month * 24) >> world()->ticks_per_world_month_shift;
minuten = ((ticks_this_month * 24 * 60) >> world()->ticks_per_world_month_shift)%60;
}
if( show_full || env_t::show_month == env_t::DATE_FMT_SEASON ) {
//DBG_MESSAGE("env_t::show_month","%d",env_t::show_month);
// since seasons 0 is always summer for backward compatibility
char const* const date = translator::get_date(year, month, tage, translator::translate(seasons[world()->get_season()]));
switch(env_t::show_month) {
case env_t::DATE_FMT_US:
case env_t::DATE_FMT_US_NO_SEASON: {
uint32 hours_ = hours % 12;
if (hours_ == 0) hours_ = 12;
sprintf(time, "%s %2d:%02d%s", date, hours_, minuten, hours < 12 ? "am" : "pm");
break;
}
case env_t::DATE_FMT_SEASON:
sprintf(time, "%s", date);
break;
case env_t::DATE_FMT_JAPANESE:
case env_t::DATE_FMT_JAPANESE_NO_SEASON:
sprintf(time, "%s %2d:%02d", date, hours, minuten);
break;
default:
sprintf(time, "%s %2d:%02dh", date, hours, minuten);
break;
case env_t::DATE_FMT_INTERNAL_MINUTE:
case env_t::DATE_FMT_JAPANESE_INTERNAL_MINUTE:
sprintf(time, "%s %s/%s", date, ticks_as_clock, month_as_clock);
break;
}
}
else {
if( ticks == 0 ) {
return translator::translate("now");
}
// suppress as much as possible, assuming this is an relative offset to the current month
sint32 num_days = ( ticks * (env_t::show_month==env_t::DATE_FMT_MONTH? 3 : tage_per_month[month]) ) >> world()->ticks_per_world_month_shift;
num_days -= ( (world()->get_ticks() % world()->ticks_per_world_month) * (env_t::show_month==env_t::DATE_FMT_MONTH? 3 : tage_per_month[month]) ) >> world()->ticks_per_world_month_shift;
char days[64];
static struct lang_info_t
{
int language;
const char* day;
const char* days;
int max_strlen;
lang_info_t() : language(-1), day(NULL), days(NULL) {}
void update()
{
int lang = translator::get_language();
if (language != lang)
{
day = translator::translate("day");
days = translator::translate("days");
max_strlen = max(strlen(day), strlen(days));
language = lang;
}
}
} lang_info;
lang_info.update();
switch (num_days) {
case 0:
sprintf( days, " %*s ", lang_info.max_strlen, "");
break;
case 1:
sprintf( days, "+1 %*s ", lang_info.max_strlen, lang_info.day);
break;
default:
sprintf( days, "%+i %*s ", num_days, lang_info.max_strlen, lang_info.days);
break;
}
// maybe round minutes
//int switchtick = world()->ticks_per_world_month_shift;
//if( env_t::show_month == env_t::DATE_FMT_MONTH ) {
// // since a month is then just three days instead of about 30 ...
// switchtick += 3;
//}
//if( switchtick <= 19 ) {
// minuten = ( (minuten + 30 ) / 60 ) * 60;
// hours += minuten /60;
// if( switchtick < 18 ) {
// // four hour intervals
// hours = (hours + 3 ) & 0xFFFFC;
// }
// else if( switchtick == 18 ) {
// // two hour intervals
// hours = (hours + 1 ) & 0xFFFFE;
// }
//}
//else if( switchtick == 20 ) {
// minuten = ( (minuten + 15) / 30 ) * 30;
//}
//else if( switchtick == 21 ) {
// minuten = ( (minuten + 7) / 15 ) * 15;
//}
//else if( switchtick == 22 ) {
minuten = ( (minuten + 2) / 5 ) * 5;
//}
// take care of overflow
hours += (minuten / 60);
minuten %= 60;
hours %= 24;
switch(env_t::show_month) {
case env_t::DATE_FMT_US:
case env_t::DATE_FMT_US_NO_SEASON: {
uint32 hours_ = hours % 12;
if (hours_ == 0) hours_ = 12;
sprintf(time, "%s% 2d:%02d%s", days, hours_, minuten, hours < 12 ? "am" : "pm");
break;
}
default:
sprintf(time, "%s% 2d:%02dh", days, hours, minuten);
break;
}
}
return time;
}