]> git.pond.sub.org Git - empserver/blob - src/lib/commands/prod.c
(prod): Fix bug #1083175. Use max_pop() not just when opt_RES_POP is
[empserver] / src / lib / commands / prod.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2004, Dave Pare, Jeff Bailey, Thomas Ruschak,
4  *                           Ken Stevens, Steve McClure
5  *
6  *  This program 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 2 of the License, or
9  *  (at your option) any later version.
10  *
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.
15  *
16  *  You should have received a copy of the GNU General Public License
17  *  along with this program; if not, write to the Free Software
18  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  *
20  *  ---
21  *
22  *  See the "LEGAL", "LICENSE", "CREDITS" and "README" files for all the
23  *  related information and legal notices. It is expected that any future
24  *  projects/authors will amend these files as needed.
25  *
26  *  ---
27  *
28  *  prod.c: Calculate production levels
29  * 
30  *  Known contributors to this file:
31  *     David Muir Sharnoff, 1987
32  *     Steve McClure, 1997-2000
33  */
34
35 #include "misc.h"
36 #include "player.h"
37 #include "xy.h"
38 #include "nsc.h"
39 #include "sect.h"
40 #include "product.h"
41 #include "nat.h"
42 #include "item.h"
43 #include "file.h"
44 #include "optlist.h"
45 #include "commands.h"
46
47 int
48 count_pop(int n)
49 {
50     int i;
51     int pop = 0;
52     struct sctstr *sp;
53
54     for (i = 0; NULL != (sp = getsectid(i)); i++) {
55         if (sp->sct_own != n)
56             continue;
57         if (sp->sct_oldown != n)
58             continue;
59         pop += sp->sct_item[I_CIVIL];
60     }
61     return pop;
62 }
63
64 int
65 prod(void)
66 {
67     struct natstr *natp;
68     struct sctstr sect;
69     struct nstr_sect nstr;
70     struct pchrstr *pp;
71     double p_e;
72     double maxr;                /* floating version of max */
73     double prodeff;
74     double real;                /* floating pt version of act */
75     int work;
76     int totpop;
77     int act;                    /* actual production */
78     int cost;
79     int i, j;
80     int max;                    /* production w/infinite materials */
81     int nsect;
82     double take;
83     double mtake;
84     int there;
85     int unit_work;              /* sum of component amounts */
86     int used;                   /* production w/infinite workforce */
87     int wforce;
88     i_type it;
89     i_type vtype;
90     s_char *resource;
91     s_char maxc[MAXPRCON][10];
92     s_char use[MAXPRCON][10];
93     int lcms, hcms;
94     int civs = 0;
95     int uws = 0;
96     int bwork;
97     int twork;
98     int type;
99     int eff;
100     int maxpop;
101     u_char otype;
102
103     if (!snxtsct(&nstr, player->argp[1]))
104         return RET_SYN;
105     player->simulation = 1;
106     prdate();
107     nsect = 0;
108     while (nxtsct(&nstr, &sect)) {
109         if (!player->owner)
110             continue;
111
112         civs = min(9999, (int)((obrate * etu_per_update + 1.0)
113                                * sect.sct_item[I_CIVIL]));
114         uws = min(9999, (int)((uwbrate * etu_per_update + 1.0)
115                               * sect.sct_item[I_UW]));
116         natp = getnatp(sect.sct_own);
117         maxpop = max_pop(natp->nat_level[NAT_RLEV], &sect);
118         civs = min(civs, maxpop);
119         uws = min(uws, maxpop);
120
121         /* This isn't quite right, since research might rise/fall */
122         /* during the update, but it's the best we can really do  */
123         wforce = (int)(((double)civs * sect.sct_work) / 100.0
124                        + uws
125                        + sect.sct_item[I_MILIT] * 2.0 / 5.0);
126         work = new_work(&sect, wforce * etu_per_update / 100);
127         bwork = work / 2;
128
129         if (sect.sct_off)
130             continue;
131         type = sect.sct_type;
132         eff = sect.sct_effic;
133         if (sect.sct_newtype != type) {
134             twork = (eff + 3) / 4;
135             if (twork > bwork) {
136                 twork = bwork;
137             }
138             bwork -= twork;
139             eff -= twork * 4;
140             otype = type;
141             if (eff <= 0) {
142                 type = sect.sct_newtype;
143                 eff = 0;
144             }
145             if (opt_BIG_CITY) {
146                 if (!eff && dchr[otype].d_pkg == UPKG &&
147                     dchr[type].d_pkg != UPKG) {
148                     if (opt_RES_POP) {
149                         natp = getnatp(sect.sct_own);
150                         civs = min(civs,
151                                    max_pop(natp->nat_level[NAT_RLEV], 0));
152                         uws = min(uws,
153                                   max_pop(natp->nat_level[NAT_RLEV], 0));
154                     } else {
155                         civs = min(9999, civs);
156                         uws = min(9999, uws);
157                     }
158                     wforce = (int)(((double)civs * sect.sct_work) / 100.0
159                                    + uws
160                                    + sect.sct_item[I_MILIT] * 2 / 5.0);
161                     work = etu_per_update * wforce / 100;
162                     bwork = min((int)(work / 2), bwork);
163                 }
164             }
165             twork = 100 - eff;
166             if (twork > bwork) {
167                 twork = bwork;
168             }
169             if (dchr[type].d_lcms > 0) {
170                 lcms = sect.sct_item[I_LCM];
171                 lcms /= dchr[type].d_lcms;
172                 if (twork > lcms)
173                     twork = lcms;
174             }
175             if (dchr[type].d_hcms > 0) {
176                 hcms = sect.sct_item[I_HCM];
177                 hcms /= dchr[type].d_hcms;
178                 if (twork > hcms)
179                     twork = hcms;
180             }
181             bwork -= twork;
182             eff += twork;
183         } else if (eff < 100) {
184             twork = 100 - eff;
185             if (twork > bwork) {
186                 twork = bwork;
187             }
188             bwork -= twork;
189             eff += twork;
190         }
191         work = (work + 1) / 2 + bwork;
192         if (eff < 60 || (type != SCT_ENLIST && eff < 61))
193             continue;
194
195         p_e = eff / 100.0;
196         if (p_e > 1.0)
197             p_e = 1.0;
198
199         if (dchr[type].d_prd == 0 && type != SCT_ENLIST)
200             continue;
201         unit_work = 0;
202         pp = &pchr[dchr[type].d_prd];
203         vtype = pp->p_type;
204         natp = getnatp(sect.sct_own);
205         /*
206          * sect p_e  (inc improvements)
207          */
208         if (type == SCT_ENLIST && sect.sct_own != sect.sct_oldown)
209             continue;
210         if (type == SCT_ENLIST)
211             goto is_enlist;
212         if (pp->p_nrndx != 0) {
213             unit_work++;
214             resource = ((s_char *)&sect) + pp->p_nrndx;
215             p_e = (*resource * p_e) / 100.0;
216         }
217         /*
218          * production effic.
219          */
220         prodeff = prod_eff(pp, natp->nat_level[pp->p_nlndx]);
221         /*
222          * raw material limit
223          */
224         used = 9999;
225         for (j = 0; j < MAXPRCON; ++j) {
226             it = pp->p_ctype[j];
227             if (!pp->p_camt[j])
228                 continue;
229             if (CANT_HAPPEN(it <= I_NONE || I_MAX < it))
230                 continue;
231             used = min(used, sect.sct_item[it] / pp->p_camt[j]);
232             unit_work += pp->p_camt[j];
233         }
234         if (unit_work == 0)
235             unit_work = 1;
236         /*
237          * is production limited by resources or
238          * workforce?
239          */
240         max = (int)(work * p_e / (double)unit_work + 0.5);
241         act = min(used, max);
242
243         real = dmin(999.0, (double)act * prodeff);
244         maxr = dmin(999.0, (double)max * prodeff);
245
246         if (vtype != I_NONE) {
247             if (real < 0.0)
248                 real = 0.0;
249             /* production backlog? */
250             there = min(ITEM_MAX, sect.sct_item[vtype]);
251             real = dmin(real, ITEM_MAX - there);
252         }
253
254         if (prodeff != 0) {
255             take = real / prodeff;
256             mtake = maxr / prodeff;
257         } else
258             mtake = take = 0.0;
259
260         cost = (int)(take * (double)pp->p_cost);
261         if (opt_TECH_POP) {
262             if (pp->p_level == NAT_TLEV) {
263                 totpop = count_pop(sect.sct_own);
264                 if (totpop > 50000)
265                     cost = (int)((double)cost * (double)totpop / 50000.0);
266             }
267         }
268
269         i = 0;
270         for (j = 0; j < MAXPRCON; ++j) {
271             it = pp->p_ctype[j];
272             if (it > I_NONE && it <= I_MAX && ichr[it].i_name != 0) {
273                 if (CANT_HAPPEN(i >= 3))
274                     break;
275                 sprintf(use[i], " %3d%c",
276                         (int)((take * (double)pp->p_camt[j]) + 0.5),
277                         ichr[it].i_name[0]);
278                 sprintf(maxc[i], " %3d%c",
279                         (int)((mtake * (double)pp->p_camt[j]) + 0.5),
280                         ichr[it].i_name[0]);
281                 ++i;
282             }
283         }
284         while (i < 3) {
285             strcpy(use[i], "     ");
286             strcpy(maxc[i], "     ");
287             ++i;
288         }
289
290       is_enlist:
291
292         if (nsect++ == 0) {
293             pr("PRODUCTION SIMULATION\n");
294             pr("   sect  des eff wkfc will make- p.e. cost  use1 use2 use3  max1 max2 max3  max\n");
295         }
296
297         prxy("%4d,%-4d", nstr.x, nstr.y, player->cnum);
298         pr(" %c", dchr[type].d_mnem);
299         pr(" %3.0f%%", p_e * 100.0);
300
301         pr(" %4d", wforce);
302         if (vtype != I_NONE) {
303             pr(" %4d", (int)(real + 0.5));
304         } else if (type != SCT_ENLIST) {
305             switch (pp->p_level) {
306             case NAT_TLEV:
307             case NAT_RLEV:
308                 pr(" %1.2f", real);
309                 break;
310             case NAT_ELEV:
311             case NAT_HLEV:
312                 pr(" %4.0f", real);
313                 break;
314             default:
315                 pr("ERROR");
316                 break;
317             }
318         } else {
319             int maxmil;
320             int enlisted;
321             int civs;
322
323             civs = min(999, (int)((obrate * etu_per_update + 1.0)
324                                   * sect.sct_item[I_CIVIL]));
325             natp = getnatp(sect.sct_own);
326             maxpop = max_pop((float)natp->nat_level[NAT_RLEV], &sect);
327             civs = min(civs, maxpop);
328             /* This isn't quite right, since research might
329                rise/fall during the update, but it's the best
330                we can really do  */
331             enlisted = 0;
332             maxmil = (civs / 2) - sect.sct_item[I_MILIT];
333             if (maxmil > 0) {
334                 enlisted = (etu_per_update
335                             * (10 + sect.sct_item[I_MILIT])
336                             * 0.05);
337                 if (enlisted > maxmil)
338                     enlisted = maxmil;
339             }
340             if (enlisted < 0)
341                 enlisted = 0;
342             if (natp->nat_priorities[type] == 0) {
343                 maxmil = 0;
344             }
345             pr(" %4d mil   1.00 $%-5d%3dc",
346                enlisted, enlisted * 3, enlisted);
347             pr("            %3dc           %4d\n",
348                enlisted, maxmil);
349             continue;
350         }
351
352         pr(" %-5.5s", pp->p_sname);
353         pr(" %.2f", prodeff);
354         pr(" $%-4d", cost);
355         for (i = 0; i < 3; i++) {
356             pr(use[i]);
357         }
358         pr(" ");
359         for (i = 0; i < 3; i++) {
360             pr(maxc[i]);
361         }
362         if (natp->nat_priorities[type] == 0) {
363             max = 0;
364             maxr = 0;
365         }
366         if (vtype != I_NONE || pp->p_level == NAT_ELEV
367             || pp->p_level == NAT_HLEV)
368             pr(" %4d\n", min(999, (int)(max * prodeff + 0.5)));
369         else
370             pr(" %1.2f\n", maxr);
371     }
372     player->simulation = 0;
373     if (nsect == 0) {
374         if (player->argp[1])
375             pr("%s: No sector(s)\n", player->argp[1]);
376         else
377             pr("%s: No sector(s)\n", "");
378         return RET_FAIL;
379     } else
380         pr("%d sector%s\n", nsect, splur(nsect));
381     return RET_OK;
382 }