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