forked from aburch/simutrans
-
Notifications
You must be signed in to change notification settings - Fork 0
/
old_blockmanager.cc
254 lines (217 loc) · 6.75 KB
/
old_blockmanager.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
/*
* 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 "simdebug.h"
#include "simworld.h"
#include "player/simplay.h"
#include "simmesg.h"
#include "display/simimg.h"
#include "obj/signal.h"
#include "obj/tunnel.h"
#include "boden/grund.h"
#include "boden/wege/schiene.h"
#include "dataobj/loadsave.h"
#include "dataobj/translator.h"
#include "dataobj/environment.h"
#include "tpl/slist_tpl.h"
#include "old_blockmanager.h"
// only needed for loading old games
class oldsignal_t : public obj_t
{
protected:
uint8 zustand;
uint8 blockend;
uint8 dir;
obj_t::typ type;
public:
oldsignal_t(loadsave_t *file, obj_t::typ type);
/*
* return direction or the state of the traffic light
* @author Hj. Malthaner
*/
ribi_t::ribi get_dir() const { return dir; }
bool ist_blockiert() const {return blockend != 0;}
obj_t::typ get_typ() const { return type; }
void rdwr(loadsave_t *file);
image_id get_image() const { return IMG_LEER; }
};
static slist_tpl <oldsignal_t *> signale;
//------------------------- old blockmanager ----------------------------
// only there to convert old games to 89.02 and higher
// these two routines for compatibility
oldsignal_t::oldsignal_t(loadsave_t *file, obj_t::typ type) : obj_t ()
{
this->type = type;
rdwr(file);
}
void
oldsignal_t::rdwr(loadsave_t *file)
{
if(!file->is_loading()) {
dbg->fatal("oldsignal_t::rdwr()","cannot be saved!");
}
// loading from blockmanager!
obj_t::rdwr(file);
file->rdwr_byte(blockend);
file->rdwr_byte(zustand);
file->rdwr_byte(dir);
}
// now the old block reader
void
old_blockmanager_t::rdwr_block(karte_t *,loadsave_t *file)
{
sint32 count;
short int typ = obj_t::signal;
// signale laden
file->rdwr_long(count);
for(int i=0; i<count; i++) {
// read the old signals (only opurpose of the here
typ=file->rd_obj_id();
oldsignal_t *sig = new oldsignal_t(file, (obj_t::typ)typ);
DBG_MESSAGE("oldsignal_t()","on %i,%i with dir=%i blockend=%i",sig->get_pos().x,sig->get_pos().y,sig->get_dir(),sig->ist_blockiert());
signale.insert( sig );
}
// counters
if(file->get_version()<=88005) {
// old style
sint32 dummy = 0;
file->rdwr_long(dummy);
file->rdwr_long(dummy);
}
else {
sint16 dummy;
file->rdwr_short(dummy);
}
}
void
old_blockmanager_t::rdwr(karte_t *welt, loadsave_t *file)
{
signale.clear();
if(file->get_version()>=89000) {
// nothing to do any more ...
return;
}
assert(file->is_loading());
// this routine just reads the of signal positions
// and converts them to the new type>
sint32 count;
file->rdwr_long(count);
for(int i=0; i<count; i++) {
rdwr_block(welt,file);
}
DBG_MESSAGE("old_blockmanager::rdwr","%d old signals loaded",count);
}
void
old_blockmanager_t::finish_rd(karte_t *welt)
{
DBG_MESSAGE("old_blockmanager::finish_rd()","convert old to new signals" );
char buf[256];
const char *err_text=translator::translate("Error restoring old signal near (%i,%i)!");
int failure=0;
while (!signale.empty()) {
oldsignal_t *os1=signale.remove_first();
oldsignal_t *os2=NULL;
grund_t *gr=welt->lookup(os1->get_pos());
grund_t *to=NULL;
uint8 directions=0;
waytype_t wt=gr->hat_weg(track_wt) ? track_wt : monorail_wt;
if( gr->get_neighbour(to,wt,os1->get_dir()) ) {
FOR(slist_tpl<oldsignal_t*>, const s, signale) {
if (s->get_pos() == to->get_pos()) {
os2 = s;
break;
}
}
if(os2==NULL) {
dbg->error("old_blockmanager_t::finish_rd()","old signal near (%i,%i) is unpaired!",gr->get_pos().x,gr->get_pos().y);
welt->get_message()->add_message(translator::translate("Orphan signal during loading!"),os1->get_pos().get_2d(),message_t::problems);
}
}
else {
dbg->error("old_blockmanager_t::finish_rd()","old signal near (%i,%i) is unpaired!",gr->get_pos().x,gr->get_pos().y);
welt->get_message()->add_message(translator::translate("Orphan signal during loading!"),os1->get_pos().get_2d(),message_t::problems);
}
// remove second signal from list
if(os2) {
signale.remove(os2);
}
// now we should have a pair of signals ... or something was very wrong
grund_t* new_signal_gr = 0;
roadsign_besch_t::types type = roadsign_besch_t::SIGN_SIGNAL;
ribi_t::ribi dir = 0;
// now find out about type and direction
if(os2 && !os2->ist_blockiert()) {
// built the signal here, if possible
grund_t *tmp=to;
to = gr;
gr = tmp;
if(os2->get_typ()==obj_t::old_presignal) {
type = roadsign_besch_t::SIGN_PRE_SIGNAL;
}
else if(os2->get_typ()==obj_t::old_choosesignal) {
type |= roadsign_besch_t::CHOOSE_SIGN;
}
dir = os2->get_dir();
directions = 1;
}
else {
// gr is already the first choice
// so we just have to determine the type
if(os1->get_typ()==obj_t::old_presignal) {
type = roadsign_besch_t::SIGN_PRE_SIGNAL;
}
else if(os1->get_typ()==obj_t::old_choosesignal) {
type |= roadsign_besch_t::CHOOSE_SIGN;
}
}
// take care of one way
if(!os1->ist_blockiert()) {
directions ++;
dir |= os1->get_dir();
}
// now check where we can built best
if(gr->hat_weg(wt) && ribi_t::is_twoway(gr->get_weg(wt)->get_ribi_unmasked())) {
new_signal_gr = gr;
}
if((new_signal_gr==NULL || !os1->ist_blockiert()) && to && to->hat_weg(wt) && ribi_t::is_twoway(to->get_weg(wt)->get_ribi_unmasked())) {
new_signal_gr = to;
}
if(directions==2 && new_signal_gr) {
dir = new_signal_gr->get_weg(wt)->get_ribi_unmasked();
}
// found a suitable location, ribi, signal type => construct
if(new_signal_gr && dir!=0) {
const roadsign_besch_t *sb=roadsign_t::roadsign_search(type,wt,0);
if(sb!=NULL) {
signal_t *sig = new signal_t(new_signal_gr->get_weg(wt)->get_owner(),new_signal_gr->get_pos(),dir,sb);
new_signal_gr->obj_add(sig);
//DBG_MESSAGE("old_blockmanager::finish_rd()","signal restored at %i,%i with dir %i",gr->get_pos().x,gr->get_pos().y,dir);
}
else {
dbg->error("old_blockmanager_t::finish_rd()","no roadsign for way %x with type %d found!",type,wt);
sprintf(buf,err_text,os1->get_pos().x,os1->get_pos().y);
welt->get_message()->add_message(buf,os1->get_pos().get_2d(),message_t::problems);
failure++;
}
}
else {
dbg->warning("old_blockmanager_t::finish_rd()","could not restore old signal near (%i,%i), dir=%i",gr->get_pos().x,gr->get_pos().y,dir);
sprintf(buf,err_text,os1->get_pos().x,os1->get_pos().y);
welt->get_message()->add_message(buf,os1->get_pos().get_2d(),message_t::problems);
failure ++;
}
os1->set_pos(koord3d::invalid);
delete os1;
if(os2) {
os2->set_pos(koord3d::invalid);
delete os2;
}
}
if(failure) {
dbg->warning("old_blockmanager_t::finish_rd()","failed on %d signal pairs.",failure);
}
}