]> git.pond.sub.org Git - empserver/blob - src/lib/subs/nuke.c
Import of Empire 4.2.12
[empserver] / src / lib / subs / nuke.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2000, 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 the "LEGAL", "LICENSE", "CREDITS" and "README" files for all the
23  *  related information and legal notices. It is expected that any future
24  *  projects/authors will amend these files as needed.
25  *
26  *  ---
27  *
28  *  nuke.c: Nuke post-read and pre-write data massage
29  * 
30  *  Known contributors to this file:
31  *     Dave Pare, 1989
32  *     Steve McClure, 1996
33  */
34
35 #include "misc.h"
36 #include "player.h"
37 #include "var.h"
38 #include "nuke.h"
39 #include "sect.h"
40 #include "xy.h"
41 #include "nsc.h"
42 #include "file.h"
43 #include "nat.h"
44 #include "deity.h"
45 #include "prototypes.h"
46
47 /*ARGSUSED*/
48 int
49 nuk_postread(int n, s_char *ptr)
50 {
51         struct  nukstr *np = (struct nukstr *) ptr;
52
53         if (np->nuk_uid != n) {
54                 logerror("nuk_postread: Error - %d != %d, zeroing.\n", np->nuk_uid, n);
55                 bzero(ptr, sizeof(struct nukstr));
56         }
57         player->owner = (player->god || np->nuk_own == player->cnum);
58         return 1;
59 }
60
61 /*ARGSUSED*/
62 int
63 nuk_prewrite(int n, s_char *ptr)
64 {
65         struct nukstr *np = (struct nukstr *) ptr;
66         struct nukstr nuke;
67
68         np->ef_type = EF_NUKE;
69         np->nuk_uid = n;
70
71         time(&np->nuk_timestamp);
72
73         getnuke(n, &nuke);
74
75         return 1;
76 }
77
78 void
79 nuk_init(int n, s_char *ptr)
80 {
81         struct  nukstr *np = (struct nukstr *) ptr;
82
83         np->ef_type = EF_NUKE;
84         np->nuk_uid = n;
85         np->nuk_own = 0;
86 }
87
88 void
89 nuk_add(coord x, coord y, int type, int num)
90 {
91         struct  nukstr nuke;
92         struct  sctstr sect;
93         int     nuk_uid;
94         natid   own;
95         int     n;
96
97         /* getsect in case of world wraparound */
98         getsect(x, y, &sect);
99         if (!(own = sect.sct_own))
100                 return;
101         x = sect.sct_x;
102         y = sect.sct_y;
103
104         /*
105          * either find a stockpile in x,y or add a new one
106          */
107         nuk_uid = -1;
108         for (n = 0; getnuke(n, &nuke); n++) {
109             if (nuke.nuk_own == own) {
110                 if (nuke.nuk_x == x && nuke.nuk_y == y) {
111                     nuk_uid = n;
112                     break;
113                 }
114             } else if (nuke.nuk_own == 0 && nuk_uid == -1)
115                 nuk_uid = n;
116         }
117         if (nuk_uid == -1)
118             nuk_uid = n;
119         if (getnuke(nuk_uid, &nuke) == 0) {
120             ef_extend(EF_NUKE, 10);
121             bzero((s_char *)&nuke, sizeof(struct nukstr));
122             nuke.nuk_uid = nuk_uid;
123             nuke.nuk_ship = -1;
124             nuke.nuk_n = 0;
125             makenotlost(EF_NUKE, nuke.nuk_own, nuke.nuk_uid, nuke.nuk_x, nuke.nuk_y);
126         }
127         nuke.nuk_x = x;
128         nuke.nuk_y = y;
129         nuke.nuk_own = own;
130         nuke.nuk_types[type] += num;
131         nuke.nuk_n += num;
132
133         if (!putnuke(nuke.nuk_uid, &nuke))
134             pr("Problem with the nuclear stockpiles, tell the deity.\n");
135
136 }
137
138 void
139 nuk_delete(struct nukstr *np, int type, int num)
140 {
141         if (np->nuk_types[type] < num)
142                 num = np->nuk_types[type];
143         np->nuk_types[type] -= num;
144         np->nuk_n -= num;
145         if (np->nuk_n <= 0) {
146                 makelost(EF_NUKE, np->nuk_own, np->nuk_uid, np->nuk_x, np->nuk_y);
147                 np->nuk_own = 0;
148         }
149         putnuke(np->nuk_uid, np);
150 }