Freeciv-3.2
Loading...
Searching...
No Matches
spaceship.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/* utility */
19#include "shared.h" /* TRUE, FALSE */
20
21#include "spaceship.h"
22
24 {19, 13, -1}, /* -1 means none required */
25 {19, 15, 0},
26 {19, 11, 0},
27 {19, 17, 1},
28 {19, 9, 2},
29 {19, 19, 3},
30 {17, 9, 4},
31 {17, 19, 5},
32 {21, 11, 2},
33 {21, 17, 3},
34 {15, 9, 6},
35 {15, 19, 7},
36 {23, 11, 8},
37 {23, 17, 9},
38 {13, 9, 10},
39 {13, 19, 11},
40 {11, 9, 14},
41 {11, 19, 15},
42 { 9, 9, 16},
43 { 9, 19, 17},
44 { 7, 9, 18},
45 { 7, 19, 19},
46 {19, 7, 4},
47 {19, 21, 5},
48 {19, 5, 22},
49 {19, 23, 23},
50 {21, 5, 24},
51 {21, 23, 25},
52 {23, 5, 26},
53 {23, 23, 27},
54 { 5, 9, 20},
55 { 5, 19, 21}
56};
57
59 {21, 13, 0},
60 {24, 13, 12},
61 {21, 15, 1},
62 {24, 15, 13},
63 {21, 9, 8}, /* or 4 */
64 {24, 9, 12},
65 {21, 19, 9}, /* or 5 */
66 {24, 19, 13},
67 {21, 7, 22},
68 {24, 7, 28},
69 {21, 21, 23},
70 {24, 21, 29},
71 {21, 3, 26},
72 {24, 3, 28},
73 {21, 25, 27},
74 {24, 25, 29}
75};
76
78 {16, 12, 0},
79 {16, 16, 1},
80 {14, 6, 10},
81 {12, 16, 15},
82 {12, 12, 14},
83 {14, 22, 11},
84 { 8, 12, 18},
85 { 8, 16, 19},
86 { 6, 6, 20},
87 { 4, 16, 31},
88 { 4, 12, 30},
89 { 6, 22, 21}
90};
91
92/*******************************************************************/
97{
98 ship->structurals = ship->components = ship->modules = 0;
99 BV_CLR_ALL(ship->structure);
100 ship->fuel = ship->propulsion = 0;
101 ship->habitation = ship->life_support = ship->solar_panels = 0;
102 ship->state = SSHIP_NONE;
103 ship->launch_year = 9999;
104
105 ship->population = ship->mass = 0;
106 ship->support_rate = ship->energy_rate =
107 ship->success_rate = ship->travel_time = 0.0;
108}
109
110/*******************************************************************/
114{
115 int i;
116 int num = 0;
117
118 for (i = 0; i < NUM_SS_STRUCTURALS; i++) {
119 if (BV_ISSET(ship->structure, i)) {
120 num++;
121 }
122 }
123
124 return num;
125}
126
127/*******************************************************************/
130bool next_spaceship_component(struct player *pplayer,
131 struct player_spaceship *ship,
132 struct spaceship_component *fill)
133{
134 fc_assert_ret_val(fill != NULL, FALSE);
135
136 if (ship->modules > (ship->habitation + ship->life_support
137 + ship->solar_panels)) {
138 /* "nice" governments prefer to keep success 100%;
139 * others build habitation first (for score?) (Thanks Massimo.)
140 */
141 fill->type =
142 (ship->habitation == 0) ? SSHIP_PLACE_HABITATION :
143 (ship->life_support == 0) ? SSHIP_PLACE_LIFE_SUPPORT :
144 (ship->solar_panels == 0) ? SSHIP_PLACE_SOLAR_PANELS :
145 ((ship->habitation < ship->life_support)
146 && (ship->solar_panels*2 >= ship->habitation + ship->life_support + 1))
148 (ship->solar_panels*2 < ship->habitation + ship->life_support)
150 (ship->life_support < ship->habitation)
152 ((ship->life_support <= ship->habitation)
153 && (ship->solar_panels * 2 >= ship->habitation + ship->life_support + 1))
156
157 if (fill->type == SSHIP_PLACE_HABITATION) {
158 fill->num = ship->habitation + 1;
159 } else if (fill->type == SSHIP_PLACE_LIFE_SUPPORT) {
160 fill->num = ship->life_support + 1;
161 } else {
162 fill->num = ship->solar_panels + 1;
163 }
164 fc_assert(fill->num <= NUM_SS_MODULES / 3);
165
166 return TRUE;
167 }
168 if (ship->components > ship->fuel + ship->propulsion) {
169 if (ship->fuel <= ship->propulsion) {
170 fill->type = SSHIP_PLACE_FUEL;
171 fill->num = ship->fuel + 1;
172 } else {
174 fill->num = ship->propulsion + 1;
175 }
176 return TRUE;
177 }
178 if (ship->structurals > num_spaceship_structurals_placed(ship)) {
179 /* Want to choose which structurals are most important.
180 Else we first want to connect one of each type of module,
181 then all placed components, pairwise, then any remaining
182 modules, or else finally in numerical order.
183 */
184 int req = -1;
185 int i;
186
187 if (!BV_ISSET(ship->structure, 0)) {
188 /* if we don't have the first structural, place that! */
190 fill->num = 0;
191 return TRUE;
192 }
193
194 if (ship->habitation >= 1
195 && !BV_ISSET(ship->structure, modules_info[0].required)) {
196 req = modules_info[0].required;
197 } else if (ship->life_support >= 1
198 && !BV_ISSET(ship->structure, modules_info[1].required)) {
199 req = modules_info[1].required;
200 } else if (ship->solar_panels >= 1
201 && !BV_ISSET(ship->structure, modules_info[2].required)) {
202 req = modules_info[2].required;
203 } else {
204 for (i = 0; i < NUM_SS_COMPONENTS; i++) {
205 if ((i % 2 == 0 && ship->fuel > (i/2))
206 || (i % 2 == 1 && ship->propulsion > (i/2))) {
207 if (!BV_ISSET(ship->structure, components_info[i].required)) {
209 break;
210 }
211 }
212 }
213 }
214 if (req == -1) {
215 for (i = 0; i < NUM_SS_MODULES; i++) {
216 if ((i % 3 == 0 && ship->habitation > (i/3))
217 || (i % 3 == 1 && ship->life_support > (i/3))
218 || (i % 3 == 2 && ship->solar_panels > (i/3))) {
219 if (!BV_ISSET(ship->structure, modules_info[i].required)) {
220 req = modules_info[i].required;
221 break;
222 }
223 }
224 }
225 }
226 if (req == -1) {
227 for (i = 0; i < NUM_SS_STRUCTURALS; i++) {
228 if (!BV_ISSET(ship->structure, i)) {
229 req = i;
230 break;
231 }
232 }
233 }
234 /* sanity: */
235 fc_assert(req != -1);
236 fc_assert(!BV_ISSET(ship->structure, req));
237
238 /* Now we want to find a structural we can build which leads to req.
239 This loop should bottom out, because everything leads back to s0,
240 and we made sure above that we do s0 first.
241 */
242 while (!BV_ISSET(ship->structure, structurals_info[req].required)) {
243 req = structurals_info[req].required;
244 }
246 fill->num = req;
247
248 return TRUE;
249 }
250
251 return FALSE;
252}
#define BV_CLR_ALL(bv)
Definition bitvector.h:95
#define BV_ISSET(bv, bit)
Definition bitvector.h:78
char * incite_cost
Definition comments.c:75
@ 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 fc_assert(condition)
Definition log.h:176
#define fc_assert_ret_val(condition, val)
Definition log.h:194
int num_spaceship_structurals_placed(const struct player_spaceship *ship)
Definition spaceship.c:113
bool next_spaceship_component(struct player *pplayer, struct player_spaceship *ship, struct spaceship_component *fill)
Definition spaceship.c:130
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_NONE
Definition spaceship.h:84
enum spaceship_place_type type
Definition spaceship.h:133
#define TRUE
Definition support.h:46
#define FALSE
Definition support.h:47