DEADSOFTWARE

5910fa5b04db00d82e9529fcb4bf31bd52180d44
[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 <stdio.h>
25 #include <stdlib.h>
26 #include "vga.h"
27 #include "error.h"
28 #include "sound.h"
29 #include "files.h"
30 #include "view.h"
31 #include "items.h"
32 #include "fx.h"
33 #include "player.h"
34 #include "monster.h"
35 #include "things.h"
36 #include "misc.h"
37 #include "memory.h"
38 #include "map.h"
40 extern map_block_t blk;
42 #pragma pack(1)
43 typedef struct{
44 obj_t o;
45 int t;
46 int s;
47 }item_t;
48 #pragma pack()
50 static void *snd[4],*spr[58];
51 static char sprd[58];
52 static int tsndtm,rsndtm;
53 static item_t it[MAXITEM];
55 int itm_rtime=1092;
57 void IT_savegame(FILE* h) {
58 int n;
60 for(n=MAXITEM;--n;) if(it[n].t) break;
61 ++n;myfwrite(&n,1,4,h);
62 myfwrite(it,1,n*sizeof(it[0]),h);
63 myfwrite(&itm_rtime,1,4,h);
64 }
66 void IT_loadgame(FILE* h) {
67 int n;
69 myfread(&n,1,4,h);
70 myfread(it,1,n*sizeof(it[0]),h);
71 myfread(&itm_rtime,1,4,h);
72 }
74 void IT_alloc(void) {
75 int i,j,n;
76 static char nm[][6]={
77 "ITEMUP","WPNUP","GETPOW","ITMBK"
78 },snm[][4]={
79 "CLIP","SHEL","ROCK","CELL","AMMO","SBOX","BROK","CELP",
80 "STIM","MEDI","BPAK",
81 "CSAW","SHOT","SGN2","MGUN","LAUN","PLAS","BFUG"
82 },n4[][4]={
83 "SOUL","SMRT","SMGT","SMBT"
84 },n3[][4]={
85 "GOR1","FCAN"
86 };
88 // logo(" items");
89 for(i=0;i<18;++i) spr[i]=Z_getspr(snm[i],0,0,sprd+i);
90 for(;i<20;++i) {
91 spr[i]=Z_getspr("ARM1",i-18,0,sprd+i);
92 spr[i+2]=Z_getspr("ARM2",i-18,0,sprd+i);
93 }i+=2;
94 for(;i<26;++i) spr[i]=Z_getspr("MEGA",i-22,0,sprd+i);
95 for(;i<30;++i) spr[i]=Z_getspr("PINV",i-26,0,sprd+i);
96 spr[30]=Z_getspr("AQUA",0,0,sprd+30);
97 spr[31]=Z_getspr("KEYR",0,0,sprd+31);
98 spr[32]=Z_getspr("KEYG",0,0,sprd+32);
99 spr[33]=Z_getspr("KEYB",0,0,sprd+33);
100 spr[34]=Z_getspr("SUIT",0,0,sprd+34);
101 for(n=35,j=0;j<4;++j)
102 for(i=0;i<4;++i,++n) spr[n]=Z_getspr(n4[j],i,0,sprd+n);
103 for(j=0;j<2;++j)
104 for(i=0;i<3;++i,++n) spr[n]=Z_getspr(n3[j],i,0,sprd+n);
105 spr[57]=Z_getspr("GUN2",0,0,sprd+57);
106 for(i=0;i<4;++i) snd[i]=Z_getsnd(nm[i]);
107 for(i=0;i<MAXITEM;++i) {it[i].o.r=10;it[i].o.h=8;}
110 void IT_init(void) {
111 int i;
113 for(i=0;i<MAXITEM;++i) {
114 it[i].t=I_NONE;
115 it[i].o.xv=it[i].o.yv=it[i].o.vx=it[i].o.vy=0;
117 tsndtm=rsndtm=0;
120 int IT_load(FILE* h) {
121 int m,i,j;
122 old_thing_t t;
124 switch(blk.t) {
125 case MB_THING:
126 for(i=0;blk.sz>0;++i,blk.sz-=8) {
127 myfread(&t,1,sizeof(t),h);
128 it[i].o.x=t.x;it[i].o.y=t.y;
129 it[i].t=t.t;it[i].s=t.f;
130 if(!it[i].t) break;
131 if((it[i].s&THF_DM) && !g_dm) it[i].t=0;
132 }m=i;
133 for(i=0,j=-1;i<m;++i) if(it[i].t==TH_PLR1) {j=i;it[i].t=0;}
134 if(!g_dm) {
135 if(j==-1) ERR_fatal("Предмет игрок_1 не найден");
136 dm_pos[0].x=it[j].o.x;dm_pos[0].y=it[j].o.y;dm_pos[0].d=it[j].s&THF_DIR;
138 for(i=0,j=-1;i<m;++i) if(it[i].t==TH_PLR2) {j=i;it[i].t=0;}
139 if(!g_dm && _2pl) {
140 if(j==-1) ERR_fatal("Предмет игрок_2 не найден");
141 dm_pos[1].x=it[j].o.x;dm_pos[1].y=it[j].o.y;dm_pos[1].d=it[j].s&THF_DIR;
143 for(i=0,j=0;i<m;++i) if(it[i].t==TH_DMSTART) {
144 if(g_dm)
145 {dm_pos[j].x=it[i].o.x;dm_pos[j].y=it[i].o.y;dm_pos[j].d=it[i].s&THF_DIR;}
146 it[i].t=0;++j;
148 if(g_dm && j<2) ERR_fatal("Меньше 2-ух точек DM");
149 if(g_dm) {
150 dm_pnum=j;
151 dm_pl1p=myrand(dm_pnum);
152 do{ dm_pl2p=myrand(dm_pnum); }while(dm_pl2p==dm_pl1p);
153 }else {dm_pl1p=0;dm_pl2p=1;dm_pnum=2;}
154 PL_spawn(&pl1,dm_pos[dm_pl1p].x,dm_pos[dm_pl1p].y,dm_pos[dm_pl1p].d);
155 if(_2pl) PL_spawn(&pl2,dm_pos[dm_pl2p].x,dm_pos[dm_pl2p].y,dm_pos[dm_pl2p].d);
156 for(i=0;i<m;++i)
157 if(it[i].t>=TH_CLIP && it[i].t<TH_DEMON) {
158 it[i].s=0;it[i].t=it[i].t-TH_CLIP+I_CLIP;
159 if(it[i].t>=I_KEYR && it[i].t<=I_KEYB) it[i].t|=0x8000;
160 }else if(it[i].t>=TH_DEMON) {
161 MN_spawn(it[i].o.x,it[i].o.y,it[i].s&THF_DIR,it[i].t-TH_DEMON+MN_DEMON);
162 it[i].t=0;
164 return 1;
165 }return 0;
168 static void takesnd(int t) {
169 if(tsndtm) return;
170 t&=0x7FFF;
171 if(t<=I_CELP || (t>=I_BPACK && t<=I_BFG) || t==I_GUN2)
172 {tsndtm=Z_sound(snd[1],128);return;}
173 if(t==I_MEGA || t==I_INVL || t==I_SUPER)
174 {tsndtm=Z_sound(snd[2],192);return;}
175 tsndtm=Z_sound(snd[0],256);
178 void IT_act(void) {
179 int i,j;
181 if(tsndtm) --tsndtm;
182 if(rsndtm) --rsndtm;
183 for(i=0;i<MAXITEM;++i) if(it[i].t)
184 if(it[i].s<0) {
185 if(++it[i].s==-8) {
186 FX_ifog(it[i].o.x,it[i].o.y);
187 if(!rsndtm) rsndtm=Z_sound(snd[3],128);
189 }else{
190 switch(it[i].t) {
191 case I_ARM1: case I_ARM2:
192 if(++it[i].s>=18) it[i].s=0; break;
193 case I_MEGA: case I_INVL:
194 case I_SUPER: case I_RTORCH: case I_GTORCH: case I_BTORCH:
195 if(++it[i].s>=8) it[i].s=0; break;
196 case I_GOR1: case I_FCAN:
197 if(++it[i].s>=6) it[i].s=0; break;
199 if(it[i].t&0x8000) {
200 if((j=Z_moveobj(&it[i].o))&Z_FALLOUT) {it[i].t=0;continue;}
201 else if(j&Z_HITWATER) Z_splash(&it[i].o,it[i].o.r+it[i].o.h);
203 if(Z_overlap(&it[i].o,&pl1.o))
204 if(PL_give(&pl1,it[i].t&0x7FFF)) {
205 takesnd(it[i].t);
206 if(_2pl) if((it[i].t&0x7FFF)>=I_KEYR && (it[i].t&0x7FFF)<=I_KEYB) continue;
207 if(!(it[i].s=-itm_rtime) || (it[i].t&0x8000)) it[i].t=0;
208 continue;
210 if(_2pl) if(Z_overlap(&it[i].o,&pl2.o))
211 if(PL_give(&pl2,it[i].t&0x7FFF)) {
212 takesnd(it[i].t);
213 if((it[i].t&0x7FFF)>=I_KEYR && (it[i].t&0x7FFF)<=I_KEYB) continue;
214 if(!(it[i].s=-itm_rtime) || (it[i].t&0x8000)) it[i].t=0;
215 continue;
220 void IT_draw(void) {
221 int i,s;
223 for(i=0;i<MAXITEM;++i) {
224 s=-1;
225 if(it[i].t && it[i].s>=0) switch(it[i].t&0x7FFF) {
226 case I_ARM1:
227 s=it[i].s/9+18;break;
228 case I_ARM2:
229 s=it[i].s/9+20;break;
230 case I_MEGA:
231 s=it[i].s/2+22;break;
232 case I_INVL:
233 s=it[i].s/2+26;break;
234 case I_SUPER: case I_RTORCH: case I_GTORCH: case I_BTORCH:
235 s=it[i].s/2+(it[i].t-I_SUPER)*4+35;break;
236 case I_GOR1: case I_FCAN:
237 s=it[i].s/2+(it[i].t-I_GOR1)*3+51;break;
238 case I_AQUA: s=30;break;
239 case I_SUIT: s=34;break;
240 case I_KEYR: case I_KEYG: case I_KEYB:
241 s=(it[i].t&0x7FFF)-I_KEYR+31;break;
242 case I_GUN2: s=57;break;
243 default:
244 s=(it[i].t&0x7FFF)-1;
246 if(s>=0) Z_drawspr(it[i].o.x,it[i].o.y,spr[s],sprd[s]);
250 void IT_spawn(int x,int y,int t) {
251 int i;
253 for(i=0;i<MAXITEM;++i) if(!it[i].t) {
254 it[i].t=t|0x8000;it[i].s=0;
255 it[i].o.x=x;it[i].o.y=y;
256 it[i].o.xv=it[i].o.yv=it[i].o.vx=it[i].o.vy=0;
257 it[i].o.r=10;it[i].o.h=8;
258 return;
262 void IT_drop_ammo(int t,int n,int x,int y) {
263 static int an[8]={10,4,1,40,50,25,5,100};
264 int a;
266 again:;
267 for(a=an[t-I_CLIP];n>=a;n-=a)
268 IT_spawn(x+myrand(3*2+1)-3,y-myrand(7),t);
269 if(t>=I_AMMO) {t-=4;goto again;}