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