2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2013, Dave Pare, Jeff Bailey, Thomas Ruschak,
4 * Ken Stevens, Steve McClure, Markus Armbruster
6 * Empire is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
21 * See files README, COPYING and CREDITS in the root of the source
22 * tree for related information and legal notices. It is expected
23 * that future projects/authors will amend these files as needed.
27 * prod.c: Calculate production levels
29 * Known contributors to this file:
30 * David Muir Sharnoff, 1987
31 * Steve McClure, 1997-2000
32 * Markus Armbruster, 2004-2010
42 static void prprod(coord, coord, int, double, double, int, char,
43 double, double, double, char[], int[], int[], int);
52 for (i = 0; NULL != (sp = getsectid(i)); i++) {
55 if (sp->sct_oldown != n)
57 pop += sp->sct_item[I_CIVIL];
67 struct nstr_sect nstr;
70 double maxr; /* floating version of max */
72 double real; /* floating pt version of act */
75 int act; /* actual production */
78 int max; /* production w/infinite materials */
83 int unit_work; /* sum of component amounts */
84 int used; /* production w/infinite workforce */
87 unsigned char *resource;
89 int cuse[MAXPRCON], cmax[MAXPRCON];
101 if (!snxtsct(&nstr, player->argp[1]))
103 player->simulation = 1;
106 while (nxtsct(&nstr, §)) {
110 civs = (1.0 + obrate * etu_per_update) * sect.sct_item[I_CIVIL];
111 uws = (1.0 + uwbrate * etu_per_update) * sect.sct_item[I_UW];
112 natp = getnatp(sect.sct_own);
113 maxpop = max_pop(natp->nat_level[NAT_RLEV], §);
115 work = new_work(§,
116 total_work(sect.sct_work, etu_per_update,
117 civs, sect.sct_item[I_MILIT], uws,
123 type = sect.sct_type;
124 eff = sect.sct_effic;
125 if (sect.sct_newtype != type) {
126 twork = (eff + 3) / 4;
134 type = sect.sct_newtype;
137 if (!eff && IS_BIG_CITY(otype) && !IS_BIG_CITY(type)) {
138 maxpop = max_population(natp->nat_level[NAT_RLEV],
140 work = new_work(§,
141 total_work(sect.sct_work, etu_per_update,
142 civs, sect.sct_item[I_MILIT],
144 bwork = MIN(work / 2, bwork);
150 if (dchr[type].d_lcms > 0) {
151 lcms = sect.sct_item[I_LCM];
152 lcms /= dchr[type].d_lcms;
156 if (dchr[type].d_hcms > 0) {
157 hcms = sect.sct_item[I_HCM];
158 hcms /= dchr[type].d_hcms;
164 } else if (eff < 100) {
172 work = (work + 1) / 2 + bwork;
180 if (type == SCT_ENLIST) {
184 if (sect.sct_own != sect.sct_oldown)
187 maxmil = MIN(civs, maxpop) / 2 - sect.sct_item[I_MILIT];
189 enlisted = (etu_per_update
190 * (10 + sect.sct_item[I_MILIT])
192 if (enlisted > maxmil)
197 prprod(sect.sct_x, sect.sct_y, type, p_e, 1.0, work,
198 ichr[I_MILIT].i_mnem, enlisted, maxmil, enlisted * 3,
199 "c\0\0", &enlisted, &enlisted, nsect++);
203 if (dchr[type].d_prd < 0)
206 pp = &pchr[dchr[type].d_prd];
209 resource = (unsigned char *)§ + pp->p_nrndx;
213 * sect p_e (inc improvements)
217 p_e *= *resource / 100.0;
222 prodeff = prod_eff(type, natp->nat_level[pp->p_nlndx]);
227 for (i = 0; i < MAXPRCON; ++i) {
231 if (CANT_HAPPEN(it <= I_NONE || I_MAX < it))
233 used = MIN(used, sect.sct_item[it] / pp->p_camt[i]);
234 unit_work += pp->p_camt[i];
239 * is production limited by resources or
242 max = (int)(work * p_e / (double)unit_work + 0.5);
243 if (resource && pp->p_nrdep != 0) {
244 if (*resource * 100 < pp->p_nrdep * max)
245 max = *resource * 100 / pp->p_nrdep;
247 act = MIN(used, max);
249 real = (double)act * prodeff;
250 maxr = (double)max * prodeff;
252 if (vtype != I_NONE) {
253 real = MIN(999.0, real);
254 maxr = MIN(999.0, maxr);
257 /* production backlog? */
258 there = MIN(ITEM_MAX, sect.sct_item[vtype]);
259 real = MIN(real, ITEM_MAX - there);
263 take = real / prodeff;
264 mtake = maxr / prodeff;
268 cost = take * pp->p_cost;
270 if (pp->p_level == NAT_TLEV) {
271 totpop = count_pop(sect.sct_own);
273 cost *= totpop / 50000.0;
277 for (i = 0; i < MAXPRCON; ++i) {
278 cmnem[i] = cuse[i] = cmax[i] = 0;
282 if (CANT_HAPPEN(it <= I_NONE || I_MAX < it))
284 cmnem[i] = ichr[it].i_mnem;
285 cuse[i] = (int)(take * pp->p_camt[i] + 0.5);
286 cmax[i] = (int)(mtake * pp->p_camt[i] + 0.5);
289 if (pp->p_type != I_NONE)
290 mnem = ichr[vtype].i_mnem;
291 else if (pp->p_level == NAT_TLEV || pp->p_level == NAT_RLEV)
295 prprod(sect.sct_x, sect.sct_y, type, p_e, prodeff, work,
296 mnem, real, maxr, cost,
297 cmnem, cuse, cmax, nsect++);
299 player->simulation = 0;
302 pr("%s: No sector(s)\n", player->argp[1]);
304 pr("%s: No sector(s)\n", "");
307 pr("%d sector%s\n", nsect, splur(nsect));
312 prprod(coord x, coord y, int type, double p_e, double prodeff, int work,
313 char mnem, double make, double max, double cost,
314 char cmnem[], int cuse[], int cmax[], int nsect)
319 pr("PRODUCTION SIMULATION\n");
320 pr(" sect des eff avail make p.e. cost use1 use2 use3 max1 max2 max3 max\n");
323 prxy("%4d,%-4d", x, y);
324 pr(" %c %3.0f%% %5d", dchr[type].d_mnem, p_e * 100.0, work);
328 pr(" %4.0f%c", make, mnem ? mnem : ' ');
329 pr(" %.2f $%-5.0f", prodeff, cost);
330 for (i = 0; i < 3; i++) {
331 if (i < MAXPRCON && cmnem[i])
332 pr("%4d%c", cuse[i], cmnem[i]);
337 for (i = 0; i < 3; i++) {
338 if (i < MAXPRCON && cmnem[i])
339 pr("%4d%c", cmax[i], cmnem[i]);