]> git.pond.sub.org Git - empserver/blobdiff - src/lib/update/main.c
Update copyright notice
[empserver] / src / lib / update / main.c
index 3564460e62114645cf987a044849b123279f2273..80c9be1ffb726447d85896c3d1c88245e9a7fded 100644 (file)
@@ -1,11 +1,11 @@
 /*
  *  Empire - A multi-player, client/server Internet based war game.
- *  Copyright (C) 1986-2007, Dave Pare, Jeff Bailey, Thomas Ruschak,
- *                           Ken Stevens, Steve McClure
+ *  Copyright (C) 1986-2017, Dave Pare, Jeff Bailey, Thomas Ruschak,
+ *                Ken Stevens, Steve McClure, Markus Armbruster
  *
- *  This program is free software; you can redistribute it and/or modify
+ *  Empire is free software: you can redistribute it and/or modify
  *  it under the terms of the GNU General Public License as published by
- *  the Free Software Foundation; either version 2 of the License, or
+ *  the Free Software Foundation, either version 3 of the License, or
  *  (at your option) any later version.
  *
  *  This program is distributed in the hope that it will be useful,
@@ -14,8 +14,7 @@
  *  GNU General Public License for more details.
  *
  *  You should have received a copy of the GNU General Public License
- *  along with this program; if not, write to the Free Software
- *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
  *
  *  ---
  *
  *  ---
  *
  *  main.c: World update main function
- * 
+ *
  *  Known contributors to this file:
  *     Dave Pare, 1994
  *     Steve McClure, 1996
  *     Doug Hay, 1998
+ *     Markus Armbruster, 2006-2016
  */
 
 #include <config.h>
 
-#include "budg.h"
+#include <sys/resource.h>
 #include "empthread.h"
+#include "game.h"
 #include "journal.h"
-#include "player.h"
+#include "optlist.h"
+#include "prototypes.h"
 #include "server.h"
+#include "unit.h"
 #include "update.h"
 
-long money[MAXNOC];
-long pops[MAXNOC];
-long sea_money[MAXNOC];
-long lnd_money[MAXNOC];
-long air_money[MAXNOC];
-long tpops[MAXNOC];
+/*
+ * Update is running.
+ * Can be used to suppress messages, or direct them to bulletins.
+ */
+int update_running;
 
-int update_pending = 0;
+struct budget nat_budget[MAXNOC];
 
-/*ARGSUSED*/
 void
