]> git.pond.sub.org Git - empserver/blob - src/lib/update/finish.c
include: Merge distribute.h into update.h
[empserver] / src / lib / update / finish.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2016, Dave Pare, Jeff Bailey, Thomas Ruschak,
4  *                Ken Stevens, Steve McClure, Markus Armbruster
5  *
6  *  Empire 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 3 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, see <http://www.gnu.org/licenses/>.
18  *
19  *  ---
20  *
21  *  See files README, COPYING and CREDITS in the root of the source
22  *  tree for related information and legal notices.  It is expected
23  *  that future projects/authors will amend these files as needed.
24  *
25  *  ---
26  *
27  *  finish.c: Finish the update
28  *
29  *  Known contributors to this file:
30  *     Dave Pare, 1986
31  *     Thomas Ruschak, 1993
32  *     Steve McClure, 1998
33  *     Markus Armbruster, 2004-2011
34  */
35
36 #include <config.h>
37
38 #include <stdlib.h>
39 #include <sys/resource.h>
40 #include "file.h"
41 #include "nat.h"
42 #include "optlist.h"
43 #include "path.h"
44 #include "sect.h"
45 #include "prototypes.h"
46 #include "update.h"
47
48 static void assemble_dist_paths(double *);
49
50 void
51 finish_sects(int etu)
52 {
53     static double *import_cost;
54     struct sctstr *sp;
55     struct natstr *np;
56     int n;
57     struct rusage rus1, rus2;
58
59     if (import_cost == NULL) {
60         logerror("First update since reboot, allocating buffer\n");
61         import_cost = malloc(WORLD_SZ() * sizeof(*import_cost));
62         if (import_cost == NULL) {
63             logerror("malloc failed in finish_sects.\n");
64             return;
65         }
66     }
67
68     logerror("delivering...\n");
69     /* Do deliveries */
70     for (n = 0; NULL != (sp = getsectid(n)); n++) {
71         if (sp->sct_type == SCT_WATER)
72             continue;
73         if (sp->sct_own == 0)
74             continue;
75         np = getnatp(sp->sct_own);
76         if (np->nat_money < 0)
77             continue;
78         dodeliver(sp);
79     }
80     logerror("done delivering\n");
81
82     logerror("assembling paths...\n");
83     getrusage(RUSAGE_SELF, &rus1);
84     assemble_dist_paths(import_cost);
85     getrusage(RUSAGE_SELF, &rus2);
86     logerror("done assembling paths %g user %g system",
87              rus2.ru_utime.tv_sec + rus2.ru_utime.tv_usec / 1e6
88              - (rus1.ru_utime.tv_sec + rus1.ru_utime.tv_usec / 1e6),
89              rus2.ru_stime.tv_sec + rus2.ru_stime.tv_usec / 1e6
90              - (rus1.ru_stime.tv_sec + rus1.ru_stime.tv_usec / 1e6));
91
92     logerror("exporting...");
93     for (n = 0; NULL != (sp = getsectid(n)); n++) {
94         if (!sp->sct_own)
95             continue;
96         np = getnatp(sp->sct_own);
97         if (np->nat_money < 0)
98             continue;
99         dodistribute(sp, EXPORT, import_cost[n]);
100     }
101     logerror("done exporting\n");
102
103     logerror("importing...");
104     for (n = 0; NULL != (sp = getsectid(n)); n++) {
105         sp->sct_off = 0;
106         if (!sp->sct_own)
107             continue;
108         np = getnatp(sp->sct_own);
109         if (np->nat_money < 0)
110             continue;
111         dodistribute(sp, IMPORT, import_cost[n]);
112     }
113     logerror("done importing\n");
114
115 }
116
117 static int
118 distcmp(const void *p, const void *q)
119 {
120     int a = *(int *)p;
121     int b = *(int *)q;
122     struct sctstr *sp = (void *)empfile[EF_SECTOR].cache;
123     int d;
124
125     d = sp[b].sct_dist_y - sp[a].sct_dist_y;
126     if (d)
127         return d;
128     d = sp[b].sct_dist_x - sp[a].sct_dist_x;
129     if (d)
130         return d;
131     return b - a;
132 }
133
134 static void
135 assemble_dist_paths(double *import_cost)
136 {
137     struct sctstr *sp;
138     struct sctstr *dist;
139     int n;
140     static int *job;
141     int uid, i;
142     coord dx = 1, dy = 0;       /* invalid */
143
144     if (!job)
145         job = malloc(WORLD_SZ() * sizeof(*job));
146
147     n = 0;
148     for (uid = 0; NULL != (sp = getsectid(uid)); uid++) {
149         import_cost[uid] = -1;
150         if (sp->sct_dist_x == sp->sct_x && sp->sct_dist_y == sp->sct_y)
151             continue;
152         job[n++] = uid;
153     }
154
155 #ifdef PATH_FIND_STATS
156     printf("dist path reuse %zu bytes, %d/%d used\n",
157            WORLD_SZ() * sizeof(*job), n, WORLD_SZ());
158 #endif
159
160     qsort(job, n, sizeof(*job), distcmp);
161
162     for (i = 0; i < n; i++) {
163         uid = job[i];
164         sp = getsectid(uid);
165         dist = getsectp(sp->sct_dist_x, sp->sct_dist_y);
166         if (CANT_HAPPEN(!dist))
167             continue;
168         if (sp->sct_own != dist->sct_own)
169             continue;
170         if (sp->sct_dist_x != dx || sp->sct_dist_y != dy) {
171             dx = sp->sct_dist_x;
172             dy = sp->sct_dist_y;
173             path_find_from(dx, dy, dist->sct_own, MOB_MOVE);
174         }
175         import_cost[uid] = path_find_to(sp->sct_x, sp->sct_y);
176     }
177     path_find_print_stats();
178 }