Browse Source

Merge branch 'master' of jp/invaders into master

@teo yup, will do
blue 4 years ago
parent
commit
8e35ca0494
11 changed files with 130 additions and 230 deletions
  1. 3 0
      .gitignore
  2. 0 1
      args.tmp
  3. 0 5
      big_alienboss.dat.old
  4. 0 1
      dati.dat
  5. 124 180
      functions.cpp
  6. 1 1
      functions.hpp
  7. 2 23
      invaders.cpp
  8. 0 8
      myboss.dat.old
  9. 0 0
      palombo
  10. 0 5
      palombo.dat.old
  11. 0 6
      spoletini.dat.old

+ 3 - 0
.gitignore

@@ -0,0 +1,3 @@
+*.o
+*.swp
+*~

+ 0 - 1
args.tmp

@@ -1 +0,0 @@
-palombo.dat

+ 0 - 5
big_alienboss.dat.old

@@ -1,5 +0,0 @@
- \__ ______ __/  
-\___  =  =  ___/ 
- |  | =  = |  |  
-     \_  _/      
-    /  ||  \     

+ 0 - 1
dati.dat

@@ -1 +0,0 @@
-lkaid landed on the strange ground of the Void Planet. It was quite an exotic panorama: blue grass and pink-leaved trees aside, the whole planet seemed to be constantly experiencing a

+ 124 - 180
functions.cpp

