author | tron |
Mon, 03 Jan 2005 08:50:44 +0000 | |
changeset 849 | c6223dbdb202 |
parent 815 | 633faa72f094 |
child 988 | 2482369a61ff |
permissions | -rw-r--r-- |
0 | 1 |
|
2 |
#include "stdafx.h" |
|
3 |
#include "ttd.h" |
|
507
8aa8100b0b22
(svn r815) Include strings.h only in the files which need it.
tron
parents:
193
diff
changeset
|
4 |
#include "table/strings.h" |
0 | 5 |
#include "command.h" |
6 |
#include "player.h" |
|
7 |
#include "gfx.h" |
|
8 |
#include "window.h" |
|
9 |
#include "saveload.h" |
|
10 |
#include "economy.h" |
|
543
e3b43338096b
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
11 |
#include "network.h" |
0 | 12 |
|
13 |
/* p1 = player |
|
14 |
p2 = face |
|
15 |
*/ |
|
16 |
||
17 |
int32 CmdSetPlayerFace(int x, int y, uint32 flags, uint32 p1, uint32 p2) |
|
18 |
{ |
|
19 |
if (flags & DC_EXEC) { |
|
20 |
DEREF_PLAYER(p1)->face = p2; |
|
21 |
MarkWholeScreenDirty(); |
|
22 |
} |
|
23 |
return 0; |
|
24 |
} |
|
25 |
||
26 |
/* p1 = player |
|
27 |
* p2 = color |
|
28 |
*/ |
|
29 |
int32 CmdSetPlayerColor(int x, int y, uint32 flags, uint32 p1, uint32 p2) |
|
30 |
{ |
|
31 |
Player *p,*pp; |
|
32 |
||
33 |
// /* can only set color for itself */ |
|
34 |
// if ( (byte)p1 != _current_player) |
|
35 |
// return CMD_ERROR; |
|
36 |
||
37 |
p = DEREF_PLAYER(p1); |
|
38 |
||
39 |
/* ensure no dups */ |
|
40 |
FOR_ALL_PLAYERS(pp) { |
|
41 |
if (pp->is_active && pp != p && pp->player_color == (byte)p2) |
|
42 |
return CMD_ERROR; |
|
43 |
} |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
84
diff
changeset
|
44 |
|
0 | 45 |
if (flags & DC_EXEC) { |
46 |
_player_colors[p1] = (byte)p2; |
|
47 |
p->player_color = (byte)p2; |
|
48 |
MarkWholeScreenDirty(); |
|
49 |
} |
|
50 |
return 0; |
|
51 |
} |
|
52 |
||
53 |
int32 CmdIncreaseLoan(int x, int y, uint32 flags, uint32 p1, uint32 p2) |
|
54 |
{ |
|
55 |
Player *p; |
|
56 |
int32 size; |
|
57 |
||
58 |
if ( (byte)p1 != _current_player) |
|
59 |
return CMD_ERROR; |
|
60 |
||
61 |
p = DEREF_PLAYER(p1); |
|
62 |
||
63 |
if (p->current_loan >= _economy.max_loan) { |
|
534
17ab2f22ff74
(svn r901) Small step in the process to clean up the DPARAM mess:
tron
parents:
507
diff
changeset
|
64 |
SetDParam(0, _economy.max_loan); |
0 | 65 |
return_cmd_error(STR_702B_MAXIMUM_PERMITTED_LOAN); |
66 |
} |
|
67 |
||
68 |
if (flags & DC_EXEC) { |
|
69 |
if (p2) |
|
70 |
size = _economy.max_loan - p->current_loan; |
|
71 |
else |
|
72 |
size = IS_HUMAN_PLAYER((byte)p1) ? 10000 : 50000; |
|
73 |
||
74 |
p->money64 += size; |
|
75 |
p->current_loan += size; |
|
76 |
UpdatePlayerMoney32(p); |
|
77 |
InvalidatePlayerWindows(p); |
|
78 |
} |
|
79 |
||
80 |
return 0; |
|
81 |
} |
|
82 |
||
83 |
int32 CmdDecreaseLoan(int x, int y, uint32 flags, uint32 p1, uint32 p2) |
|
84 |
{ |
|
85 |
Player *p; |
|
86 |
int32 size; |
|
87 |
if ( (byte)p1 != _current_player) |
|
88 |
return CMD_ERROR; |
|
89 |
||
90 |
p = DEREF_PLAYER(p1); |
|
91 |
||
92 |
if (p->current_loan == 0) |
|
93 |
return_cmd_error(STR_702D_LOAN_ALREADY_REPAYED); |
|
94 |
||
95 |
size = p->current_loan; |
|
96 |
||
97 |
// p2 is true while CTRL is pressed (repay all possible loan, or max money you have) |
|
84
1e0721c29bad
(svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
0
diff
changeset
|
98 |
if (!p2) { |
1e0721c29bad
(svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
0
diff
changeset
|
99 |
if (_patches.ainew_active) |
1e0721c29bad
(svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
0
diff
changeset
|
100 |
size = min(size, 10000); |
1e0721c29bad
(svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
0
diff
changeset
|
101 |
else |
1e0721c29bad
(svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
0
diff
changeset
|
102 |
size = min(size, IS_HUMAN_PLAYER((byte)p1) ? 10000 : 50000); |
1e0721c29bad
(svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
0
diff
changeset
|
103 |
} else { // only repay in chunks of 10K |
0 | 104 |
size = min(size, p->player_money); |
105 |
size = max(size, 10000); |
|
106 |
size -= size % 10000; |
|
107 |
} |
|
108 |
||
109 |
if (p->player_money < size) { |
|
534
17ab2f22ff74
(svn r901) Small step in the process to clean up the DPARAM mess:
tron
parents:
507
diff
changeset
|
110 |
SetDParam(0, size); |
0 | 111 |
return_cmd_error(STR_702E_REQUIRED); |
112 |
} |
|
113 |
||
114 |
if (flags & DC_EXEC) { |
|
115 |
p->money64 -= size; |
|
116 |
p->current_loan -= size; |
|
117 |
UpdatePlayerMoney32(p); |
|
118 |
InvalidatePlayerWindows(p); |
|
119 |
} |
|
120 |
return 0; |
|
121 |
} |
|
122 |
||
123 |
int32 CmdChangeCompanyName(int x, int y, uint32 flags, uint32 p1, uint32 p2) |
|
124 |
{ |
|
125 |
StringID str,old_str; |
|
126 |
Player *p; |
|
127 |
||
543
e3b43338096b
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
128 |
str = AllocateNameUnique((byte*)_decode_parameters, 4); |
0 | 129 |
if (str == 0) |
130 |
return CMD_ERROR; |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
84
diff
changeset
|
131 |
|
0 | 132 |
if (flags & DC_EXEC) { |
133 |
p = DEREF_PLAYER(p1); |
|
134 |
old_str = p->name_1; |
|
135 |
p->name_1 = str; |
|
136 |
DeleteName(old_str); |
|
137 |
MarkWholeScreenDirty(); |
|
138 |
} else { |
|
139 |
DeleteName(str); |
|
140 |
} |
|
141 |
||
142 |
return 0; |
|
143 |
} |
|
144 |
||
145 |
int32 CmdChangePresidentName(int x, int y, uint32 flags, uint32 p1, uint32 p2) |
|
146 |
{ |
|
147 |
StringID str,old_str; |
|
148 |
Player *p; |
|
149 |
||
543
e3b43338096b
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
150 |
str = AllocateNameUnique((byte*)_decode_parameters, 4); |
0 | 151 |
if (str == 0) |
152 |
return CMD_ERROR; |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
84
diff
changeset
|
153 |
|
0 | 154 |
if (flags & DC_EXEC) { |
155 |
p = DEREF_PLAYER(p1); |
|
156 |
old_str = p->president_name_1; |
|
157 |
p->president_name_1 = str; |
|
158 |
DeleteName(old_str); |
|
159 |
||
160 |
if (p->name_1 == STR_SV_UNNAMED) { |
|
161 |
byte *s = " Transport"; |
|
162 |
byte *d = (byte*)_decode_parameters, b; |
|
163 |
d--; do d++; while (*d); |
|
164 |
do *d++ = b = *s++; while(d != (byte*)endof(_decode_parameters) && b != 0); |
|
165 |
DoCommandByTile(0, p1, 0, DC_EXEC, CMD_CHANGE_COMPANY_NAME); |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
84
diff
changeset
|
166 |
} |
0 | 167 |
MarkWholeScreenDirty(); |
168 |
} else { |
|
169 |
DeleteName(str); |
|
170 |
} |
|
171 |
||
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
84
diff
changeset
|
172 |
return 0; |
0 | 173 |
} |
174 |
||
175 |
static void UpdateSignVirtCoords(SignStruct *ss) |
|
176 |
{ |
|
177 |
Point pt = RemapCoords(ss->x, ss->y, ss->z); |
|
534
17ab2f22ff74
(svn r901) Small step in the process to clean up the DPARAM mess:
tron
parents:
507
diff
changeset
|
178 |
SetDParam(0, ss->str); |
0 | 179 |
UpdateViewportSignPos(&ss->sign, pt.x, pt.y - 6, STR_2806); |
180 |
} |
|
181 |
||
182 |
void UpdateAllSignVirtCoords() |
|
183 |
{ |
|
184 |
SignStruct *ss; |
|
185 |
for(ss=_sign_list; ss != endof(_sign_list); ss++) |
|
186 |
if (ss->str != 0) |
|
187 |
UpdateSignVirtCoords(ss); |
|
188 |
||
189 |
} |
|
190 |
||
191 |
static void MarkSignDirty(SignStruct *ss) |
|
192 |
{ |
|
193 |
MarkAllViewportsDirty( |
|
194 |
ss->sign.left-6, |
|
195 |
ss->sign.top-3, |
|
196 |
ss->sign.left+ss->sign.width_1*4+12, |
|
197 |
ss->sign.top + 45 |
|
198 |
); |
|
199 |
} |
|
200 |
||
201 |
||
202 |
int32 CmdPlaceSign(int x, int y, uint32 flags, uint32 p1, uint32 p2) |
|
203 |
{ |
|
204 |
SignStruct *ss; |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
84
diff
changeset
|
205 |
|
0 | 206 |
for(ss=_sign_list; ss != endof(_sign_list); ss++) { |
207 |
if (ss->str == 0) { |
|
208 |
if (flags & DC_EXEC) { |
|
209 |
ss->str = STR_280A_SIGN; |
|
210 |
ss->x = x; |
|
211 |
ss->y = y; |
|
212 |
ss->z = GetSlopeZ(x,y); |
|
213 |
UpdateSignVirtCoords(ss); |
|
214 |
MarkSignDirty(ss); |
|
215 |
_new_sign_struct = ss; |
|
216 |
} |
|
217 |
return 0; |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
84
diff
changeset
|
218 |
} |
0 | 219 |
} |
220 |
||
221 |
return_cmd_error(STR_2808_TOO_MANY_SIGNS); |
|
222 |
} |
|
223 |
||
224 |
// p1 = sign index |
|
225 |
// p2: 1 -> remove sign |
|
226 |
int32 CmdRenameSign(int x, int y, uint32 flags, uint32 p1, uint32 p2) |
|
227 |
{ |
|
228 |
StringID str,old_str; |
|
229 |
SignStruct *ss; |
|
230 |
||
231 |
if (_decode_parameters[0] != 0 && !p2) { |
|
543
e3b43338096b
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
232 |
str = AllocateNameUnique((byte*)_decode_parameters, 0); |
0 | 233 |
if (str == 0) |
234 |
return CMD_ERROR; |
|
235 |
||
236 |
if (flags & DC_EXEC) { |
|
237 |
ss = &_sign_list[p1]; |
|
238 |
MarkSignDirty(ss); |
|
239 |
DeleteName(ss->str); |
|
240 |
ss->str = str; |
|
241 |
UpdateSignVirtCoords(ss); |
|
242 |
MarkSignDirty(ss); |
|
243 |
} else { |
|
244 |
DeleteName(str); |
|
245 |
} |
|
246 |
} else { |
|
247 |
if (flags & DC_EXEC) { |
|
248 |
ss = &_sign_list[p1]; |
|
249 |
old_str = ss->str; |
|
250 |
ss->str = 0; |
|
251 |
DeleteName(old_str); |
|
252 |
MarkSignDirty(ss); |
|
253 |
} |
|
254 |
} |
|
255 |
return 0; |
|
256 |
} |
|
257 |
||
258 |
// p1 = 0 decrease pause counter |
|
259 |
// p1 = 1 increase pause counter |
|
260 |
int32 CmdPause(int x, int y, uint32 flags, uint32 p1, uint32 p2) |
|
261 |
{ |
|
262 |
if (flags & DC_EXEC) { |
|
263 |
_pause += p1?1:-1; |
|
264 |
if(_pause==(byte)-1) _pause = 0; |
|
265 |
InvalidateWindow(WC_STATUS_BAR, 0); |
|
266 |
InvalidateWindow(WC_MAIN_TOOLBAR, 0); |
|
267 |
} |
|
268 |
return 0; |
|
269 |
} |
|
270 |
||
271 |
||
272 |
||
273 |
int32 CmdResume(int x, int y, uint32 flags, uint32 p1, uint32 p2) |
|
274 |
{ |
|
275 |
return 0; |
|
276 |
} |
|
277 |
||
278 |
int32 CmdMoneyCheat(int x, int y, uint32 flags, uint32 p1, uint32 p2) |
|
279 |
{ |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
84
diff
changeset
|
280 |
SET_EXPENSES_TYPE(EXPENSES_OTHER); |
0 | 281 |
return (int32)p1; |
282 |
} |
|
283 |
||
543
e3b43338096b
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
284 |
int32 CmdGiveMoney(int x, int y, uint32 flags, uint32 p1, uint32 p2) |
e3b43338096b
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
285 |
{ |
e3b43338096b
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
286 |
SET_EXPENSES_TYPE(EXPENSES_OTHER); |
e3b43338096b
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
287 |
|
815
633faa72f094
(svn r1286) -Fix: oeps, I clamp'd some numbers wrong :$
truelight
parents:
813
diff
changeset
|
288 |
p1 = clamp(p1, 0, 0xFFFFFF); // Clamp between 16 million and 0 |
813
90d6adb8d4ad
(svn r1284) -Fix: Fixed 'money-cheat' (read: bug which could give people a lot of money)
truelight
parents:
543
diff
changeset
|
289 |
|
90d6adb8d4ad
(svn r1284) -Fix: Fixed 'money-cheat' (read: bug which could give people a lot of money)
truelight
parents:
543
diff
changeset
|
290 |
if (p1 == 0) |
90d6adb8d4ad
(svn r1284) -Fix: Fixed 'money-cheat' (read: bug which could give people a lot of money)
truelight
parents:
543
diff
changeset
|
291 |
return CMD_ERROR; |
90d6adb8d4ad
(svn r1284) -Fix: Fixed 'money-cheat' (read: bug which could give people a lot of money)
truelight
parents:
543
diff
changeset
|
292 |
|
543
e3b43338096b
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
293 |
if (flags & DC_EXEC) { |
e3b43338096b
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
294 |
// Add money to player |
e3b43338096b
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
295 |
byte old_cp = _current_player; |
e3b43338096b
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
296 |
_current_player = p2; |
e3b43338096b
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
297 |
SubtractMoneyFromPlayer(-(int32)p1); |
e3b43338096b
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
298 |
_current_player = old_cp; |
e3b43338096b
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
299 |
} |
e3b43338096b
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
300 |
|
e3b43338096b
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
301 |
// Subtract money from local-player |
e3b43338096b
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
302 |
return (int32)p1; |
e3b43338096b
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
303 |
} |
e3b43338096b
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
304 |
|
0 | 305 |
int32 CmdChangeDifficultyLevel(int x, int y, uint32 flags, uint32 p1, uint32 p2) |
306 |
{ |
|
307 |
if (flags & DC_EXEC) { |
|
308 |
if (p1 != (uint32)-1L) { |
|
309 |
((int*)&_opt_mod_ptr->diff)[p1] = p2; |
|
310 |
_opt_mod_ptr->diff_level = 3; |
|
311 |
} else { |
|
312 |
_opt_mod_ptr->diff_level = p2; |
|
313 |
} |
|
543
e3b43338096b
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
314 |
// If we are a network-client, update the difficult setting (if it is open) |
e3b43338096b
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
315 |
if (_networking && !_network_server && FindWindowById(WC_GAME_OPTIONS, 0) != NULL) |
e3b43338096b
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
316 |
memcpy(&_opt_mod_temp, _opt_mod_ptr, sizeof(GameOptions)); |
0 | 317 |
InvalidateWindow(WC_GAME_OPTIONS, 0); |
318 |
} |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
84
diff
changeset
|
319 |
return 0; |
0 | 320 |
} |
321 |
||
322 |
static const byte _sign_desc[] = { |
|
323 |
SLE_VAR(SignStruct,str, SLE_UINT16), |
|
849
c6223dbdb202
(svn r1330) Increase size of some vars from int16 to int32 to guard against future overflows
tron
parents:
815
diff
changeset
|
324 |
SLE_CONDVAR(SignStruct,x, SLE_FILE_I16 | SLE_VAR_I32, 0, 4), |
c6223dbdb202
(svn r1330) Increase size of some vars from int16 to int32 to guard against future overflows
tron
parents:
815
diff
changeset
|
325 |
SLE_CONDVAR(SignStruct,y, SLE_FILE_I16 | SLE_VAR_I32, 0, 4), |
c6223dbdb202
(svn r1330) Increase size of some vars from int16 to int32 to guard against future overflows
tron
parents:
815
diff
changeset
|
326 |
SLE_CONDVAR(SignStruct,x, SLE_INT32, 5, 255), |
c6223dbdb202
(svn r1330) Increase size of some vars from int16 to int32 to guard against future overflows
tron
parents:
815
diff
changeset
|
327 |
SLE_CONDVAR(SignStruct,y, SLE_INT32, 5, 255), |
0 | 328 |
SLE_VAR(SignStruct,z, SLE_UINT8), |
329 |
SLE_END() |
|
330 |
}; |
|
331 |
||
332 |
static void Save_SIGN() |
|
333 |
{ |
|
334 |
SignStruct *s; |
|
335 |
int i; |
|
336 |
for(i=0,s=_sign_list; i!=lengthof(_sign_list); i++,s++) { |
|
337 |
if (s->str != 0) { |
|
338 |
SlSetArrayIndex(i); |
|
339 |
SlObject(s, _sign_desc); |
|
340 |
} |
|
341 |
} |
|
342 |
} |
|
343 |
||
344 |
static void Load_SIGN() |
|
345 |
{ |
|
346 |
int index; |
|
347 |
while ((index = SlIterateArray()) != -1) { |
|
348 |
SlObject(&_sign_list[index], _sign_desc); |
|
349 |
} |
|
350 |
} |
|
351 |
||
352 |
const ChunkHandler _sign_chunk_handlers[] = { |
|
353 |
{ 'SIGN', Save_SIGN, Load_SIGN, CH_ARRAY | CH_LAST}, |
|
354 |
}; |
|
355 |
||
356 |