forked from aburch/simutrans
-
Notifications
You must be signed in to change notification settings - Fork 0
/
simintr.cc
275 lines (239 loc) · 6.91 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
/*
* Copyright (c) 1997 - 2001 Hansjörg Malthaner
*
* This file is part of the Simutrans project under the artistic license.
* (see license.txt)
*/
#include <stdio.h>
#include <string.h>
#include "simdebug.h"
#include "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 karte_t *welt_modell = NULL;
static main_view_t *welt_ansicht = NULL;
static uint32 last_time;
static bool enabled = false;
#define FRAME_TIME_MULTI (16)
// pause between two frames
static uint32 frame_time = 36*FRAME_TIME_MULTI;
bool reduce_frame_time()
{
if(frame_time > 10*FRAME_TIME_MULTI) {
frame_time -= 1;
if( frame_time>150*FRAME_TIME_MULTI ) {
frame_time -= 8;
}
return true;
}
else {
frame_time = 10*FRAME_TIME_MULTI;
return false;
}
}
bool increase_frame_time()
{
if(frame_time < 255*FRAME_TIME_MULTI) {
frame_time ++;
return true;
} else {
return false;
}
}
uint32 get_frame_time()
{
return frame_time/FRAME_TIME_MULTI;
}
void set_frame_time(uint32 time)
{
frame_time = clamp( time, 10, 250 )*FRAME_TIME_MULTI;
}
void intr_refresh_display(bool dirty)
{
wasser_t::prepare_for_refresh();
dr_prepare_flush();
welt_ansicht->display( dirty );
if( env_t::player_finance_display_account ) {
win_display_flush( (double)welt_modell->get_active_player()->get_finance()->get_account_balance()/100.0 );
}
else {
win_display_flush( (double)welt_modell->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);
if(enabled) {
static uint32 last_ms = 0;
if( !welt_modell->is_fast_forward() || welt_modell->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)*welt_modell->get_time_multiplier())/16;
if( diff>0 ) {
enabled = false;
last_time = now;
welt_modell->sync_step( diff, !welt_modell->is_fast_forward(), true );
enabled = true;
}
}
last_ms = welt_modell->get_ticks();
}
(void)caller_info;
}
void intr_set(karte_t *welt, main_view_t *view)
{
welt_modell = welt;
welt_ansicht = view;
last_time = dr_time();
enabled = true;
}
/**
* @author Hj. Malthaner
*/
void intr_set_last_time(uint32 time)
{
last_time = time;
}
void intr_disable()
{
enabled = false;
}
void intr_enable()
{
enabled = true;
}
char const *tick_to_string( sint32 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;
// World model might not be initalized if this is called while reading saved windows.
if (welt_modell == NULL) {
return time;
}
sint32 month = welt_modell->get_last_month();
sint32 year = welt_modell->get_last_year();
// calculate right month first
const uint32 ticks_this_month = ticks % welt_modell->ticks_per_world_month;
month += ticks_this_month / welt_modell->ticks_per_world_month;
while( month>11 ) {
month -= 12;
year ++;
}
while( month<0 ) {
month += 12;
year --;
}
uint32 tage, hours, minuten;
if (env_t::show_month > env_t::DATE_FMT_MONTH) {
tage = (((sint64)ticks_this_month*tage_per_month[month]) >> welt_modell->ticks_per_world_month_shift) + 1;
hours = (((sint64)ticks_this_month*tage_per_month[month]) >> (welt_modell->ticks_per_world_month_shift-16));
minuten = (((hours*3) % 8192)*60)/8192;
hours = ((hours*3) / 8192)%24;
}
else {
tage = 0;
hours = (ticks_this_month * 24) >> welt_modell->ticks_per_world_month_shift;
minuten = ((ticks_this_month * 24 * 60) >> welt_modell->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);
// @author hsiegeln - updated to show month
// @author prissi - also show date if desired
// since seasons 0 is always summer for backward compatibility
char const* const date = translator::get_date(year, month, tage, translator::translate(seasons[welt_modell->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;
default:
sprintf(time, "%s %2d:%02dh", date, hours, minuten);
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]) ) >> welt_modell->ticks_per_world_month_shift;
num_days -= ( (welt_modell->get_ticks() % welt_modell->ticks_per_world_month) * (env_t::show_month==env_t::DATE_FMT_MONTH? 3 : tage_per_month[month]) ) >> welt_modell->ticks_per_world_month_shift;
char days[64];
days[0] = 0;
if( num_days!=0 ) {
sprintf( days, "%+i ", num_days );
}
// maybe round minutes
int switchtick = welt_modell->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_GERMAN:
case env_t::DATE_FMT_GERMAN_NO_SEASON:
sprintf(time, "%s%2d:%02dh", days, hours, minuten);
break;
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;
}
case env_t::DATE_FMT_JAPANESE:
case env_t::DATE_FMT_JAPANESE_NO_SEASON:
sprintf(time, "%s%2d:%02dh", days, hours, minuten);
break;
case env_t::DATE_FMT_MONTH:
sprintf(time, "%s%2d:%02dh", days, hours, minuten);
break;
}
}
return time;
}