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