src/lib/update/nat.c

Go to the documentation of this file.
00001 /*
00002  *  Empire - A multi-player, client/server Internet based war game.
00003  *  Copyright (C) 1986-2007, Dave Pare, Jeff Bailey, Thomas Ruschak,
00004  *                           Ken Stevens, Steve McClure
00005  *
00006  *  This program is free software; you can redistribute it and/or modify
00007  *  it under the terms of the GNU General Public License as published by
00008  *  the Free Software Foundation; either version 2 of the License, or
00009  *  (at your option) any later version.
00010  *
00011  *  This program is distributed in the hope that it will be useful,
00012  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  *  GNU General Public License for more details.
00015  *
00016  *  You should have received a copy of the GNU General Public License
00017  *  along with this program; if not, write to the Free Software
00018  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00019  *
00020  *  ---
00021  *
00022  *  See files README, COPYING and CREDITS in the root of the source
00023  *  tree for related information and legal notices.  It is expected
00024  *  that future projects/authors will amend these files as needed.
00025  *
00026  *  ---
00027  *
00028  *  nat.c: Accumulate tech, edu, research and happiness.
00029  * 
00030  *  Known contributors to this file:
00031  *     Dave Pare, 1989
00032  *     Steve McClure, 1997
00033  */
00034 
00035 #include <config.h>
00036 
00037 #include <math.h>
00038 #include "budg.h"
00039 #include "game.h"
00040 #include "item.h"
00041 #include "update.h"
00042 
00043 /*
00044  * hap and edu avg mean that the weight on current happiness is
00045  *  (cur_hap * hap_avg + hap_prod * etu) / (hap_avg + etu);
00046  * same for education.
00047  * right now, happiness has 1 day (48 etu) average, prod of 10 from
00048  * initial level of 0 yields (1) 1.42, (6) 6.03, (12) 8.42, (18) 9.37
00049  *
00050  * education has 4 day (192 etu) average, prod of 10 from initial
00051  * level of 0 yields (1) 0.4, (6) 2.2, (12) 3.9, (18) 5.2.
00052  */
00053 
00054 static void share_incr(double *, double *);
00055 
00056 /*
00057  * for values below the "easy level" values, production is
00058  * as normal.  For values above "easy", production gets harder
00059  * based on an equation in "limit_level()" in update/nat.c.
00060  * Basically, the smaller the the values for "level_log", the
00061  * smaller return on investment above level_easy[] values.
00062  */
00063 /*
00064  * Damn! I hate this, but ...
00065  * The values here for tech are *not* the real ones.
00066  * They are changed later in the limit_level routine.
00067  */
00068                         /*tech   res   edu   hap */
00069 static float level_easy[4] = { 0.75, 0.75, 5.00, 5.00 };
00070 static float level_log[4] = { 1.75, 2.00, 4.00, 6.00 };
00071 
00072 float levels[MAXNOC][4];
00073 
00074 /*
00075  * technique to limit the sharpers who turn entire countries
00076  * into tech plants overnight...
00077  */
00078 
00079 double
00080 logx(double d, double base)
00081 {
00082     if (base == 1.0)
00083         return d;
00084     return log10(d) / log10(base);
00085 }
00086 
00087 static double
00088 limit_level(double level, int type, int flag)
00089 {
00090     double above_easy;
00091     double above;
00092     double logbase;
00093     double easy;
00094 
00095 /*
00096  * Begin ugly hack.
00097  */
00098     level_easy[0] = easy_tech;
00099     level_log[0] = tech_log_base;
00100 /*
00101  * End ugly hack.
00102  */
00103 
00104     if (level > level_easy[type]) {
00105         logbase = level_log[type];
00106         easy = level_easy[type];
00107         above_easy = level - easy;
00108         if (flag)
00109             above = above_easy / logx(logbase + above_easy, logbase);
00110         else
00111             above = logx(above_easy + 1.0, logbase);
00112         if (above > 250)
00113             above = 250;
00114         return above < 0 ? easy : easy + above;
00115     } else
00116         return level;
00117 }
00118 
00119 void
00120 prod_nat(int etu)
00121 {
00122     struct natstr *np;
00123     float hap;
00124     float edu;
00125     float hap_edu;
00126     long pop;
00127     double rlev;
00128     double tlev;
00129     double tech[MAXNOC];
00130     double res[MAXNOC];
00131     double newvalue;
00132     natid n;
00133     int cn;
00134     struct natstr *cnp;
00135 
00136     for (n = 0; NULL != (np = getnatp(n)); n++) {
00137         grant_btus(np, game_reset_tick(&np->nat_access));
00138         if (np->nat_stat < STAT_ACTIVE)
00139             continue;
00140         /*
00141          * hap_edu: the more education people have, the
00142          * more happiness they want.
00143          */
00144         hap_edu = np->nat_level[NAT_ELEV];
00145         hap_edu = 1.5 - ((hap_edu + 10.0) / (hap_edu + 20.0));
00146         pop = pops[n] + 1;
00147         /*
00148          * get per-population happiness and education
00149          * see what the total per-civilian production is
00150          * for this time period.
00151          */
00152         hap = levels[n][NAT_HLEV] * hap_edu * hap_cons /
00153             ((float)pop * etu);
00154         edu = levels[n][NAT_ELEV] * edu_cons / ((float)pop * etu);
00155         wu((natid)0, n, "%3.0f happiness, %3.0f education produced\n",
00156            levels[n][NAT_HLEV], levels[n][NAT_ELEV]);
00157         hap = limit_level(hap, NAT_HLEV, 1);
00158         edu = limit_level(edu, NAT_ELEV, 1);
00159         /*
00160          * change the "moving average"...old happiness and
00161          * education levels are weighted heavier than current
00162          * production.
00163          */
00164         newvalue = (np->nat_level[NAT_HLEV] * hap_avg + hap * etu) /
00165             (hap_avg + etu);
00166         np->nat_level[NAT_HLEV] = newvalue;
00167         newvalue = (np->nat_level[NAT_ELEV] * edu_avg + edu * etu) /
00168             (edu_avg + etu);
00169         np->nat_level[NAT_ELEV] = newvalue;
00170         /*
00171          * limit tech/research production
00172          */
00173         levels[n][NAT_TLEV] =
00174             limit_level(levels[n][NAT_TLEV] / 1, NAT_TLEV, 0) * 1;
00175         levels[n][NAT_RLEV] =
00176             limit_level(levels[n][NAT_RLEV] / 1, NAT_RLEV, 0) * 1;
00177         wu((natid)0, n,
00178            "total pop was %ld, yielding %4.2f hap, %4.2f edu\n",
00179            pop - 1, hap, edu);
00180     }
00181     if (ally_factor > 0.0)
00182         share_incr(res, tech);
00183     else {
00184         memset(res, 0, sizeof(res));
00185         memset(tech, 0, sizeof(tech));
00186     }
00187     for (n = 0; NULL != (np = getnatp(n)); n++) {
00188         if (np->nat_stat < STAT_ACTIVE)
00189             continue;
00190         tlev = levels[n][NAT_TLEV];
00191         rlev = levels[n][NAT_RLEV];
00192         if (tech[n] != 0.0 || res[n] != 0.0) {
00193             wu((natid)0, n,
00194                "%5.4f technology (%5.4f + %5.4f), ",
00195                tlev + tech[n], tlev, tech[n]);
00196             wu((natid)0, n,
00197                "%5.4f research (%5.4f + %5.4f) produced\n",
00198                rlev + res[n], rlev, res[n]);
00199         } else
00200             wu((natid)0, n,
00201                "%5.4f tech, %5.4f research produced\n", tlev, rlev);
00202         rlev += res[n];
00203         tlev += tech[n];
00204         if (rlev != 0.0)
00205             np->nat_level[NAT_RLEV] += rlev;
00206         if (tlev != 0.0)
00207             np->nat_level[NAT_TLEV] += tlev;
00208         if ((sea_money[n] != 0) || (air_money[n] != 0) ||
00209             (lnd_money[n] != 0))
00210             wu((natid)0, n,
00211                "Army delta $%ld, Navy delta $%ld, Air force delta $%ld\n",
00212                lnd_money[n], sea_money[n], air_money[n]);
00213         wu((natid)0, n, "money delta was $%ld for this update\n",
00214            np->nat_money - money[n]);
00215         if (opt_LOSE_CONTACT) {
00216             for (cn = 1; cn < MAXNOC; cn++) {
00217                 if ((cnp = getnatp(cn)) != NULL)
00218                     agecontact(cnp);
00219             }
00220         }
00221     }
00222 }
00223 
00224 /*
00225  * find out everyones increment
00226  */
00227 static void
00228 share_incr(double *res, double *tech)
00229 {
00230     struct natstr *np;
00231     struct natstr *other;
00232     natid i;
00233     natid j;
00234     int rnc;
00235     int tnc;
00236 
00237     for (i = 0; NULL != (np = getnatp(i)); i++) {
00238         res[i] = tech[i] = 0.0;
00239         if (np->nat_stat < STAT_SANCT || np->nat_stat == STAT_GOD)
00240             continue;
00241         rnc = tnc = 0;
00242         for (j = 0; NULL != (other = getnatp(j)); j++) {
00243             if (j == i)
00244                 continue;
00245             if (other->nat_stat != STAT_ACTIVE)
00246                 continue;
00247             if (opt_HIDDEN) {
00248                 if (!getcontact(np, j))
00249                     continue;
00250             }
00251             if (!opt_ALL_BLEED) {
00252                 if (getrel(np, j) != ALLIED)
00253                     continue;
00254                 if (getrel(other, i) != ALLIED)
00255                     continue;
00256                 res[i] += levels[j][NAT_RLEV];
00257                 tech[i] += levels[j][NAT_TLEV];
00258                 rnc++;
00259                 tnc++;
00260             } else {
00261                 if (levels[j][NAT_TLEV] > 0.001) {
00262                     tech[i] += levels[j][NAT_TLEV];
00263                     tnc++;
00264                 }
00265                 if (levels[j][NAT_RLEV] > 0.001) {
00266                     res[i] += levels[j][NAT_RLEV];
00267                     rnc++;
00268                 }
00269             }
00270         }
00271         if (rnc > 0) {
00272             res[i] /= rnc * ally_factor;
00273         }
00274         if (tnc > 0) {
00275             tech[i] /= tnc * ally_factor;
00276         }
00277 /*              logerror("Country #%d gets %g res from %d allies, %g tech from %d allies", i, res[i], rnc, tech[i], tnc);*/
00278     }
00279 }

Generated on Fri Mar 28 11:01:14 2008 for empserver by  doxygen 1.5.2