]> git.pond.sub.org Git - empserver/blob - src/lib/commands/prod.c
Update known contributors comment.
[empserver] / src / lib / commands / prod.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2006, 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 files README, COPYING and CREDITS in the root of the source
23  *  tree for related information and legal notices.  It is expected
24  *  that future 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  *     Markus Armbruster, 2004-2005
34  */
35
36 #include <config.h>
37
38 #include "misc.h"
39 #include "player.h"
40 #include "xy.h"
41 #include "nsc.h"
42 #include "sect.h"
43 #include "product.h"
44 #include "nat.h"
45 #include "item.h"
46 #include "file.h"
47 #include "optlist.h"
48 #include "commands.h"
49
50 int
51 count_pop(int n)
52 {
53     int i;
54     int pop = 0;
55     struct sctstr *sp;
56
57     for (i = 0; NULL != (sp = getsectid(i)); i++) {
58         if (sp->sct_own != n)
59             continue;
60         if (sp->sct_oldown != n)
61             continue;
62         pop += sp->sct_item[I_CIVIL];
63     }
64     return pop;
65 }
66
67 int
68 prod(void)
69 {
70     struct natstr *natp;
71     struct sctstr sect;
72     struct nstr_sect nstr;
73     struct pchrstr *pp;
74     double p_e;
75     double maxr;                /* floating version of max */
76     double prodeff;
77     double real;                /* floating pt version of act */
78     int work;
79     int totpop;
80     int act;                    /* actual production */
81     int cost;
82     int i, j;
83     int max;                    /* production w/infinite materials */
84     int nsect;
85     double take;
86     double mtake;
87     int there;
88     int unit_work;              /* sum of component amounts */
89     int used;                   /* production w/infinite workforce */
90     i_type it;
91     i_type vtype;
92     u_char *resource;
93     s_char maxc[MAXPRCON][10];
94     s_char use[MAXPRCON][10];
95     int lcms, hcms;
96     int civs = 0;
97     int uws = 0;
98     int bwork;
99     int twork;
100     int type;
101     int eff;
102     int maxpop;
103     u_char otype;
104
105     if (!snxtsct(&nstr, player->argp[1]))
106         return RET_SYN;
107     player->simulation = 1;
108     prdate();
109     nsect = 0;
110     while (nxtsct(&nstr, &sect)) {
111         if (!player->owner)
112             continue;
113
114         civs = (1.0 + obrate * etu_per_update) * sect.sct_item[I_CIVIL];
115         uws = (1.0 + uwbrate * etu_per_update) * sect.sct_item[I_UW];
116         natp = getnatp(sect.sct_own);
117         maxpop = max_pop(natp->nat_level[NAT_RLEV], &sect);
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         work = new_work(&sect,
122                         total_work(sect.sct_work, etu_per_update,
123                                    civs, sect.sct_item[I_MILIT], uws, maxpop));
124         bwork = work / 2;
125
126         if (sect.sct_off)
127             continue;
128         type = sect.sct_type;
129         eff = sect.sct_effic;
130         if (sect.sct_newtype != type) {
131             twork = (eff + 3) / 4;
132             if (twork > bwork) {
133                 twork = bwork;
134             }
135             bwork -= twork;
136             eff -= twork * 4;
137             otype = type;
138             if (eff <= 0) {
139                 type = sect.sct_newtype;
140                 eff = 0;
141             }
142             if (!eff && IS_BIG_CITY(otype) &&
143                 !IS_BIG_CITY(type)) {
144                 natp = getnatp(sect.sct_own);
145                 maxpop = max_population(natp->nat_level[NAT_RLEV],
146                                         type, eff);
147                 work = new_work(&sect,
148                                 total_work(sect.sct_work, etu_per_update,
149                                            civs, sect.sct_item[I_MILIT],
150                                            uws, maxpop));
151                 bwork = MIN(work / 2, bwork);
152             }
153             twork = 100 - eff;
154             if (twork > bwork) {
155                 twork = bwork;
156             }
157             if (dchr[type].d_lcms > 0) {
158                 lcms = sect.sct_item[I_LCM];
159                 lcms /= dchr[type].d_lcms;
160                 if (twork > lcms)
161                     twork = lcms;
162             }
163             if (dchr[type].d_hcms > 0) {
164                 hcms = sect.sct_item[I_HCM];
165                 hcms /= dchr[type].d_hcms;
166                 if (twork > hcms)
167                     twork = hcms;
168             }
169             bwork -= twork;
170             eff += twork;
171         } else if (eff < 100) {
172             twork = 100 - eff;
173             if (twork > bwork) {
174                 twork = bwork;
175             }
176             bwork -= twork;
177             eff += twork;
178         }
179         work = (work + 1) / 2 + bwork;
180         if (eff < 60)
181             continue;
182
183         p_e = eff / 100.0;
184         if (p_e > 1.0)
185             p_e = 1.0;
186
187         if (dchr[type].d_prd == 0 && type != SCT_ENLIST)
188             continue;
189         unit_work = 0;
190         pp = &pchr[dchr[type].d_prd];
191         vtype = pp->p_type;
192         natp = getnatp(sect.sct_own);
193         /*
194          * sect p_e  (inc improvements)
195          */
196         if (type == SCT_ENLIST && sect.sct_own != sect.sct_oldown)
197             continue;
198         if (type == SCT_ENLIST)
199             goto is_enlist;
200         if (pp->p_nrndx != 0) {
201             unit_work++;
202             resource = (u_char *)&sect + pp->p_nrndx;
203             p_e = (*resource * p_e) / 100.0;
204         }
205         /*
206          * production effic.
207          */
208         prodeff = prod_eff(pp, natp->nat_level[pp->p_nlndx]);
209         /*
210          * raw material limit
211          */
212         used = 9999;
213         for (j = 0; j < MAXPRCON; ++j) {
214             it = pp->p_ctype[j];
215             if (!pp->p_camt[j])
216                 continue;
217             if (CANT_HAPPEN(it <= I_NONE || I_MAX < it))
218                 continue;
219             used = MIN(used, sect.sct_item[it] / pp->p_camt[j]);
220             unit_work += pp->p_camt[j];
221         }
222         if (unit_work == 0)
223             unit_work = 1;
224         /*
225          * is production limited by resources or
226          * workforce?
227          */
228         max = (int)(work * p_e / (double)unit_work + 0.5);
229         if (pp->p_nrdep != 0 && vtype != I_NONE) {
230             if (*resource * 100 < pp->p_nrdep * max)
231                 max = *resource * 100 / pp->p_nrdep;
232         }
233         act = MIN(used, max);
234
235         real = MIN(999.0, (double)act * prodeff);
236         maxr = MIN(999.0, (double)max * prodeff);
237
238         if (vtype != I_NONE) {
239             if (real < 0.0)
240                 real = 0.0;
241             /* production backlog? */
242             there = MIN(ITEM_MAX, sect.sct_item[vtype]);
243             real = MIN(real, ITEM_MAX - there);
244         }
245
246         if (prodeff != 0) {
247             take = real / prodeff;
248             mtake = maxr / prodeff;
249         } else
250             mtake = take = 0.0;
251
252         cost = (int)(take * (double)pp->p_cost);
253         if (opt_TECH_POP) {
254             if (pp->p_level == NAT_TLEV) {
255                 totpop = count_pop(sect.sct_own);
256                 if (totpop > 50000)
257                     cost = (int)((double)cost * (double)totpop / 50000.0);
258             }
259         }
260
261         i = 0;
262         for (j = 0; j < MAXPRCON; ++j) {
263             it = pp->p_ctype[j];
264             if (it > I_NONE && it <= I_MAX && ichr[it].i_mnem != 0) {
265                 if (CANT_HAPPEN(i >= 3))
266                     break;
267                 sprintf(use[i], "%4d%c",
268                         (int)((take * (double)pp->p_camt[j]) + 0.5),
269                         ichr[it].i_mnem);
270                 sprintf(maxc[i], "%4d%c",
271                         (int)((mtake * (double)pp->p_camt[j]) + 0.5),
272                         ichr[it].i_mnem);
273                 ++i;
274             }
275         }
276         while (i < 3) {
277             strcpy(use[i], "     ");
278             strcpy(maxc[i], "     ");
279             ++i;
280         }
281
282       is_enlist:
283
284         if (nsect++ == 0) {
285             pr("PRODUCTION SIMULATION\n");
286             pr("   sect  des eff avail  make p.e. cost   use1 use2 use3  max1 max2 max3   max\n");
287         }
288
289         prxy("%4d,%-4d", nstr.x, nstr.y, player->cnum);
290         pr(" %c", dchr[type].d_mnem);
291         pr(" %3.0f%%", p_e * 100.0);
292         pr(" %5d", work);
293
294         if (vtype != I_NONE) {
295             pr(" %4d%c", (int)(real + 0.5), ichr[vtype].i_mnem);
296         } else if (type != SCT_ENLIST) {
297             switch (pp->p_level) {
298             case NAT_TLEV:
299             case NAT_RLEV:
300                 pr(" %5.2f", real);
301                 break;
302             case NAT_ELEV:
303             case NAT_HLEV:
304                 pr(" %4.0f ", real);
305                 break;
306             default:
307                 CANT_HAPPEN("bad TYPE");
308                 pr("  ??? ");
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%c 1.00 $%-5d%4dc           %4dc           %5d\n",
338                enlisted, ichr[I_MILIT].i_mnem, enlisted * 3,
339                enlisted, enlisted, maxmil);
340             continue;
341         }
342
343         pr(" %.2f", prodeff);
344         pr(" $%-5d", 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(" %5d\n", MIN(999, (int)(max * prodeff + 0.5)));
359         else
360             pr(" %5.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 }