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