Browse Source

Merge branch 'local_variables' of matteozeccolimarazzini/invaders into master

Remove global state

This fixes #11. I wouldn't have to come up with something to write if gogs supported rebase and fast-forward.
blue 4 years ago
parent
commit
1bf956d7ba
5 changed files with 58 additions and 58 deletions
  1. 19 21
      bossrush.cpp
  2. 0 3
      definitions.hpp
  3. 23 14
      functions.cpp
  4. 4 4
      functions.hpp
  5. 12 16
      invaders.cpp

+ 19 - 21
bossrush.cpp

@@ -31,8 +31,6 @@ using std::endl;
 using std::min;
 using std::max;
 
-int ENEMY_NUM=1;	// This variable isn't used, but we have to define it since it is decleared in definitions.hpp"
-
 int main(int argc,char** argv)
 {
 
@@ -76,6 +74,8 @@ int main(int argc,char** argv)
 	
 	//PARAMETERS/UTILITIES
 	double shootrate;				  	//probability of an enemy shooting a bomb 
+	double poweruprate;					//probability of a powerup being dropped
+	int walls_num;						//number of walls
 	int command;						//keyboard input
 	int score=0;					   	//score: gain +100 when an enemy is destroyed and +50 when a bomb is destroyed
 	int level=1;						//difficulty level
@@ -112,7 +112,7 @@ int main(int argc,char** argv)
 	boss1.alive=true;
 
 	player1.weaponclass=1;
-	
+
 	//NCURSES STUFF
 	initscr();
 	curs_set(0);
@@ -130,35 +130,33 @@ int main(int argc,char** argv)
 	init_pair(7,COLOR_RED,COLOR_RED);
 	init_pair(8,COLOR_GREEN,COLOR_GREEN);
 	init_pair(9,COLOR_BLACK,COLOR_GREEN);
-	
-	WALLS_NUM=3;
-	
-	w_vec walls(WALLS_NUM);
-	
+
 	Score=newwin(3,10,R/3,C+3);
 	BossHP=newwin(3,15,R/3-3,C+3);
-	
+
+	level = choose_level_bossrush(commands);
+	setup_level_bossrush(level, shootrate, poweruprate, walls_num, refresh_time);
+
+	w_vec walls(walls_num);
 	int i=0;
 	for(w_vec::iterator it=walls.begin(); it!=walls.end(); ++it, ++i)				//creating walls (in a quite symmetric pattern)
-		it->create((i+1)*(C/(3*WALLS_NUM+1))+i*(2*C/(3*WALLS_NUM+1)),2*R/3,(int)min(6,2*C/(3*WALLS_NUM+1)),2,3);
+		it->create((i+1)*(C/(3*walls_num+1))+i*(2*C/(3*walls_num+1)),2*R/3,(int)min(6,2*C/(3*walls_num+1)),2,3);
+
 
-	level = choose_level_bossrush(commands);
-	setup_level_bossrush(level, shootrate, refresh_time);
-	
 	player1.set_commands(commands);
-	
+
 	if(level<3)
 		player1.weaponclass=2;
 
 	erase();
-	
+
 	if(argc>1)
 		if(strcmp(argv[1],"--uber-debug")==0){ player1.weaponclass=5; player1.weaponclassrkt=5; player1.rocketlauncher=true; player1.length=MAX_LENGTH; }
-	
+
 	std::thread t_draw(draw,std::ref(player1),std::ref(bullets),std::ref(bombs),std::ref(enemies),std::ref(walls),std::ref(powerups),std::ref(rockets),std::ref(boss1));
-	
+
 /////////ENTERING MAIN LOOP
-	
+
 	while(1)
 	{
 		napms(refresh_time);					//ncurses sleep function (ms)
@@ -234,7 +232,7 @@ int main(int argc,char** argv)
 				
 		powerups.unique(); 	 //FIXME: THIS ONLY WORKS ON CONSECUTIVE ELEMENTS OF THE LIST!! we should at least sort powerups before calling unique()
 
-		interactions(player1,bullets,bombs,enemies,walls,powerups,rockets,boss1,score);
+		interactions(player1,bullets,bombs,enemies,walls,powerups,rockets,boss1,score,poweruprate);
 		
 		
 		if(boss1.health<=0)
@@ -292,8 +290,8 @@ int main(int argc,char** argv)
 					refresh();
 					if(choice=='n')
 						level = choose_level_bossrush(commands);
-					setup_level_bossrush(level, shootrate, refresh_time);
-					reset(player1, enemies, boss1, bullets, bombs, walls, powerups,rockets, level,chflag); // reset box, player, enemy and deletes all bullets and bombs
+					setup_level_bossrush(level, shootrate, poweruprate, walls_num, refresh_time);
+					reset(player1, enemies, boss1, bullets, bombs, walls, walls_num, powerups, rockets, chflag); // reset box, player, enemy and deletes all bullets and bombs
 					resetbosses(bosses,boss1,player1);
 					num=0;
 					if(level<3)

+ 0 - 3
definitions.hpp

@@ -87,9 +87,6 @@
 #define backcyan "\033[46m"
 #define backgreen "\033[1;42m" 
 
-extern int ENEMY_NUM;
-static int WALLS_NUM;
-
 extern std::mutex m_player;
 extern std::mutex m_enemies;
 extern std::mutex m_bombs;

+ 23 - 14
functions.cpp

@@ -294,7 +294,7 @@ int choose_level(int* commands)
 	return input - '0';
 }
 
-void setup_level(int level, double& shootr, int& refresh_t, boss& boss1, int& enemy_num, int* commands)
+void setup_level(int level, double& shootr, double& poweruprate, int& refresh_t, boss& boss1, int& enemy_num, int& walls_num)
 {
 	switch(level)        //setting game parameters
 	{
@@ -308,6 +308,7 @@ void setup_level(int level, double& shootr, int& refresh_t, boss& boss1, int& en
 			bossname=bossname+RECORD_DIR+BOSS_FILE1+".dat";
 			boss1.loadpicture(bossname.c_str());
 			enemy_num=30;
+			walls_num = 3;
 			break;
 		}
 		case 2:
@@ -320,6 +321,7 @@ void setup_level(int level, double& shootr, int& refresh_t, boss& boss1, int& en
 			bossname=bossname+RECORD_DIR+BOSS_FILE2+".dat";
 			boss1.loadpicture(bossname.c_str());
 			enemy_num=40;
+			walls_num = 2;
 			break;
 		}
 		case 3:
@@ -332,6 +334,7 @@ void setup_level(int level, double& shootr, int& refresh_t, boss& boss1, int& en
 			bossname=bossname+RECORD_DIR+BOSS_FILE3+".dat";
 			boss1.loadpicture(bossname.c_str());
 			enemy_num=45;
+			walls_num = 2;
 			break;
 		}
 		case 4:
@@ -344,9 +347,11 @@ void setup_level(int level, double& shootr, int& refresh_t, boss& boss1, int& en
 			bossname=bossname+RECORD_DIR+BOSS_FILE4+".dat";
 			boss1.loadpicture(bossname.c_str());
 			enemy_num=50;
+			walls_num = 2;
 			break;
 		}
 	}
+	poweruprate = (double)AVERAGE_DROP/enemy_num;
 }
 
 void change_commands(int* commands){
@@ -457,7 +462,7 @@ bool gameover(player& _player,b_list& bombs)		//Checks if a bomb/enemy/boss hit
 }
 
 
-void reset(player& _player, e_list &enemies, boss& boss1, b_list& bullets, b_list& bombs, w_vec& walls, b_list& powerups, r_list& rockets, char level,int& chflag)
+void reset(player& _player, e_list &enemies, boss& boss1, b_list& bullets, b_list& bombs, w_vec& walls, int walls_num, b_list& powerups, r_list& rockets, int& chflag)
 {
 	
 	_player.x=C/2;				//resetting initial position and stats of player1
@@ -490,14 +495,10 @@ void reset(player& _player, e_list &enemies, boss& boss1, b_list& bullets, b_lis
 	rockets.clear();
 	chflag=0;
 	
-	WALLS_NUM=2;				//resetting walls
-	
-	if(atoi(&level)==1) WALLS_NUM=3;
-	
-	walls.resize(WALLS_NUM);
+	walls.resize(walls_num);
 
-	for(int i=0;i<walls.size();i++)
-		walls[i].create((i+1)*(C/(3*walls.size()+1))+i*(2*C/(3*walls.size()+1)),2*R/3,(int)min(6,2*C/(3*(int)(walls.size())+1)),2,5);
+	for(int i=0;i<walls_num;i++)
+		walls[i].create((i+1)*(C/(3*walls_num+1))+i*(2*C/(3*walls_num+1)),2*R/3,(int)min(6,2*C/(3*(int)(walls_num)+1)),2,5);
 	
 }
 	
@@ -921,7 +922,7 @@ void drop_powerup(int x,int y,b_list& powerups)
 }
 
 
-void interactions(player& player1,b_list& bullets,b_list& bombs,e_list& enemies,w_vec& walls,b_list& powerups,r_list& rockets,boss& boss1,int& score){
+void interactions(player& player1,b_list& bullets,b_list& bombs,e_list& enemies,w_vec& walls,b_list& powerups,r_list& rockets,boss& boss1,int& score, double poweruprate){
 	bool should_continue;
 	
 	b_list::iterator it1=bullets.begin();
@@ -955,7 +956,7 @@ void interactions(player& player1,b_list& bullets,b_list& bombs,e_list& enemies,
 				it1=bullets.erase(it1);
 				should_continue = true;
 				it3->alive=false;
-				if((double)rand()/RAND_MAX<(double)AVERAGE_DROP/ENEMY_NUM)	//a powerup is dropped (AVERAGE_DROP is the mean number of powerups dropped per play in Normal mode)		
+				if((double)rand()/RAND_MAX<poweruprate)	//a powerup is dropped
 					drop_powerup(it3->x,it3->y+1,powerups);
 				score+=100;
 				break;
@@ -1079,7 +1080,7 @@ void interactions(player& player1,b_list& bullets,b_list& bombs,e_list& enemies,
  		++itp;
 	}
         	
-	for(int i=0;i<walls.size();i++)
+	for(size_t i=0;i<walls.size();i++)
 	  	for(int j=0;j<walls[i].width;j++)
 	  		for(int k=0;k<walls[i].height;k++)
 	  		{
@@ -1438,7 +1439,7 @@ int choose_level_bossrush(int* commands)
 	return input - '0';
 }
 
-void setup_level_bossrush(int level, double& shootr, int& refresh_t)
+void setup_level_bossrush(int level, double& shootr, double& poweruprate, int& walls_num, int& refresh_t)
 {
 	switch(level)        //setting game parameters
 	{
@@ -1446,24 +1447,32 @@ void setup_level_bossrush(int level, double& shootr, int& refresh_t)
 		{
 			shootr = 0.006;
 			refresh_t = (int)(0.09*1E3);
+			poweruprate = 0.066;
+			break;
 		}
 		case 2:
 		{
 			shootr = 0.009;
 			refresh_t = (int)(0.085*1E3);
+			poweruprate = 0.05;
+			break;
 		}
 		case 3:
 		{
 			shootr = 0.015;
 			refresh_t = (int)(0.08*1E3);
+			poweruprate = 0.044;
+			break;
 		}
 		case 4:
 		{
 			shootr = 0.020;
 			refresh_t = (int)(0.07*1E3);
+			poweruprate = 0.04;
+			break;
 		}
 	}
-	
+	walls_num = 3;
 }
 
 void resetbosses(boss* Bosses,boss& boss1,player& player1){

+ 4 - 4
functions.hpp

@@ -64,7 +64,7 @@ void print_scores();
 
 int choose_level(int* commands);
 
-void setup_level(int level, double& shootr, int& refresh_t, boss& boss1, int& enemy_num, int* commands);
+void setup_level(int level, double& shootr, double& poweruprate, int& refresh_t, boss& boss1, int& enemy_num, int& walls_num);
 
 void change_commands(int* commands);
 
@@ -74,7 +74,7 @@ char playagain(WINDOW*);
 
 bool gameover(player&,b_list&);
 
-void reset(player&, e_list &, boss&, b_list& bullets, b_list& bombs, w_vec& walls, b_list &powerups,r_list&,char level,int& chflag);
+void reset(player&, e_list &, boss&, b_list& bullets, b_list& bombs, w_vec& walls, int walls_num, b_list &powerups, r_list&, int& chflag);
 
 bool enemyalive(e_list &);
 
@@ -92,7 +92,7 @@ void Defeat(int);
 
 int choose_level_bossrush(int* commands);
 
-void setup_level_bossrush(int level, double& shootr,int& refresh_t);
+void setup_level_bossrush(int level, double& shootr, double& poweruprate, int& walls_num, int& refresh_t);
 
 void resetbosses(boss* Bosses,boss& boss1,player& player1);
 
@@ -112,7 +112,7 @@ void activate_combo(boss&,e_list&,b_list&,b_list&,b_list&,r_list&);
 
 void drop_powerup(int x,int y,b_list& powerups);
 
-void interactions(player& player1,b_list& bullets,b_list& bombs,e_list& enemies,w_vec& walls,b_list& powerups,r_list& rockets,boss& boss1,int& score);
+void interactions(player& player1,b_list& bullets,b_list& bombs,e_list& enemies,w_vec& walls,b_list& powerups,r_list& rockets,boss& boss1,int& score, double poweruprate);
 
 void draw(player& player1,b_list& bullets,b_list& bombs,e_list& enemies,w_vec& walls,b_list& powerups,r_list& rockets,boss& boss1);
 

+ 12 - 16
invaders.cpp

@@ -19,8 +19,6 @@ using std::cout;
 using std::endl; 
 using std::min;
 
-int ENEMY_NUM=1;
-
 int main(int argc,char** argv)
 {
 	if(argc>1){
@@ -60,6 +58,9 @@ int main(int argc,char** argv)
 	
 	//PARAMETERS/UTILITIES
 	double shootrate;				  	//probability of an enemy shooting a bomb 
+	double poweruprate;					//probability of a powerup being dropped
+	int enemy_num;						//number of enemies in the current match
+	int walls_num;						//number of walls
 	int command;						//keyboard input
 	int score=0;					   	//score: gain +100 when an enemy is destroyed and +50 when a bomb is destroyed
 	int level=1;						//difficulty level
@@ -103,25 +104,20 @@ int main(int argc,char** argv)
 	
 	//////////PROGRAM START: creation of game objects and set of parameters
 	
-	WALLS_NUM=2;
-	
 	level = choose_level(commands);     	// get desired level
-	setup_level(level, shootrate, refresh_time, boss1, ENEMY_NUM, commands);     	// set game parameters and boss
-	load_enemies(enemies,ENEMY_NUM);
+	setup_level(level, shootrate, poweruprate, refresh_time, boss1, enemy_num, walls_num);     	// set game parameters and boss
+	load_enemies(enemies,enemy_num);
 	player1.set_commands(commands);
 	erase();
 	
-	if(level==1) WALLS_NUM=3;
-	
-	
-	w_vec walls(WALLS_NUM);
+	w_vec walls(walls_num);
 	
 	Score=newwin(3,10,R/3,C+3);
 	BossHP=newwin(3,15,R/3-3,C+3);
 	
 	int i=0;
 	for(w_vec::iterator it=walls.begin(); it!=walls.end(); ++it, ++i)				//creating walls (in a quite symmetric pattern)
-		it->create((i+1)*(C/(3*walls.size()+1))+i*(2*C/(3*walls.size()+1)),2*R/3,(int)min(6,2*C/(3*(int)(walls.size())+1)),2,3);
+		it->create((i+1)*(C/(3*walls_num+1))+i*(2*C/(3*walls_num+1)),2*R/3,(int)min(6,2*C/(3*(int)(walls_num)+1)),2,3);
 
 	/////////ENTERING MAIN LOOP
 	
@@ -191,7 +187,7 @@ int main(int argc,char** argv)
 			{
 				//mvaddch(it->y,it->x,' ');
 				it->next_pos();                                        //evaluate new positions
-				if(it->alive && (double)rand()/RAND_MAX<(shootrate*ENEMY_NUM/n_enemies)){     //try a bomb-dropping
+				if(it->alive && (double)rand()/RAND_MAX<(shootrate*enemy_num/n_enemies)){     //try a bomb-dropping
 						//if(sound) shoot_sound = pthread_create(&thread[1],NULL,penshoot_sound,NULL);
 						it->shoot(bombs);
 				}
@@ -246,7 +242,7 @@ int main(int argc,char** argv)
 				
 		powerups.unique(); 	 //FIXME: THIS ONLY WORKS ON CONSECUTIVE ELEMENTS OF THE LIST!! we should at least sort powerups before calling unique()
 		
-		interactions(player1,bullets,bombs,enemies,walls,powerups,rockets,boss1,score);
+		interactions(player1,bullets,bombs,enemies,walls,powerups,rockets,boss1,score,poweruprate);
 
 		///////ENDGAME CHECKS               
 		if(boss1.health<1 || gameover(player1,bombs))
@@ -279,9 +275,9 @@ int main(int argc,char** argv)
 					refresh();
 					if(choice=='n')
 						level = choose_level(commands);     	// get desired level
-					setup_level(level, shootrate, refresh_time, boss1, ENEMY_NUM, commands);     	// set game parameters and boss
-					load_enemies(enemies,ENEMY_NUM);
-					reset(player1, enemies, boss1, bullets, bombs, walls, powerups,rockets, level,chflag); // reset box, player, enemy and deletes all bullets and bombs
+					setup_level(level, shootrate, poweruprate, refresh_time, boss1, enemy_num, walls_num);     	// set game parameters and boss
+					load_enemies(enemies,enemy_num);
+					reset(player1, enemies, boss1, bullets, bombs, walls, walls_num, powerups, rockets, chflag); // reset box, player, enemy and deletes all bullets and bombs
 					erase();
 					refresh();
 					continue;	// Go to the beginning of the main loop