]> git.pond.sub.org Git - empserver/blob - src/lib/update/produce.c
COPYING duplicates information from README. Remove. Move GPL from
[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 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     struct pchrstr *product;
60     double p_e;
61     double prodeff;
62     u_char *resource;
63     double output;
64     int actual;
65     int unit_work;
66     i_type 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     item = product->p_type;
76     *amount = 0;
77     *cost = 0;
78
79     if ((material_limit = materials_cost(product, vec, &unit_work)) <= 0)
80         return 0;
81     /*
82      * calculate production efficiency.
83      */
84     p_e = neweff / 100.0;
85     if (product->p_nrndx != 0) {
86         unit_work++;
87         resource = (u_char *)sp + product->p_nrndx;
88         p_e = (*resource * p_e) / 100.0;
89     }
90     /*
91      * determine number that can be made with
92      * the available workforce
93      */
94     if (unit_work == 0)
95         unit_work = 1;
96     material_consume = material_limit;
97     worker_limit = roundavg(work * p_e / unit_work);
98     if (material_consume > worker_limit)
99         material_consume = worker_limit;
100     if (material_consume == 0)
101         return 0;
102     prodeff = prod_eff(product, np->nat_level[product->p_nlndx]);
103     if (prodeff <= 0.0 && !player->simulation) {
104         wu(0, sp->sct_own,
105            "%s level too low to produce in %s (need %d)\n",
106            levelnames[product->p_nlndx], ownxy(sp), product->p_nlmin);
107         return 0;
108     }
109     /*
110      * Adjust produced amount by commodity production ratio
111      */
112     output = material_consume * prodeff;
113     actual = roundavg(output);
114     if (actual <= 0)
115         return 0;
116     if (item == I_NONE) {
117         if (!player->simulation) {
118             levels[sp->sct_own][product->p_level] += output;
119             wu((natid)0, sp->sct_own, "%s (%.2f) produced in %s\n",
120                product->p_name, output, ownxy(sp));
121         }
122     } else {
123         if (product->p_nrdep != 0) {
124             if (*resource * 100 < product->p_nrdep * actual)
125                 actual = *resource * 100 / product->p_nrdep;
126         }
127         if (actual > 999) {
128             material_consume = roundavg(999.0 * material_consume / actual);
129             actual = 999;
130         }
131         if (vec[item] + actual > ITEM_MAX) {
132             material_consume = roundavg((double)(ITEM_MAX - vec[item])
133                                         * material_consume / actual);
134             if (material_consume < 0)
135                 material_consume = 0;
136             vec[item] = ITEM_MAX;
137             if (sp->sct_own && !player->simulation)
138                 wu(0, sp->sct_own,
139                    "%s production backlog in %s\n",
140                    product->p_name, ownxy(sp));
141         } else
142             vec[item] += actual;
143     }
144     /*
145      * Reset produced amount by commodity production ratio
146      */
147     if (!player->simulation) {
148         materials_charge(product, vec, material_consume);
149         if (product->p_nrdep != 0) {
150             /*
151              * lower natural resource in sector depending on
152              * amount produced
153              */
154             val = *resource - roundavg(product->p_nrdep *
155                                        material_consume / 100.0);
156             if (val < 0)
157                 val = 0;
158             *resource = val;
159         }
160     }
161     *amount = actual;
162     *cost = product->p_cost * material_consume;
163
164     if (opt_TECH_POP) {
165         if (product->p_level == NAT_TLEV) {
166             if (tpops[sp->sct_own] > 50000)
167                 *cost =
168                     (double)*cost * (double)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 }