]> git.pond.sub.org Git - empserver/blob - src/lib/update/finish.c
Update copyright notice
[empserver] / src / lib / update / finish.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2020, 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-2016
34  */
35
36 #include <config.h>
37
38 #include <stdlib.h>
39 #include <sys/resource.h>
40 #include "optlist.h"
41 #include "path.h"
42 #include "sect.h"
43 #include "prototypes.h"
44 #include "update.h"
45
46 static void assemble_dist_paths(double *);
47
48 void
49 finish_sects(int etu)
50 {
51     static double *import_cost;
52     struct sctstr *sp;
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_own || sp->sct_type == SCT_SANCT)
69             continue;
70         if (nat_budget[sp->sct_own].money < 0)
71             continue;
72         dodeliver(sp);
73     }
74     logerror("done delivering\n");
75
76     logerror("assembling paths...\n");
77     getrusage(RUSAGE_SELF, &rus1);
78     assemble_dist_paths(import_cost);
79     getrusage(RUSAGE_SELF, &rus2);
80     logerror("done assembling paths %g user %g system",
81              rus2.ru_utime.tv_sec + rus2.ru_utime.tv_usec / 1e6
82              - (rus1.ru_utime.tv_sec + rus1.ru_utime.tv_usec / 1e6),
83              rus2.ru_stime.tv_sec + rus2.ru_stime.tv_usec / 1e6
84              - (rus1.ru_stime.tv_sec + rus1.ru_stime.tv_usec / 1e6));
85
86     logerror("exporting...");
87     for (n = 0; NULL != (sp = getsectid(n)); n++) {
88         if (!sp->sct_own || sp->sct_type == SCT_SANCT)
89             continue;
90         if (nat_budget[sp->sct_own].money < 0)
91             continue;
92         dodistribute(sp, EXPORT, import_cost[n]);
93     }
94     logerror("done exporting\n");
95
96     logerror("importing...");
97     for (n = 0; NULL != (sp = getsectid(n)); n++) {
98         sp->sct_off = 0;
99         if (!sp->sct_own || sp->sct_type == SCT_SANCT)
100             continue;
101         if (nat_budget[sp->sct_own].money < 0)
102             continue;
103         dodistribute(sp, IMPORT, import_cost[n]);
104     }
105     logerror("done importing\n");
106
107 }
108
109 static int
110 distcmp(const void *p, const void *q)
111 {
112     int a = *(int *)p;
113     int b = *(int *)q;
114     struct sctstr *sp = (void *)empfile[EF_SECTOR].cache;
115     int d;
116
117     d = sp[b].sct_dist_y - sp[a].sct_dist_y;
118     if (d)
119         return d;
120     d = sp[b].sct_dist_x - sp[a].sct_dist_x;
121     if (d)
122         return d;
123     return b - a;
124 }
125
126 static void
127 assemble_dist_paths(double *import_cost)
128 {
129     struct sctstr *sp;
130     struct sctstr *dist;
131     int n;
132     static int *job;
133     int uid, i;
134     coord dx = 1, dy = 0;       /* invalid */
135
136     if (!job)
137         job = malloc(WORLD_SZ() * sizeof(*job));
138
139     n = 0;
140     for (uid = 0; NULL != (sp = getsectid(uid)); uid++) {
141         import_cost[uid] = -1;
142         if (sp->sct_dist_x == sp->sct_x && sp->sct_dist_y == sp->sct_y)
143             continue;
144         job[n++] = uid;
145     }
146
147 #ifdef PATH_FIND_STATS
148     printf("dist path reuse %zu bytes, %d/%d used\n",
149            WORLD_SZ() * sizeof(*job), n, WORLD_SZ());
150 #endif
151
152     qsort(job, n, sizeof(*job), distcmp);
153
154     for (i = 0; i < n; i++) {
155         uid = job[i];
156         sp = getsectid(uid);
157         dist = getsectp(sp->sct_dist_x, sp->sct_dist_y);
158         if (CANT_HAPPEN(!dist))
159             continue;
160         if (sp->sct_own != dist->sct_own)
161             continue;
162         if (sp->sct_dist_x != dx || sp->sct_dist_y != dy) {
163             dx = sp->sct_dist_x;
164             dy = sp->sct_dist_y;
165             path_find_from(dx, dy, dist->sct_own, MOB_MOVE);
166         }
167         import_cost[uid] = path_find_to(sp->sct_x, sp->sct_y);
168     }
169     path_find_print_stats();
170 }