Freeciv-3.2
Loading...
Searching...
No Matches
spacerace.c
Go to the documentation of this file.
1/***********************************************************************
2 Freeciv - Copyright (C) 1996 - A Kjeldberg, L Gregersen, P Unold
3 This program is free software; you can redistribute it and/or modify
4 it under the terms of the GNU General Public License as published by
5 the Free Software Foundation; either version 2, or (at your option)
6 any later version.
7
8 This program is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 GNU General Public License for more details.
12***********************************************************************/
13
14#ifdef HAVE_CONFIG_H
15#include <fc_config.h>
16#endif
17
18#include <string.h>
19
20/* utility */
21#include "fcintl.h"
22#include "log.h"
23#include "shared.h"
24
25/* common */
26#include "calendar.h"
27#include "events.h"
28#include "game.h"
29#include "nation.h"
30#include "packets.h"
31#include "spaceship.h"
32
33/* server */
34#include "plrhand.h"
35#include "notify.h"
36#include "srv_main.h"
37
38#include "spacerace.h"
39
40/**********************************************************************/
47{
48 int i;
49 /* these are how many are connected: */
50 int fuel = 0;
51 int propulsion = 0;
52 int habitation = 0;
53 int life_support = 0;
54 int solar_panels = 0;
55
56 fc_assert_ret(ship->structurals <= NUM_SS_STRUCTURALS);
57 fc_assert_ret(ship->components <= NUM_SS_COMPONENTS);
59
60 ship->mass = 0;
61 ship->support_rate = ship->energy_rate =
62 ship->success_rate = ship->travel_time = 0.0;
63
64 for (i = 0; i < NUM_SS_STRUCTURALS; i++) {
65 if (BV_ISSET(ship->structure, i)) {
66 ship->mass += (i < 6) ? 200 : 100;
67 /* s0 to s3 are heavier; actually in Civ1 its a bit stranger
68 than this, but not worth figuring out --dwp */
69 }
70 }
71 for (i = 0; i < ship->fuel; i++) {
72 if (BV_ISSET(ship->structure, components_info[i * 2].required)) {
73 fuel++;
74 }
75 }
76 for (i = 0; i < ship->propulsion; i++) {
77 if (BV_ISSET(ship->structure, components_info[i * 2 + 1].required)) {
78 propulsion++;
79 }
80 }
81 for (i = 0; i < ship->habitation; i++) {
82 if (BV_ISSET(ship->structure, modules_info[i * 3].required)) {
83 habitation++;
84 }
85 }
86 for (i = 0; i < ship->life_support; i++) {
87 if (BV_ISSET(ship->structure, modules_info[i * 3 + 1].required)) {
88 life_support++;
89 }
90 }
91 for (i = 0; i < ship->solar_panels; i++) {
92 if (BV_ISSET(ship->structure, modules_info[i * 3 + 2].required)) {
93 solar_panels++;
94 }
95 }
96
97 ship->mass += 1600 * (habitation + life_support)
98 + 400 * (solar_panels + propulsion + fuel);
99
100 ship->population = habitation * 10000;
101
102 if (habitation > 0) {
103 ship->support_rate = life_support / (double) habitation;
104 }
105 if (life_support + habitation > 0) {
106 ship->energy_rate = 2.0 * solar_panels / (double)(life_support+habitation);
107 }
108 if (fuel>0 && propulsion>0) {
109 ship->success_rate =
110 MIN(ship->support_rate, 1.0) * MIN(ship->energy_rate, 1.0);
111 }
112
113 /* The Success% can be less by up to a few % in some cases
114 (I think if P != F or if P and/or F too small (eg <= 2?) ?)
115 but probably not worth worrying about.
116 Actually, the Civ1 manual suggests travel time is relevant. --dwp
117 */
118
119 ship->travel_time = ship->mass * game.server.spaceship_travel_pct
120 / 100 / (200.0 * MIN(propulsion,fuel) + 20.0);
121
122}
123
124/**********************************************************************/
129void send_spaceship_info(struct player *src, struct conn_list *dest)
130{
131 if (!dest) {
132 dest = game.est_connections;
133 }
134
135 players_iterate(pplayer) {
136 if (!src || pplayer == src) {
137 struct packet_spaceship_info info;
138 struct player_spaceship *ship = &pplayer->spaceship;
139
140 info.player_num = player_number(pplayer);
141 info.sship_state = ship->state;
142 info.structurals = ship->structurals;
143 info.components = ship->components;
144 info.modules = ship->modules;
145 info.fuel = ship->fuel;
146 info.propulsion = ship->propulsion;
147 info.habitation = ship->habitation;
148 info.life_support = ship->life_support;
149 info.solar_panels = ship->solar_panels;
150 info.launch_year = ship->launch_year;
151 info.population = ship->population;
152 info.mass = ship->mass;
153 info.support_rate = ship->support_rate;
154 info.energy_rate = ship->energy_rate;
155 info.success_rate = ship->success_rate;
156 info.travel_time = ship->travel_time;
157 info.structure = ship->structure;
158
159 lsend_packet_spaceship_info(dest, &info);
160 }
162}
163
164/**********************************************************************/
167void handle_spaceship_launch(struct player *pplayer)
168{
169 struct player_spaceship *ship = &pplayer->spaceship;
170 int arrival;
171
172 if (!player_primary_capital(pplayer)) {
174 _("You need to have a capital in order to launch "
175 "your spaceship."));
176 return;
177 }
178 if (ship->state >= SSHIP_LAUNCHED) {
180 _("Your spaceship is already launched!"));
181 return;
182 }
183 if (ship->state != SSHIP_STARTED
184 || ship->success_rate == 0.0) {
186 _("Your spaceship can't be launched yet!"));
187 return;
188 }
189
190 ship->state = SSHIP_LAUNCHED;
191 ship->launch_year = game.info.year;
192 arrival = ship->launch_year + (int) ship->travel_time;
193
195 _("The %s have launched a spaceship! "
196 "It is estimated to arrive at Alpha Centauri in %s."),
199
200 send_spaceship_info(pplayer, NULL);
201}
202
203/**********************************************************************/
206void handle_spaceship_place(struct player *pplayer,
207 enum spaceship_place_type type, int num)
208{
209 (void) do_spaceship_place(pplayer, ACT_REQ_PLAYER, type, num);
210}
211
212/**********************************************************************/
215bool do_spaceship_place(struct player *pplayer, enum action_requester from,
216 enum spaceship_place_type type, int num)
217{
218 struct player_spaceship *ship = &pplayer->spaceship;
219
220 if (ship->state == SSHIP_NONE) {
221 if (from == ACT_REQ_PLAYER) {
223 _("Spaceship action received,"
224 " but you don't have a spaceship!"));
225 }
226
227 return FALSE;
228 }
229
230 if (ship->state >= SSHIP_LAUNCHED) {
231 if (from == ACT_REQ_PLAYER) {
233 _("You can't modify your spaceship after launch!"));
234 }
235
236 return FALSE;
237 }
238
241 || BV_ISSET(ship->structure, num)) {
242 return FALSE;
243 }
244 if (num_spaceship_structurals_placed(ship) >= ship->structurals) {
245 if (from == ACT_REQ_PLAYER) {
247 _("You don't have any unplaced Space Structurals!"));
248 }
249
250 return FALSE;
251 }
252 if (num != 0
253 && !BV_ISSET(ship->structure, structurals_info[num].required)) {
254 if (from == ACT_REQ_PLAYER) {
256 _("That Space Structural would not be connected!"));
257 }
258
259 return FALSE;
260 }
261
262 BV_SET(ship->structure, num);
264 send_spaceship_info(pplayer, NULL);
265 return TRUE;
266 }
267
268 if (type == SSHIP_PLACE_FUEL) {
269 if (ship->fuel != num - 1) {
270 return FALSE;
271 }
272 if (ship->fuel + ship->propulsion >= ship->components) {
273 if (from == ACT_REQ_PLAYER) {
275 _("You don't have any unplaced Space Components!"));
276 }
277
278 return FALSE;
279 }
280 if (num > NUM_SS_COMPONENTS/2) {
281 if (from == ACT_REQ_PLAYER) {
283 _("Your spaceship already has"
284 " the maximum number of Fuel Components!"));
285 }
286
287 return FALSE;
288 }
289
290 ship->fuel++;
292 send_spaceship_info(pplayer, NULL);
293 return TRUE;
294 }
295
297 if (ship->propulsion != num - 1) {
298 return FALSE;
299 }
300 if (ship->fuel + ship->propulsion >= ship->components) {
301 if (from == ACT_REQ_PLAYER) {
303 _("You don't have any unplaced"
304 " Space Components!"));
305 }
306
307 return FALSE;
308 }
309 if (num > NUM_SS_COMPONENTS/2) {
310 if (from == ACT_REQ_PLAYER) {
312 _("Your spaceship already has the"
313 " maximum number of Propulsion Components!"));
314 }
315
316 return FALSE;
317 }
318
319 ship->propulsion++;
321 send_spaceship_info(pplayer, NULL);
322 return TRUE;
323 }
324
326 if (ship->habitation != num - 1) {
327 return FALSE;
328 }
329 if (ship->habitation + ship->life_support + ship->solar_panels
330 >= ship->modules) {
331 if (from == ACT_REQ_PLAYER) {
333 _("You don't have any unplaced Space Modules!"));
334 }
335
336 return FALSE;
337 }
338 if (num > NUM_SS_MODULES / 3) {
339 if (from == ACT_REQ_PLAYER) {
341 _("Your spaceship already has the"
342 " maximum number of Habitation Modules!"));
343 }
344
345 return FALSE;
346 }
347
348 ship->habitation++;
350 send_spaceship_info(pplayer, NULL);
351 return TRUE;
352 }
353
355 if (ship->life_support != num - 1) {
356 return FALSE;
357 }
358 if (ship->habitation + ship->life_support + ship->solar_panels
359 >= ship->modules) {
360 if (from == ACT_REQ_PLAYER) {
362 _("You don't have any unplaced Space Modules!"));
363 }
364
365 return FALSE;
366 }
367 if (num > NUM_SS_MODULES / 3) {
368 if (from == ACT_REQ_PLAYER) {
370 _("Your spaceship already has the"
371 " maximum number of Life Support Modules!"));
372 }
373
374 return FALSE;
375 }
376
377 ship->life_support++;
379 send_spaceship_info(pplayer, NULL);
380 return TRUE;
381 }
382
384 if (ship->solar_panels != num - 1) {
385 return FALSE;
386 }
387 if (ship->habitation + ship->life_support + ship->solar_panels
388 >= ship->modules) {
389 if (from == ACT_REQ_PLAYER) {
391 _("You don't have any unplaced Space Modules!"));
392 }
393
394 return FALSE;
395 }
396 if (num > NUM_SS_MODULES / 3) {
397 if (from == ACT_REQ_PLAYER) {
399 _("Your spaceship already has the"
400 " maximum number of Solar Panel Modules!"));
401 }
402
403 return FALSE;
404 }
405
406 ship->solar_panels++;
408 send_spaceship_info(pplayer, NULL);
409 return TRUE;
410 }
411
412 log_error("Received unknown spaceship place type %d from %s",
413 type, player_name(pplayer));
414 return FALSE;
415}
416
417/**********************************************************************/
420void spaceship_arrived(struct player *pplayer)
421{
423 _("The %s spaceship has arrived at Alpha Centauri."),
425 pplayer->spaceship.state = SSHIP_ARRIVED;
426}
427
428/**********************************************************************/
431void spaceship_lost(struct player *pplayer)
432{
434 _("Without guidance from the capital, the %s "
435 "spaceship is lost!"),
437 spaceship_init(&pplayer->spaceship);
438 send_spaceship_info(pplayer, NULL);
439}
440
441/**********************************************************************/
446double spaceship_arrival(const struct player *pplayer)
447{
448 const struct player_spaceship *ship = &pplayer->spaceship;
449
450 return ship->launch_year + ship->travel_time;
451}
452
453/**********************************************************************/
460int rank_spaceship_arrival(struct player **result)
461{
462 int n = 0, i;
463
464 shuffled_players_iterate(pplayer) {
465 struct player_spaceship *ship = &pplayer->spaceship;
466
467 if (ship->state == SSHIP_LAUNCHED) {
468 result[n++] = pplayer;
469 }
471
472 /* An insertion sort will do; n is probably small, and we need a
473 * stable sort to preserve the shuffled order for tie-breaking, so can't
474 * use qsort() */
475 for (i = 1; i < n; i++) {
476 int j;
477 for (j = i;
478 j > 0
479 && spaceship_arrival(result[j-1]) > spaceship_arrival(result[j]);
480 j--) {
481 struct player *tmp = result[j];
482 result[j] = result[j-1];
483 result[j-1] = tmp;
484 }
485 }
486
487 return n;
488}
#define n
Definition astring.c:77
#define BV_SET(bv, bit)
Definition bitvector.h:81
#define BV_ISSET(bv, bit)
Definition bitvector.h:78
const char * textyear(int year)
Definition calendar.c:120
char * incite_cost
Definition comments.c:75
spaceship_place_type
Definition fc_types.h:1296
@ SSHIP_PLACE_PROPULSION
Definition fc_types.h:1299
@ SSHIP_PLACE_STRUCTURAL
Definition fc_types.h:1297
@ SSHIP_PLACE_LIFE_SUPPORT
Definition fc_types.h:1301
@ SSHIP_PLACE_SOLAR_PANELS
Definition fc_types.h:1302
@ SSHIP_PLACE_FUEL
Definition fc_types.h:1298
@ SSHIP_PLACE_HABITATION
Definition fc_types.h:1300
#define _(String)
Definition fcintl.h:67
const struct ft_color ftc_server
struct civ_game game
Definition game.c:62
GType type
Definition repodlgs.c:1313
#define fc_assert_ret(condition)
Definition log.h:191
#define log_error(message,...)
Definition log.h:103
const char * nation_adjective_for_player(const struct player *pplayer)
Definition nation.c:169
const char * nation_plural_for_player(const struct player *pplayer)
Definition nation.c:178
void notify_player(const struct player *pplayer, const struct tile *ptile, enum event_type event, const struct ft_color color, const char *format,...)
Definition notify.c:291
void lsend_packet_spaceship_info(struct conn_list *dest, const struct packet_spaceship_info *packet)
int player_number(const struct player *pplayer)
Definition player.c:837
const char * player_name(const struct player *pplayer)
Definition player.c:895
struct city * player_primary_capital(const struct player *pplayer)
Definition player.c:1337
#define players_iterate_end
Definition player.h:537
#define players_iterate(_pplayer)
Definition player.h:532
#define shuffled_players_iterate_end
Definition plrhand.h:108
#define shuffled_players_iterate(NAME_pplayer)
Definition plrhand.h:98
#define MIN(x, y)
Definition shared.h:55
int rank_spaceship_arrival(struct player **result)
Definition spacerace.c:460
void spaceship_lost(struct player *pplayer)
Definition spacerace.c:431
bool do_spaceship_place(struct player *pplayer, enum action_requester from, enum spaceship_place_type type, int num)
Definition spacerace.c:215
double spaceship_arrival(const struct player *pplayer)
Definition spacerace.c:446
void spaceship_calc_derived(struct player_spaceship *ship)
Definition spacerace.c:46
void send_spaceship_info(struct player *src, struct conn_list *dest)
Definition spacerace.c:129
void handle_spaceship_place(struct player *pplayer, enum spaceship_place_type type, int num)
Definition spacerace.c:206
void handle_spaceship_launch(struct player *pplayer)
Definition spacerace.c:167
void spaceship_arrived(struct player *pplayer)
Definition spacerace.c:420
int num_spaceship_structurals_placed(const struct player_spaceship *ship)
Definition spaceship.c:113
void spaceship_init(struct player_spaceship *ship)
Definition spaceship.c:96
const struct sship_part_info structurals_info[NUM_SS_STRUCTURALS]
Definition spaceship.c:23
const struct sship_part_info modules_info[NUM_SS_MODULES]
Definition spaceship.c:77
const struct sship_part_info components_info[NUM_SS_COMPONENTS]
Definition spaceship.c:58
#define NUM_SS_MODULES
Definition spaceship.h:89
#define NUM_SS_COMPONENTS
Definition spaceship.h:88
#define NUM_SS_STRUCTURALS
Definition spaceship.h:87
@ SSHIP_ARRIVED
Definition spaceship.h:85
@ SSHIP_STARTED
Definition spaceship.h:84
@ SSHIP_LAUNCHED
Definition spaceship.h:85
@ SSHIP_NONE
Definition spaceship.h:84
int spaceship_travel_pct
Definition game.h:183
struct conn_list * est_connections
Definition game.h:97
struct packet_game_info info
Definition game.h:89
struct civ_game::@31::@35 server
bv_spaceship_structure structure
enum spaceship_state state
Definition spaceship.h:108
struct player_spaceship spaceship
Definition player.h:284
#define TRUE
Definition support.h:46
#define FALSE
Definition support.h:47