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