]> git.pond.sub.org Git - empserver/blob - src/lib/subs/land.c
subs: Simplify MOB_ACCESS mobility update
[empserver] / src / lib / subs / land.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  *  land.c: Land unit post-read and pre-write
28  *
29  *  Known contributors to this file:
30  *     Steve McClure, 1996
31  *     Markus Armbruster, 2004-2016
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 "optlist.h"
41 #include "player.h"
42 #include "prototypes.h"
43 #include "server.h"
44 #include "unit.h"
45 #include "update.h"
46
47 void
48 lnd_postread(int n, void *ptr)
49 {
50     struct lndstr *lp = ptr;
51
52     if (lp->lnd_uid != n) {
53         logerror("lnd_postread: Error - %d != %d, zeroing.\n",
54                  lp->lnd_uid, n);
55         memset(lp, 0, sizeof(struct lndstr));
56     }
57
58     player->owner = (player->god || lp->lnd_own == player->cnum);
59
60     if (opt_MOB_ACCESS && lp->lnd_own && !update_running)
61         mob_inc_land(lp, game_tick_to_now(&lp->lnd_access));
62 }
63
64 void
65 lnd_prewrite(int n, void *old, void *new)
66 {
67     struct lndstr *oldlp = old;
68     struct lndstr *lp = new;
69     natid own = lp->lnd_effic < LAND_MINEFF ? 0 : lp->lnd_own;
70     int ship = lp->lnd_ship;
71     int land = lp->lnd_land;
72
73     /* Be careful with writing to *lp, in case oldlp == lp */
74
75     if (!own) {
76         lp->lnd_effic = 0;
77         ship = land = -1;
78     }
79     item_prewrite(lp->lnd_item);
80
81     if (CANT_HAPPEN(ship >= 0 && land >= 0))
82         land = -1;
83     if (oldlp->lnd_ship != ship)
84         lnd_carrier_change(lp, EF_SHIP, oldlp->lnd_ship, ship);
85     if (oldlp->lnd_land != land)
86         lnd_carrier_change(lp, EF_LAND, oldlp->lnd_land, land);
87
88     if (oldlp->lnd_own != own) {
89         lost_and_found(EF_LAND, oldlp->lnd_own, own,
90                        lp->lnd_uid, lp->lnd_x, lp->lnd_y);
91         CANT_HAPPEN(!oldlp->lnd_own
92                     && unit_update_cargo((struct empobj *)oldlp));
93     }
94
95     lp->lnd_own = own;
96     lp->lnd_ship = ship;
97     lp->lnd_land = land;
98     if (!own || lp->lnd_x != oldlp->lnd_x || lp->lnd_y != oldlp->lnd_y)
99         unit_update_cargo((struct empobj *)lp);
100 }
101
102 char *
103 prland(struct lndstr *lp)
104 {
105     return prbuf("%s #%d", lchr[(int)lp->lnd_type].l_name, lp->lnd_uid);
106 }