File: spaceship.c

package info (click to toggle)
freeciv 2.6.2-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 212,508 kB
  • sloc: ansic: 443,831; cpp: 29,541; sh: 7,982; makefile: 7,886; python: 1,933; xml: 945
file content (246 lines) | stat: -rw-r--r-- 7,590 bytes parent folder | download | duplicates (4)
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
/********************************************************************** 
 Freeciv - Copyright (C) 1996 - A Kjeldberg, L Gregersen, P Unold
   This program 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 2, or (at your option)
   any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
***********************************************************************/

#ifdef HAVE_CONFIG_H
#include <fc_config.h>
#endif

#include "shared.h"		/* TRUE, FALSE */

#include "spaceship.h"

const struct sship_part_info structurals_info[NUM_SS_STRUCTURALS] = {
  {19, 13, -1},		/* -1 means none required */
  {19, 15,  0},
  {19, 11,  0},
  {19, 17,  1},
  {19,  9,  2},
  {19, 19,  3},
  {17,  9,  4},
  {17, 19,  5},
  {21, 11,  2},
  {21, 17,  3},
  {15,  9,  6},
  {15, 19,  7},
  {23, 11,  8},
  {23, 17,  9},
  {13,  9, 10},
  {13, 19, 11},
  {11,  9, 14},
  {11, 19, 15},
  { 9,  9, 16},
  { 9, 19, 17},
  { 7,  9, 18},
  { 7, 19, 19},
  {19,  7,  4},
  {19, 21,  5},
  {19,  5, 22},
  {19, 23, 23},
  {21,  5, 24},
  {21, 23, 25},
  {23,  5, 26},
  {23, 23, 27},
  { 5,  9, 20},
  { 5, 19, 21}
};

const struct sship_part_info components_info[NUM_SS_COMPONENTS] = {
  {21, 13,  0},
  {24, 13, 12},
  {21, 15,  1},
  {24, 15, 13},
  {21,  9,  8},  /* or 4 */
  {24,  9, 12},
  {21, 19,  9},  /* or 5 */
  {24, 19, 13},
  {21,  7, 22},
  {24,  7, 28},
  {21, 21, 23},
  {24, 21, 29},
  {21,  3, 26},
  {24,  3, 28},
  {21, 25, 27},
  {24, 25, 29}
};

const struct sship_part_info modules_info[NUM_SS_MODULES] = {
  {16, 12,  0},
  {16, 16,  1},
  {14,  6, 10},
  {12, 16, 15},
  {12, 12, 14},
  {14, 22, 11},
  { 8, 12, 18},
  { 8, 16, 19},
  { 6,  6, 20},
  { 4, 16, 31},
  { 4, 12, 30},
  { 6, 22, 21}
};

/**********************************************************************
Initialize spaceship struct; could also be used to "cancel" a
spaceship (eg, if/when capital-capture effect implemented).
**********************************************************************/
void spaceship_init(struct player_spaceship *ship)
{
  ship->structurals = ship->components = ship->modules = 0;
  BV_CLR_ALL(ship->structure);
  ship->fuel = ship->propulsion = 0;
  ship->habitation = ship->life_support = ship->solar_panels = 0;
  ship->state = SSHIP_NONE;
  ship->launch_year = 9999;

  ship->population = ship->mass = 0;
  ship->support_rate = ship->energy_rate =
    ship->success_rate = ship->travel_time = 0.0;
}

/**********************************************************************
Count the number of structurals placed; that is, in ship->structure[]
**********************************************************************/
int num_spaceship_structurals_placed(const struct player_spaceship *ship)
{
  int i, num = 0;
  for(i=0; i<NUM_SS_STRUCTURALS; i++) {
    if (BV_ISSET(ship->structure, i)) num++;
  }
  return num;
}

/****************************************************************************
  Find (default) place for next spaceship component.
****************************************************************************/
bool next_spaceship_component(struct player *pplayer,
                              struct player_spaceship *ship,
                              struct spaceship_component *fill)
{
  fc_assert_ret_val(fill != NULL, FALSE);
  
