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