1 #include "main.h"
2 
3 #define ShotColor SDL_MapRGB(Screen->format,200,0,0)
4 #define SPEED 32
5 #define RELOAD 10000
6 
7 ShotType Shot[MAX_SHOT_NUM];
8 int ShotCount=0, LeftHandedMouse=0;
9 int ShotNum[3]={10,10,10};
10 float ShotReload[3];
11 
CreateShot(int x,int y,int button)12 void CreateShot(int x, int y, int button)
13 {
14     int SourceX=100,SourceY=515,i;
15 
16     if (y > 510) y = 510;
17 
18     if (button>3) {
19 	//printf("Wrong mouse button\n");
20 	return;
21     }
22     if (LeftHandedMouse) {
23 	button=4-button;
24     }
25 
26     SourceX=-300+button*350;
27     if ((ShotCount==0)||(x!=Shot[ShotCount-1].DestX)||(y!=Shot[ShotCount-1].DestY))
28     if (ShotNum[button-1]>0) {
29 	if (ShotCount<MAX_SHOT_NUM) {
30 
31     	    ShotType *This;
32 	    float Vec;
33 
34 	    Vec=100*hypot((x-SourceX),(y-SourceY));
35     	    This=&Shot[ShotCount];
36 	    This->x=SourceX;
37     	    This->y=SourceY;
38     	    This->xspeed=SPEED*(x-SourceX)/Vec;
39 	    This->yspeed=SPEED*(y-SourceY)/Vec;
40     	    This->CurrentPixel=0;
41     	    This->Erase=0;
42 	    This->SourceX=SourceX;
43 	    This->SourceY=SourceY;
44 	    This->DestX=x;
45 	    This->DestY=y;
46 	    for (i=0;i<LINE_LENGTH;i++)
47 		{This->Pixels[i].x=0;This->Pixels[i].y=0;}
48 	    ShotCount++;
49 	    if (ShotNum[button-1]==10)
50 		ShotReload[button-1]=RELOAD;
51 	    ShotNum[button-1]--;
52     	    DrawShotNum(button-1);
53 	    ActivateCursor();
54 	} else {
55 	    printf("Too many Shots\n");
56 	}
57     }
58 
59 //    Line(SourceX,SourceY,x,y,ShotColor);
60 //    Blit(x,y,ShotPic);
61 }
62 
Reload()63 void Reload()
64 {
65     int i;
66 
67 //    printf ("%d\n",CityNum);
68     for (i=0;i<3;i++) {
69 	if (!CannonDestroyed[i]) {
70 	    ShotReload[i]-=Speed*CityNum*2;
71 	    if ((ShotReload[i]<=0)&&(ShotNum[i]<10)) {
72 		ShotReload[i]=RELOAD;
73 		ShotNum[i]++;
74 		DrawShotNum(i);
75 	    }
76 	}
77     }
78 }
79 
RemoveShotPix(int i,int a)80 void RemoveShotPix(int i, int a)
81 {
82     SDL_Rect rect;
83     int DelPix;
84 
85     rect.w=1;
86     rect.h=1;
87     DelPix=(Shot[i].CurrentPixel-a) % LINE_LENGTH;
88     rect.x=Shot[i].Pixels[DelPix].x;
89     rect.y=Shot[i].Pixels[DelPix].y;
90 
91     if (rect.x>=800) return;
92 
93     PutPixel(Screen,rect.x,rect.y,ShotColor);
94     SDL_BlitSurface(BackBuffer, &rect, Screen, &rect);
95     AddRect(rect.x,rect.y,rect.x+1,rect.y+1);
96 }
97 
HandleShot()98 void HandleShot()
99 {
100     int i,a;
101 
102     for (i=0;i<ShotCount;i++)
103     {
104 	if (Shot[i].Erase==0)
105 	{
106     	    // Move Shot
107 	    Shot[i].x+=Shot[i].xspeed*Speed;
108     	    Shot[i].y+=Shot[i].yspeed*Speed;
109 	    if (Shot[i].x>=800) {
110 		printf("WARNING: shot too far right!\n");
111 	    }
112 	}
113 	// Delete Shot
114 	if  (Shot[i].y-Shot[i].DestY<=0)
115 	{
116 	    Shot[i].y=Shot[i].DestY;
117 	    Shot[i].x=Shot[i].DestX;
118     	    if (Shot[i].Erase==0) CreateExplosion(Shot[i].x,Shot[i].y);
119 	    Shot[i].Erase+=Speed/(float)7;
120 	    if (Shot[i].Erase>255) {
121     		SDL_SetAlpha(BackBuffer,0,0);
122  		for (a=Shot[i].CurrentPixel;a>=0;a--) RemoveShotPix(i,a);
123 		Shot[i]=Shot[ShotCount-1];
124 		ShotCount--;
125 	    }
126 	    else {
127 		SDL_SetAlpha(BackBuffer,SDL_SRCALPHA,Shot[i].Erase);
128 		for (a=Shot[i].CurrentPixel;a>=0;a--) RemoveShotPix(i,a);
129  	    }
130 	}
131     }
132     if (ArcadeMode) Reload();
133 }
134 
DrawShot()135 void DrawShot()
136 {
137     int i;
138 
139     lock();
140     for (i=0;i<ShotCount;i++) {
141 	if (Shot[i].Erase==0) {
142 	    if (
143 		    ((int)Shot[i].x!=Shot[i].Pixels[Shot[i].CurrentPixel].x)
144 		    ||((int)Shot[i].y!=Shot[i].Pixels[Shot[i].CurrentPixel].y)
145 		)
146 	    {
147 		if (++Shot[i].CurrentPixel>=LINE_LENGTH)
148 		    printf("Line too long in function DrawShot\n");
149 		PutPixel(Screen,Shot[i].x,Shot[i].y,ShotColor);
150 		Shot[i].Pixels[Shot[i].CurrentPixel].x=Shot[i].x;
151 		Shot[i].Pixels[Shot[i].CurrentPixel].y=Shot[i].y;
152 		Shot[i].Proceeded=1;
153 		if (Shot[i].CurrentPixel>1) {
154 		    if (++Shot[i].CurrentPixel>=LINE_LENGTH)
155 			printf("Line too long in function DrawShot\n");
156 		    Shot[i].Pixels[Shot[i].CurrentPixel].x=(Shot[i].x+Shot[i].Pixels[Shot[i].CurrentPixel-1].x)/2;
157 		    Shot[i].Pixels[Shot[i].CurrentPixel].y=(Shot[i].y+Shot[i].Pixels[Shot[i].CurrentPixel-1].y)/2;
158 		    Shot[i].Proceeded=1;
159 		    PutPixel(Screen,Shot[i].Pixels[Shot[i].CurrentPixel].x,
160 		    Shot[i].Pixels[Shot[i].CurrentPixel].y,ShotColor);
161 		}
162 	    } else {
163 		Shot[i].Proceeded=0;
164 	    }
165 	}
166     }
167     unlock();
168 }
169 
UndrawShot()170 void UndrawShot()
171 {
172 
173 }
174 
DrawShotNum(int Where)175 void DrawShotNum(int Where)
176 {
177     int x,y,shots;
178     SDL_Rect rect;
179 
180     rect.x=8+Where*360;
181     rect.y=550;
182     rect.w=60;
183     rect.h=45;
184     BlitPart(rect.x,rect.y,BackPic,rect);
185     y=0; shots=0;
186     while (shots<ShotNum[Where]) {
187 	x=0;
188 	while ((shots<ShotNum[Where])&&(x<=y)) {
189 	    Blit((38+Where*360)+(x-y/(float)2)*10,550+y*7,ShotPic);
190 	    shots++;
191 	    x++;
192 	}
193 	y++;
194     }
195     SDL_BlitSurface(Screen, &rect, BackBuffer, &rect);
196 }
197