]> git.pond.sub.org Git - empserver/blob - src/lib/commands/prod.c
(max_population): New, factored out of max_pop().
[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 = (1.0 + obrate * etu_per_update) * sect.sct_item[I_CIVIL];
113         uws = (1.0 + uwbrate * etu_per_update) * sect.sct_item[I_UW];
114         natp = getnatp(sect.sct_own);
115         maxpop = max_pop(natp->nat_level[NAT_RLEV], &sect);
116         civs = min(civs, maxpop);
117         uws = min(uws, maxpop);
118
119         /* This isn't quite right, since research might rise/fall */
120         /* during the update, but it's the best we can really do  */
121         wforce = (int)(((double)civs * sect.sct_work) / 100.0
122                        + uws
123                        + sect.sct_item[I_MILIT] * 2.0 / 5.0);
124         work = new_work(&sect, wforce * etu_per_update / 100);
125         bwork = work / 2;
126
127         if (sect.sct_off)
128             continue;
129         type = sect.sct_type;
130         eff = sect.sct_effic;
131         if (sect.sct_newtype != type) {
132             twork = (eff + 3) / 4;
133             if (twork > bwork) {
134                 twork = bwork;
135             }
136             bwork -= twork;
137             eff -= twork * 4;
138             otype = type;
139             if (eff <= 0) {
140                 type = sect.sct_newtype;
141                 eff = 0;
142             }
143             if (opt_BIG_CITY) {
144                 if (!eff && dchr[otype].d_pkg == UPKG &&
145                     dchr[type].d_pkg != UPKG) {
146                     natp = getnatp(sect.sct_own);
147                     maxpop = max_population(natp->nat_level[NAT_RLEV],
148                                             type, eff);
149                     civs = min(civs, maxpop);
150                     uws = min(uws, maxpop);
151                     wforce = (int)(((double)civs * sect.sct_work) / 100.0
152                                    + uws
153                                    + sect.sct_item[I_MILIT] * 2 / 5.0);
154                     work = etu_per_update * wforce / 100;
155                     bwork = min((int)(work / 2), bwork);
156                 }
157             }
158             twork = 100 - eff;
159             if (twork > bwork) {
160                 twork = bwork;
161             }
162             if (dchr[type].d_lcms > 0) {
163                 lcms = sect.sct_item[I_LCM];
164                 lcms /= dchr[type].d_lcms;
165                 if (twork > lcms)
166                     twork = lcms;
167             }
168             if (dchr[type].d_hcms > 0) {
169                 hcms = sect.sct_item[I_HCM];
170                 hcms /= dchr[type].d_hcms;
171                 if (twork > hcms)
172                     twork = hcms;
173             }
174             bwork -= twork;
175             eff += twork;
176         } else if (eff < 100) {
177             twork = 100 - eff;
178             if (twork > bwork) {
179                 twork = bwork;
180             }
181             bwork -= twork;
182             eff += twork;
183         }
184         work = (work + 1) / 2 + bwork;
185         if (eff < 60 || (type != SCT_ENLIST && eff < 61))
186             continue;
187
188         p_e = eff / 100.0;
189         if (p_e > 1.0)
190             p_e = 1.0;
191
192         if (dchr[type].d_prd == 0 && type != SCT_ENLIST)
193             continue;
194         unit_work = 0;
195         pp = &pchr[dchr[type].d_prd];
196         vtype = pp->p_type;
197         natp = getnatp(sect.sct_own);
198         /*
199          * sect p_e  (inc improvements)
200          */
201         if (type == SCT_ENLIST && sect.sct_own != sect.sct_oldown)
202             continue;
203         if (type == SCT_ENLIST)
204             goto is_enlist;
205         if (pp->p_nrndx != 0) {
206             unit_work++;
207             resource = ((s_char *)&sect) + pp->p_nrndx;
208             p_e = (*resource * p_e) / 100.0;
209         }
210         /*
211          * production effic.
212          */
213         prodeff = prod_eff(pp, natp->nat_level[pp->p_nlndx]);
214         /*
215          * raw material limit
216          */
217         used = 9999;
218         for (j = 0; j < MAXPRCON; ++j) {
219             it = pp->p_ctype[j];
220             if (!pp->p_camt[j])
221                 continue;
222             if (CANT_HAPPEN(it <= I_NONE || I_MAX < it))
223                 continue;
224             used = min(used, sect.sct_item[it] / pp->p_camt[j]);
225             unit_work += pp->p_camt[j];
226         }
227         if (unit_work == 0)
228             unit_work = 1;
229         /*
230          * is production limited by resources or
231          * workforce?
232          */
233         max = (int)(work * p_e / (double)unit_work + 0.5);
234         act = min(used, max);
235
236         real = dmin(999.0, (double)act * prodeff);
237         maxr = dmin(999.0, (double)max * prodeff);
238
239         if (vtype != I_NONE) {
240             if (real < 0.0)
241                 real = 0.0;
242             /* production backlog? */
243             there = min(ITEM_MAX, sect.sct_item[vtype]);
244             real = dmin(real, ITEM_MAX - there);
245         }
246
247         if (prodeff != 0) {
248             take = real / prodeff;
249             mtake = maxr / prodeff;
250         } else
251             mtake = take = 0.0;
252
253         cost = (int)(take * (double)pp->p_cost);
254         if (opt_TECH_POP) {
255             if (pp->p_level == NAT_TLEV) {
256                 totpop = count_pop(sect.sct_own);
257                 if (totpop > 50000)
258                     cost = (int)((double)cost * (double)totpop / 50000.0);
259             }
260         }
261
262         i = 0;
263         for (j = 0; j < MAXPRCON; ++j) {
264             it = pp->p_ctype[j];
265             if (it > I_NONE && it <= I_MAX && ichr[it].i_name != 0) {
266                 if (CANT_HAPPEN(i >= 3))
267                     break;
268                 sprintf(use[i], " %3d%c",
269                         (int)((take * (double)pp->p_camt[j]) + 0.5),
270                         ichr[it].i_name[0]);
271                 sprintf(maxc[i], " %3d%c",
272                         (int)((mtake * (double)pp->p_camt[j]) + 0.5),
273                         ichr[it].i_name[0]);
274                 ++i;
275             }
276         }
277         while (i < 3) {
278             strcpy(use[i], "     ");
279             strcpy(maxc[i], "     ");
280             ++i;
281         }
282
283       is_enlist:
284
285         if (nsect++ == 0) {
286             pr("PRODUCTION SIMULATION\n");
287             pr("   sect  des eff wkfc will make- p.e. cost  use1 use2 use3  max1 max2 max3  max\n");
288         }
289
290         prxy("%4d,%-4d", nstr.x, nstr.y, player->cnum);
291         pr(" %c", dchr[type].d_mnem);
292         pr(" %3.0f%%", p_e * 100.0);
293
294         pr(" %4d", wforce);
295         if (vtype != I_NONE) {
296             pr(" %4d", (int)(real + 0.5));
297         } else if (type != SCT_ENLIST) {
298             switch (pp->p_level) {
299             case NAT_TLEV:
300             case NAT_RLEV:
301                 pr(" %1.2f", real);
302                 break;
303             case NAT_ELEV:
304             case NAT_HLEV:
305                 pr(" %4.0f", real);
306                 break;
307             default:
308                 pr("ERROR");
309                 break;
310             }
311         } else {
312             int maxmil;
313             int enlisted;
314             int civs;
315
316             civs = (1.0 + obrate * etu_per_update) * sect.sct_item[I_CIVIL];
317             natp = getnatp(sect.sct_own);
318             maxpop = max_pop(natp->nat_level[NAT_RLEV], &sect);
319             civs = min(civs, maxpop);
320             /* This isn't quite right, since research might
321                rise/fall during the update, but it's the best
322                we can really do  */
323             enlisted = 0;
324             maxmil = (civs / 2) - sect.sct_item[I_MILIT];
325             if (maxmil > 0) {
326                 enlisted = (etu_per_update
327                             * (10 + sect.sct_item[I_MILIT])
328                             * 0.05);
329                 if (enlisted > maxmil)
330                     enlisted = maxmil;
331             }
332             if (enlisted < 0)
333                 enlisted = 0;
334             if (natp->nat_priorities[type] == 0) {
335                 maxmil = 0;
336             }
337             pr(" %4d mil   1.00 $%-5d%3dc",
338                enlisted, enlisted * 3, enlisted);
339             pr("            %3dc           %4d\n",
340                enlisted, maxmil);
341             continue;
342         }
343
344         pr(" %-5.5s", pp->p_sname);
345         pr(" %.2f", prodeff);
346         pr(" $%-4d", cost);
347         for (i = 0; i < 3; i++) {
348             pr(use[i]);
349         }
350         pr(" ");
351         for (i = 0; i < 3; i++) {
352             pr(maxc[i]);
353         }
354         if (natp->nat_priorities[type] == 0) {
355             max = 0;
356             maxr = 0;
357         }
358         if (vtype != I_NONE || pp->p_level == NAT_ELEV
359             || pp->p_level == NAT_HLEV)
360             pr(" %4d\n", min(999, (int)(max * prodeff + 0.5)));
361         else
362             pr(" %1.2f\n", maxr);
363     }
364     player->simulation = 0;
365     if (nsect == 0) {
366         if (player->argp[1])
367             pr("%s: No sector(s)\n", player->argp[1]);
368         else
369             pr("%s: No sector(s)\n", "");
370         return RET_FAIL;
371     } else
372         pr("%d sector%s\n", nsect, splur(nsect));
373     return RET_OK;
374 }