]> git.pond.sub.org Git - empserver/blob - src/lib/update/produce.c
(prod): Round the final cost instead of truncating along the way.
[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 *= tpops[sp->sct_own] / 50000.0;
169         }
170     }
171
172     /* The MIN() here is to take care of integer rounding errors */
173     if (p_e > 0.0) {
174         return MIN(work, (int)(unit_work * material_consume / p_e));
175     }
176     return 0;
177 }
178
179 static int
180 materials_cost(struct pchrstr *pp, short *vec, int *costp)
181 {
182     int count;
183     int cost;
184     int i, n;
185
186     count = 9999;
187     cost = 0;
188     for (i = 0; i < MAXPRCON; ++i) {
189         if (!pp->p_camt[i])
190             continue;
191         if (CANT_HAPPEN(pp->p_ctype[i] <= I_NONE || I_MAX < pp->p_ctype[i]))
192             continue;
193         n = vec[pp->p_ctype[i]] / pp->p_camt[i];
194         if (n < count)
195             count = n;
196         cost += pp->p_camt[i];
197     }
198     *costp = cost;
199     return count;
200 }
201
202 static void
203 materials_charge(struct pchrstr *pp, short *vec, int count)
204 {
205     int i, n;
206     i_type item;
207
208     for (i = 0; i < MAXPRCON; ++i) {
209         item = pp->p_ctype[i];
210         if (!pp->p_camt[i])
211             continue;
212         if (CANT_HAPPEN(item <= I_NONE || I_MAX < item))
213             continue;
214         n = vec[item] - pp->p_camt[i] * count;
215         if (CANT_HAPPEN(n < 0))
216             n = 0;
217         vec[item] = n;
218     }
219 }
220
221 /*
222  * Return level p.e. for product PP.
223  * Zero means level is too low for production.
224  * LEVEL is the affecting production of PP; it must match PP->p_nlndx.
225  */
226 double
227 prod_eff(struct pchrstr *pp, float level)
228 {
229     double level_p_e;
230
231     if (pp->p_nlndx < 0)
232         level_p_e = 1.0;
233     else {
234         double delta = (double)level - (double)pp->p_nlmin;
235
236         if (delta < 0.0)
237             return 0.0;
238         if (CANT_HAPPEN(delta + pp->p_nllag <= 0))
239             return 0.0;
240         level_p_e = delta / (delta + pp->p_nllag);
241     }
242
243     return level_p_e * pp->p_effic * 0.01;
244 }