forked from longturn/freeciv21
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcityhand.cpp
596 lines (503 loc) · 16.8 KB
/
cityhand.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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
/*__ ___ ***************************************
/ \ / \ Copyright (c) 1996-2020 Freeciv21 and Freeciv
\_ \ / __/ contributors. This file is part of Freeciv21.
_\ \ / /__ Freeciv21 is free software: you can redistribute it
\___ \____/ __/ and/or modify it under the terms of the GNU General
\_ _/ Public License as published by the Free Software
| @ @ \_ Foundation, either version 3 of the License,
| or (at your option) any later version.
_/ /\ You should have received a copy of the GNU
/o) (o/\ \_ General Public License along with Freeciv21.
\_____/ / If not, see https://www.gnu.org/licenses/.
\____/ ********************************************************/
#ifdef HAVE_CONFIG_H
#include <fc_config.h>
#endif
#include <cstdio>
#include <cstdlib>
#include <cstring>
// utility
#include "fcintl.h"
#include "log.h"
#include "rand.h"
#include "support.h"
// common
#include "city.h"
#include "events.h"
#include "game.h"
#include "idex.h"
#include "map.h"
#include "player.h"
#include "specialist.h"
#include "unit.h"
#include "worklist.h"
/* common/aicore */
#include "cm.h"
// server
#include "citytools.h"
#include "cityturn.h"
#include "notify.h"
#include "plrhand.h"
#include "sanitycheck.h"
#include "unithand.h"
#include "unittools.h"
#include "cityhand.h"
/**
Send city_name_suggestion packet back to requesting conn, with
suggested name and with same id which was passed in (either unit id
for city builder or existing city id for rename, we don't care here).
*/
void handle_city_name_suggestion_req(struct player *pplayer, int unit_id)
{
struct unit *punit = player_unit_by_number(pplayer, unit_id);
if (NULL == punit) {
// Probably died or bribed.
qDebug("handle_city_name_suggestion_req() invalid unit %d", unit_id);
return;
}
if (action_prob_possible(action_prob_vs_tile(punit, ACTION_FOUND_CITY,
unit_tile(punit), NULL))) {
qDebug("handle_city_name_suggest_req(unit_pos (%d, %d))",
TILE_XY(unit_tile(punit)));
dlsend_packet_city_name_suggestion_info(
pplayer->connections, unit_id,
city_name_suggestion(pplayer, unit_tile(punit)));
// The rest of this function is error handling.
return;
}
qDebug("handle_city_name_suggest_req(unit_pos (%d, %d)): "
"cannot build there.",
TILE_XY(unit_tile(punit)));
illegal_action_msg(pplayer, E_BAD_COMMAND, punit, ACTION_FOUND_CITY,
unit_tile(punit), NULL, NULL);
}
/**
Handle request to change specialist type
*/
void handle_city_change_specialist(struct player *pplayer, int city_id,
Specialist_type_id from,
Specialist_type_id to)
{
struct city *pcity = player_city_by_number(pplayer, city_id);
if (!pcity) {
return;
}
if (to < 0 || to >= specialist_count() || from < 0
|| from >= specialist_count() || !city_can_use_specialist(pcity, to)
|| pcity->specialists[from] == 0) {
/* This could easily just be due to clicking faster on the specialist
* than the server can cope with. */
qDebug("Error in specialist change request from client.");
return;
}
pcity->specialists[from]--;
pcity->specialists[to]++;
city_refresh(pcity);
sanity_check_city(pcity);
send_city_info(pplayer, pcity);
}
/**
Handle request to change city worker in to specialist.
*/
void handle_city_make_specialist(struct player *pplayer, int city_id,
int tile_id)
{
struct tile *ptile = index_to_tile(&(wld.map), tile_id);
struct city *pcity = player_city_by_number(pplayer, city_id);
if (NULL == pcity) {
// Probably lost.
qDebug("handle_city_make_specialist() bad city number %d.", city_id);
return;
}
if (NULL == ptile) {
qCritical("handle_city_make_specialist() bad tile number %d.", tile_id);
return;
}
if (!city_map_includes_tile(pcity, ptile)) {
qCritical("handle_city_make_specialist() tile (%d, %d) not in the "
"city map of \"%s\".",
TILE_XY(ptile), city_name_get(pcity));
return;
}
if (is_free_worked(pcity, ptile)) {
auto_arrange_workers(pcity);
} else if (tile_worked(ptile) == pcity) {
city_map_update_empty(pcity, ptile);
pcity->specialists[DEFAULT_SPECIALIST]++;
} else {
qDebug("handle_city_make_specialist() not working (%d, %d) "
"\"%s\".",
TILE_XY(ptile), city_name_get(pcity));
}
city_refresh(pcity);
sanity_check_city(pcity);
sync_cities();
}
/**
Handle request to turn specialist in to city worker. Client cannot
tell which kind of specialist is to be taken, but this just makes worker
from first available specialist.
*/
void handle_city_make_worker(struct player *pplayer, int city_id,
int tile_id)
{
struct tile *ptile = index_to_tile(&(wld.map), tile_id);
struct city *pcity = player_city_by_number(pplayer, city_id);
if (NULL == pcity) {
// Probably lost.
qDebug("handle_city_make_worker() bad city number %d.", city_id);
return;
}
if (NULL == ptile) {
qCritical("handle_city_make_worker() bad tile number %d.", tile_id);
return;
}
if (!city_map_includes_tile(pcity, ptile)) {
qCritical("handle_city_make_worker() tile (%d, %d) not in the "
"city map of \"%s\".",
TILE_XY(ptile), city_name_get(pcity));
return;
}
if (is_free_worked(pcity, ptile)) {
auto_arrange_workers(pcity);
sync_cities();
return;
}
if (tile_worked(ptile) == pcity) {
qDebug("handle_city_make_worker() already working (%d, %d) \"%s\".",
TILE_XY(ptile), city_name_get(pcity));
return;
}
if (0 == city_specialists(pcity)) {
qDebug("handle_city_make_worker() no specialists (%d, %d) \"%s\".",
TILE_XY(ptile), city_name_get(pcity));
return;
}
if (!city_can_work_tile(pcity, ptile)) {
qDebug("handle_city_make_worker() cannot work here (%d, %d) \"%s\".",
TILE_XY(ptile), city_name_get(pcity));
return;
}
city_map_update_worker(pcity, ptile);
specialist_type_iterate(i)
{
if (pcity->specialists[i] > 0) {
pcity->specialists[i]--;
break;
}
}
specialist_type_iterate_end;
city_refresh(pcity);
sanity_check_city(pcity);
sync_cities();
}
/**
Handle improvement selling request. Caller is responsible to validate
input before passing to this function if it comes from untrusted source.
*/
void really_handle_city_sell(struct player *pplayer, struct city *pcity,
struct impr_type *pimprove)
{
enum test_result sell_result;
int price;
sell_result = test_player_sell_building_now(pplayer, pcity, pimprove);
if (sell_result == TR_ALREADY_SOLD) {
notify_player(pplayer, pcity->tile, E_BAD_COMMAND, ftc_server,
_("You have already sold something here this turn."));
return;
}
if (sell_result != TR_SUCCESS) {
return;
}
pcity->did_sell = true;
price = impr_sell_gold(pimprove);
notify_player(pplayer, pcity->tile, E_IMP_SOLD, ftc_server,
PL_("You sell %s in %s for %d gold.",
"You sell %s in %s for %d gold.", price),
improvement_name_translation(pimprove), city_link(pcity),
price);
do_sell_building(pplayer, pcity, pimprove, "sold");
city_refresh(pcity);
// If we sold the walls the other players should see it
send_city_info(NULL, pcity);
send_player_info_c(pplayer, pplayer->connections);
}
/**
Handle improvement selling request. This function does check its
parameters as they may come from untrusted source over the network.
*/
void handle_city_sell(struct player *pplayer, int city_id, int build_id)
{
struct city *pcity = player_city_by_number(pplayer, city_id);
struct impr_type *pimprove = improvement_by_number(build_id);
if (!pcity || !pimprove) {
return;
}
really_handle_city_sell(pplayer, pcity, pimprove);
}
/**
Handle buying request. Caller is responsible to validate input before
passing to this function if it comes from untrusted source.
*/
void really_handle_city_buy(struct player *pplayer, struct city *pcity)
{
int cost, total;
// This function corresponds to city_can_buy() in the client.
fc_assert_ret(pcity && player_owns_city(pplayer, pcity));
if (pcity->turn_founded == game.info.turn) {
notify_player(pplayer, pcity->tile, E_BAD_COMMAND, ftc_server,
_("Cannot buy in city created this turn."));
return;
}
if (pcity->did_buy) {
notify_player(pplayer, pcity->tile, E_BAD_COMMAND, ftc_server,
_("You have already bought this turn."));
return;
}
if (city_production_has_flag(pcity, IF_GOLD)) {
notify_player(
pplayer, pcity->tile, E_BAD_COMMAND, ftc_server,
_("You don't buy %s!"),
improvement_name_translation(pcity->production.value.building));
return;
}
if (VUT_UTYPE == pcity->production.kind && pcity->anarchy != 0) {
notify_player(pplayer, pcity->tile, E_BAD_COMMAND, ftc_server,
_("Can't buy units when city is in disorder."));
return;
}
total = city_production_build_shield_cost(pcity);
cost = city_production_buy_gold_cost(pcity);
if (cost <= 0) {
return; // sanity
}
if (cost > pplayer->economic.gold) {
/* In case something changed while player tried to buy, or player
* tried to cheat! */
// Split into two to allow localization of two pluralisations.
char buf[MAX_LEN_MSG];
/* TRANS: This whole string is only ever used when included in one
* other string (search for this string to find it). */
fc_snprintf(buf, ARRAY_SIZE(buf),
PL_("%d gold required.", "%d gold required.", cost), cost);
notify_player(pplayer, pcity->tile, E_BAD_COMMAND, ftc_server,
/* TRANS: %s is a pre-pluralised string:
* "%d gold required." */
PL_("%s You only have %d gold.",
"%s You only have %d gold.", pplayer->economic.gold),
buf, pplayer->economic.gold);
return;
}
pplayer->economic.gold -= cost;
if (pcity->shield_stock < total) {
/* As we never put penalty on disbanded_shields, we can
* fully well add the missing shields there. */
pcity->disbanded_shields += total - pcity->shield_stock;
pcity->shield_stock = total; // AI wants this -- Syela
pcity->did_buy = true; // !PS: no need to set buy flag otherwise
pcity->did_buy_production = true;
}
city_refresh(pcity);
if (VUT_UTYPE == pcity->production.kind) {
notify_player(pplayer, pcity->tile, E_UNIT_BUY, ftc_server,
// TRANS: bought an unit.
Q_("?unit:You bought %s in %s."),
utype_name_translation(pcity->production.value.utype),
city_name_get(pcity));
} else if (VUT_IMPROVEMENT == pcity->production.kind) {
notify_player(
pplayer, pcity->tile, E_IMP_BUY, ftc_server,
/* TRANS: bought an improvement .*/
Q_("?improvement:You bought %s in %s."),
improvement_name_translation(pcity->production.value.building),
city_name_get(pcity));
}
conn_list_do_buffer(pplayer->connections);
send_city_info(pplayer, pcity);
send_player_info_c(pplayer, pplayer->connections);
conn_list_do_unbuffer(pplayer->connections);
}
/**
Handle city worklist update request
*/
void handle_city_worklist(struct player *pplayer, int city_id,
const struct worklist *worklist)
{
struct city *pcity = player_city_by_number(pplayer, city_id);
if (!pcity) {
return;
}
worklist_copy(&pcity->worklist, worklist);
send_city_info(pplayer, pcity);
}
/**
Handle buying request. This function does properly check its input as
it may come from untrusted source over the network.
*/
void handle_city_buy(struct player *pplayer, int city_id)
{
struct city *pcity = player_city_by_number(pplayer, city_id);
if (!pcity) {
return;
}
really_handle_city_buy(pplayer, pcity);
}
/**
Handle city refresh request
*/
void handle_city_refresh(struct player *pplayer, int city_id)
{
if (city_id != 0) {
struct city *pcity = player_city_by_number(pplayer, city_id);
if (!pcity) {
return;
}
city_refresh(pcity);
send_city_info(pplayer, pcity);
} else {
city_refresh_for_player(pplayer);
}
}
/**
Handle request to change current production.
*/
void handle_city_change(struct player *pplayer, int city_id,
int production_kind, int production_value)
{
struct universal prod;
struct city *pcity = player_city_by_number(pplayer, city_id);
if (!universals_n_is_valid(universals_n(production_kind))) {
qCritical("[%s] bad production_kind %d.", __FUNCTION__, production_kind);
prod.kind = VUT_NONE;
return;
} else {
prod =
universal_by_number(universals_n(production_kind), production_value);
if (!universals_n_is_valid(prod.kind)) {
qCritical("[%s] production_kind %d with bad production_value %d.",
__FUNCTION__, production_kind, production_value);
prod.kind = VUT_NONE;
return;
}
}
if (!pcity) {
return;
}
if (are_universals_equal(&pcity->production, &prod)) {
// The client probably shouldn't send such a packet.
return;
}
if (!can_city_build_now(pcity, &prod)) {
return;
}
if (!city_can_change_build(pcity)) {
notify_player(pplayer, pcity->tile, E_BAD_COMMAND, ftc_server,
_("You have bought this turn, can't change."));
return;
}
change_build_target(pplayer, pcity, &prod, E_CITY_PRODUCTION_CHANGED);
city_refresh(pcity);
sanity_check_city(pcity);
send_city_info(pplayer, pcity);
}
/**
'struct packet_city_rename' handler.
*/
void handle_city_rename(struct player *pplayer, int city_id,
const char *name)
{
struct city *pcity = player_city_by_number(pplayer, city_id);
char message[1024];
if (!pcity) {
return;
}
if (!is_allowed_city_name(pplayer, name, message, sizeof(message))) {
notify_player(pplayer, pcity->tile, E_BAD_COMMAND, ftc_server, "%s",
message);
return;
}
sz_strlcpy(pcity->name, name);
city_refresh(pcity);
send_city_info(NULL, pcity);
}
/**
Handles a packet from the client that requests the city options for the
given city be changed.
*/
void handle_city_options_req(struct player *pplayer, int city_id,
bv_city_options options)
{
struct city *pcity = player_city_by_number(pplayer, city_id);
if (!pcity) {
return;
}
pcity->city_options = options;
send_city_info(pplayer, pcity);
}
/**
Handles a request to set city rally point for new units.
*/
void handle_city_rally_point(struct player *pplayer, int city_id, int length,
bool persistent, bool vigilant,
const struct unit_order *orders)
{
struct city *pcity = player_city_by_number(pplayer, city_id);
struct unit_order *checked_orders;
if (NULL == pcity) {
// Probably lost.
qDebug("handle_city_rally_point() bad city number %d.", city_id);
return;
}
if (0 > length || MAX_LEN_ROUTE < length) {
// Shouldn't happen
qCritical("handle_city_rally_point() invalid packet length %d (max %d)",
length, MAX_LEN_ROUTE);
return;
}
pcity->rally_point.length = length;
if (length == 0) {
pcity->rally_point.vigilant = false;
pcity->rally_point.persistent = false;
if (pcity->rally_point.orders) {
FCPP_FREE(pcity->rally_point.orders);
}
} else {
checked_orders = create_unit_orders(length, orders);
if (!checked_orders) {
pcity->rally_point.length = 0;
qCritical("invalid rally point orders for city number %d.", city_id);
return;
}
pcity->rally_point.persistent = persistent;
pcity->rally_point.vigilant = vigilant;
pcity->rally_point.orders = checked_orders;
}
send_city_info(pplayer, pcity);
}
/**
Handles a request to set city manager parameter.
*/
void handle_city_manager(struct player *pplayer, int city_id, bool enabled,
struct cm_parameter parameter)
{
struct city *pcity = player_city_by_number(pplayer, city_id);
if (NULL == pcity) {
// Probably lost.
qDebug("handle_city_manager() bad city number %d.", city_id);
return;
}
if (!enabled) {
if (pcity->cm_parameter) {
free(pcity->cm_parameter);
pcity->cm_parameter = NULL;
send_city_info(pplayer, pcity);
}
return;
}
if (!pcity->cm_parameter) {
pcity->cm_parameter = new cm_parameter[1]();
}
cm_copy_parameter(pcity->cm_parameter, ¶meter);
auto_arrange_workers(pcity);
sync_cities();
}