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