]> git.pond.sub.org Git - empserver/blob - src/lib/commands/prod.c
include: Move update stuff from prototypes.h to update.h
[empserver] / src / lib / commands / prod.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2016, Dave Pare, Jeff Bailey, Thomas Ruschak,
4  *                Ken Stevens, Steve McClure, Markus Armbruster
5  *
6  *  Empire 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 3 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, see <http://www.gnu.org/licenses/>.
18  *
19  *  ---
20  *
21  *  See files README, COPYING and CREDITS in the root of the source
22  *  tree for related information and legal notices.  It is expected
23  *  that future projects/authors will amend these files as needed.
24  *
25  *  ---
26  *
27  *  prod.c: Calculate production levels
28  *
29  *  Known contributors to this file:
30  *     David Muir Sharnoff, 1987
31  *     Steve McClure, 1997-2000
32  *     Markus Armbruster, 2004-2016
33  */
34
35 #include <config.h>
36
37 #include "commands.h"
38 #include "item.h"
39 #include "optlist.h"
40 #include "product.h"
41 #include "update.h"
42
43 static void prprod(struct sctstr *, double, double, 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 totpop;
75     int material_consume;       /* actual production */
76     double cost;
77     int i;
78     int max_consume;            /* production w/infinite materials */
79     int nsect;
80     double take;
81     double mtake;
82     int there;
83     int unit_work;              /* sum of component amounts */
84     int mat_limit, res_limit;
85     double worker_limit;
86     i_type it;
87     i_type vtype;
88     unsigned char *resource;
89     char cmnem[MAXPRCON];
90     int cuse[MAXPRCON], cmax[MAXPRCON];
91     char mnem;
92
93     if (!snxtsct(&nstr, player->argp[1]))
94         return RET_SYN;
95     player->simulation = 1;
96     prdate();
97     nsect = 0;
98     while (nxtsct(&nstr, &sect)) {
99         if (!player->owner)
100             continue;
101         if (sect.sct_off)
102             continue;
103
104         natp = getnatp(sect.sct_own);
105         do_feed(&sect, natp, etu_per_update, 1);
106         buildeff(&sect);
107         if (sect.sct_effic < 60)
108             continue;
109
110         if (sect.sct_type == SCT_ENLIST) {
111             int maxmil;
112             int enlisted;
113
114             if (sect.sct_own != sect.sct_oldown)
115                 continue;
116             enlisted = 0;
117             maxmil = sect.sct_item[I_CIVIL] / 2 - sect.sct_item[I_MILIT];
118             if (maxmil > 0) {
119                 enlisted = (etu_per_update
120                             * (10 + sect.sct_item[I_MILIT])
121                             * 0.05);
122                 if (enlisted > maxmil)
123                     enlisted = maxmil;
124             }
125             if (enlisted < 0)
126                 enlisted = 0;
127             prprod(&sect, sect.sct_effic / 100.0, 1.0,
128                    ichr[I_MILIT].i_mnem, enlisted, maxmil, enlisted * 3,
129                    "c\0\0", &enlisted, &enlisted, nsect++);
130             continue;
131         }
132
133         if (dchr[sect.sct_type].d_prd < 0)
134             continue;
135         pp = &pchr[dchr[sect.sct_type].d_prd];
136         vtype = pp->p_type;
137         if (pp->p_nrndx)
138             resource = (unsigned char *)&sect + pp->p_nrndx;
139         else
140             resource = NULL;
141
142         mat_limit = prod_materials_cost(pp, sect.sct_item, &unit_work);
143
144         /* sector p.e. */
145         p_e = sect.sct_effic / 100.0;
146         if (resource) {
147             unit_work++;
148             p_e *= *resource / 100.0;
149         }
150         if (unit_work == 0)
151             unit_work = 1;
152
153         worker_limit = sect.sct_avail * p_e / (double)unit_work;
154         res_limit = prod_resource_limit(pp, resource);
155
156         max_consume = res_limit;
157         if (max_consume > worker_limit)
158             max_consume = (int)worker_limit;
159         material_consume = MIN(max_consume, mat_limit);
160
161         prodeff = prod_eff(sect.sct_type, natp->nat_level[pp->p_nlndx]);
162         real = (double)material_consume * prodeff;
163         maxr = (double)max_consume * prodeff;
164
165         if (vtype != I_NONE) {
166             real = MIN(999.0, real);
167             maxr = MIN(999.0, maxr);
168             if (real < 0.0)
169                 real = 0.0;
170             /* production backlog? */
171             there = MIN(ITEM_MAX, sect.sct_item[vtype]);
172             real = MIN(real, ITEM_MAX - there);
173         }
174
175         if (prodeff != 0) {
176             take = real / prodeff;
177             mtake = maxr / prodeff;
178         } else
179             mtake = take = 0.0;
180
181         cost = take * pp->p_cost;
182         if (opt_TECH_POP) {
183             if (pp->p_level == NAT_TLEV) {
184                 totpop = count_pop(sect.sct_own);
185                 if (totpop > 50000)
186                     cost *= totpop / 50000.0;
187             }
188         }
189
190         for (i = 0; i < MAXPRCON; ++i) {
191             cmnem[i] = cuse[i] = cmax[i] = 0;
192             if (!pp->p_camt[i])
193                 continue;
194             it = pp->p_ctype[i];
195             if (CANT_HAPPEN(it <= I_NONE || I_MAX < it))
196                 continue;
197             cmnem[i] = ichr[it].i_mnem;
198             cuse[i] = (int)(take * pp->p_camt[i] + 0.5);
199             cmax[i] = (int)(mtake * pp->p_camt[i] + 0.5);
200         }
201
202         if (pp->p_type != I_NONE)
203             mnem = ichr[vtype].i_mnem;
204         else if (pp->p_level == NAT_TLEV || pp->p_level == NAT_RLEV)
205             mnem = '.';
206         else
207             mnem = 0;
208         prprod(&sect, p_e, prodeff, mnem, real, maxr, cost,
209                cmnem, cuse, cmax, nsect++);
210     }
211     player->simulation = 0;
212     if (nsect == 0) {
213         if (player->argp[1])
214             pr("%s: No sector(s)\n", player->argp[1]);
215         else
216             pr("%s: No sector(s)\n", "");
217         return RET_FAIL;
218     } else
219         pr("%d sector%s\n", nsect, splur(nsect));
220     return RET_OK;
221 }
222
223 static void
224 prprod(struct sctstr *sp, double p_e, double prodeff,
225        char mnem, double make, double max, double cost,
226        char cmnem[], int cuse[], int cmax[], int nsect)
227 {
228     int i;
229
230     if (nsect == 0) {
231         pr("PRODUCTION SIMULATION\n");
232         pr("   sect  des eff avail  make p.e. cost   use1 use2 use3  max1 max2 max3   max\n");
233     }
234
235     prxy("%4d,%-4d", sp->sct_x, sp->sct_y);
236     pr(" %c %3.0f%% %5d",
237        dchr[sp->sct_type].d_mnem, p_e * 100.0, sp->sct_avail);
238     if (mnem == '.')
239         pr(" %5.2f", make);
240     else
241         pr(" %4.0f%c", make, mnem ? mnem : ' ');
242     pr(" %.2f $%-5.0f", prodeff, cost);
243     for (i = 0; i < 3; i++) {
244         if (i < MAXPRCON && cmnem[i])
245             pr("%4d%c", cuse[i], cmnem[i]);
246         else
247             pr("     ");
248     }
249     pr(" ");
250     for (i = 0; i < 3; i++) {
251         if (i < MAXPRCON && cmnem[i])
252             pr("%4d%c", cmax[i], cmnem[i]);
253         else
254             pr("     ");
255     }
256     if (mnem == '.')
257         pr(" %5.2f\n", max);
258     else
259         pr(" %5.0f\n", max);
260 }