]> git.pond.sub.org Git - empserver/blob - src/lib/update/finish.c
Merge dodistribute() parameters dist_i_cost, dist_e_cost
[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  */
35
36 #include <config.h>
37
38 #include <stdlib.h>
39 #include <sys/resource.h>
40 #include "distribute.h"
41 #include "path.h"
42 #include "update.h"
43
44 /* Used for building up distribution info */
45 struct distinfo {
46     double imcost;              /* import cost */
47     double excost;              /* export cost */
48 };
49
50 /* This is our global buffer of distribution pointers.  Note that
51  * We only malloc this once, and never again (until reboot time
52  * of course :) ) We do clear it each and every time. */
53 static struct distinfo *g_distptrs;
54
55 static void assemble_dist_paths(struct distinfo *distptrs);
56 static char *ReversePath(char *path);
57
58 void
59 finish_sects(int etu)
60 {
61     struct sctstr *sp;
62     struct natstr *np;
63     int n;
64     struct rusage rus1, rus2;
65     struct distinfo *infptr;
66
67     if (g_distptrs == NULL) {
68         logerror("First update since reboot, allocating buffer\n");
69         /* Allocate the information buffer */
70         g_distptrs = malloc(WORLD_SZ() * sizeof(*g_distptrs));
71         if (g_distptrs == NULL) {
72             logerror("malloc failed in finish_sects.\n");
73             return;
74         }
75     }
76
77     /* Wipe it clean */
78     memset(g_distptrs, 0, WORLD_SZ() * sizeof(*g_distptrs));
79
80     logerror("delivering...\n");
81     /* Do deliveries */
82     for (n = 0; NULL != (sp = getsectid(n)); n++) {
83         if (sp->sct_type == SCT_WATER)
84             continue;
85         if (sp->sct_own == 0)
86             continue;
87         np = getnatp(sp->sct_own);
88         if (np->nat_money < 0)
89             continue;
90         dodeliver(sp);
91     }
92     logerror("done delivering\n");
93
94     logerror("assembling paths...\n");
95     getrusage(RUSAGE_SELF, &rus1);
96
97     /* First, enable the best_path cacheing */
98     bp_enable_cachepath();
99
100     /* Now assemble the paths */
101     assemble_dist_paths(g_distptrs);
102
103     /* Now disable the best_path cacheing */
104     bp_disable_cachepath();
105
106     /* Now, clear the best_path cache that may have been created */
107     bp_clear_cachepath();
108
109     getrusage(RUSAGE_SELF, &rus2);
110     logerror("done assembling paths %g user %g system",
111              rus2.ru_utime.tv_sec + rus2.ru_utime.tv_usec / 1e6
112              - (rus1.ru_utime.tv_sec + rus1.ru_utime.tv_usec / 1e6),
113              rus2.ru_stime.tv_sec + rus2.ru_stime.tv_usec / 1e6
114              - (rus1.ru_stime.tv_sec + rus1.ru_stime.tv_usec / 1e6));
115
116     logerror("exporting...");
117     for (n = 0; NULL != (sp = getsectid(n)); n++) {
118         if (sp->sct_type == SCT_WATER || sp->sct_own == 0)
119             continue;
120         np = getnatp(sp->sct_own);
121         if (np->nat_money < 0)
122             continue;
123         /* Get the pointer */
124         infptr = &g_distptrs[sp->sct_uid];
125         dodistribute(sp, EXPORT, infptr->excost);
126     }
127     logerror("done exporting\n");
128
129     logerror("importing...");
130     for (n = 0; NULL != (sp = getsectid(n)); n++) {
131         /* Get the pointer (we do it first so we can free if needed) */
132         infptr = &g_distptrs[sp->sct_uid];
133         if (sp->sct_type == SCT_WATER || sp->sct_own == 0)
134             continue;
135         np = getnatp(sp->sct_own);
136         if (np->nat_money < 0)
137             continue;
138         dodistribute(sp, IMPORT, infptr->imcost);
139         sp->sct_off = 0;
140     }
141     logerror("done importing\n");
142
143 }
144
145 static void
146 assemble_dist_paths(struct distinfo *distptrs)
147 {
148     char *path, *p;
149     double d;
150     struct sctstr *sp;
151     struct sctstr *dist;
152     struct distinfo *infptr;
153     int n;
154     char buf[512];
155
156     for (n = 0; NULL != (sp = getsectid(n)); n++) {
157         if ((sp->sct_dist_x == sp->sct_x) && (sp->sct_dist_y == sp->sct_y))
158             continue;
159         /* Set the pointer */
160         infptr = &distptrs[sp->sct_uid];
161         /* now, get the dist sector */
162         dist = getsectp(sp->sct_dist_x, sp->sct_dist_y);
163         if (dist == NULL) {
164             logerror("Bad dist sect %d,%d for %d,%d !\n",
165                      sp->sct_dist_x, sp->sct_dist_y,
166                      sp->sct_x, sp->sct_y);
167             continue;
168         }
169         /* Now, get the best distribution path over roads */
170         /* Note we go from the dist center to the sector.  This gives
171            us the import path for that sector. */
172         path = BestDistPath(buf, dist, sp, &d);
173
174         /* Now, we have a path */
175         if (!path)
176             infptr->imcost = infptr->excost = -1.0;
177         else {
178             /* Save the import cost */
179             infptr->imcost = d;
180             /* Now, reverse the path */
181             p = ReversePath(path);
182             /* And walk the path back to the dist center to get the export
183                cost */
184             infptr->excost = pathcost(sp, p, MOB_MOVE);
185         }
186     }
187 }
188
189 static char *
190 ReversePath(char *path)
191 {
192     char *patharray = "aucdefjhigklmyopqrstbvwxnz";
193     static char new_path[512];
194     int ind;
195
196     if (path == NULL)
197         return NULL;
198
199     ind = strlen(path);
200     if (ind == 0)
201         return NULL;
202
203     if (path[ind - 1] == 'h')
204         ind--;
205
206     new_path[ind--] = '\0';
207     new_path[ind] = '\0';
208
209     while (ind >= 0) {
210         new_path[ind--] = patharray[*(path++) - 'a'];
211     }
212
213     return new_path;
214 }