  if (ship->modules > (ship->habitation + ship->life_support
		       + ship->solar_panels)) {
    /* "nice" governments prefer to keep success 100%;
     * others build habitation first (for score?)  (Thanks Massimo.)
     */
    fill->type =
      (ship->habitation == 0)   ? SSHIP_PLACE_HABITATION :
      (ship->life_support == 0) ? SSHIP_PLACE_LIFE_SUPPORT :
      (ship->solar_panels == 0) ? SSHIP_PLACE_SOLAR_PANELS :
      ((ship->habitation < ship->life_support)
       && (ship->solar_panels*2 >= ship->habitation + ship->life_support + 1))
                              ? SSHIP_PLACE_HABITATION :
      (ship->solar_panels*2 < ship->habitation + ship->life_support)
                              ? SSHIP_PLACE_SOLAR_PANELS :
      (ship->life_support < ship->habitation)
                              ? SSHIP_PLACE_LIFE_SUPPORT :
      ((ship->life_support <= ship->habitation)
       && (ship->solar_panels * 2 >= ship->habitation + ship->life_support + 1))
                              ? SSHIP_PLACE_LIFE_SUPPORT :
                                SSHIP_PLACE_SOLAR_PANELS;

    if (fill->type == SSHIP_PLACE_HABITATION) {
      fill->num = ship->habitation + 1;
    } else if (fill->type == SSHIP_PLACE_LIFE_SUPPORT) {
      fill->num = ship->life_support + 1;
    } else {
      fill->num = ship->solar_panels + 1;
    }
    fc_assert(fill->num <= NUM_SS_MODULES / 3);

    return TRUE;
  }
  if (ship->components > ship->fuel + ship->propulsion) {
    if (ship->fuel <= ship->propulsion) {
      fill->type = SSHIP_PLACE_FUEL;
      fill->num = ship->fuel + 1;
    } else {
      fill->type = SSHIP_PLACE_PROPULSION;
      fill->num = ship->propulsion + 1;
    }
    return TRUE;
  }
  if (ship->structurals > num_spaceship_structurals_placed(ship)) {
    /* Want to choose which structurals are most important.
       Else we first want to connect one of each type of module,
       then all placed components, pairwise, then any remaining
       modules, or else finally in numerical order.
    */
    int req = -1;
    int i;

    if (!BV_ISSET(ship->structure, 0)) {
      /* if we don't have the first structural, place that! */
      fill->type = SSHIP_PLACE_STRUCTURAL;
      fill->num = 0;
      return TRUE;
    }
    
    if (ship->habitation >= 1
        && !BV_ISSET(ship->structure, modules_info[0].required)) {
      req = modules_info[0].required;
    } else if (ship->life_support >= 1
               && !BV_ISSET(ship->structure, modules_info[1].required)) {
      req = modules_info[1].required;
    } else if (ship->solar_panels >= 1
               && !BV_ISSET(ship->structure, modules_info[2].required)) {
      req = modules_info[2].required;
    } else {
      for (i = 0; i < NUM_SS_COMPONENTS; i++) {
	if ((i % 2 == 0 && ship->fuel > (i/2))
	    || (i % 2 == 1 && ship->propulsion > (i/2))) {
	  if (!BV_ISSET(ship->structure, components_info[i].required)) {
	    req = components_info[i].required;
	    break;
	  }
	}
      }
    }
    if (req == -1) {
      for (i = 0; i < NUM_SS_MODULES; i++) {
	if ((i % 3 == 0 && ship->habitation > (i/3))
	    || (i % 3 == 1 && ship->life_support > (i/3))
	    || (i % 3 == 2 && ship->solar_panels > (i/3))) {
	  if (!BV_ISSET(ship->structure, modules_info[i].required)) {
	    req = modules_info[i].required;
	    break;
	  }
	}
      }
    }
    if (req == -1) {
      for (i = 0; i < NUM_SS_STRUCTURALS; i++) {
        if (!BV_ISSET(ship->structure, i)) {
	  req = i;
	  break;
	}
      }
    }
    /* sanity: */
    fc_assert(req != -1);
    fc_assert(!BV_ISSET(ship->structure, req));

    /* Now we want to find a structural we can build which leads to req.
       This loop should bottom out, because everything leads back to s0,
       and we made sure above that we do s0 first.
     */
    while(!BV_ISSET(ship->structure, structurals_info[req].required)) {
      req = structurals_info[req].required;
    }
    fill->type = SSHIP_PLACE_STRUCTURAL;
    fill->num = req;

    return TRUE;
  }

  return FALSE;
}