DEADSOFTWARE

b0f079a715fef9fcd7bb9f2375bfa63d4cb5ccbe
[flatwaifu.git] / src / items.c
1 /*
2 Copyright (C) Prikol Software 1996-1997
3 Copyright (C) Aleksey Volynskov 1996-1997
4 Copyright (C) <ARembo@gmail.com> 2011
6 This file is part of the Doom2D:Rembo project.
8 Doom2D:Rembo is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License version 2 as
10 published by the Free Software Foundation.
12 Doom2D:Rembo is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, see <http://www.gnu.org/licenses/> or
19 write to the Free Software Foundation, Inc.,
20 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21 */
23 #include "glob.h"
24 #include <stdlib.h>
25 #include "error.h"
26 #include "view.h"
27 #include "items.h"
28 #include "fx.h"
29 #include "player.h"
30 #include "monster.h"
31 #include "things.h"
32 #include "misc.h"
33 #include "map.h"
34 #include "my.h"
35 #include "files.h"
36 #include "game.h"
37 item_t it[MAXITEM];
39 static void *snd[4];
40 static int tsndtm, rsndtm;
42 int itm_rtime = 1092;
44 void IT_savegame (FILE *h) {
45 int i, n;
46 for (n = MAXITEM - 1; n >= 0 && it[n].t == 0; n--) {
47 // empty
48 }
49 n += 1;
50 myfwrite32(n, h);
51 for (i = 0; i < n; i++) {
52 myfwrite32(it[i].o.x, h);
53 myfwrite32(it[i].o.y, h);
54 myfwrite32(it[i].o.xv, h);
55 myfwrite32(it[i].o.yv, h);
56 myfwrite32(it[i].o.vx, h);
57 myfwrite32(it[i].o.vy, h);
58 myfwrite32(it[i].o.r, h);
59 myfwrite32(it[i].o.h, h);
60 myfwrite32(it[i].t, h);
61 myfwrite32(it[i].s, h);
62 }
63 myfwrite32(itm_rtime, h);
64 }
66 void IT_loadgame (FILE *h) {
67 int i, n;
68 n = myfread32(h);
69 for (i = 0; i < n; i++) {
70 it[i].o.x = myfread32(h);
71 it[i].o.y = myfread32(h);
72 it[i].o.xv = myfread32(h);
73 it[i].o.yv = myfread32(h);
74 it[i].o.vx = myfread32(h);
75 it[i].o.vy = myfread32(h);
76 it[i].o.r = myfread32(h);
77 it[i].o.h = myfread32(h);
78 it[i].t = myfread32(h);
79 it[i].s = myfread32(h);
80 }
81 itm_rtime = myfread32(h);
82 }
84 void IT_alloc (void) {
85 int i, j, n;
86 static char nm[][6] = {
87 "ITEMUP", "WPNUP", "GETPOW", "ITMBK"
88 };
89 for (i = 0; i < 4; ++i) {
90 snd[i] = Z_getsnd(nm[i]);
91 }
92 for (i = 0; i < MAXITEM; ++i) {
93 it[i].o.r = 10;
94 it[i].o.h = 8;
95 }
96 }
98 void IT_init (void) {
99 int i;
100 for (i = 0; i < MAXITEM; ++i) {
101 it[i].t = I_NONE;
102 it[i].o.xv = 0;
103 it[i].o.yv = 0;
104 it[i].o.vx = 0;
105 it[i].o.vy = 0;
107 tsndtm = 0;
108 rsndtm = 0;
111 int IT_load (FILE *h) {
112 int m, i, j;
113 old_thing_t t;
114 switch (blk.t) {
115 case MB_THING:
116 for (i = 0; blk.sz > 0; ++i, blk.sz -= 8) {
117 t.x = myfread16(h);
118 t.y = myfread16(h);
119 t.t = myfread16(h);
120 t.f = myfread16(h);
121 it[i].o.x = t.x;
122 it[i].o.y = t.y;
123 it[i].t = t.t;
124 it[i].s = t.f;
125 if (it[i].t && (it[i].s & THF_DM) && !g_dm) {
126 it[i].t=0;
129 m = i;
130 for (i = 0, j = -1; i < m; ++i) {
131 if (it[i].t == TH_PLR1) {
132 j = i;
133 it[i].t = 0;
136 if (!g_dm) {
137 if (j == -1) {
138 ERR_fatal("Предмет игрок_1 не найден");
140 dm_pos[0].x = it[j].o.x;
141 dm_pos[0].y = it[j].o.y;
142 dm_pos[0].d = it[j].s & THF_DIR;
144 for (i = 0, j = -1; i < m; ++i) {
145 if (it[i].t == TH_PLR2) {
146 j = i;
147 it[i].t = 0;
150 if (!g_dm && _2pl) {
151 if (j == -1) {
152 ERR_fatal("Предмет игрок_2 не найден");
154 dm_pos[1].x = it[j].o.x;
155 dm_pos[1].y = it[j].o.y;
156 dm_pos[1].d = it[j].s & THF_DIR;
158 for (i = 0, j = 0; i < m; ++i) {
159 if (it[i].t == TH_DMSTART) {
160 if (g_dm) {
161 dm_pos[j].x = it[i].o.x;
162 dm_pos[j].y = it[i].o.y;
163 dm_pos[j].d = it[i].s & THF_DIR;
165 it[i].t = 0;
166 ++j;
169 if (g_dm && j < 2) {
170 ERR_fatal("Меньше 2-ух точек DM");
172 if (g_dm) {
173 dm_pnum = j;
174 dm_pl1p = myrand(dm_pnum);
175 do {
176 dm_pl2p = myrand(dm_pnum);
177 } while (dm_pl2p == dm_pl1p);
178 } else {
179 dm_pl1p = 0;
180 dm_pl2p = 1;
181 dm_pnum = 2;
183 PL_spawn(&pl1, dm_pos[dm_pl1p].x, dm_pos[dm_pl1p].y, dm_pos[dm_pl1p].d);
184 if (_2pl) {
185 PL_spawn(&pl2, dm_pos[dm_pl2p].x, dm_pos[dm_pl2p].y, dm_pos[dm_pl2p].d);
187 for (i = 0; i < m; ++i) {
188 if (it[i].t >= TH_CLIP && it[i].t < TH_DEMON) {
189 it[i].s = 0;
190 it[i].t = it[i].t - TH_CLIP + I_CLIP;
191 if (it[i].t >= I_KEYR && it[i].t <= I_KEYB) {
192 it[i].t |= 0x8000;
194 } else if (it[i].t >= TH_DEMON) {
195 MN_spawn(it[i].o.x, it[i].o.y, it[i].s & THF_DIR, it[i].t - TH_DEMON + MN_DEMON);
196 it[i].t = 0;
199 return 1;
201 return 0;
204 static void takesnd (int t) {
205 if(tsndtm) return;
206 t&=0x7FFF;
207 if(t<=I_CELP || (t>=I_BPACK && t<=I_BFG) || t==I_GUN2)
208 {tsndtm=Z_sound(snd[1],128);return;}
209 if(t==I_MEGA || t==I_INVL || t==I_SUPER)
210 {tsndtm=Z_sound(snd[2],192);return;}
211 tsndtm=Z_sound(snd[0], 255);
214 void IT_act (void) {
215 int i,j;
217 if(tsndtm) --tsndtm;
218 if(rsndtm) --rsndtm;
219 for(i=0;i<MAXITEM;++i) if(it[i].t)
220 if(it[i].s<0) {
221 if(++it[i].s==-8) {
222 FX_ifog(it[i].o.x,it[i].o.y);
223 if(!rsndtm) rsndtm=Z_sound(snd[3],128);
225 }else{
226 switch(it[i].t) {
227 case I_ARM1: case I_ARM2:
228 if(++it[i].s>=18) it[i].s=0; break;
229 case I_MEGA: case I_INVL:
230 case I_SUPER: case I_RTORCH: case I_GTORCH: case I_BTORCH:
231 if(++it[i].s>=8) it[i].s=0; break;
232 case I_GOR1: case I_FCAN:
233 if(++it[i].s>=6) it[i].s=0; break;
235 if(it[i].t&0x8000) {
236 if((j=Z_moveobj(&it[i].o))&Z_FALLOUT) {it[i].t=0;continue;}
237 else if(j&Z_HITWATER) Z_splash(&it[i].o,it[i].o.r+it[i].o.h);
239 if(Z_overlap(&it[i].o,&pl1.o))
240 if(PL_give(&pl1,it[i].t&0x7FFF)) {
241 takesnd(it[i].t);
242 if(_2pl) if((it[i].t&0x7FFF)>=I_KEYR && (it[i].t&0x7FFF)<=I_KEYB) continue;
243 if(!(it[i].s=-itm_rtime) || (it[i].t&0x8000)) it[i].t=0;
244 continue;
246 if(_2pl) if(Z_overlap(&it[i].o,&pl2.o))
247 if(PL_give(&pl2,it[i].t&0x7FFF)) {
248 takesnd(it[i].t);
249 if((it[i].t&0x7FFF)>=I_KEYR && (it[i].t&0x7FFF)<=I_KEYB) continue;
250 if(!(it[i].s=-itm_rtime) || (it[i].t&0x8000)) it[i].t=0;
251 continue;
256 void IT_spawn (int x,int y,int t) {
257 int i;
259 for(i=0;i<MAXITEM;++i) if(!it[i].t) {
260 it[i].t=t|0x8000;it[i].s=0;
261 it[i].o.x=x;it[i].o.y=y;
262 it[i].o.xv=it[i].o.yv=it[i].o.vx=it[i].o.vy=0;
263 it[i].o.r=10;it[i].o.h=8;
264 return;
268 void IT_drop_ammo (int t, int n, int x, int y) {
269 static int an[8]={10,4,1,40,50,25,5,100};
270 int a;
272 again:;
273 for(a=an[t-I_CLIP];n>=a;n-=a)
274 IT_spawn(x+myrand(3*2+1)-3,y-myrand(7),t);
275 if(t>=I_AMMO) {t-=4;goto again;}