DEADSOFTWARE

save: move save code to separated file
[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_alloc (void) {
45 int i, j, n;
46 static char nm[][6] = {
47 "ITEMUP", "WPNUP", "GETPOW", "ITMBK"
48 };
49 for (i = 0; i < 4; ++i) {
50 snd[i] = Z_getsnd(nm[i]);
51 }
52 for (i = 0; i < MAXITEM; ++i) {
53 it[i].o.r = 10;
54 it[i].o.h = 8;
55 }
56 }
58 void IT_init (void) {
59 int i;
60 for (i = 0; i < MAXITEM; ++i) {
61 it[i].t = I_NONE;
62 it[i].o.xv = 0;
63 it[i].o.yv = 0;
64 it[i].o.vx = 0;
65 it[i].o.vy = 0;
66 }
67 tsndtm = 0;
68 rsndtm = 0;
69 }
71 int IT_load (FILE *h) {
72 int m, i, j;
73 old_thing_t t;
74 switch (blk.t) {
75 case MB_THING:
76 for (i = 0; blk.sz > 0; ++i, blk.sz -= 8) {
77 t.x = myfread16(h);
78 t.y = myfread16(h);
79 t.t = myfread16(h);
80 t.f = myfread16(h);
81 it[i].o.x = t.x;
82 it[i].o.y = t.y;
83 it[i].t = t.t;
84 it[i].s = t.f;
85 if (it[i].t && (it[i].s & THF_DM) && !g_dm) {
86 it[i].t=0;
87 }
88 }
89 m = i;
90 for (i = 0, j = -1; i < m; ++i) {
91 if (it[i].t == TH_PLR1) {
92 j = i;
93 it[i].t = 0;
94 }
95 }
96 if (!g_dm) {
97 if (j == -1) {
98 ERR_fatal("Предмет игрок_1 не найден");
99 }
100 dm_pos[0].x = it[j].o.x;
101 dm_pos[0].y = it[j].o.y;
102 dm_pos[0].d = it[j].s & THF_DIR;
104 for (i = 0, j = -1; i < m; ++i) {
105 if (it[i].t == TH_PLR2) {
106 j = i;
107 it[i].t = 0;
110 if (!g_dm && _2pl) {
111 if (j == -1) {
112 ERR_fatal("Предмет игрок_2 не найден");
114 dm_pos[1].x = it[j].o.x;
115 dm_pos[1].y = it[j].o.y;
116 dm_pos[1].d = it[j].s & THF_DIR;
118 for (i = 0, j = 0; i < m; ++i) {
119 if (it[i].t == TH_DMSTART) {
120 if (g_dm) {
121 dm_pos[j].x = it[i].o.x;
122 dm_pos[j].y = it[i].o.y;
123 dm_pos[j].d = it[i].s & THF_DIR;
125 it[i].t = 0;
126 ++j;
129 if (g_dm && j < 2) {
130 ERR_fatal("Меньше 2-ух точек DM");
132 if (g_dm) {
133 dm_pnum = j;
134 dm_pl1p = myrand(dm_pnum);
135 do {
136 dm_pl2p = myrand(dm_pnum);
137 } while (dm_pl2p == dm_pl1p);
138 } else {
139 dm_pl1p = 0;
140 dm_pl2p = 1;
141 dm_pnum = 2;
143 PL_spawn(&pl1, dm_pos[dm_pl1p].x, dm_pos[dm_pl1p].y, dm_pos[dm_pl1p].d);
144 if (_2pl) {
145 PL_spawn(&pl2, dm_pos[dm_pl2p].x, dm_pos[dm_pl2p].y, dm_pos[dm_pl2p].d);
147 for (i = 0; i < m; ++i) {
148 if (it[i].t >= TH_CLIP && it[i].t < TH_DEMON) {
149 it[i].s = 0;
150 it[i].t = it[i].t - TH_CLIP + I_CLIP;
151 if (it[i].t >= I_KEYR && it[i].t <= I_KEYB) {
152 it[i].t |= 0x8000;
154 } else if (it[i].t >= TH_DEMON) {
155 MN_spawn(it[i].o.x, it[i].o.y, it[i].s & THF_DIR, it[i].t - TH_DEMON + MN_DEMON);
156 it[i].t = 0;
159 return 1;
161 return 0;
164 static void takesnd (int t) {
165 if(tsndtm) return;
166 t&=0x7FFF;
167 if(t<=I_CELP || (t>=I_BPACK && t<=I_BFG) || t==I_GUN2)
168 {tsndtm=Z_sound(snd[1],128);return;}
169 if(t==I_MEGA || t==I_INVL || t==I_SUPER)
170 {tsndtm=Z_sound(snd[2],192);return;}
171 tsndtm=Z_sound(snd[0], 255);
174 void IT_act (void) {
175 int i,j;
177 if(tsndtm) --tsndtm;
178 if(rsndtm) --rsndtm;
179 for(i=0;i<MAXITEM;++i) if(it[i].t)
180 if(it[i].s<0) {
181 if(++it[i].s==-8) {
182 FX_ifog(it[i].o.x,it[i].o.y);
183 if(!rsndtm) rsndtm=Z_sound(snd[3],128);
185 }else{
186 switch(it[i].t) {
187 case I_ARM1: case I_ARM2:
188 if(++it[i].s>=18) it[i].s=0; break;
189 case I_MEGA: case I_INVL:
190 case I_SUPER: case I_RTORCH: case I_GTORCH: case I_BTORCH:
191 if(++it[i].s>=8) it[i].s=0; break;
192 case I_GOR1: case I_FCAN:
193 if(++it[i].s>=6) it[i].s=0; break;
195 if(it[i].t&0x8000) {
196 if((j=Z_moveobj(&it[i].o))&Z_FALLOUT) {it[i].t=0;continue;}
197 else if(j&Z_HITWATER) Z_splash(&it[i].o,it[i].o.r+it[i].o.h);
199 if(Z_overlap(&it[i].o,&pl1.o))
200 if(PL_give(&pl1,it[i].t&0x7FFF)) {
201 takesnd(it[i].t);
202 if(_2pl) if((it[i].t&0x7FFF)>=I_KEYR && (it[i].t&0x7FFF)<=I_KEYB) continue;
203 if(!(it[i].s=-itm_rtime) || (it[i].t&0x8000)) it[i].t=0;
204 continue;
206 if(_2pl) if(Z_overlap(&it[i].o,&pl2.o))
207 if(PL_give(&pl2,it[i].t&0x7FFF)) {
208 takesnd(it[i].t);
209 if((it[i].t&0x7FFF)>=I_KEYR && (it[i].t&0x7FFF)<=I_KEYB) continue;
210 if(!(it[i].s=-itm_rtime) || (it[i].t&0x8000)) it[i].t=0;
211 continue;
216 void IT_spawn (int x,int y,int t) {
217 int i;
219 for(i=0;i<MAXITEM;++i) if(!it[i].t) {
220 it[i].t=t|0x8000;it[i].s=0;
221 it[i].o.x=x;it[i].o.y=y;
222 it[i].o.xv=it[i].o.yv=it[i].o.vx=it[i].o.vy=0;
223 it[i].o.r=10;it[i].o.h=8;
224 return;
228 void IT_drop_ammo (int t, int n, int x, int y) {
229 static int an[8]={10,4,1,40,50,25,5,100};
230 int a;
232 again:;
233 for(a=an[t-I_CLIP];n>=a;n-=a)
234 IT_spawn(x+myrand(3*2+1)-3,y-myrand(7),t);
235 if(t>=I_AMMO) {t-=4;goto again;}