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