]> 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-2017, 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 "nat.h"
41 #include "optlist.h"
42 #include "path.h"
43 #include "sect.h"
44 #include "prototypes.h"
45 #include "update.h"
46
47 static void assemble_dist_paths(double *);
48
49 void
50 finish_sects(int etu)
51 {
52     static double *import_cost;
53     struct sctstr *sp;
54     int n;
55     struct rusage rus1, rus2;
56
57     if (import_cost == NULL) {
58         logerror("First update since reboot, allocating buffer\n");
59         import_cost = malloc(WORLD_SZ() * sizeof(*import_cost));
60         if (import_cost == NULL) {
61             logerror("malloc failed in finish_sects.\n");
62             return;
63         }
64     }
65
66     logerror("delivering...\n");
67     /* Do deliveries */
68     for (n = 0; NULL != (sp = getsectid(n)); n++) {
69         if (!sp->sct_own || sp->sct_type == SCT_SANCT)
70             continue;
71         if (nat_budget[sp->sct_own].money < 0)
72             continue;
73         dodeliver(sp);
74     }
75     logerror("done delivering\n");
76
77     logerror("assembling paths...\n");
78     getrusage(RUSAGE_SELF, &rus1);
79     assemble_dist_paths(import_cost);
80     getrusage(RUSAGE_SELF, &rus2);
81     logerror("done assembling paths %g user %g system",
82              rus2.ru_utime.tv_sec + rus2.ru_utime.tv_usec / 1e6
83              - (rus1.ru_utime.tv_sec + rus1.ru_utime.tv_usec / 1e6),
84              rus2.ru_stime.tv_sec + rus2.ru_stime.tv_usec / 1e6
85              - (rus1.ru_stime.tv_sec + rus1.ru_stime.tv_usec / 1e6));
86
87     logerror("exporting...");
88     for (n = 0; NULL != (sp = getsectid(n)); n++) {
89         if (!sp->sct_own || sp->sct_type == SCT_SANCT)
90             continue;
91         if (nat_budget[sp->sct_own].money < 0)
92             continue;
93         dodistribute(sp, EXPORT, import_cost[n]);
94     }
95     logerror("done exporting\n");
96
97     logerror("importing...");
98     for (n = 0; NULL != (sp = getsectid(n)); n++) {
99         sp->sct_off = 0;
100         if (!sp->sct_own || sp->sct_type == SCT_SANCT)
101             continue;
102         if (nat_budget[sp->sct_own].money < 0)
103             continue;
104         dodistribute(sp, IMPORT, import_cost[n]);
105     }
106     logerror("done importing\n");
107
108 }
109
110 static int
111 distcmp(const void *p, const void *q)
112 {
113     int a = *(int *)p;
114     int b = *(int *)q;
115     struct sctstr *sp = (void *)empfile[EF_SECTOR].cache;
116     int d;
117
118     d = sp[b].sct_dist_y - sp[a].sct_dist_y;
119     if (d)
120         return d;
121     d = sp[b].sct_dist_x - sp[a].sct_dist_x;
122     if (d)
123         return d;
124     return b - a;
125 }
126
127 static void
128 assemble_dist_paths(double *import_cost)
129 {
130     struct sctstr *sp;
131     struct sctstr *dist;
132     int n;
133     static int *job;
134     int uid, i;
135     coord dx = 1, dy = 0;       /* invalid */
136
137     if (!job)
138         job = malloc(WORLD_SZ() * sizeof(*job));
139
140     n = 0;
141     for (uid = 0; NULL != (sp = getsectid(uid)); uid++) {
142         import_cost[uid] = -1;
143         if (sp->sct_dist_x == sp->sct_x && sp->sct_dist_y == sp->sct_y)
144             continue;
145         job[n++] = uid;
146     }
147
148 #ifdef PATH_FIND_STATS
149     printf("dist path reuse %zu bytes, %d/%d used\n",
150            WORLD_SZ() * sizeof(*job), n, WORLD_SZ());
151 #endif
152
153     qsort(job, n, sizeof(*job), distcmp);
154
155     for (i = 0; i < n; i++) {
156         uid = job[i];
157         sp = getsectid(uid);
158         dist = getsectp(sp->sct_dist_x, sp->sct_dist_y);
159         if (CANT_HAPPEN(!dist))
160             continue;
161         if (sp->sct_own != dist->sct_own)
162             continue;
163         if (sp->sct_dist_x != dx || sp->sct_dist_y != dy) {
164             dx = sp->sct_dist_x;
165             dy = sp->sct_dist_y;
166             path_find_from(dx, dy, dist->sct_own, MOB_MOVE);
167         }
168         import_cost[uid] = path_find_to(sp->sct_x, sp->sct_y);
169     }
170     path_find_print_stats();
171 }