]> git.pond.sub.org Git - empserver/blob - src/lib/subs/land.c
Update lost file from prewrite callbacks
[empserver] / src / lib / subs / land.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2008, 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  *  land.c: Land unit post-read and pre-write
29  * 
30  *  Known contributors to this file:
31  *     Steve McClure, 1996
32  */
33
34 #include <config.h>
35
36 #include "file.h"
37 #include "land.h"
38 #include "lost.h"
39 #include "misc.h"
40 #include "nsc.h"
41 #include "optlist.h"
42 #include "plane.h"
43 #include "player.h"
44 #include "prototypes.h"
45 #include "ship.h"
46
47 void
48 lnd_postread(int n, void *ptr)
49 {
50     struct lndstr *llp = ptr;
51     struct shpstr theship;
52     struct lndstr theland;
53
54     if (llp->lnd_uid != n) {
55         logerror("lnd_postread: Error - %d != %d, zeroing.\n",
56                  llp->lnd_uid, n);
57         memset(llp, 0, sizeof(struct lndstr));
58     }
59     if (llp->lnd_ship >= 0 && llp->lnd_own
60         && llp->lnd_effic >= LAND_MINEFF) {
61         if (getship(llp->lnd_ship, &theship)
62             && (theship.shp_effic >= SHIP_MINEFF)) {
63             /* wooof!  Carriers are a pain */
64             if (llp->lnd_mission) {
65                 /*
66                  *  If the unit is on a mission centered
67                  *  on it's loc, the op-area travels with
68                  *  the unit.
69                  */
70                 if ((llp->lnd_opx == llp->lnd_x) &&
71                     (llp->lnd_opy == llp->lnd_y)) {
72                     llp->lnd_opx = theship.shp_x;
73                     llp->lnd_opy = theship.shp_y;
74                 }
75             }
76             if (llp->lnd_x != theship.shp_x || llp->lnd_y != theship.shp_y)
77                 time(&llp->lnd_timestamp);
78             llp->lnd_x = theship.shp_x;
79             llp->lnd_y = theship.shp_y;
80         }
81     }
82     if (llp->lnd_land >= 0 && llp->lnd_own
83         && llp->lnd_effic >= LAND_MINEFF) {
84         if (getland(llp->lnd_land, &theland)
85             && (theland.lnd_effic >= LAND_MINEFF)) {
86             /* wooof!  Carriers are a pain */
87             if (llp->lnd_mission) {
88                 /*
89                  *  If the unit is on a mission centered
90                  *  on it's loc, the op-area travels with
91                  *  the unit.
92                  */
93                 if ((llp->lnd_opx == llp->lnd_x) &&
94                     (llp->lnd_opy == llp->lnd_y)) {
95                     llp->lnd_opx = theland.lnd_x;
96                     llp->lnd_opy = theland.lnd_y;
97                 }
98             }
99             if (llp->lnd_x != theland.lnd_x || llp->lnd_y != theland.lnd_y)
100                 time(&llp->lnd_timestamp);
101             llp->lnd_x = theland.lnd_x;
102             llp->lnd_y = theland.lnd_y;
103         }
104     }
105     if (opt_MOB_ACCESS)
106         lnd_do_upd_mob(llp);
107
108     player->owner = (player->god || llp->lnd_own == player->cnum);
109 }
110
111 void
112 lnd_prewrite(int n, void *old, void *new)
113 {
114     struct lndstr *oldlp = old;
115     struct lndstr *llp = new;
116     natid own = llp->lnd_own;
117     struct lndstr *lp;
118     struct plnstr *pp;
119     int i;
120
121     if (llp->lnd_own && llp->lnd_effic < LAND_MINEFF) {
122         own = 0;
123
124         for (i = 0; NULL != (lp = getlandp(i)); i++) {
125             if (lp->lnd_own && lp->lnd_land == n) {
126                 mpr(lp->lnd_own, "%s MIA!\n", prland(lp));
127                 makelost(EF_LAND, lp->lnd_own, lp->lnd_uid,
128                          lp->lnd_x, lp->lnd_y);
129                 lp->lnd_own = 0;
130                 lp->lnd_effic = 0;
131                 lp->lnd_ship = -1;
132                 lp->lnd_land = -1;
133                 putland(lp->lnd_uid, lp);
134             }
135         }
136         for (i = 0; NULL != (pp = getplanep(i)); i++) {
137             if (pp->pln_own && pp->pln_land == n) {
138                 mpr(pp->pln_own, "%s MIA!\n", prplane(pp));
139                 makelost(EF_PLANE, pp->pln_own, pp->pln_uid,
140                          pp->pln_x, pp->pln_y);
141                 pp->pln_own = 0;
142                 pp->pln_effic = 0;
143                 pp->pln_ship = -1;
144                 pp->pln_land = -1;
145                 putplane(pp->pln_uid, pp);
146             }
147         }
148     } else {
149         item_prewrite(llp->lnd_item);
150     }
151
152     /* We've avoided assigning to llp->lnd_own, in case oldsp == sp */
153     if (oldlp->lnd_own != own) {
154         if (oldlp->lnd_own)
155             makelost(EF_LAND, oldlp->lnd_own,
156                      llp->lnd_uid, llp->lnd_x, llp->lnd_y);
157         if (own)
158             makenotlost(EF_LAND, own,
159                         llp->lnd_uid, llp->lnd_x, llp->lnd_y);
160     }
161
162     llp->lnd_own = own;
163 }
164
165 char *
166 prland(struct lndstr *lp)
167 {
168     return prbuf("%s #%d", lchr[(int)lp->lnd_type].l_name, lp->lnd_uid);
169 }