PlaySound when bullets hits walls
[Lilanci.git] / elilanci.c
blob439bca63c16da2674cc366cb66878bf34fbbf3d8
1 #include <math.h>
2 #include <time.h>
3 #include "geometry.h"
4 #include "util.h"
5 #include "config.h"
6 #include "gr.h"
7 #include "map.h"
8 #include "snd.h"
9 #include "particle.h"
10 #include "path.h"
11 #include "font.h"
14 int gamestate=0;
16 typedef struct{
17 int Up;
18 int Down;
19 int Left;
20 int Right;
21 int Shoot;
22 int Suicide;
23 int Switch;
24 }TKeyBinding;//TODO: s/int/real data type for a key
26 typedef struct{
27 Vect Pos; //where he is
28 Vect Move; //how he want to move
29 Vect BSize; //Size of bounding rect
30 Area *Shape; //how fat he is
31 //Area *TrShape; //translated Shape -- don't optimize yet
32 int Frags; //no comment
33 int Deaths; //R.I.P.
34 double Speed; //meters per second
35 int Orientation; //1-up 2-left 3-down 4-right 0-down
36 int WannaShoot; //1-want 0-don't want
37 double ReloadTime; //time from last shot
38 int WannaDie; //1-want suicide 2-died and still holding suicide 0-don't want
39 double AnimationTime; //in seconds
40 TKeyBinding Keys;
41 int *Ammo;
42 int ActiveWeapon;
43 int SwitchLock;
44 int Alive;
45 char Name[16];
46 SDL_Color Color;
47 }TLilanek;
50 typedef struct{
51 Vect Pos;
52 Area *Shape;
53 int WID; //Weapon ID
54 int Exists; // 0- doesn't exist 1-exists
55 int Ammo;
56 int MaxAmmo;
57 int Burst;
58 int RandomWeight;
59 double ReloadTime;
60 }TWeapon;
62 typedef struct{
63 Vect Pos;
64 Area *Shape;
65 Vect Vel; //velocity i m/s
66 double Speed; //for Templates
67 double Range; //How far it can travel
68 int WID; //Weapon ID
69 int lid; //who shoot this bullet?
70 int Exists; // 0- doesn't exist 1-exists
71 double SFuzzy; // Fuzzy factor for spawning 0-1 :)
72 double VFuzzy; // Fuzzy factor for traveling 0-1 :)
73 }TBullet;
76 typedef struct{
77 TMap *Map; //where we are playing
78 TLilanek *Lilanek; //our heroes-ehm bad guys
79 int noLilaneks;
81 TWeapon *Weapon; //Weapons lying on floor
82 int noWeapons;
83 TWeapon *WeaponTemplate;
84 TBullet *BulletTemplate;
85 int noWID; //biggest WID, also number of WeaponTemplates
86 TBullet *Bullet;
87 int noBullets;
88 double PWeapondrop; // Probability of weapon drop per second
89 }Game;
91 Vect Orientations[5];
93 Sprite *lil[5];
94 Sprite *bg;
95 Sprite *kul;
96 Sprite *explo;
97 Sprite *dust;
98 Sprite *blood;
99 Sprite *brok;
100 Sprite *kulka;
101 Sprite *mine;
102 Sprite *mines;
103 Sprite *hud;
104 Sprite *hud_mine;
105 Sprite *hud_kul;
106 Sprite *hud_brok;
107 Sprite *hud_ammo;
108 Sprite *hud_reload;
109 int s_die;
110 int s_bonus;
111 int s_expl;
112 int s_shoot;
113 int s_mine;
115 int frames=0;
116 void SwitchNextWeapon(Game *g, int lid);
118 int InitBase(){
119 Orientations[0].x=0;
120 Orientations[0].y=0;
121 Orientations[1].x=0;
122 Orientations[1].y=-1;
123 Orientations[2].x=-1;
124 Orientations[2].y=0;
125 Orientations[3].x=0;
126 Orientations[3].y=1;
127 Orientations[4].x=1;
128 Orientations[4].y=0;
129 //TODO: write code
131 return 0;
134 void SpawnLilanek(Game *g, int lid){
135 Vect Pos;
136 int tries;
137 Area *tp, *oltp;
138 int good;
139 int olid;
141 oltp = 0;
142 tp = 0;
143 for(tries=0; tries <1000; tries++){ //TODO: remove exact number
144 Pos.x = RandD(0,g->Map->XX);
145 Pos.y = RandD(0,g->Map->YY);
146 good=1;
147 tp = TranslateArea(g->Lilanek[lid].Shape, &Pos);
148 printf("%s\n", Area2String(tp));
149 if(!AreaInMap(g->Map, tp,0,1)){
150 good=0;
153 for(olid=0; olid < g->noLilaneks; olid++){
154 if(olid == lid)
155 continue;
156 if(!g->Lilanek[olid].Alive){
157 continue;
159 oltp = TranslateArea(g->Lilanek[olid].Shape, &g->Lilanek[olid].Pos);
160 if(AreaInArea(tp, oltp)){
161 good=0;
163 FreeArea(oltp);
164 oltp = 0;
167 if(good){
168 FreeArea(tp);
169 tp=0;
170 g->Lilanek[lid].Pos.x = Pos.x;
171 g->Lilanek[lid].Pos.y = Pos.y;
172 g->Lilanek[lid].Alive = 1;
173 break;
175 FreeArea(tp);
176 tp=0;
180 int RandWID(Game *g) {
181 int SumRandWeight = 0;
182 int i, j;
184 for (i=0; i < g->noWID; i++) {
185 SumRandWeight += g->WeaponTemplate[i].RandomWeight;
187 j = RandI(0, SumRandWeight);
188 for (i=0; i < g->noWID; i++) {
189 j -= g->WeaponTemplate[i].RandomWeight;
190 if (j <= 0)
191 return i;
193 return 0;
196 void DropWeapon(Game *g, int weapon){
197 Vect Pos;
198 int tries;
199 int WID;
200 Area *tp;
202 for(tries=0; tries <100; tries++){ //TODO: remove exact number
203 Pos.x = RandD(0,g->Map->XX);
204 Pos.y = RandD(0,g->Map->YY);
205 WID = RandWID(g);
206 tp = TranslateArea(g->WeaponTemplate[WID].Shape, &Pos);
207 if(AreaInMap(g->Map, tp, 0, g->Map->noLayer)){
208 FreeArea(tp);
209 tp=0;
210 memcpy(&g->Weapon[weapon], &g->WeaponTemplate[WID], sizeof(TWeapon));
211 g->Weapon[weapon].Shape = g->WeaponTemplate[WID].Shape;
212 g->Weapon[weapon].Pos.x = Pos.x;
213 g->Weapon[weapon].Pos.y = Pos.y;
214 g->Weapon[weapon].WID = WID;
215 g->Weapon[weapon].Exists = 1;
216 break;
218 FreeArea(tp);
219 tp=0;
223 void DropWeapons(Game *g, double dtime){
224 int i;
226 if( RandD(0.0, 1.0) >= (g->PWeapondrop*dtime))
227 return;
228 for(i=0; i < g->noWeapons; i++){
229 if(g->Weapon[i].Exists)continue; //we don't like teleporting weapons :)
231 printf("A Weapon drop!\n");
232 DropWeapon(g, i);
233 break; //spawn max one weapon per update
237 void WeaponPickedUp(Game *g, double dtime, int lid, int wid){
238 TLilanek *l;
239 TWeapon *w;
241 l = &g->Lilanek[lid];
242 w = &g->Weapon[wid];
244 l->Ammo[w->WID] += w->Ammo;
245 if(l->Ammo[w->WID] > w->MaxAmmo)
246 l->Ammo[w->WID] = w->MaxAmmo;
247 printf("Ammo: %d\n",l->Ammo[w->WID]);
248 l->ActiveWeapon = w->WID;
249 printf("AW: %d\n",l->ActiveWeapon);
251 w->Exists = 0;
253 PlaySound(s_bonus);
256 void PickUpWeapons(Game *g, double dtime){
257 int lid,wid;
258 Area *WArea, *LArea;
260 for(lid=0; lid<g->noLilaneks; lid++){
261 LArea = TranslateArea(g->Lilanek[lid].Shape, &g->Lilanek[lid].Pos);
262 for(wid=0; wid < g->noWeapons; wid++){
263 if(!g->Weapon[wid].Exists)
264 continue;
265 WArea = TranslateArea(g->Weapon[wid].Shape, &g->Weapon[wid].Pos);
266 if(!AreaInArea( WArea, LArea)){
267 FreeArea(WArea);
268 continue;
270 WeaponPickedUp(g, dtime, lid, wid);
271 FreeArea(WArea);
273 FreeArea(LArea);
277 void BulletExplodes(Game *g, int i){
278 g->Bullet[i].Exists=0;
279 //TODO: some effects
280 PlaySound (s_expl);
283 void MoveBullet(Game *g, double dtime, int i){
284 double ff;
285 Vect dp;
286 dp.x = g->Bullet[i].Pos.x;
287 dp.y = g->Bullet[i].Pos.y;
288 ff= g->Bullet[i].Speed*g->Bullet[i].VFuzzy;
289 g->Bullet[i].Pos.x += (g->Bullet[i].Vel.x += RandD(-ff,ff))* dtime;
290 g->Bullet[i].Pos.y += (g->Bullet[i].Vel.y += RandD(-ff,ff))* dtime;
291 dp.x -= g->Bullet[i].Pos.x;
292 dp.y -= g->Bullet[i].Pos.y;
293 g->Bullet[i].Range -= sqrt(DotProduct(&dp, &dp));
296 void CollideBulletMap(Game *g, double dtime, int i){
297 Area *p;
299 p=TranslateArea(g->Bullet[i].Shape, &g->Bullet[i].Pos);
300 if(AreaInMap(g->Map, p, 1,1)==0)
301 BulletExplodes(g, i);
302 FreeArea(p);
305 void BloodColorFunc(double temp, SDL_Color *c)
307 SDL_Color col={255,255,255,255};
308 col.r *=(0.5+0.5*temp);
309 //col.g *=temp;
310 //col.b *=temp;
311 //col.unused *=temp*temp;
312 if (c != 0) memcpy(c, &col, sizeof(SDL_Color));
315 void BloodParticles(Game *g, double dtime, int lid)
317 int i;
318 double a;
319 double v;
320 Particle *p;
322 for(i = 0; i < 5; i ++){
323 p = malloc(sizeof(Particle));
324 memset(p, 0, sizeof(Particle));
325 a = RandD(0.0, 20.0);
326 v = RandD(0.0, 2.0);
327 p->x = g->Lilanek[lid].Pos.x-g->Lilanek[lid].BSize.x*0.5;
328 p->y = g->Lilanek[lid].Pos.y-g->Lilanek[lid].BSize.y*0.5;
329 p->t = 1.0;
330 p->vx = v*sin(a) + g->Lilanek[lid].Move.x;
331 p->vy = v*cos(a) + g->Lilanek[lid].Move.y;
332 p->fx = 1.5;
333 p->fy = 1.5;
334 p->vt = 0.0-1.0;
335 p->s = blood;
336 p->tf = &BloodColorFunc;
337 p->z = 1;
338 AddParticle(p);
342 void LilanekKilled(Game *g, double dtime, int BulletID, int LilanekID){
343 int i;
344 g->Bullet[BulletID].Exists = 0;
345 g->Lilanek[LilanekID].Deaths ++;
346 if (LilanekID != g->Bullet[BulletID].lid) {
347 g->Lilanek[g->Bullet[BulletID].lid].Frags ++;
348 }else{ //self-kill
349 g->Lilanek[g->Bullet[BulletID].lid].Frags --;
351 BloodParticles(g, dtime, LilanekID);
352 SpawnLilanek(g, LilanekID);
353 for(i=0; i<g->noWID; i++){
354 g->Lilanek[LilanekID].Ammo[i] = 0;
356 PlaySound(s_die);
359 #define NO_EXPLOSIONS 10 //MAGIC_NUMBER
360 #define EXPLOSION_SPEED 5 //MAGIC_NUMBER
361 #define EXPLOSION_RANGE 1.0 //MAGIC_NUMBER
363 void SpawnExplosions(Game *g, double dtime, int BulletID){
364 int bid = 0;
365 int i = 0;
366 TBullet *b;
367 Vect Pos;
368 double speed;
370 Pos.x = g->Bullet[BulletID].Pos.x;
371 Pos.y = g->Bullet[BulletID].Pos.y;
372 for(i=0; i<NO_EXPLOSIONS; i++){
373 for(bid=0; bid<g->noBullets; bid++){
374 if(g->Bullet[bid].Exists)
375 continue;
376 b = &g->Bullet[bid];
377 b->Pos.x = Pos.x;
378 b->Pos.y = Pos.y;
379 b->Vel.x = RandD(0.0, 1.0)*(RandI(0,100)>50?-1:1);
380 b->Vel.y = RandD(0.0, 1.0)*(RandI(0,100)>50?-1:1);
381 NormV(&b->Vel);
382 speed = RandD(0.3, 1.0)*EXPLOSION_SPEED;
383 b->Speed = speed;
384 b->Vel.x *= speed;
385 b->Vel.y *= speed;
386 b->WID = -1;
387 b->lid = g->Bullet[BulletID].lid;
388 b->Exists = 1;
389 b->SFuzzy = 0;
390 b->VFuzzy = 0;
391 b->Range = EXPLOSION_RANGE;
392 b->Shape = g->Bullet[BulletID].Shape;
393 printf("%lf ", speed);
394 break;
397 g->Bullet[BulletID].Exists = 0;
398 PlaySound(s_expl);
401 void CollideBulletLilanek(Game *g, double dtime, int BulletID){
402 Area *BArea, *LArea;
403 int LilanekID;
405 BArea=TranslateArea(g->Bullet[BulletID].Shape, &g->Bullet[BulletID].Pos);
406 for(LilanekID=0; LilanekID<g->noLilaneks; LilanekID++){
407 LArea=TranslateArea(g->Lilanek[LilanekID].Shape, &g->Lilanek[LilanekID].Pos);
408 if(AreaInArea(LArea,BArea)){
409 //if it is a mine, spawn explosion instead of killing instantly
410 if(g->Bullet[BulletID].WID == 1){
411 SpawnExplosions(g, dtime, BulletID);
412 }else{
413 LilanekKilled(g, dtime, BulletID, LilanekID);
416 FreeArea(LArea);
417 LArea=0;
419 FreeArea(BArea);
420 BArea=0;
423 void UpdateBullets(Game *g, double dtime){
424 int BulletID;
425 for(BulletID=0; BulletID<g->noBullets; BulletID++){
426 if(g->Bullet[BulletID].Exists==0)
427 continue; //We won't update non-existending bullets
428 MoveBullet(g, dtime, BulletID);
429 if(g->Bullet[BulletID].Range < 0){
430 g->Bullet[BulletID].Exists = 0;
431 continue;
433 CollideBulletMap(g, dtime, BulletID);
434 CollideBulletLilanek(g, dtime, BulletID);
438 int CheckLilanekvsLilaneks(Game *g, Area *a, int LilanekID){ //returns 0 if he collides with others, 1 if it's OK to move there
439 int i;
440 Area *nb;
441 int rval=1;
443 nb=0;
444 for(i=0; i<g->noLilaneks && rval==1; i++){
445 if(i==LilanekID)
446 continue;
447 nb = TranslateArea(g->Lilanek[i].Shape, &g->Lilanek[i].Pos);
448 if(AreaInArea(nb, a))
449 rval=0;
450 FreeArea(nb);
451 nb=0;
453 return rval;
456 void MoveLilaneks(Game *g, double dtime){
457 int i, steps;
458 Area *pa;
459 Vect va;
460 double dt;
462 pa=0;
463 for(i=0; i<g->noLilaneks; i++){
464 //skip this Lilanek if he doesn't wanna move
465 if(g->Lilanek[i].Move.x == 0.0 && g->Lilanek[i].Move.y == 0.0)
466 continue;
467 for(dt=dtime, steps=0; steps<4; dt/=2.0, steps++){ //TODO: get rid of exact number
468 //make future Area
469 va.x = g->Lilanek[i].Pos.x + dt*g->Lilanek[i].Move.x;
470 va.y = g->Lilanek[i].Pos.y + dt*g->Lilanek[i].Move.y;
471 FreeArea(pa); //we don't want memory leaks
472 pa = TranslateArea(g->Lilanek[i].Shape, &va);
473 //check for collision with map
474 if(AreaInMap(g->Map, pa, 0, 1)==0)
475 continue; //try smaller dt if he collided with map
476 //check for collision with other Lilaneks
477 if(CheckLilanekvsLilaneks(g, pa, i)==0)
478 continue;
479 //move him if we got here
480 g->Lilanek[i].Pos.x += dt*g->Lilanek[i].Move.x;
481 g->Lilanek[i].Pos.y += dt*g->Lilanek[i].Move.y;
482 FreeArea(pa);
483 pa=0;
485 FreeArea(pa); //For sure
486 pa=0;
490 void ParseInput(Game *g, double dtime){
491 int i;
492 Uint8 *keystate;
493 TLilanek *curlil;
494 int wannamove;
496 keystate = SDL_GetKeyState(NULL);
497 //pause
498 if(gamestate == 1){
499 if(keystate['o']){
500 printf("unpause\n");
501 gamestate = 0;
502 }else return;
504 if(keystate['p']){
505 printf("pause\n");
506 gamestate = 1;
509 for(i=0; i< g->noLilaneks; i++){
510 curlil = &g->Lilanek[i];
511 wannamove=0;
512 // Up;
513 if(keystate[curlil->Keys.Up]){
514 curlil->Orientation = 1;
515 curlil->Move.x = curlil->Speed * Orientations[1].x;
516 curlil->Move.y = curlil->Speed * Orientations[1].y;
517 wannamove=1;
519 // Down;
520 if(keystate[curlil->Keys.Down]){
521 curlil->Orientation = 3;
522 curlil->Move.x = curlil->Speed * Orientations[3].x;
523 curlil->Move.y = curlil->Speed * Orientations[3].y;
524 wannamove=1;
526 // Left;
527 if(keystate[curlil->Keys.Left]){
528 curlil->Orientation = 2;
529 curlil->Move.x = curlil->Speed * Orientations[2].x;
530 curlil->Move.y = curlil->Speed * Orientations[2].y;
531 wannamove=1;
533 // Right;
534 if(keystate[curlil->Keys.Right]){
535 curlil->Orientation = 4;
536 curlil->Move.x = curlil->Speed * Orientations[4].x;
537 curlil->Move.y = curlil->Speed * Orientations[4].y;
538 wannamove=1;
541 if(!wannamove){
542 curlil->Move.x = 0;
543 curlil->Move.y = 0;
545 // Shoot;
546 if(keystate[curlil->Keys.Shoot]){
547 curlil->WannaShoot = 1;
548 }else{
549 curlil->WannaShoot = 0;
551 // Suicide;
552 if(keystate[curlil->Keys.Suicide]){
553 if(curlil->WannaDie != 2){
554 curlil->WannaDie = 1;
556 }else{
557 curlil->WannaDie = 0;
559 // Switch;
560 if(keystate[curlil->Keys.Switch]){
561 if(!curlil->SwitchLock){
562 SwitchNextWeapon(g, i);
563 curlil->SwitchLock = 1;
565 }else{
566 curlil->SwitchLock = 0;
571 void SpawnBullet(Game *g, double dtime, int lid, int bid){
572 Area *LArea, *BArea;
573 TBullet *b, *bt;
574 double vx, vy; //because we need to spawn also mines with no velocity
577 LArea = TranslateArea(g->Lilanek[lid].Shape, &g->Lilanek[lid]. Pos);
578 b = &g->Bullet[bid];
579 bt = &g->BulletTemplate[g->Lilanek[lid].ActiveWeapon];
580 memcpy (b, bt, sizeof(TBullet));
581 b->Exists = 1;
582 b->Pos.x = g->Lilanek[lid].Pos.x+g->Lilanek[lid].BSize.x*0.5;
583 b->Pos.y = g->Lilanek[lid].Pos.y+g->Lilanek[lid].BSize.y*0.5;
584 b->WID = bt->WID;
585 b->Range = bt->Range;
586 b->Vel.x = Orientations[g->Lilanek[lid].Orientation].x * b->Speed +RandD(-b->Speed*b->SFuzzy, b->Speed*b->SFuzzy);
587 b->Vel.y = Orientations[g->Lilanek[lid].Orientation].y * b->Speed +RandD(-b->Speed*b->SFuzzy, b->Speed*b->SFuzzy);
588 if(DotProduct(&b->Vel, &b->Vel) <=0.01){
589 vx = -1*Orientations[g->Lilanek[lid].Orientation].x;
590 vy = -1*Orientations[g->Lilanek[lid].Orientation].y;
591 }else{
592 vx = b->Vel.x;
593 vy = b->Vel.y;
595 b->lid =lid;
596 while(1){
597 BArea = TranslateArea(b->Shape, &b->Pos);
598 if(!AreaInArea(BArea, LArea))
599 break;
600 b->Pos.x += vx* dtime*0.5;
601 b->Pos.y += vy* dtime*0.5;
603 FreeArea(LArea);
606 void DustColorFunc(double temp, SDL_Color *c)
608 SDL_Color col={255,255,255,255};
609 col.r *=temp;
610 col.g *=temp;
611 col.b *=temp;
612 col.unused *=temp*temp;
613 if (c != 0) memcpy(c, &col, sizeof(SDL_Color));
616 void ShootParticles(Game *g, double dtime, int lid)
618 int i;
619 double a;
620 double v;
621 Particle *p;
623 for(i = 1; i < 10; i ++){
624 p = malloc(sizeof(Particle));
625 memset(p, 0, sizeof(Particle));
626 a = RandD(0.0, 2*M_PI);
627 v = RandD(0.0, 2.0);
628 p->x = g->Lilanek[lid].Pos.x-g->Lilanek[lid].BSize.x*0.5;
629 p->y = g->Lilanek[lid].Pos.y-g->Lilanek[lid].BSize.y*0.5;
630 p->t = 1.0;
631 p->vx = v*sin(a) + g->Lilanek[lid].Move.x;
632 p->vy = v*cos(a) + g->Lilanek[lid].Move.y;
633 p->vt = 0.0-2.0;
634 p->s = dust;
635 p->z = 1;
636 p->tf = &DustColorFunc;
637 AddParticle(p);
641 void Shoot(Game *g, double dtime, int lid){
642 int bid;
643 int i;
645 for(i=0; i<g->WeaponTemplate[g->Lilanek[lid].ActiveWeapon].Burst; i++){
646 if(g->Lilanek[lid].Ammo[g->Lilanek[lid].ActiveWeapon] == 0)
647 return;
648 for(bid=0; bid<g->noBullets; bid++){
649 if(g->Bullet[bid].Exists)
650 continue;
651 SpawnBullet(g, dtime, lid, bid);
652 g->Lilanek[lid].Ammo[g->Lilanek[lid].ActiveWeapon]--;
653 g->Lilanek[lid].ReloadTime = 0;
654 break;
657 ShootParticles(g, dtime, lid);
658 switch(g->Lilanek[lid].ActiveWeapon){
659 case 0: PlaySound(s_shoot); break;
660 case 1: PlaySound(s_mine); break;
661 case 2: PlaySound(s_shoot); break;
662 default: break;
666 void SwitchNextWeapon(Game *g, int lid){
667 int i;
668 TLilanek *l;
669 l = &g->Lilanek[lid];
670 for(i = 1; i < g->noWID; i++){
671 if(l->Ammo[(l->ActiveWeapon+i)%(g->noWID)]){
672 l->ActiveWeapon = (l->ActiveWeapon+i)%(g->noWID);
673 return;
678 void ShootIfTheyWantTo(Game *g, double dtime){
679 int lid;
681 for(lid=0; lid<g->noLilaneks; lid++){
682 //if(!g->Lilanek[lid].Exists)
683 //continue;
684 if(g->Lilanek[lid].Ammo[g->Lilanek[lid].ActiveWeapon] == 0)
685 SwitchNextWeapon(g,lid);
687 if(g->Lilanek[lid].ReloadTime < g->WeaponTemplate[g->Lilanek[lid].ActiveWeapon].ReloadTime){
688 g->Lilanek[lid].ReloadTime += dtime;
689 }else{
690 if(!g->Lilanek[lid].WannaShoot)
691 continue;
692 Shoot(g, dtime, lid);
697 int UpdateLogic(Game *g,double dtime){
698 if(gamestate == 0){
699 DropWeapons(g, dtime);
700 UpdateBullets(g, dtime);
702 ParseInput(g, dtime);
703 if(gamestate == 0){
704 MoveLilaneks(g, dtime);
705 PickUpWeapons(g, dtime);
706 ShootIfTheyWantTo(g, dtime);
708 return 0; //TODO: return something useful
711 Game *testgame(){ //return testing game, function for use before we have some menu/loading mechanism
712 Game *g;
713 Poly *p, *pp;
714 Area *a;
715 int i;
716 Config conf = GetConfig();
718 g = malloc(sizeof(Game));
719 memset(g, 0, sizeof(Game));
720 g->noWID=3;
721 g->Map = malloc(sizeof(TMap));
722 g->Map->noLayer=2;
723 g->Map->Layer = malloc(sizeof(TMapLayer *)*g->Map->noLayer);
724 g->Map->Layer[0] = malloc(sizeof(TMapLayer));
725 g->Map->Layer[0]->noFArea = 1;
726 g->Map->Layer[0]->FArea = malloc(sizeof(Area *) * g->Map->Layer[0]->noFArea);
727 p = NewRect(4,2.5,3,2);
728 a = NewArea();
729 AddToArea(a, p);
730 FreePoly(p);
731 g->Map->Layer[0]->FArea[0] = a;
733 g->Map->Layer[1] = malloc(sizeof(TMapLayer));
734 g->Map->Layer[1]->noFArea = 1;
735 g->Map->Layer[1]->FArea = malloc(sizeof(Area *) * g->Map->Layer[1]->noFArea);
736 p = NewPoly();
737 AddToPolyXY(p, 0.30, 0.41);
738 AddToPolyXY(p, 0.73, 0.41);
739 AddToPolyXY(p, 0.77, 0.79);
740 AddToPolyXY(p, 0.65, 0.955);
741 AddToPolyXY(p, 0.365, 0.965);
742 AddToPolyXY(p, 0.235, 0.785);
743 MultiplyPoly(p, 4, 4);
744 pp = TranslatePolyXY(p, 7, 7);
745 a = NewArea();
746 AddToArea(a, pp);
747 FreePoly(pp);
748 FreePoly(p);
749 g->Map->Layer[1]->FArea[0] = a;
750 p = NewRect(1,1,16,15);
751 a = NewArea();
752 AddToArea(a, p);
753 FreePoly(p);
754 g->Map->BoundingArea = a;
755 g->Map->XX = 16;
756 g->Map->YY = 15;
757 g->Map->X = 0;
758 g->Map->Y = 0;
760 SetParticleBoundingBox(0,0,16,15);
764 g->Map->noSprites = 2;
765 g->Map->Sprites = (Sprite **) malloc(sizeof(Sprite*)*g->Map->noSprites);
766 g->Map->SpritePos = (Vect *) malloc(sizeof(Vect)*g->Map->noSprites);
767 g->Map->SpritePos[0].x = 3.0;
768 g->Map->SpritePos[0].y = 1.0;
769 g->Map->SpritePos[1].x = 6.0;
770 g->Map->SpritePos[1].y = 6.0;
772 g->noWeapons = 8;
773 g->Weapon = malloc(sizeof(TWeapon) * g->noWeapons);
774 if(!g->Weapon){
775 fprintf(stderr, "Cannot allocate memory for Weapons\n");
777 for(i=0; i<g->noWeapons; i++){
778 g->Weapon[i].Exists = 0;
779 g->Weapon[i].Shape = 0;
782 g->noLilaneks = 2;
783 g->Lilanek = malloc(sizeof(TLilanek) * g->noLilaneks);
784 memset(g->Lilanek, 0, sizeof(TLilanek) * g->noLilaneks);
785 g->Lilanek[0].BSize.x = 1;
786 g->Lilanek[0].BSize.y = 1;
787 //g->Lilanek[0].Shape = NewRect(0.6,0.2,0.8,1.6);
788 p = NewPoly();
789 AddToPolyXY(p, 1, 0);
790 AddToPolyXY(p, 1.4, 0.2);
791 AddToPolyXY(p, 1.66, 1.38);
792 AddToPolyXY(p, 1.42, 2);
793 AddToPolyXY(p, 0.6, 2);
794 AddToPolyXY(p, 0.4, 1.46);
795 AddToPolyXY(p, 0.66, 0.2);
796 MultiplyPoly(p, 0.5*g->Lilanek[0].BSize.x, 0.5*g->Lilanek[0].BSize.y);
797 a = NewArea();
798 AddToArea(a, p);
799 FreePoly(p);
800 g->Lilanek[0].Shape = a;
801 g->Lilanek[0].Pos.x = 12;
802 g->Lilanek[0].Pos.y = 4;
803 g->Lilanek[0].Speed = 4;
804 g->Lilanek[0].Alive = 0;
805 g->Lilanek[0].Keys.Up = conf.pl1_key_up;
806 g->Lilanek[0].Keys.Down = conf.pl1_key_down;
807 g->Lilanek[0].Keys.Left = conf.pl1_key_left;
808 g->Lilanek[0].Keys.Right = conf.pl1_key_right;
809 g->Lilanek[0].Keys.Shoot = conf.pl1_key_fire;
810 g->Lilanek[0].Keys.Suicide = conf.pl1_key_suicide;
811 g->Lilanek[0].Keys.Switch = conf.pl1_key_switch;
812 g->Lilanek[0].Ammo = malloc(sizeof(int)*g->noWID);
813 for(i=0; i<g->noWID; i++){
814 g->Lilanek[0].Ammo[i] = 0;
816 g->Lilanek[0].ActiveWeapon = 0;
818 SDL_Color red = {255,211,211,255};
819 char redn[8]="Red";
820 g->Lilanek[0].Color = red;
821 strcpy(g->Lilanek[0].Name, redn);
823 g->Lilanek[1].Alive = 0; // little hack for SpawnLilanek to work
824 SpawnLilanek(g, 0);
826 p = g->Lilanek[0].Shape->p[0];
827 a = NewArea();
828 AddToArea(a, p);
829 g->Lilanek[1].Shape = a;
830 g->Lilanek[1].BSize.x = g->Lilanek[0].BSize.x;
831 g->Lilanek[1].BSize.y = g->Lilanek[0].BSize.y;
832 g->Lilanek[1].Pos.x = 14;
833 g->Lilanek[1].Pos.y = 4;
834 g->Lilanek[1].Speed = 4;
835 g->Lilanek[1].Alive = 0;
836 g->Lilanek[1].Keys.Up = conf.pl2_key_up;
837 g->Lilanek[1].Keys.Down = conf.pl2_key_down;
838 g->Lilanek[1].Keys.Left = conf.pl2_key_left;
839 g->Lilanek[1].Keys.Right = conf.pl2_key_right;
840 g->Lilanek[1].Keys.Shoot = conf.pl2_key_fire;
841 g->Lilanek[1].Keys.Suicide = conf.pl2_key_suicide;
842 g->Lilanek[1].Keys.Switch = conf.pl2_key_switch;
843 g->Lilanek[1].Ammo = malloc(sizeof(int)*g->noWID);
844 for(i=0; i<g->noWID; i++){
845 g->Lilanek[1].Ammo[i] = 0;
847 g->Lilanek[1].ActiveWeapon = 0;
849 SDL_Color blue = {180,180,255, 255};
850 char bluen[8]="Blue";
851 g->Lilanek[1].Color = blue;
852 strcpy(g->Lilanek[1].Name, bluen);
854 SpawnLilanek(g, 1);
856 g->WeaponTemplate = malloc(sizeof(TWeapon)*g->noWID);
857 p = NewRect(0,0,0.5,0.5);
858 a = NewArea();
859 AddToArea(a, p);
860 FreePoly(p);
861 g->WeaponTemplate[0].Shape = a;
862 g->WeaponTemplate[0].WID = 0;
863 g->WeaponTemplate[0].Exists = 1;
864 g->WeaponTemplate[0].Ammo = 30;
865 g->WeaponTemplate[0].MaxAmmo = 120;
866 g->WeaponTemplate[0].ReloadTime = 0.7;
867 g->WeaponTemplate[0].Burst = 10;
868 g->WeaponTemplate[0].RandomWeight = 10;
870 g->BulletTemplate = malloc(sizeof(TBullet) * g->noWID);
871 p = NewRect(0,0,0.1,0.1);
872 a = NewArea();
873 AddToArea(a, p);
874 FreePoly(p);
875 g->BulletTemplate[0].Shape = a;
876 g->BulletTemplate[0].WID = 0;
877 g->BulletTemplate[0].Speed = 20;
878 g->BulletTemplate[0].Range = 20;
879 g->BulletTemplate[0].SFuzzy = 0.160;
880 g->BulletTemplate[0].VFuzzy = 0.000;
882 p = NewRect(0,0,0.5,0.5);
883 a = NewArea();
884 AddToArea(a, p);
885 FreePoly(p);
886 g->WeaponTemplate[1].Shape = a;
887 g->WeaponTemplate[1].WID = 1;
888 g->WeaponTemplate[1].Exists = 1;
889 g->WeaponTemplate[1].Ammo = 3;
890 g->WeaponTemplate[1].MaxAmmo = 6;
891 g->WeaponTemplate[1].ReloadTime = 0.5;
892 g->WeaponTemplate[1].Burst = 1;
893 g->WeaponTemplate[1].RandomWeight = 5;
895 p = NewRect(0,0,0.5,0.5);
896 a = NewArea();
897 AddToArea(a, p);
898 FreePoly(p);
899 g->BulletTemplate[1].Shape = a;
900 g->BulletTemplate[1].WID = 1;
901 g->BulletTemplate[1].Speed = 0;
902 g->BulletTemplate[1].Range = 20;
903 g->BulletTemplate[1].SFuzzy = 0.0;
904 g->BulletTemplate[1].VFuzzy = 0.0;
906 p = NewRect(0,0,0.5,0.5);
907 a = NewArea();
908 AddToArea(a, p);
909 FreePoly(p);
910 g->WeaponTemplate[2].Shape = a;
911 g->WeaponTemplate[2].WID = 0;
912 g->WeaponTemplate[2].Exists = 1;
913 g->WeaponTemplate[2].Ammo = 10;
914 g->WeaponTemplate[2].MaxAmmo = 50;
915 g->WeaponTemplate[2].ReloadTime = 0.1;
916 g->WeaponTemplate[2].Burst = 1;
917 g->WeaponTemplate[2].RandomWeight = 15;
919 p = NewRect(0,0,0.1,0.1);
920 a = NewArea();
921 AddToArea(a, p);
922 FreePoly(p);
923 g->BulletTemplate[2].Shape = a;
924 g->BulletTemplate[2].WID = 0;
925 g->BulletTemplate[2].Speed = 20;
926 g->BulletTemplate[2].Range = 20;
927 g->BulletTemplate[2].SFuzzy = 0.040;
928 g->BulletTemplate[2].VFuzzy = 0.010;
929 g->noBullets = 64;
930 g->Bullet = malloc(sizeof(TBullet) * g->noBullets);
931 for(i=0; i<g->noBullets; i++){
932 g->Bullet[i].Exists = 0;
933 g->Bullet[i].Shape = 0;
936 g->PWeapondrop = 0.4;
938 return g;
942 void onExit()
944 GrKill();
945 SDL_Quit();
948 Uint32 callback_fps(Uint32 interval, void *param)
950 SDL_Event event;
951 event.type = SDL_USEREVENT;
952 SDL_PushEvent(&event);
954 return 1000;
957 SDL_Color InvertColor(SDL_Color c) {
958 SDL_Color t;
959 t.r = 255 - c.r;
960 t.g = 255 - c.g;
961 t.b = 255 - c.b;
962 t.unused = c.unused;
963 return t;
966 void Draw(Game *g){
967 int i;
969 QueueDrawSprite(bg, 0.0, 0.0,-1.0);
970 for(i=0; i < g->Map->noSprites; i++)
971 QueueDrawSprite(g->Map->Sprites[i], g->Map->SpritePos[i].x, g->Map->SpritePos[i].y, g->Map->Sprites[i]->z);
972 //Lilaneks
973 for(i=0; i < g->noLilaneks; i++){
974 SDL_Color c;
975 double pd;
976 char fragsbuf[16];
977 c =g->Lilanek[i].Color;
978 if(g->Lilanek[i].ReloadTime < g->WeaponTemplate[g->Lilanek[i].ActiveWeapon].ReloadTime){
979 c.r *= 0.5;
980 c.g *= 0.5;
981 c.b *= 0.5;
982 c.unused *= 0.8;
984 QueueDrawSpriteColorize(lil[g->Lilanek[i].Orientation], g->Lilanek[i].Pos.x-1.0, g->Lilanek[i].Pos.y-1.0, 1, c );
985 QueueDrawSpriteColorize(hud,16.0,4.0*i, 1.0, c );
986 switch(g->Lilanek[i].ActiveWeapon){
987 case 0: QueueDrawSprite(hud_brok, 16.0, 0.0+4.0*i,0.0);
988 break;
989 case 1: QueueDrawSprite(hud_mine, 16.0, 0.0+4.0*i,0.0);
990 break;
991 case 2: QueueDrawSprite(hud_kul, 16.0, 0.0+4.0*i,0.0);
992 break;
994 pd = (double)g->Lilanek[i].Ammo[g->Lilanek[i].ActiveWeapon]/(double)g->WeaponTemplate[g->Lilanek[i].ActiveWeapon].MaxAmmo;
995 QueueDrawSpriteColorizeStretch(hud_ammo, 18.0-2.0*pd, 0.0+4.0*i,4.0*pd, 4.0, 2.0,c);
996 pd = (double)g->Lilanek[i].ReloadTime/(double)g->WeaponTemplate[g->Lilanek[i].ActiveWeapon].ReloadTime;
997 if(pd>1.0)pd=1.0;
998 QueueDrawSpriteColorizeStretch(hud_reload, 18.0-2.0*pd, 0.0+4.0*i,4.0*pd, 4.0, 2.0,c);
999 snprintf (fragsbuf, 16, "F: %d", g->Lilanek[i].Frags);
1000 QueueDrawTextColorize (fragsbuf, 18.1, 4.0*i+0.5, 4.0, InvertColor(g->Lilanek[i].Color));
1001 snprintf (fragsbuf, 16, "D: %d", g->Lilanek[i].Deaths);
1002 QueueDrawTextColorize (fragsbuf, 18.1, 4.0*i+0.5*2.0, 4.0, InvertColor(g->Lilanek[i].Color));
1004 //Weapons
1005 for(i=0; i < g->noWeapons; i++)
1006 if(g->Weapon[i].Exists)
1007 switch(g->Weapon[i].WID){
1008 case 0: QueueDrawSprite(brok, g->Weapon[i].Pos.x - 1.0, g->Weapon[i].Pos.y - 1.0,1.0);
1009 break;
1010 case 1: QueueDrawSprite(mines, g->Weapon[i].Pos.x - 1.0, g->Weapon[i].Pos.y - 1.0,1.0);
1011 break;
1012 case 2: QueueDrawSprite(kul, g->Weapon[i].Pos.x - 1.0, g->Weapon[i].Pos.y - 1.0,1.0);
1013 break;
1016 for(i=0; i < g->noBullets; i++)
1017 if(g->Bullet[i].Exists)
1018 switch(g->Bullet[i].WID){
1019 case 0: QueueDrawSprite(kulka, g->Bullet[i].Pos.x - 1.0, g->Bullet[i].Pos.y - 1.0,1.0);break;
1020 case 1: QueueDrawSprite(mine, g->Bullet[i].Pos.x - 1.0, g->Bullet[i].Pos.y - 1.0,1.0);break;
1021 case 2: QueueDrawSprite(kulka, g->Bullet[i].Pos.x - 1.0, g->Bullet[i].Pos.y - 1.0,1.0);break;
1022 case -1: QueueDrawSprite(explo, g->Bullet[i].Pos.x - 1.0, g->Bullet[i].Pos.y - 1.0,1.0);break;
1027 int InitAll(Game **g){
1028 Config conf;
1029 conf = GetConfig();
1030 SaveConfig();
1031 //TODO: odstranit SaveConfig... je to tu jenom kvuli debugovani
1034 if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER |SDL_INIT_AUDIO ) < 0) {
1035 fprintf(stderr, "Unable to init SDL: %s\n", SDL_GetError());
1036 return 1;
1038 atexit(onExit);
1040 InitBase();
1041 GrInit();
1042 SoundInit();
1043 FontInit();
1044 *g = testgame();
1045 printf(PATH_GRAPHICS "lil.svg");
1046 lil[0] = LoadSpriteSVG(PATH_GRAPHICS "lil.svg", 1.0, 1.0);
1047 lil[1] = LoadSpriteSVG(PATH_GRAPHICS "lilb.svg", 1.0, 1.0);
1048 lil[2] = LoadSpriteSVG(PATH_GRAPHICS "lill.svg", 1.0, 1.0);
1049 lil[3] = LoadSpriteSVG(PATH_GRAPHICS "lil.svg", 1.0, 1.0);
1050 lil[4] = LoadSpriteSVG(PATH_GRAPHICS "lilr.svg", 1.0, 1.0);
1051 (*g)->Map->Sprites[0] = LoadSpriteSVG(PATH_GRAPHICS "prek1.svg", 3.0, 3.0);
1052 (*g)->Map->Sprites[1] = LoadSpriteSVG(PATH_GRAPHICS "prek2.svg", 4.0, 4.0);
1053 (*g)->Map->Sprites[0]->z = 1;
1054 (*g)->Map->Sprites[1]->z = 1;
1055 bg=0;
1056 bg = LoadSpriteSVG(PATH_GRAPHICS "bg.svg", 16.0, 15.0);
1057 kul = LoadSpriteSVG(PATH_GRAPHICS "kul.svg", 0.5, 0.5);
1058 explo = LoadSpriteSVG(PATH_GRAPHICS "exp.svg", 0.5, 0.5);
1059 dust = LoadSpriteSVG(PATH_GRAPHICS "dust.svg", 0.5, 0.5);
1060 blood = LoadSpriteSVG(PATH_GRAPHICS "blood.svg", 0.5, 0.5);
1061 brok = LoadSpriteSVG(PATH_GRAPHICS "brok.svg", 0.5, 0.5);
1062 kulka = LoadSpriteSVG(PATH_GRAPHICS "kulka.svg", 0.1, 0.1);
1063 mine = LoadSpriteSVG(PATH_GRAPHICS "mine.svg", 0.5, 0.5);
1064 mines = LoadSpriteSVG(PATH_GRAPHICS "mines.svg", 0.5, 0.5);
1065 hud = LoadSpriteSVG(PATH_GRAPHICS "hud.svg", 4.0, 4.0);
1066 hud_mine = LoadSpriteSVG(PATH_GRAPHICS "mines.svg", 2.2, 2.2);
1067 hud_kul = LoadSpriteSVG(PATH_GRAPHICS "kul.svg", 2.2, 2.2);
1068 hud_brok = LoadSpriteSVG(PATH_GRAPHICS "brok.svg", 2.2, 2.2);
1069 hud_ammo = LoadSpriteSVG(PATH_GRAPHICS "hud_ammo.svg", 4.0, 4.0);
1070 hud_reload = LoadSpriteSVG(PATH_GRAPHICS "hud_reload.svg", 4.0, 4.0);
1072 s_die = LoadSound(PATH_SOUNDS "die.wav");
1073 s_bonus = LoadSound(PATH_SOUNDS "bonus.wav");
1074 s_expl = LoadSound(PATH_SOUNDS "expl.wav");
1075 s_shoot = LoadSound(PATH_SOUNDS "shoot.wav");
1076 s_mine = LoadSound(PATH_SOUNDS "mine.wav");
1078 SDL_AddTimer(1000, callback_fps, NULL);
1080 return 0;
1083 int GameLoop(Game *g){
1084 SDL_Event event;
1085 Config conf;
1086 double lasttime, dtime;
1088 lasttime = SDL_GetTicks()*0.001;
1089 while (1) {
1090 while (SDL_PollEvent(&event)) {
1091 switch (event.type) {
1092 case SDL_KEYDOWN:
1093 if (event.key.keysym.sym == SDLK_q) {
1094 return 0;
1096 break;
1098 case SDL_QUIT:
1099 return 0;
1100 break;
1102 case SDL_VIDEORESIZE:
1103 /* Resize the screen and redraw */
1104 conf=GetConfig();
1105 conf.screen_width=event.resize.w;
1106 conf.screen_height=event.resize.h;
1107 SetConfig(conf);
1109 InvalidateSprites();
1110 break;
1112 case SDL_USEREVENT:
1113 printf("%d frames\n",frames);
1114 frames=0;
1115 break;
1117 default:
1118 break;
1121 dtime = SDL_GetTicks()*0.001-lasttime;
1122 lasttime += dtime;
1123 if(dtime < 0)
1124 dtime=0.0;
1125 if(dtime > 0.1)
1126 dtime=0.1;
1127 UpdateLogic(g, dtime*0.5);
1128 UpdateLogic(g, dtime*0.5);
1129 ClrScr();
1130 Draw(g);
1131 UpdateAndQueueDrawParticles(dtime);
1132 DrawSprites();
1133 frames++;
1134 //we don't want to waste cpu...
1135 if(frames >=50){
1136 SDL_Delay(10);
1139 return 0;
1142 int main(int argc, char **argv){
1143 Game *g;
1144 srand(time(0));
1145 InitAll(&g);
1146 GameLoop(g);
1147 return 0;