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