]> git.pond.sub.org Git - empserver/blob - src/lib/update/produce.c
(produce, prod): Remove some code related to depletion that has no
[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 level_p_e;
63     s_char *resource;
64     int 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     level_p_e = 1.0;
105     if (product->p_nlndx >= 0) {
106         level_p_e = np->nat_level[product->p_nlndx] - product->p_nlmin;
107         if ((level_p_e < 0.0) && (!player->simulation)) {
108             wu(0, sp->sct_own,
109                "%s level too low to produce in %s (need %d)\n",
110                levelnames[product->p_nlndx], ownxy(sp), product->p_nlmin);
111             return 0;
112         }
113         level_p_e = level_p_e / (level_p_e + product->p_nllag);
114     }
115     /*
116      * Adjust produced amount by commodity production ratio
117      */
118     output = roundavg(product->p_effic * 0.01 * material_consume);
119     if ((vtype == 0) && (!player->simulation)) {
120         levels[sp->sct_own][product->p_level] += output * level_p_e;
121         wu((natid)0, sp->sct_own, "%s (%.2f) produced in %s\n",
122            product->p_name, output * level_p_e, ownxy(sp));
123     } else {
124         if ((actual = roundavg(level_p_e * output)) <= 0)
125             return 0;
126         if (product->p_nrdep != 0) {
127             if (*resource * 100 < product->p_nrdep * actual)
128                 actual = *resource * 100 / product->p_nrdep;
129         }
130         if (actual > 999) {
131             material_consume = roundavg(999.0 * material_consume / actual);
132             actual = 999;
133         }
134         if (vec[item] + actual > ITEM_MAX) {
135             material_consume = roundavg((double)(ITEM_MAX - vec[item])
136                                         * material_consume / actual);
137             if (material_consume < 0)
138                 material_consume = 0;
139             vec[item] = ITEM_MAX;
140             if (sp->sct_own && !player->simulation)
141                 wu(0, sp->sct_own,
142                    "%s production backlog in %s\n",
143                    product->p_name, ownxy(sp));
144         } else
145             vec[item] += actual;
146     }
147     /*
148      * Reset produced amount by commodity production ratio
149      */
150     if (!player->simulation) {
151         materials_charge(product, vec, material_consume);
152         if (product->p_nrdep != 0) {
153             /*
154              * lower natural resource in sector depending on
155              * amount produced
156              */
157             val = *resource - roundavg(product->p_nrdep *
158                                        material_consume / 100.0);
159             if (val < 0)
160                 val = 0;
161             *resource = val;
162         }
163     }
164     *amount = actual;
165     *cost = product->p_cost * material_consume;
166
167     if (opt_TECH_POP) {
168         if (product->p_level == NAT_TLEV) {
169             if (tpops[sp->sct_own] > 50000)
170                 *cost =
171                     (double)*cost * (double)tpops[sp->sct_own] / 50000.0;
172         }
173     }
174
175     /* The min() here is to take care of integer rounding errors */
176     if (p_e > 0.0) {
177         return min(work, (int)(unit_work * material_consume / p_e));
178     }
179     return 0;
180 }
181
182 static int
183 materials_cost(struct pchrstr *product, short *vec, int *costp)
184 {
185     register u_char *vp;
186     register u_short *ap;
187     register int count;
188     register int cost;
189     register int n;
190     register u_char *endp;
191
192     count = 9999;
193     cost = 0;
194     ap = product->p_vamt;
195     endp = product->p_vtype + product->p_nv;
196     for (vp = product->p_vtype; vp < endp; vp++, ap++) {
197         if (!*ap)
198             continue;
199         n = vec[*vp & ~VT_ITEM] / *ap;
200         if (n < count)
201             count = n;
202         cost += *ap;
203     }
204     *costp = cost;
205     return count;
206 }
207
208 static void
209 materials_charge(struct pchrstr *product, short *vec, int count)
210 {
211     register u_char *vp;
212     register u_short *ap;
213     register u_char *endp;
214     register int item;
215     register int n;
216
217     ap = product->p_vamt;
218     endp = product->p_vtype + product->p_nv;
219     for (vp = product->p_vtype; vp < endp; vp++, ap++) {
220         item = *vp & ~VT_ITEM;
221         if (item < 0 || item > I_MAX) {
222             logerror("materials_charge: bad item %d", item);
223             continue;
224         }
225         if ((n = vec[item] - *ap * count) < 0) {
226             logerror("materials_charge: %d > %d item #%d",
227                      n, vec[item], item);
228             n = 0;
229         }
230         vec[item] = n;
231     }
232 }