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