@@ -759,7 +759,7 @@ void activate_combo(boss& boss1,e_list& enemies,b_list& bullets,b_list& bombs,b_
 
 //=============BOX'S REPLACEMENT FUNCTIONS============================
 
-void draw(player& player1,b_list& bullets,b_list& bombs,e_list& enemies,w_vec& walls,b_list& powerups,r_list& rockets,boss& boss1, int& refresh_time, bool& keep_drawing,bool& kill_yourself){
+void draw(player& player1,b_list& bullets,b_list& bombs,e_list& enemies,w_vec& walls,b_list& powerups,r_list& rockets,boss& boss1){
 	
 	struct point
 	{
@@ -768,154 +768,132 @@ void draw(player& player1,b_list& bullets,b_list& bombs,e_list& enemies,w_vec& w
 		int x,y;
 	};
 	
-	std::list<point> bullets_p, enemies_p, bombs_p, powerups_p, rockets_p, walls_p, player_p, boss_p;
-	
-	printw("STARTED");	
-	while(!kill_yourself)
-			if(keep_drawing)
-			{				
-				//REDRAW BULLETS
-				m_bullets.lock();
-				for(auto bullet_p: bullets_p)
-					mvaddch(bullet_p.y,bullet_p.x,' ');
-				bullets_p.clear();
-				
-				attron(COLOR_PAIR(3));
-				for(auto bullet: bullets)
-				{
-					mvaddch(bullet.y,bullet.x,BULLET_SPRITE);
-					bullets_p.push_back(point(bullet.x,bullet.y));
-				}
-				m_bullets.unlock();
-				
-				//REDRAW BOMBS
-				m_bombs.lock();
-				for(auto bomb_p: bombs_p)
-					mvaddch(bomb_p.y,bomb_p.x,' ');
-				bombs_p.clear();
-				
-				attron(COLOR_PAIR(6));
-				for(auto bomb: bombs)
-				{
-				 //if(bomb.y<R)
-					mvaddch(bomb.y,bomb.x,BOMB_SPRITE);
-					bombs_p.push_back(point(bomb.x,bomb.y)); 
-				}
-				m_bombs.unlock();
-				
-				//REDRAW ENEMIES
-				m_enemies.lock();
-				for(auto enemy_p: enemies_p)
-					mvaddch(enemy_p.y,enemy_p.x,' ');
-				enemies_p.clear();
-				
-				attron(COLOR_PAIR(2));
-				for(auto enemy: enemies)
-				{
-					if(enemy.alive==true)
-					{
-						mvaddch(enemy.y,enemy.x,ENEMY_SPRITE);
-						enemies_p.push_back(point(enemy.x,enemy.y));
-					}
-				}
-				m_enemies.unlock();
-				
-				//REDRAW ROCKETS
-				m_rockets.lock();
-				for(auto rocket_p: rockets_p)
-					mvaddch(rocket_p.y,rocket_p.x,' ');
-				rockets_p.clear();
-				
-				attron(COLOR_PAIR(3));
-				for(auto rocket: rockets)
-				{
-					mvaddch(rocket.y,rocket.x,ROCKET_SPRITE);
-					rockets_p.push_back(point(rocket.x,rocket.y));
-				}
-				m_rockets.unlock();
-				
-				
-				//REDRAW POWERUPS
-				m_pow.lock();
-				for(auto powerup_p: powerups_p)
-					mvaddch(powerup_p.y,powerup_p.x,' ');
-				powerups_p.clear();
-				
-				for(auto powerup: powerups){
-					if(powerup.id == 64){
-						attron(COLOR_PAIR(3));
-						mvaddch(powerup.y,powerup.x,POWERUP1_SPRITE);
-					}
-					else if(powerup.id == 128){
-						attron(COLOR_PAIR(1));
-						mvaddch(powerup.y,powerup.x,POWERUP2_SPRITE); 
-					}
-					else{
-						attron(COLOR_PAIR(4));
-						mvaddch(powerup.y,powerup.x,POWERUP3_SPRITE);
-					}
-					powerups_p.push_back(point(powerup.x,powerup.y));
-				}
-				m_pow.unlock();
-				
-				//REDRAW BOSS
-				m_boss.lock();
-				for(auto boss_point: boss_p)
-					mvaddch(boss_point.y,boss_point.x,' ');
-				boss_p.clear();
-				
-				if(boss1.alive)
-					for(int i=boss1.y; i<boss1.y+boss1.height; i++)
-						for(int j=boss1.x; j<boss1.x+boss1.width; j++){
-							if(boss1.health>.5*boss1.healthmax)
-								attron(COLOR_PAIR(5));
-							else if(boss1.health>.25*boss1.healthmax)
-								attron(COLOR_PAIR(3));
-							else
-								attron(COLOR_PAIR(2));
-							mvaddch(i,j,boss1.picture[i-boss1.y][j-boss1.x]);
-							boss_p.push_back(point(j,i));
-						}
-				m_boss.unlock();
-				
-				
-				//REDRAW PLAYER
-				m_player.lock();
-				for(auto play_point: player_p)
-					mvaddch(play_point.y,play_point.x,' ');
-				player_p.clear();
-				
-				if(player1.godmode) attron(COLOR_PAIR(5));
-				else attron(COLOR_PAIR(1));
-				for(int i=player1.x;i<player1.x+player1.length;++i)
+	static std::vector<point> bullets_p, enemies_p, bombs_p, powerups_p, rockets_p, walls_p, player_p, boss_p;
+	
+	//REDRAW BULLETS
+	for(auto bullet_p: bullets_p)
+		mvaddch(bullet_p.y,bullet_p.x,' ');
+	bullets_p.clear();
+	
+	attron(COLOR_PAIR(3));
+	for(auto bullet: bullets)
+	{
+		mvaddch(bullet.y,bullet.x,BULLET_SPRITE);
+		bullets_p.push_back(point(bullet.x,bullet.y));
+	}
+	
+	//REDRAW BOMBS
+	for(auto bomb_p: bombs_p)
+		mvaddch(bomb_p.y,bomb_p.x,' ');
+	bombs_p.clear();
+	
+	attron(COLOR_PAIR(6));
+	for(auto bomb: bombs)
+	{
+	 //if(bomb.y<R)
+		mvaddch(bomb.y,bomb.x,BOMB_SPRITE);
+		bombs_p.push_back(point(bomb.x,bomb.y)); 
+	}
+	
+	//REDRAW ENEMIES
+	for(auto enemy_p: enemies_p)
+		mvaddch(enemy_p.y,enemy_p.x,' ');
+	enemies_p.clear();
+	
+	attron(COLOR_PAIR(2));
+	for(auto enemy: enemies)
+	{
+		if(enemy.alive==true)
+		{
+			mvaddch(enemy.y,enemy.x,ENEMY_SPRITE);
+			enemies_p.push_back(point(enemy.x,enemy.y));
+		}
+	}
+	
+	//REDRAW ROCKETS
+	for(auto rocket_p: rockets_p)
+		mvaddch(rocket_p.y,rocket_p.x,' ');
+	rockets_p.clear();
+	
+	attron(COLOR_PAIR(3));
+	for(auto rocket: rockets)
+	{
+		mvaddch(rocket.y,rocket.x,ROCKET_SPRITE);
+		rockets_p.push_back(point(rocket.x,rocket.y));
+	}
+	
+	
+	//REDRAW POWERUPS
+	for(auto powerup_p: powerups_p)
+		mvaddch(powerup_p.y,powerup_p.x,' ');
+	powerups_p.clear();
+	
+	for(auto powerup: powerups){
+		if(powerup.id == 64){
+			attron(COLOR_PAIR(3));
+			mvaddch(powerup.y,powerup.x,POWERUP1_SPRITE);
+		}
+		else if(powerup.id == 128){
+			attron(COLOR_PAIR(1));
+			mvaddch(powerup.y,powerup.x,POWERUP2_SPRITE); 
+		}
+		else{
+			attron(COLOR_PAIR(4));
+			mvaddch(powerup.y,powerup.x,POWERUP3_SPRITE);
+		}
+		powerups_p.push_back(point(powerup.x,powerup.y));
+	}
+	
+	//REDRAW BOSS
+	for(auto boss_point: boss_p)
+		mvaddch(boss_point.y,boss_point.x,' ');
+	boss_p.clear();
+	
+	if(boss1.alive)
+		for(int i=boss1.y; i<boss1.y+boss1.height; i++)
+			for(int j=boss1.x; j<boss1.x+boss1.width; j++){
+				if(boss1.health>.5*boss1.healthmax)
+					attron(COLOR_PAIR(5));
+				else if(boss1.health>.25*boss1.healthmax)
+					attron(COLOR_PAIR(3));
+				else
+					attron(COLOR_PAIR(2));
+				mvaddch(i,j,boss1.picture[i-boss1.y][j-boss1.x]);
+				boss_p.push_back(point(j,i));
+			}
+	
+	
+	//REDRAW PLAYER
+	for(auto play_point: player_p)
+		mvaddch(play_point.y,play_point.x,' ');
+	player_p.clear();
+	
+	if(player1.godmode) attron(COLOR_PAIR(5));
+	else attron(COLOR_PAIR(1));
+	for(int i=player1.x;i<player1.x+player1.length;++i)
+	{
+		mvaddch(player1.y,i,PLAYER_SPRITE);	
+		player_p.push_back(point(i,player1.y));
+	}
+	
+	
+	//REDRAW WALLS
+	for(auto wall_p: walls_p)
+		mvaddch(wall_p.y,wall_p.x,' ');
+	walls_p.clear();
+	
+	for(int i=0;i<walls.size();i++)
+		for(int k=0;k<walls[i].width;k++)
+			for(int j=0;j<walls[i].height;j++){
+				if(walls[i].health[j][k]==1) attron(COLOR_PAIR(2));
+				else attron(COLOR_PAIR(4));
+				if(walls[i].health[j][k]>0)
 				{
-					mvaddch(player1.y,i,PLAYER_SPRITE);	
-					player_p.push_back(point(i,player1.y));
+					mvaddch(j+walls[i].y,k+walls[i].x,WALL_SPRITE);
+					walls_p.push_back(point(k+walls[i].x,j+walls[i].y));
 				}
-				m_player.unlock();
-				
-				
-				//REDRAW WALLS
-				m_walls.lock();
-				for(auto wall_p: walls_p)
-					mvaddch(wall_p.y,wall_p.x,' ');
-				walls_p.clear();
-				
-				for(int i=0;i<walls.size();i++)
-					for(int k=0;k<walls[i].width;k++)
-						for(int j=0;j<walls[i].height;j++){
-							if(walls[i].health[j][k]==1) attron(COLOR_PAIR(2));
-							else attron(COLOR_PAIR(4));
-							if(walls[i].health[j][k]>0)
-							{
-								mvaddch(j+walls[i].y,k+walls[i].x,WALL_SPRITE);
-								walls_p.push_back(point(k+walls[i].x,j+walls[i].y));
-							}
-						}
-				m_walls.unlock();
-			
-				napms(refresh_time/2);
 			}
+			
 }
 
 
@@ -941,7 +919,6 @@ 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){
 	bool should_continue;
 	
-	m_bullets.lock();
 	b_list::iterator it1=bullets.begin();
 	while(it1!=bullets.end())
 	{		 
@@ -953,7 +930,6 @@ void interactions(player& player1,b_list& bullets,b_list& bombs,e_list& enemies,
 			continue;
 		}
 		
-		m_bombs.lock();
 		b_list::iterator it2 = bombs.begin();
 		while(it2!=bombs.end())
 		{
@@ -967,10 +943,8 @@ void interactions(player& player1,b_list& bullets,b_list& bombs,e_list& enemies,
 			else
 				++it2;
 		}
-		m_bombs.unlock();
 		if(should_continue) continue;
 		
-		m_enemies.lock();
 		for(e_list::iterator it3=enemies.begin(); it3!=enemies.end(); ++it3)
 			if(it1->x==it3->x && it1->y==it3->y && it3->alive){			//bullet+enemy
 				it1=bullets.erase(it1);
@@ -981,30 +955,24 @@ void interactions(player& player1,b_list& bullets,b_list& bombs,e_list& enemies,
 				score+=100;
 				break;
 			}
-		m_enemies.unlock();
 		if(should_continue) continue;
 		
 		
 			
-		m_boss.lock();
 		if((it1->x>=boss1.x+boss1.width/2-2 && it1->x<=boss1.x+boss1.width/2+2) && boss1.alive){	//bullet+boss
 				boss1.health--;
 				if(it1->y==boss1.y) it1=bullets.erase(it1);	//bullet gets destroyed only when has reached boss's top
 		        	else
 				{
 					++it1;
-					m_boss.unlock();
 					continue;
 				}
 								//(to make the boss lose life more quickly)
 		}
-		m_boss.unlock();
 		
 		++it1;
 	}
-	m_bullets.unlock();
     
-   m_rockets.lock();
    r_list::iterator itr=rockets.begin();   
 	while(itr != rockets.end())
 	{
@@ -1016,7 +984,6 @@ void interactions(player& player1,b_list& bullets,b_list& bombs,e_list& enemies,
 			continue;
 		}
 		
-		m_bombs.lock();
 		b_list::iterator it2=bombs.begin(); //bomb+rocket
 		while(it2!=bombs.end())
 		{
@@ -1030,10 +997,8 @@ void interactions(player& player1,b_list& bullets,b_list& bombs,e_list& enemies,
 			else
 				++it2;
 		}
-		m_bombs.unlock();
 		if(should_continue) continue;
 		
-		m_enemies.lock();
 		e_list::iterator it3 = enemies.begin();
 		while(it3!=enemies.end())
 		{
@@ -1046,27 +1011,21 @@ void interactions(player& player1,b_list& bullets,b_list& bombs,e_list& enemies,
 			else
 				++it3;
 		}
-		m_enemies.unlock();
 		if(should_continue) continue;
 		
-		m_boss.lock();
 		if((itr->x>=boss1.x+boss1.width/2-2 && itr->x<=boss1.x+boss1.width/2+2) && boss1.alive) 	//rocket+boss
 		{
 			boss1.health--;
 			if(itr->y==boss1.y) 
 			{
 				itr=rockets.erase(itr);
-				m_boss.unlock();
    			continue;
    		}
    	}
-	   m_boss.unlock();
 	   
 	   ++itr;
 	}
-	m_rockets.unlock();
     	
-	m_bombs.lock();
    b_list::iterator itb=bombs.begin();
    while(itb!=bombs.end()){
 		if(itb->y>=R)					//bomb reached the floor
@@ -1083,10 +1042,7 @@ void interactions(player& player1,b_list& bullets,b_list& bombs,e_list& enemies,
 		
 		++itb;
 	}
-	m_bombs.unlock();
         
-	m_pow.lock();
-	m_player.lock();
 	b_list::iterator itp=powerups.begin();
 	while( itp!=powerups.end()){
 	  	if(itp->y==player1.y && itp->x>=player1.x && itp->x<player1.x+player1.length){
@@ -1117,17 +1073,13 @@ void interactions(player& player1,b_list& bullets,b_list& bombs,e_list& enemies,
 	 
  		++itp;
 	}
-	m_pow.unlock();
-	m_player.unlock();
         	
-	m_walls.lock();
 	for(int i=0;i<walls.size();i++)
 	  	for(int j=0;j<walls[i].width;j++)
 	  		for(int k=0;k<walls[i].height;k++)
 	  		{
 	  			if(walls[i].health[k][j]>0)
 	  			{
-	  				m_bullets.lock();
 	  				b_list::iterator it1=bullets.begin();
 					while(it1!=bullets.end())
 					{
@@ -1138,9 +1090,7 @@ void interactions(player& player1,b_list& bullets,b_list& bombs,e_list& enemies,
 						else
 							++it1;
 					}
-					m_bullets.unlock();
 					
-					m_bombs.lock();
 					b_list::iterator it2=bombs.begin();
 					while(it2!=bombs.end())
 					{
@@ -1151,9 +1101,7 @@ void interactions(player& player1,b_list& bullets,b_list& bombs,e_list& enemies,
 						else
 							++it2;
 					}
-					m_bombs.unlock();
 					
-					m_rockets.lock();
 					r_list::iterator it3=rockets.begin();
 					while(it3!=rockets.end())
 					{
@@ -1164,9 +1112,7 @@ void interactions(player& player1,b_list& bullets,b_list& bombs,e_list& enemies,
 						else
 							++it3;
 					}
-					m_rockets.unlock();
 					
-					m_pow.lock();
 					b_list::iterator it4=powerups.begin();
 					while(it4!=powerups.end())
 					{
@@ -1176,10 +1122,8 @@ void interactions(player& player1,b_list& bullets,b_list& bombs,e_list& enemies,
 						else
 							++it4;
 					}
-					m_pow.unlock();
 			}
 		}
-		m_walls.unlock();
 }
 
 

+ 1 - 1
functions.hpp

@@ -97,7 +97,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 draw(player& player1,b_list& bullets,b_list& bombs,e_list& enemies,w_vec& walls,b_list& powerups,r_list& rockets,boss& boss1, int& refresh_time, bool& keep_drawing,bool& kill_yourself);
+void draw(player& player1,b_list& bullets,b_list& bombs,e_list& enemies,w_vec& walls,b_list& powerups,r_list& rockets,boss& boss1);
 
 void create_readme();
 

+ 2 - 23
invaders.cpp

@@ -6,16 +6,6 @@
 #include "definitions.hpp"
 #include "functions.hpp"
 #include "wall.hpp"
-#include <thread>
-
-std::mutex m_player;
-std::mutex m_enemies;
-std::mutex m_bombs;
-std::mutex m_bullets;
-std::mutex m_rockets;
-std::mutex m_pow;
-std::mutex m_boss;
-std::mutex m_walls;
 
 using std::cout;
 using std::endl; 
@@ -86,6 +76,7 @@ int main(int argc,char** argv)
 	initscr();
 	curs_set(0);
 	noecho();	
+	nodelay(NULL, true);
 	cbreak();
 	keypad(stdscr,TRUE);
 	start_color();
@@ -121,10 +112,6 @@ int main(int argc,char** argv)
 	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);
 
-	bool kill_yourself = false;
-	bool keep_drawing = true;
-	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),std::ref(refresh_time),std::ref(keep_drawing),std::ref(kill_yourself));
-	
 	/////////ENTERING MAIN LOOP
 	
 	/*if(sound)
@@ -145,8 +132,6 @@ int main(int argc,char** argv)
 	    		command = tolower(command);     
 		    		           
 	    	if(command == commands[8]){ 						//q = exit game
-	    		kill_yourself = true;
-	    		t_draw.join();
 	    		endwin();
 	    		//pkill_music(thread[0]);
 	    		cout<<lightgreen<<"Game exited correctly."<<none<<endl;
@@ -281,8 +266,6 @@ int main(int argc,char** argv)
 				continue;  
 			}
 			else { 
-				kill_yourself = true;
-				t_draw.join();
 				endwin();
 				cout<<lightgreen<<"Game exited correctly."<<none<<endl;
 				return 0;
@@ -292,8 +275,6 @@ int main(int argc,char** argv)
 			
 		if(gameover(player1,bombs))
 		{
-			keep_drawing = false;
-				
 			Defeat(score);
 			
 			WINDOW* replay;
@@ -310,20 +291,18 @@ int main(int argc,char** argv)
 				refresh();
 				choose_level(shootrate, refresh_time, boss1, level, commands);
 				reset(player1, enemies, boss1, bullets, bombs, walls, powerups,rockets,level,chflag);       // reset box, player, enemy and delete all bullets and bombs
-				keep_drawing = true;
 				erase();
 				refresh();
 				continue;  
 			}
 			else {
-				kill_yourself = true;
-				t_draw.join();
 				endwin();
 				cout<<lightgreen<<"Game exited correctly."<<none<<endl;
 				return 0;
 			}
 		}
 		
+		draw(player1,bullets,bombs,enemies,walls,powerups,rockets,boss1);
 		write_score(Score,score);
 	
 		if(boss1.alive){

+ 0 - 8
myboss.dat.old

@@ -1,8 +0,0 @@
-ertyuertyu
-ertyuertyu
-ertyuertyu
-ertyuertyu
-ertyuertyu
-ertyuertyu
-ertyuertyu
-ertyuertyu

+ 0 - 0
palombo


+ 0 - 5
palombo.dat.old

@@ -1,5 +0,0 @@
-  _______  
- W _ _ _ W 
-|_|o| |o|_|
-c    c    P
- \__NNN__/ 

+ 0 - 6
spoletini.dat.old

@@ -1,6 +0,0 @@
-  _____  
-//     \\
-|  0 0  |
-c   L   P
-\  MMM  /
- \_____/