]> git.pond.sub.org Git - empserver/blob - src/lib/update/finish.c
781fd80f555cfc018dce9c43ad9322614ada66e7
[empserver] / src / lib / update / finish.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2010, 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  *  finish.c: Finish the update
29  *
30  *  Known contributors to this file:
31  *     Dave Pare, 1986
32  *     Thomas Ruschak, 1993
33  *     Steve McClure, 1998
34  *     Markus Armbruster, 2004-2011
35  */
36
37 #include <config.h>
38
39 #include <stdlib.h>
40 #include <sys/resource.h>
41 #include "distribute.h"
42 #include "path.h"
43 #include "update.h"
44
45 static void assemble_dist_paths(double *);
46
47 void
48 finish_sects(int etu)
49 {
50     static double *import_cost;
51     struct sctstr *sp;
52     struct natstr *np;
53     int n;
54     struct rusage rus1, rus2;
55
56     if (import_cost == NULL) {
57         logerror("First update since reboot, allocating buffer\n");
58         import_cost = malloc(WORLD_SZ() * sizeof(*import_cost));
59         if (import_cost == NULL) {
60             logerror("malloc failed in finish_sects.\n");
61             return;
62         }
63     }
64
65     logerror("delivering...\n");
66     /* Do deliveries */
67     for (n = 0; NULL != (sp = getsectid(n)); n++) {
68         if (sp->sct_type == SCT_WATER)
69             continue;
70         if (sp->sct_own == 0)
71             continue;
72         np = getnatp(sp->sct_own);
73         if (np->nat_money < 0)
74             continue;
75         dodeliver(sp);
76     }
77     logerror("done delivering\n");
78
79     logerror("assembling paths...\n");
80     getrusage(RUSAGE_SELF, &rus1);
81
82     /* First, enable the best_path cacheing */
83     bp_enable_cachepath();
84
85     /* Now assemble the paths */
86     assemble_dist_paths(import_cost);
87
88     /* Now disable the best_path cacheing */
89     bp_disable_cachepath();
90
91     /* Now, clear the best_path cache that may have been created */
92     bp_clear_cachepath();
93
94     getrusage(RUSAGE_SELF, &rus2);
95     logerror("done assembling paths %g user %g system",
96              rus2.ru_utime.tv_sec + rus2.ru_utime.tv_usec / 1e6
97              - (rus1.ru_utime.tv_sec + rus1.ru_utime.tv_usec / 1e6),
98              rus2.ru_stime.tv_sec + rus2.ru_stime.tv_usec / 1e6
99              - (rus1.ru_stime.tv_sec + rus1.ru_stime.tv_usec / 1e6));
100
101     logerror("exporting...");
102     for (n = 0; NULL != (sp = getsectid(n)); n++) {
103         if (sp->sct_type == SCT_WATER || sp->sct_own == 0)
104             continue;
105         np = getnatp(sp->sct_own);
106         if (np->nat_money < 0)
107             continue;
108         dodistribute(sp, EXPORT, import_cost[n]);
109     }
110     logerror("done exporting\n");
111
112     logerror("importing...");
113     for (n = 0; NULL != (sp = getsectid(n)); n++) {
114         if (sp->sct_type == SCT_WATER || sp->sct_own == 0)
115             continue;
116         np = getnatp(sp->sct_own);
117         if (np->nat_money < 0)
118             continue;
119         dodistribute(sp, IMPORT, import_cost[n]);
120         sp->sct_off = 0;
121     }
122     logerror("done importing\n");
123
124 }
125
126 static void
127 assemble_dist_paths(double *import_cost)
128 {
129     char *path;
130     double d;
131     struct sctstr *sp;
132     struct sctstr *dist;
133     int n;
134     char buf[512];
135
136     for (n = 0; NULL != (sp = getsectid(n)); n++) {
137         import_cost[n] = -1;
138         if ((sp->sct_dist_x == sp->sct_x) && (sp->sct_dist_y == sp->sct_y))
139             continue;
140         dist = getsectp(sp->sct_dist_x, sp->sct_dist_y);
141         if (CANT_HAPPEN(!dist))
142             continue;
143         /* Now, get the best distribution path over roads */
144         /* Note we go from the dist center to the sector.  This gives
145            us the import path for that sector. */
146         path = BestDistPath(buf, dist, sp, &d);
147         if (path)
148             import_cost[n] = d;
149     }
150 }