]> git.pond.sub.org Git - empserver/blob - src/lib/update/produce.c
Declare update related variables in budg.h. Remove some redundant
[empserver] / src / lib / update / produce.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2000, 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  *  produce.c: Produce goodies
29  * 
30  *  Known contributors to this file:
31  *    
32  */
33
34 #include "misc.h"
35 #include "var.h"
36 #include "sect.h"
37 #include "product.h"
38 #include "nat.h"
39 #include "file.h"
40 #include "xy.h"
41 #include "player.h"
42 #include "update.h"
43 #include "gen.h"
44 #include "subs.h"
45 #include "common.h"
46 #include "optlist.h"
47 #include "budg.h"
48
49 static void materials_charge(struct pchrstr *, register int *,
50                              register int);
51 static int materials_cost(struct pchrstr *, register int *, int *);
52
53 s_char *levelnames[] =
54     { "Technology", "Research", "Education", "Happiness" };
55
56 int
57 produce(struct natstr *np, struct sctstr *sp, int *vec, int work,
58         int sctwork, int desig, int neweff, int *cost, int *amount)
59 {
60     register struct pchrstr *product;
61     int vtype;
62     double p_e;
63     double level_p_e;
64     s_char *resource;
65     int output;
66     int actual;
67     int unit_work;
68     double depend;
69     int item;
70     int worker_limit;
71     int material_limit;
72     int material_consume;
73     int val;
74
75     product = &pchr[dchr[desig].d_prd];
76     if (product == &pchr[0])
77         return 0;
78     vtype = product->p_type;
79     item = vtype & ~VT_ITEM;
80     *amount = 0;
81     *cost = 0;
82
83     if ((material_limit = materials_cost(product, vec, &unit_work)) <= 0)
84         return 0;
85     /*
86      * calculate production efficiency.
87      */
88     p_e = neweff / 100.0;
89     if (product->p_nrndx != 0) {
90         unit_work++;
91         resource = ((s_char *)sp) + product->p_nrndx;
92         p_e = (*resource * p_e) / 100.0;
93         if (product->p_nrdep > 0) {
94             /* XXX this looks way wrong */
95             depend = (*resource * 100.0) / product->p_nrdep;
96             if (p_e > depend)
97                 p_e = depend;
98         }
99     }
100     /*
101      * determine number that can be made with
102      * the available workforce
103      */
104     if (unit_work == 0)
105         unit_work = 1;
106     material_consume = material_limit;
107     worker_limit = roundavg(work * p_e / unit_work);
108     if (material_consume > worker_limit)
109         material_consume = worker_limit;
110     if (material_consume == 0)
111         return 0;
112     level_p_e = 1.0;
113     if (product->p_nlndx >= 0) {
114         level_p_e = np->nat_level[product->p_nlndx] - product->p_nlmin;
115         if ((level_p_e < 0.0) && (!player->simulation)) {
116             wu(0, sp->sct_own,
117                "%s level too low to produce in %s (need %d)\n",
118                levelnames[product->p_nlndx], ownxy(sp), product->p_nlmin);
119             return 0;
120         }
121         level_p_e = level_p_e / (level_p_e + product->p_nllag);
122     }
123     /*
124      * Adjust produced amount by commodity production ratio
125      */
126     output = roundavg(product->p_effic * 0.01 * material_consume);
127     if ((vtype == 0) && (!player->simulation)) {
128         levels[sp->sct_own][product->p_level] += output * level_p_e;
129         wu((natid)0, sp->sct_own, "%s (%.2f) produced in %s\n",
130            product->p_name, output * level_p_e, ownxy(sp));
131     } else {
132         if ((actual = roundavg(level_p_e * output)) <= 0)
133             return 0;
134         if (product->p_nrdep != 0) {
135             if (*resource * 100 < product->p_nrdep * actual)
136                 actual = *resource * 100 / product->p_nrdep;
137         }
138         if (actual > 999) {
139             actual = 999;
140             material_consume = (int)(actual / (product->p_effic * 0.01));
141         }
142         vec[item] += actual;
143         if (vec[item] > 9999) {
144             material_consume =
145                 roundavg((9999.0 - vec[item] + actual) *
146                          material_consume / actual);
147             if (material_consume < 0)
148                 material_consume = 0;
149             vec[item] = 9999;
150             if (( /* vtype != V_FOOD && */ sp->sct_own) &&
151                 (!player->simulation))
152                 wu(0, sp->sct_own,
153                    "%s production backlog in %s\n",
154                    product->p_name, ownxy(sp));
155         }
156     }
157     /*
158      * Reset produced amount by commodity production ratio
159      */
160     if (!player->simulation) {
161         materials_charge(product, vec, material_consume);
162         if (product->p_nrdep != 0) {
163             /*
164              * lower natural resource in sector depending on
165              * amount produced
166              */
167             val = *resource - roundavg(product->p_nrdep *
168                                        material_consume / 100.0);
169             if (val < 0)
170                 val = 0;
171             *resource = val;
172         }
173     }
174     *amount = actual;
175     *cost = product->p_cost * material_consume;
176
177     if (opt_TECH_POP) {
178         if (product->p_level == NAT_TLEV) {
179             if (tpops[sp->sct_own] > 50000)
180                 *cost =
181                     (double)*cost * (double)tpops[sp->sct_own] / 50000.0;
182         }
183     }
184
185     /* The min() here is to take care of integer rounding errors */
186     if (p_e > 0.0) {
187         return min(work, (int)(unit_work * material_consume / p_e));
188     }
189     return 0;
190 }
191
192 static int
193 materials_cost(struct pchrstr *product, register int *vec, int *costp)
194 {
195     register u_char *vp;
196     register u_short *ap;
197     register int count;
198     register int cost;
199     register int n;
200     register u_char *endp;
201
202     count = 9999;
203     cost = 0;
204     ap = product->p_vamt;
205     endp = product->p_vtype + product->p_nv;
206     for (vp = product->p_vtype; vp < endp; vp++, ap++) {
207         if (!*ap)
208             continue;
209         n = vec[*vp & ~VT_ITEM] / *ap;
210         if (n < count)
211             count = n;
212         cost += *ap;
213     }
214     *costp = cost;
215     return count;
216 }
217
218 static void
219 materials_charge(struct pchrstr *product, register int *vec,
220                  register int count)
221 {
222     register u_char *vp;
223     register u_short *ap;
224     register u_char *endp;
225     register int item;
226     register int n;
227
228     ap = product->p_vamt;
229     endp = product->p_vtype + product->p_nv;
230     for (vp = product->p_vtype; vp < endp; vp++, ap++) {
231         item = *vp & ~VT_ITEM;
232         if (item < 0 || item > I_MAX) {
233             logerror("materials_charge: bad item %d", item);
234             continue;
235         }
236         if ((n = vec[item] - *ap * count) < 0) {
237             logerror("materials_charge: %d > %d item #%d",
238                      n, vec[item], item);
239             n = 0;
240         }
241         vec[item] = n;
242     }
243 }