]> git.pond.sub.org Git - empserver/blob - src/lib/update/nat.c
Update copyright notice.
[empserver] / src / lib / update / nat.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  *  nat.c: Accumulate tech, edu, research and happiness.
29  * 
30  *  Known contributors to this file:
31  *     Dave Pare, 1989
32  *     Steve McClure, 1997
33  */
34
35 #include <math.h>
36 #include "misc.h"
37 #include "sect.h"
38 #include "nat.h"
39 #include "item.h"
40 #include "file.h"
41 #include "xy.h"
42 #include "optlist.h"
43 #include "update.h"
44 #include "subs.h"
45 #include "budg.h"
46
47 /*
48  * hap and edu avg mean that the weight on current happiness is
49  *  (cur_hap * hap_avg + hap_prod * etu) / (hap_avg + etu);
50  * same for education.
51  * right now, happiness has 1 day (48 etu) average, prod of 10 from
52  * initial level of 0 yields (1) 1.42, (6) 6.03, (12) 8.42, (18) 9.37
53  *
54  * education has 4 day (192 etu) average, prod of 10 from initial
55  * level of 0 yields (1) 0.4, (6) 2.2, (12) 3.9, (18) 5.2.
56  */
57
58 static void share_incr(double *, double *);
59
60 /*
61  * for values below the "easy level" values, production is
62  * as normal.  For values above "easy", production gets harder
63  * based on an equation in "limit_level()" in update/nat.c.
64  * Basically, the smaller the the values for "level_log", the
65  * smaller return on investment above level_easy[] values.
66  */
67 /*
68  * Damn! I hate this, but ...
69  * The values here for tech are *not* the real ones.
70  * They are changed later in the limit_level routine.
71  */
72                         /*tech   res   edu   hap */
73 float level_easy[4] = { 0.75, 0.75, 5.00, 5.00 };
74 float level_log[4] = { 1.75, 2.00, 4.00, 6.00 };
75
76 float levels[MAXNOC][4];
77
78 /*
79  * technique to limit the sharpers who turn entire countries
80  * into tech plants overnight...
81  */
82
83 double
84 logx(double d, double base)
85 {
86     if (base == 1.0)
87         return d;
88     return log10(d) / log10(base);
89 }
90
91 static double
92 limit_level(double level, int type, int flag)
93 {
94     double above_easy;
95     double above;
96     double logbase;
97     double easy;
98
99 /*
100  * Begin ugly hack.
101  */
102     level_easy[0] = easy_tech;
103     level_log[0] = tech_log_base;
104 /*
105  * End ugly hack.
106  */
107
108     if (level > level_easy[type]) {
109         logbase = level_log[type];
110         easy = level_easy[type];
111         above_easy = level - easy;
112         if (flag)
113             above = above_easy / logx(logbase + above_easy, logbase);
114         else
115             above = logx(above_easy + 1.0, logbase);
116         if (above > 250)
117             above = 250;
118         return ((above) < 0) ? easy : (easy + above);
119     } else
120         return level;
121 }
122
123 void
124 prod_nat(int etu)
125 {
126     struct natstr *np;
127     float hap;
128     float edu;
129     float hap_edu;
130     long pop;
131     double rlev;
132     double tlev;
133     double tech[MAXNOC];
134     double res[MAXNOC];
135     double newvalue;
136     natid n;
137     int cn, cont;
138
139     for (n = 0; NULL != (np = getnatp(n)); n++) {
140         if ((np->nat_stat & STAT_NORM) == 0)
141             continue;
142         /*
143          * hap_edu: the more education people have, the
144          * more happiness they want.
145          */
146         hap_edu = np->nat_level[NAT_ELEV];
147         hap_edu = 1.5 - ((hap_edu + 10.0) / (hap_edu + 20.0));
148         pop = pops[n] + 1;
149         /*
150          * get per-population happiness and education
151          * see what the total per-civilian production is
152          * for this time period.
153          */
154         hap = levels[n][NAT_HLEV] * hap_edu * hap_cons /
155             ((float)pop * etu);
156         edu = levels[n][NAT_ELEV] * edu_cons / ((float)pop * etu);
157         wu((natid)0, n, "%3.0f happiness, %3.0f education produced\n",
158            levels[n][NAT_HLEV], levels[n][NAT_ELEV]);
159         hap = limit_level(hap, NAT_HLEV, 1);
160         edu = limit_level(edu, NAT_ELEV, 1);
161         /*
162          * change the "moving average"...old happiness and
163          * education levels are weighted heavier than current
164          * production.
165          */
166         newvalue = (np->nat_level[NAT_HLEV] * hap_avg + hap * etu) /
167             (hap_avg + etu);
168         np->nat_level[NAT_HLEV] = newvalue;
169         newvalue = (np->nat_level[NAT_ELEV] * edu_avg + edu * etu) /
170             (edu_avg + etu);
171         np->nat_level[NAT_ELEV] = newvalue;
172         /*
173          * limit tech/research production
174          */
175         levels[n][NAT_TLEV] =
176             limit_level(levels[n][NAT_TLEV] / 1, NAT_TLEV, 0) * 1;
177         levels[n][NAT_RLEV] =
178             limit_level(levels[n][NAT_RLEV] / 1, NAT_RLEV, 0) * 1;
179         wu((natid)0, n,
180            "total pop is %ld, yielding %4.2f hap, %4.2f edu\n",
181            pop - 1, hap, edu);
182     }
183     if (ally_factor > 0.0)
184         share_incr(res, tech);
185     else {
186         memset(res, 0, sizeof(res));
187         memset(tech, 0, sizeof(tech));
188     }
189     for (n = 0; NULL != (np = getnatp(n)); n++) {
190         if ((np->nat_stat & STAT_NORM) == 0)
191             continue;
192         tlev = levels[n][NAT_TLEV];
193         rlev = levels[n][NAT_RLEV];
194         if (tech[n] != 0.0 || res[n] != 0.0) {
195             wu((natid)0, n,
196                "%5.4f technology (%5.4f + %5.4f), ",
197                tlev + tech[n], tlev, tech[n]);
198             wu((natid)0, n,
199                "%5.4f research (%5.4f + %5.4f) produced\n",
200                rlev + res[n], rlev, res[n]);
201         } else
202             wu((natid)0, n,
203                "%5.4f tech, %5.4f research produced\n", tlev, rlev);
204         rlev += res[n];
205         tlev += tech[n];
206         if (rlev != 0.0)
207             np->nat_level[NAT_RLEV] += rlev;
208         if (tlev != 0.0)
209             np->nat_level[NAT_TLEV] += tlev;
210         if ((sea_money[n] != 0) || (air_money[n] != 0) ||
211             (lnd_money[n] != 0))
212             wu((natid)0, n,
213                "Army delta $%ld, Navy delta $%ld, Air force delta $%ld\n",
214                lnd_money[n], sea_money[n], air_money[n]);
215         wu((natid)0, n, "money delta was $%ld for this update\n",
216            np->nat_money - money[n]);
217         if (opt_LOSE_CONTACT) {
218             for (cn = 0; cn <= MAXNOC; cn++) {
219                 cont = getcontact(np, cn);
220                 if (cont > 0) {
221                     logerror("country %d at level %d with country %d.\n",
222                              n, cont, cn);
223                     setcont(n, cn, cont - 1);
224                 }
225             }
226         }
227     }
228 }
229
230 /*
231  * find out everyones increment
232  */
233 static void
234 share_incr(double *res, double *tech)
235 {
236     struct natstr *np;
237     struct natstr *other;
238     natid i;
239     natid j;
240     int rnc;
241     int tnc;
242
243     for (i = 0; NULL != (np = getnatp(i)); i++) {
244         res[i] = tech[i] = 0.0;
245         if ((np->nat_stat & STAT_INUSE) == 0)
246             continue;
247         if (np->nat_stat & STAT_GOD)
248             continue;
249         if (np->nat_stat == VIS)
250             continue;
251         rnc = tnc = 0;
252         for (j = 0; NULL != (other = getnatp(j)); j++) {
253             if (j == i)
254                 continue;
255             if (other->nat_stat & STAT_GOD)
256                 continue;
257             if (other->nat_stat == VIS)
258                 continue;
259             if ((other->nat_stat & STAT_INUSE) == 0)
260                 continue;
261             if (opt_HIDDEN) {
262                 if (!getcontact(np, j))
263                     continue;
264             }
265             if (!opt_ALL_BLEED) {
266                 if (getrel(np, j) != ALLIED)
267                     continue;
268                 if (getrel(other, i) != ALLIED)
269                     continue;
270                 res[i] += levels[j][NAT_RLEV];
271                 tech[i] += levels[j][NAT_TLEV];
272                 rnc++;
273                 tnc++;
274             } else {
275                 if (levels[j][NAT_TLEV] > 0.001) {
276                     tech[i] += levels[j][NAT_TLEV];
277                     tnc++;
278                 }
279                 if (levels[j][NAT_RLEV] > 0.001) {
280                     res[i] += levels[j][NAT_RLEV];
281                     rnc++;
282                 }
283             }
284         }
285         if (rnc == 0 && tnc == 0)
286             continue;
287         if (rnc > 0) {
288             res[i] /= rnc * ally_factor;
289         }
290         if (tnc > 0) {
291             tech[i] /= tnc * ally_factor;
292         }
293 /*              logerror("Country #%d gets %g res from %d allies, %g tech from %d allies", i, res[i], rnc, tech[i], tnc);*/
294     }
295 }