]> git.pond.sub.org Git - empserver/blob - src/lib/commands/prod.c
e0461d4095a3b3d9cd88ce5fe324f65d128aa9ad
[empserver] / src / lib / commands / prod.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2009, 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-2009
34  */
35
36 #include <config.h>
37
38 #include "commands.h"
39 #include "item.h"
40 #include "optlist.h"
41 #include "product.h"
42
43 static void prprod(coord, coord, int, double, double, int, char,
44                    double, double, double, char[], int[], int[], int);
45
46 int
47 count_pop(int n)
48 {
49     int i;
50     int pop = 0;
51     struct sctstr *sp;
52
53     for (i = 0; NULL != (sp = getsectid(i)); i++) {
54         if (sp->sct_own != n)
55             continue;
56         if (sp->sct_oldown != n)
57             continue;
58         pop += sp->sct_item[I_CIVIL];
59     }
60     return pop;
61 }
62
63 int
64 prod(void)
65 {
66     struct natstr *natp;
67     struct sctstr sect;
68     struct nstr_sect nstr;
69     struct pchrstr *pp;
70     double p_e;
71     double maxr;                /* floating version of max */
72     double prodeff;
73     double real;                /* floating pt version of act */
74     int work;
75     int totpop;
76     int act;                    /* actual production */
77     double cost;
78     int i;
79     int max;                    /* production w/infinite materials */
80     int nsect;
81     double take;
82     double mtake;
83     int there;
84     int unit_work;              /* sum of component amounts */
85     int used;                   /* production w/infinite workforce */
86     i_type it;
87     i_type vtype;
88     unsigned char *resource;
89     char cmnem[MAXPRCON];
90     int cuse[MAXPRCON], cmax[MAXPRCON];
91     int lcms, hcms;
92     int civs;
93     int uws;
94     int bwork;
95     int twork;
96     int type;
97     int eff;
98     int maxpop;
99     int otype;
100     char mnem;
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         work = new_work(&sect,
117                         total_work(sect.sct_work, etu_per_update,
118                                    civs, sect.sct_item[I_MILIT], uws,
119                                    maxpop));
120         bwork = work / 2;
121
122         if (sect.sct_off)
123             continue;
124         type = sect.sct_type;
125         eff = sect.sct_effic;
126         if (sect.sct_newtype != type) {
127             twork = (eff + 3) / 4;
128             if (twork > bwork) {
129                 twork = bwork;
130             }
131             bwork -= twork;
132             eff -= twork * 4;
133             otype = type;
134             if (eff <= 0) {
135                 type = sect.sct_newtype;
136                 eff = 0;
137             }
138             if (!eff && IS_BIG_CITY(otype) && !IS_BIG_CITY(type)) {
139                 natp = getnatp(sect.sct_own);
140                 maxpop = max_population(natp->nat_level[NAT_RLEV],
141                                         type, eff);
142                 work = new_work(&sect,
143                                 total_work(sect.sct_work, etu_per_update,
144                                            civs, sect.sct_item[I_MILIT],
145                                            uws, maxpop));
146                 bwork = MIN(work / 2, bwork);
147             }
148             twork = 100 - eff;
149             if (twork > bwork) {
150                 twork = bwork;
151             }
152             if (dchr[type].d_lcms > 0) {
153                 lcms = sect.sct_item[I_LCM];
154                 lcms /= dchr[type].d_lcms;
155                 if (twork > lcms)
156                     twork = lcms;
157             }
158             if (dchr[type].d_hcms > 0) {
159                 hcms = sect.sct_item[I_HCM];
160                 hcms /= dchr[type].d_hcms;
161                 if (twork > hcms)
162                     twork = hcms;
163             }
164             bwork -= twork;
165             eff += twork;
166         } else if (eff < 100) {
167             twork = 100 - eff;
168             if (twork > bwork) {
169                 twork = bwork;
170             }
171             bwork -= twork;
172             eff += twork;
173         }
174         work = (work + 1) / 2 + bwork;
175         if (eff < 60)
176             continue;
177
178         p_e = eff / 100.0;
179         if (p_e > 1.0)
180             p_e = 1.0;
181
182         if (type == SCT_ENLIST) {
183             int maxmil;
184             int enlisted;
185
186             if (sect.sct_own != sect.sct_oldown)
187                 continue;
188             civs = (1.0 + obrate * etu_per_update) * sect.sct_item[I_CIVIL];
189             natp = getnatp(sect.sct_own);
190             maxpop = max_pop(natp->nat_level[NAT_RLEV], &sect);
191             civs = MIN(civs, maxpop);
192             enlisted = 0;
193             maxmil = (civs / 2) - sect.sct_item[I_MILIT];
194             if (maxmil > 0) {
195                 enlisted = (etu_per_update
196                             * (10 + sect.sct_item[I_MILIT])
197                             * 0.05);
198                 if (enlisted > maxmil)
199                     enlisted = maxmil;
200             }
201             if (enlisted < 0)
202                 enlisted = 0;
203             prprod(sect.sct_x, sect.sct_y, type, p_e, 1.0, work,
204                    ichr[I_MILIT].i_mnem, enlisted, maxmil, enlisted * 3,
205                    "c\0\0", &enlisted, &enlisted, nsect++);
206             continue;
207         }
208
209         if (dchr[type].d_prd < 0)
210             continue;
211         unit_work = 0;
212         pp = &pchr[dchr[type].d_prd];
213         vtype = pp->p_type;
214         if (pp->p_nrndx)
215             resource = (unsigned char *)&sect + pp->p_nrndx;
216         else
217             resource = NULL;
218         natp = getnatp(sect.sct_own);
219         /*
220          * sect p_e  (inc improvements)
221          */
222         if (resource) {
223             unit_work++;
224             p_e *= *resource / 100.0;
225         }
226         /*
227          * production effic.
228          */
229         prodeff = prod_eff(type, natp->nat_level[pp->p_nlndx]);
230         /*
231          * raw material limit
232          */
233         used = 9999;
234         for (i = 0; i < MAXPRCON; ++i) {
235             it = pp->p_ctype[i];
236             if (!pp->p_camt[i])
237                 continue;
238             if (CANT_HAPPEN(it <= I_NONE || I_MAX < it))
239                 continue;
240             used = MIN(used, sect.sct_item[it] / pp->p_camt[i]);
241             unit_work += pp->p_camt[i];
242         }
243         if (unit_work == 0)
244             unit_work = 1;
245         /*
246          * is production limited by resources or
247          * workforce?
248          */
249         max = (int)(work * p_e / (double)unit_work + 0.5);
250         if (resource && pp->p_nrdep != 0) {
251             if (*resource * 100 < pp->p_nrdep * max)
252                 max = *resource * 100 / pp->p_nrdep;
253         }
254         act = MIN(used, max);
255
256         real = (double)act * prodeff;
257         maxr = (double)max * prodeff;
258
259         if (vtype != I_NONE) {
260             real = MIN(999.0, real);
261             maxr = MIN(999.0, maxr);
262             if (real < 0.0)
263                 real = 0.0;
264             /* production backlog? */
265             there = MIN(ITEM_MAX, sect.sct_item[vtype]);
266             real = MIN(real, ITEM_MAX - there);
267         }
268
269         if (prodeff != 0) {
270             take = real / prodeff;
271             mtake = maxr / prodeff;
272         } else
273             mtake = take = 0.0;
274
275         cost = take * pp->p_cost;
276         if (opt_TECH_POP) {
277             if (pp->p_level == NAT_TLEV) {
278                 totpop = count_pop(sect.sct_own);
279                 if (totpop > 50000)
280                     cost *= totpop / 50000.0;
281             }
282         }
283
284         for (i = 0; i < MAXPRCON; ++i) {
285             cmnem[i] = cuse[i] = cmax[i] = 0;
286             if (!pp->p_camt[i])
287                 continue;
288             it = pp->p_ctype[i];
289             if (CANT_HAPPEN(it <= I_NONE || I_MAX < it))
290                 continue;
291             cmnem[i] = ichr[it].i_mnem;
292             cuse[i] = (int)(take * pp->p_camt[i] + 0.5);
293             cmax[i] = (int)(mtake * pp->p_camt[i] + 0.5);
294         }
295
296         if (pp->p_type != I_NONE)
297             mnem = ichr[vtype].i_mnem;
298         else if (pp->p_level == NAT_TLEV || pp->p_level == NAT_RLEV)
299             mnem = '.';
300         else
301             mnem = 0;
302         prprod(sect.sct_x, sect.sct_y, type, p_e, prodeff, work,
303                mnem, real, maxr, cost,
304                cmnem, cuse, cmax, nsect++);
305     }
306     player->simulation = 0;
307     if (nsect == 0) {
308         if (player->argp[1])
309             pr("%s: No sector(s)\n", player->argp[1]);
310         else
311             pr("%s: No sector(s)\n", "");
312         return RET_FAIL;
313     } else
314         pr("%d sector%s\n", nsect, splur(nsect));
315     return RET_OK;
316 }
317
318 static void
319 prprod(coord x, coord y, int type, double p_e, double prodeff, int work,
320        char mnem, double make, double max, double cost,
321        char cmnem[], int cuse[], int cmax[], int nsect)
322 {
323     int i;
324
325     if (nsect == 0) {
326         pr("PRODUCTION SIMULATION\n");
327         pr("   sect  des eff avail  make p.e. cost   use1 use2 use3  max1 max2 max3   max\n");
328     }
329
330     prxy("%4d,%-4d", x, y, player->cnum);
331     pr(" %c %3.0f%% %5d", dchr[type].d_mnem, p_e * 100.0, work);
332     if (mnem == '.')
333         pr(" %5.2f", make);
334     else
335         pr(" %4.0f%c", make, mnem ? mnem : ' ');
336     pr(" %.2f $%-5.0f", prodeff, cost);
337     for (i = 0; i < 3; i++) {
338         if (i < MAXPRCON && cmnem[i])
339             pr("%4d%c", cuse[i], cmnem[i]);
340         else
341             pr("     ");
342     }
343     pr(" ");
344     for (i = 0; i < 3; i++) {
345         if (i < MAXPRCON && cmnem[i])
346             pr("%4d%c", cmax[i], cmnem[i]);
347         else
348             pr("     ");
349     }
350     if (mnem == '.')
351         pr(" %5.2f\n", max);
352     else
353         pr(" %5.0f\n", max);
354 }