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