forked from Henne/Bright-Eyes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
seg064.cpp
326 lines (276 loc) · 11 KB
/
seg064.cpp
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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
/*
* Rewrite of DSA1 v3.02_de functions of seg064 (harbour_helper)
* Functions rewritten: 6/6 (complete)
*
* Borlandified and identical
* Compiler: Borland C++ 3.1
* Call: BCC.EXE -mlarge -O- -c -1 -Yo seg064.cpp
*/
#include <stdio.h>
#include <string.h>
#include "v302de.h"
#include "seg007.h"
#include "seg028.h"
#if !defined(__BORLANDC__)
namespace M302de {
#endif
/**
* \brief returns a pointer to the name of a ship
*
* \param passage_type eg 0 = longship, 5 = coastal ship, 6 = cutter,7 = fishing boat
* \param nr_ships_created number of ships which have already been created before (for avoiding duplicate ship names)
*/
RealPt get_ship_name(signed char passage_type, signed short nr_ships_created)
/* Function is called in a single place only, which is contained in the code of Original-Bug 23 (see below).
* Therefore, the function could be removed if M302de_ORIGINAL_BUGFIX is activated.
*/
{
signed char done, i;
signed short name;
done = 0;
do {
name = passage_type * 10 + random_schick(10) + 0x29;
done = 1;
/* check if there is already a ship with the same name */
for (i = 0; i < nr_ships_created; i++) {
if (ds_readd(HARBOR_OPTIONS + i * SIZEOF_HARBOR_OPTION + HARBOR_OPTION_SHIP_NAME_PTR)
== host_readd(Real2Host(ds_readd(TX_INDEX)) + name * 4)) {
done = 0;
break;
}
}
} while (!done);
return (RealPt)host_readd(Real2Host(ds_readd(TX_INDEX)) + name * 4);
}
/**
* \brief */
unsigned short prepare_passages(void)
{
signed short prepared;
signed short i;
RealPt ent;
#if !defined(__BORLANDC__)
ent = RealMake(datseg, SEA_ROUTES);
#else
ent = p_datseg + SEA_ROUTES;
#endif
for (i = prepared = 0; i < 45; ent += SIZEOF_SEA_ROUTE, i++) {
if (!host_readbs(Real2Host(ent) + SEA_ROUTE_PASSAGE_TIMER) && /* passage is available today */
(host_readb(Real2Host(ent) + SEA_ROUTE_TOWN_1) == ds_readb(CURRENT_TOWN) ||
(host_readb(Real2Host(ent) + SEA_ROUTE_TOWN_2) == ds_readb(CURRENT_TOWN)))) {
/* prepare an entry of 12 byte for a passage today */
ds_writeb((HARBOR_OPTIONS + HARBOR_OPTION_ROUTE_ID) + prepared * SIZEOF_HARBOR_OPTION, (unsigned char)i);
ds_writed((HARBOR_OPTIONS + HARBOR_OPTION_ROUTE_PTR) + prepared * SIZEOF_HARBOR_OPTION, (Bit32u)ent);
ds_writeb((HARBOR_OPTIONS + HARBOR_OPTION_SHIP_TIMER) + prepared * SIZEOF_HARBOR_OPTION, 0);
ds_writeb((HARBOR_OPTIONS + HARBOR_OPTION_SHIP_TYPE) + prepared * SIZEOF_HARBOR_OPTION, host_readb(Real2Host(ent) + SEA_ROUTE_PASSAGE_TYPE));
#ifndef M302de_ORIGINAL_BUGFIX
/* Original-Bug 23:
* In the function get_ship_name(..), the ship names are created randomly every time the party checks the available ships at a harbour.
* In this way, the names of the ships can (and usually do) change. */
ds_writed(HARBOR_OPTIONS + prepared * SIZEOF_HARBOR_OPTION + HARBOR_OPTION_SHIP_NAME_PTR,
(Bit32u)get_ship_name(host_readb(Real2Host(ent) + SEA_ROUTE_PASSAGE_TYPE), prepared));
#else
/* As a fix, we derive the name from the PASSAGE_PRICE_MOD entry of the SEA_ROUTE, which is created
* randomly once the new passage of the route is set up, and is kept fixed over the lifetime of the passage.
*
* In this way, it may however happen that two ships of the same name are loceted in a harbour
* (which has been avoided in the original random assignment code). But this is a rare event and not be a big issue anyway.
*/
ds_writed(HARBOR_OPTIONS + prepared * SIZEOF_HARBOR_OPTION + HARBOR_OPTION_SHIP_NAME_PTR,
(RealPt)host_readd(Real2Host(ds_readd(TX_INDEX)) + 4 * (host_readb(Real2Host(ent) + SEA_ROUTE_PASSAGE_TYPE) * 10 + (host_readb(Real2Host(ent) + SEA_ROUTE_PASSAGE_PRICE_MOD)) % 10 + 0x2a))
);
#endif
ds_writeb((HARBOR_OPTIONS + HARBOR_OPTION_DESTINATION) + prepared * SIZEOF_HARBOR_OPTION,
host_readb(Real2Host(ent) + SEA_ROUTE_TOWN_1) == ds_readb(CURRENT_TOWN) ?
host_readb(Real2Host(ent) + SEA_ROUTE_TOWN_2) :
host_readb(Real2Host(ent) + SEA_ROUTE_TOWN_1)
); /* store the other town of the connection */
prepared++;
} else {
/* not before 14.00 o'clock */
if (((signed long)ds_readd(DAY_TIMER) > (0x1518 * 14L))
/* only for ships tomorrow */
&& (host_readb(Real2Host(ent) + SEA_ROUTE_PASSAGE_TIMER) == 1)
/* only in this city */
&& ((host_readb(Real2Host(ent) + SEA_ROUTE_TOWN_1) == ds_readb(CURRENT_TOWN))
|| (host_readb(Real2Host(ent) + SEA_ROUTE_TOWN_2) == ds_readb(CURRENT_TOWN))))
{
/* prepare an entry of 12 byte for a passage tomorrow */
ds_writeb((HARBOR_OPTIONS + HARBOR_OPTION_ROUTE_ID) + prepared * SIZEOF_HARBOR_OPTION, (unsigned char)i);
ds_writed((HARBOR_OPTIONS + HARBOR_OPTION_ROUTE_PTR) + prepared * SIZEOF_HARBOR_OPTION, (Bit32u)ent);
ds_writeb((HARBOR_OPTIONS + HARBOR_OPTION_SHIP_TIMER) + prepared * SIZEOF_HARBOR_OPTION, 1);
ds_writeb((HARBOR_OPTIONS + HARBOR_OPTION_SHIP_TYPE) + prepared * SIZEOF_HARBOR_OPTION, host_readb(Real2Host(ent) + SEA_ROUTE_PASSAGE_TYPE));
ds_writed(HARBOR_OPTIONS + prepared * SIZEOF_HARBOR_OPTION + HARBOR_OPTION_SHIP_NAME_PTR,
(Bit32u)get_ship_name(host_readb(Real2Host(ent) + SEA_ROUTE_PASSAGE_TYPE), prepared));
ds_writeb((HARBOR_OPTIONS + HARBOR_OPTION_DESTINATION) + prepared * SIZEOF_HARBOR_OPTION ,
host_readb(Real2Host(ent) + SEA_ROUTE_TOWN_1) == ds_readb(CURRENT_TOWN) ?
host_readb(Real2Host(ent) + SEA_ROUTE_TOWN_2) :
host_readb(Real2Host(ent) + SEA_ROUTE_TOWN_1)
);
prepared++;
}
}
}
return prepared;
}
/**
* \brief calculates the price and prints it to the text buffer
*
* \param price base price per unit of distance
* \param route_pointer pointer to the sea route
* \return a pointer to the text buffer.
*/
RealPt print_passage_price(signed short price, Bit8u *route_ptr)
{
unsigned short di;
if (price != 0) {
/* calc price per distance */
di = ((unsigned char)host_readb(route_ptr + SEA_ROUTE_PASSAGE_PRICE_MOD) * price + 4) / 10;
/* multiply with distance */
di = di * (unsigned char)host_readb(route_ptr + SEA_ROUTE_DISTANCE);
/* round up and divide by 100 */
price = (di + 49) / 100;
/* generate a price string "%d^HELLER" */
sprintf((char*)Real2Host(ds_readd(TEXT_OUTPUT_BUF)),
(char*)p_datseg + SEA_TRAVEL_STR_HELLER, price);
} else {
/* "NICHTS" */
strcpy((char*)Real2Host(ds_readd(TEXT_OUTPUT_BUF)),
(char*)p_datseg + SEA_TRAVEL_STR_NOTHING);
}
ds_writew(SEA_TRAVEL_PASSAGE_PRICE, price);
return (RealPt)ds_readd(TEXT_OUTPUT_BUF);
}
/**
* \brief calculates the travelling time of a ship passage in hours
*
* \param distance the distance
* \param base_speed base speed
* \return the travelling time in hours
*/
unsigned short get_passage_travel_hours(signed short distance, signed short base_speed)
{
Bit32u tmp;
base_speed = (base_speed * 10 + 11) / 24;
/* WEATHER1 = random(6)
* WEATHER2 = random(7) */
ds_writew(SEA_TRAVEL_PASSAGE_SPEED2,
(base_speed * (ds_readw(WEATHER2) + 6) * (ds_readw(WEATHER1) * 15 + 100) + 499L) / 1000L);
tmp = (ds_readws(SEA_TRAVEL_PASSAGE_SPEED2) + 4) / 10; /* the speed of the ship */
if (tmp == 0)
tmp = 1;
tmp = distance / tmp; /* now 'tmp' is the number of travelling hours */
return (unsigned short)tmp;
}
/**
* \brief get destination harbours
*
* \param type 1 = passages next days / 2 = all passages
*/
unsigned short get_next_passages(unsigned short type)
{
Bit8u *entry;
signed short destinations;
signed short i;
entry = p_datseg + SEA_ROUTES;
for (i = destinations = 0; i < 45; entry += SIZEOF_SEA_ROUTE, i++) {
if (type == 1) {
/* check passages in the next two days */
if (host_readb(entry + SEA_ROUTE_PASSAGE_TIMER) == 1 || host_readb(entry + SEA_ROUTE_PASSAGE_TIMER) == 2) {
/* compare town */
if (host_readb(entry + SEA_ROUTE_TOWN_1) == ds_readb(CURRENT_TOWN) ||
host_readb(entry + SEA_ROUTE_TOWN_2) == ds_readb(CURRENT_TOWN))
{
#if !defined(__BORLANDC__)
ds_writeb(HARBOR_OPTIONS + 10 + destinations * SIZEOF_HARBOR_OPTION,
host_readb(entry + SEA_ROUTE_TOWN_1) == ds_readb(CURRENT_TOWN) ?
host_readb(entry + SEA_ROUTE_TOWN_2):
host_readb(entry + SEA_ROUTE_TOWN_1));
#else
((struct passages*)(p_datseg + HARBOR_OPTIONS))[destinations].town =
host_readb(entry + SEA_ROUTE_TOWN_1) == ds_readb(CURRENT_TOWN) ?
host_readb(entry + SEA_ROUTE_TOWN_2):
host_readb(entry + SEA_ROUTE_TOWN_1);
#endif
destinations++;
}
}
} else {
/* compare town */
if (host_readb(entry + SEA_ROUTE_TOWN_1) == ds_readb(CURRENT_TOWN) ||
host_readb(entry + SEA_ROUTE_TOWN_2) == ds_readb(CURRENT_TOWN))
{
#if !defined(__BORLANDC__)
ds_writeb(HARBOR_OPTIONS + 10 + destinations * SIZEOF_HARBOR_OPTION,
host_readb(entry) == ds_readb(CURRENT_TOWN) ?
host_readb(entry + SEA_ROUTE_TOWN_2):
host_readb(entry + SEA_ROUTE_TOWN_1));
#else
((struct passages*)(p_datseg + HARBOR_OPTIONS))[destinations].town =
host_readb(entry + SEA_ROUTE_TOWN_1) == ds_readb(CURRENT_TOWN) ?
host_readb(entry + SEA_ROUTE_TOWN_2):
host_readb(entry + SEA_ROUTE_TOWN_1);
#endif
destinations++;
}
}
}
return destinations;
}
unsigned short passage_arrival(void)
{
signed short tmp;
Bit8u *harbor_ptr;
Bit8u *buildings;
Bit8u *p_sched;
signed short si;
signed short harbor_id;
harbor_id = 0;
harbor_ptr = p_datseg + HARBORS;
p_sched = p_datseg + SEA_ROUTES + ds_readb(SEA_TRAVEL_PASSAGE_ID) * SIZEOF_SEA_ROUTE;
/* write the destination to a global variable (assignement in condition)*/
if ((ds_writew(TRV_DEST_REACHED, host_readb(p_sched + SEA_ROUTE_TOWN_1))) == ds_readbs(CURRENT_TOWN))
ds_writew(TRV_DEST_REACHED, host_readb(p_sched + SEA_ROUTE_TOWN_2));
do {
if (host_readb(harbor_ptr) == ds_readw(TRV_DEST_REACHED)) {
si = 0;
do {
tmp = host_readb(Real2Host(host_readd(harbor_ptr + 2)) + si) - 1;
if (host_readb(p_datseg + SEA_ROUTES + tmp * SIZEOF_SEA_ROUTE + SEA_ROUTE_TOWN_1) == ds_readb(CURRENT_TOWN) ||
host_readb(p_datseg + SEA_ROUTES + tmp * SIZEOF_SEA_ROUTE + SEA_ROUTE_TOWN_2) == ds_readb(CURRENT_TOWN)) {
harbor_id = (unsigned char)host_readb(harbor_ptr + 1);
break;
}
si++;
} while (host_readb(Real2Host(host_readd(harbor_ptr + 2)) + si) != 0xff);
}
/* set pointer to the next structure */
harbor_ptr += 6;
} while (harbor_id == 0 && host_readb(harbor_ptr) != 0xff);
if (harbor_id != 0) {
/* save the old town in tmp */
tmp = (signed char)ds_readb(CURRENT_TOWN);
/* set the new current_town */
ds_writeb(CURRENT_TOWN, ds_readb(TRV_DEST_REACHED));
/* load the area of the new town */
call_load_area(1);
/* search for the harbour in the map */
buildings = p_datseg + LOCATIONS_TAB;
while ((host_readb(buildings + 2) != 0x0b) ||
(host_readb(buildings + 3) != harbor_id)) {
buildings += 6;
}
/* set the position of the party */
si = host_readw(buildings + 4);
ds_writew(ARRIVAL_X_TARGET, (si >> 8) & 0xff);
ds_writew(ARRIVAL_Y_TARGET, si & 0x0f);
ds_writew(ARRIVAL_DIRECTION, (si >> 4) & 0x0f);
/* restore the old town area / TODO: a bit bogus */
ds_writeb(CURRENT_TOWN, (unsigned char)tmp);
call_load_area(1);
}
return 0;
}
#if !defined(__BORLANDC__)
}
#endif