]> git.pond.sub.org Git - empserver/blob - src/lib/update/produce.c
(u_char, u_short, u_int): BSDisms. Figuring out whether sys/types.h
[empserver] / src / lib / update / produce.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2006, 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 files README, COPYING and CREDITS in the root of the source
23  *  tree for related information and legal notices.  It is expected
24  *  that future 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 <config.h>
35
36 #include "misc.h"
37 #include "sect.h"
38 #include "product.h"
39 #include "nat.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 static char *levelnames[] = {
53     "Technology", "Research", "Education", "Happiness"
54 };
55
56 int
57 produce(struct natstr *np, struct sctstr *sp, short *vec, int work,
58         int desig, int neweff, int *cost, int *amount)
59 {
60     struct pchrstr *product;
61     double p_e;
62     double prodeff;
63     unsigned char *resource;
64     double output;
65     int actual;
66     int unit_work;
67     i_type 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     item = product->p_type;
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 = (unsigned char *)sp + product->p_nrndx;
89         p_e = (*resource * p_e) / 100.0;
90     }
91     /*
92      * determine number that can be made with
93      * the available workforce
94      */
95     if (unit_work == 0)
96         unit_work = 1;
97     material_consume = material_limit;
98     worker_limit = roundavg(work * p_e / unit_work);
99     if (material_consume > worker_limit)
100         material_consume = worker_limit;
101     if (material_consume == 0)
102         return 0;
103     prodeff = prod_eff(product, np->nat_level[product->p_nlndx]);
104     if (prodeff <= 0.0 && !player->simulation) {
105         wu(0, sp->sct_own,
106            "%s level too low to produce in %s (need %d)\n",
107            levelnames[product->p_nlndx], ownxy(sp), product->p_nlmin);
108         return 0;
109     }
110     /*
111      * Adjust produced amount by commodity production ratio
112      */
113     output = material_consume * prodeff;
114     actual = roundavg(output);
115     if (actual <= 0)
116         return 0;
117     if (item == I_NONE) {
118         if (!player->simulation) {
119             levels[sp->sct_own][product->p_level] += output;
120             wu((natid)0, sp->sct_own, "%s (%.2f) produced in %s\n",
121                product->p_name, output, ownxy(sp));
122         }
123     } else {
124         if (product->p_nrdep != 0) {
125             if (*resource * 100 < product->p_nrdep * actual)
126                 actual = *resource * 100 / product->p_nrdep;
127         }
128         if (actual > 999) {
129             material_consume = roundavg(999.0 * material_consume / actual);
130             actual = 999;
131         }
132         if (vec[item] + actual > ITEM_MAX) {
133             material_consume = roundavg((double)(ITEM_MAX - vec[item])
134                                         * material_consume / actual);
135             if (material_consume < 0)
136                 material_consume = 0;
137             vec[item] = ITEM_MAX;
138             if (sp->sct_own && !player->simulation)
139                 wu(0, sp->sct_own,
140                    "%s production backlog in %s\n",
141                    product->p_name, ownxy(sp));
142         } else
143             vec[item] += actual;
144     }
145     /*
146      * Reset produced amount by commodity production ratio
147      */
148     if (!player->simulation) {
149         materials_charge(product, vec, material_consume);
150         if (product->p_nrdep != 0) {
151             /*
152              * lower natural resource in sector depending on
153              * amount produced
154              */
155             val = *resource - roundavg(product->p_nrdep *
156                                        material_consume / 100.0);
157             if (val < 0)
158                 val = 0;
159             *resource = val;
160         }
161     }
162     *amount = actual;
163     *cost = product->p_cost * material_consume;
164
165     if (opt_TECH_POP) {
166         if (product->p_level == NAT_TLEV) {
167             if (tpops[sp->sct_own] > 50000)
168                 *cost =
169                     (double)*cost * (double)tpops[sp->sct_own] / 50000.0;
170         }
171     }
172
173     /* The MIN() here is to take care of integer rounding errors */
174     if (p_e > 0.0) {
175         return MIN(work, (int)(unit_work * material_consume / p_e));
176     }
177     return 0;
178 }
179
180 static int
181 materials_cost(struct pchrstr *pp, short *vec, int *costp)
182 {
183     int count;
184     int cost;
185     int i, n;
186
187     count = 9999;
188     cost = 0;
189     for (i = 0; i < MAXPRCON; ++i) {
190         if (!pp->p_camt[i])
191             continue;
192         if (CANT_HAPPEN(pp->p_ctype[i] <= I_NONE || I_MAX < pp->p_ctype[i]))
193             continue;
194         n = vec[pp->p_ctype[i]] / pp->p_camt[i];
195         if (n < count)
196             count = n;
197         cost += pp->p_camt[i];
198     }
199     *costp = cost;
200     return count;
201 }
202
203 static void
204 materials_charge(struct pchrstr *pp, short *vec, int count)
205 {
206     int i, n;
207     i_type item;
208
209     for (i = 0; i < MAXPRCON; ++i) {
210         item = pp->p_ctype[i];
211         if (!pp->p_camt[i])
212             continue;
213         if (CANT_HAPPEN(item <= I_NONE || I_MAX < item))
214             continue;
215         n = vec[item] - pp->p_camt[i] * count;
216         if (CANT_HAPPEN(n < 0))
217             n = 0;
218         vec[item] = n;
219     }
220 }
221
222 /*
223  * Return level p.e. for product PP.
224  * Zero means level is too low for production.
225  * LEVEL is the affecting production of PP; it must match PP->p_nlndx.
226  */
227 double
228 prod_eff(struct pchrstr *pp, float level)
229 {
230     double level_p_e;
231
232     if (pp->p_nlndx < 0)
233         level_p_e = 1.0;
234     else {
235         double delta = (double)level - (double)pp->p_nlmin;
236
237         if (delta < 0.0)
238             return 0.0;
239         if (CANT_HAPPEN(delta + pp->p_nllag <= 0))
240             return 0.0;
241         level_p_e = delta / (delta + pp->p_nllag);
242     }
243
244     return level_p_e * pp->p_effic * 0.01;
245 }