-update_main(void *unused)
+update_main(void)
 {
     int etu = etu_per_update;
+    struct rusage rus1, rus2;
     int n;
-    int x;
-    struct bp *bp;
-    int cn, cn2, rel;
-    struct natstr *cnp;
+    int i;
     struct natstr *np;
 
-    if (CANT_HAPPEN(!update_pending))
-        update_pending = 1;
-
+    update_running = 1;
     logerror("production update (%d etus)", etu);
+    getrusage(RUSAGE_SELF, &rus1);
+    game_record_update(time(NULL));
     journal_update(etu);
+    /* Ensure back-to-back production reports are separate: */
+    for (n = 0; n < MAXNOC; n++)
+       clear_telegram_is_new(n);
 
-    /* First, make sure all mobility is updated correctly. */
-    if (opt_MOB_ACCESS) {
-       mob_ship(etu);
-       mob_sect(etu);
-       mob_plane(etu);
-       mob_land(etu);
-    }
-    player->proc = empth_self();
-    player->cnum = 0;
-    player->god = 1;
+    /* Credit the turn's remaining MOB_ACCESS mobility */
+    if (opt_MOB_ACCESS)
+       mob_access_all();
 
     if (opt_AUTO_POWER)
        update_power();
@@ -88,86 +83,44 @@ update_main(void *unused)
      * sector production routine (for producing education,
      * happiness, and printing out the state of the nation)
      */
-    memset(pops, 0, sizeof(pops));
-    memset(air_money, 0, sizeof(air_money));
-    memset(sea_money, 0, sizeof(sea_money));
-    memset(lnd_money, 0, sizeof(lnd_money));
-    bp = bp_alloc();
+    memset(nat_budget, 0, sizeof(nat_budget));
     for (n = 0; n < MAXNOC; n++) {
-       money[n] = 0;
        if (!(np = getnatp(n)))
            continue;
-       money[n] = np->nat_money;
-       tpops[n] = count_pop(n);
+       nat_budget[n].start_money = nat_budget[n].money = np->nat_money;
     }
 
+    prep_ships(etu, NULL);
+    prep_planes(etu, NULL);
+    prep_lands(etu, NULL);
     logerror("preparing sectors...");
-    prepare_sects(etu, bp);
+    prepare_sects(etu, NULL);
     logerror("done preparing sectors.");
-    logerror("producing for countries...");
-    for (x = 0; x < MAXNOC; x++) {
-       long p_sect[SCT_TYPE_MAX+2][2];
-
-       memset(p_sect, 0, sizeof(p_sect));
-       if (!(np = getnatp(x)))
-           continue;
-       if (np->nat_stat == STAT_SANCT) {
-#ifdef DEBUG
-           logerror("Country %i is in sanctuary and did not update", x);
-#endif
-           continue;
-       }
-       np->nat_money += (int)(np->nat_reserve * money_res * etu);
-
-       /* maintain units */
-       prod_ship(etu, x, bp, 0);
-       prod_plane(etu, x, bp, 0);
-       prod_land(etu, x, bp, 0);
+    for (i = 0; i < MAXNOC; i++)
+       pay_reserve(getnatp(i), etu);
 
-       /* produce all sects */
-       produce_sect(x, etu, bp, p_sect);
-
-       /* build units */
-       prod_ship(etu, x, bp, 1);
-       prod_plane(etu, x, bp, 1);
-       prod_land(etu, x, bp, 1);
-    }
+    logerror("producing for countries...");
+    /* maintain units */
+    prod_ship(etu, NULL, 0);
+    prod_plane(etu, NULL, 0);
+    prod_land(etu, NULL, 0);
+
+    /* produce all sects */
+    produce_sect(etu, NULL);
+
+    /* build units */
+    prod_ship(etu, NULL, 1);
+    prod_plane(etu, NULL, 1);
+    prod_land(etu, NULL, 1);
     logerror("done producing for countries.");
 
     finish_sects(etu);
     prod_nat(etu);
     age_levels(etu);
-    free(bp);
-    /*flushwu(); */
-    if (opt_SLOW_WAR) {
-       /* Update war declarations */
-       /* MOBILIZATION->SITZKRIEG->AT_WAR */
-       for (cn = 1; cn < MAXNOC; cn++) {
-           if ((cnp = getnatp(cn)) == 0)
-               break;
-           for (cn2 = 1; cn2 < MAXNOC; cn2++) {
-               if (cn2 == cn)
-                   continue;
-               rel = getrel(cnp, cn2);
-               if (rel == MOBILIZATION) {
-                   rel = SITZKRIEG;
-                   setrel(cn, cn2, rel);
-               } else if (rel == SITZKRIEG) {
-                   rel = AT_WAR;
-                   setrel(cn, cn2, rel);
-               }
-           }
-       }
-    }
-    /* Only update mobility for non-MOB_ACCESS here, since it doesn't
-       get done for MOB_ACCESS anyway during the update */
-    if (!opt_MOB_ACCESS) {
-       mob_ship(etu);
-       mob_sect(etu);
-       mob_plane(etu);
-       mob_land(etu);
-    }
-    if (opt_DEMANDUPDATE)
+    mob_inc_all(etu);
+
+    if (update_demand == UPD_DEMAND_SCHED
+       || update_demand == UPD_DEMAND_ASYNC)
        update_removewants();
     /* flush all mem file objects to disk */
     ef_flush(EF_NATION);
@@ -175,14 +128,15 @@ update_main(void *unused)
     ef_flush(EF_SHIP);
     ef_flush(EF_PLANE);
     ef_flush(EF_LAND);
+    unit_cargo_init();
     delete_old_announcements();
     delete_old_news();
-    /* Clear all the telegram flags */
-    for (cn = 0; cn < MAXNOC; cn++)
-       clear_telegram_is_new(cn);
-    update_pending = 0;
-    logerror("End update");
-    player_delete(player);
-    empth_exit();
-    /*NOTREACHED*/
+    delete_old_lostitems();
+    getrusage(RUSAGE_SELF, &rus2);
+    logerror("End update %g user %g system",
+            rus2.ru_utime.tv_sec + rus2.ru_utime.tv_usec / 1e6
+            - (rus1.ru_utime.tv_sec + rus1.ru_utime.tv_usec / 1e6),
+            rus2.ru_stime.tv_sec + rus2.ru_stime.tv_usec / 1e6
+            - (rus1.ru_stime.tv_sec + rus1.ru_stime.tv_usec / 1e6));
+    update_running = 0;
 }