bossrush.cpp 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. #include "definitions.hpp"
  2. #include "rocket.hpp"
  3. #include "enemy.hpp"
  4. #include "player.hpp"
  5. #include "boss.hpp"
  6. #include "powerup.hpp"
  7. #include "functions.hpp"
  8. #include "wall.hpp"
  9. #include <mutex>
  10. #include <thread>
  11. std::mutex m_player;
  12. std::mutex m_enemies;
  13. std::mutex m_bombs;
  14. std::mutex m_bullets;
  15. std::mutex m_rockets;
  16. std::mutex m_pow;
  17. std::mutex m_boss;
  18. std::mutex m_walls;
  19. using std::cout;
  20. using std::endl;
  21. using std::min;
  22. using std::max;
  23. int ENEMY_NUM=1; // This variable isn't used, but we have to define it since it is decleared in definitions.hpp"
  24. int main(int argc,char** argv)
  25. {
  26. if(argc>1){
  27. if(strcmp(argv[1],"--uber-debug")!=0 && strcmp(argv[1],"--info")!=0){
  28. cout<<"Usage: "<<argv[0]<<" [--info]"<<endl;
  29. return 1;
  30. }
  31. if(strcmp(argv[1],"--info")==0){
  32. cout<<endl<<"Curses Invaders 4.4 - Bossrush"<<endl;
  33. cout<<endl<<"Game developed by Giacomo Parolini (jp) and Enrico Guiraud (blue) in years 2010-2013."<<endl;
  34. cout<<"Source code is available under request to jp@lcm.mi.infn.it (it's quite ugly, I warn you ;-) )"<<endl;
  35. cout<<"Report any bug to the same mail address."<<endl<<endl;
  36. return 1;
  37. }
  38. }
  39. typedef std::list<bullet> b_list;
  40. typedef std::vector<wall> w_vec;
  41. typedef std::list<rocket> r_list;
  42. srand(time(NULL));
  43. //OBJECTS INIZIALIZATION
  44. player player1; //player is automatically created in [C/2][R]
  45. boss bosses[5]; //vector of bosses
  46. b_list bullets; //list of player's bullets
  47. b_list bombs; //list of enemies' bombs
  48. b_list powerups; //list of powerups
  49. r_list rockets; //list of rockets
  50. boss boss1;
  51. std::list<enemy> enemies;
  52. int refresh_time=100; //how long the program waits before refreshing the screen
  53. int chflag=0; //cheat flag: 0=normal, 1=cheats allowed.
  54. int num=0;
  55. int commands[CMD_NUM]={'a','d','w','s','p',' ','l','m','q'};
  56. WINDOW *Score,*BossHP;
  57. //PARAMETERS/UTILITIES
  58. double shootrate; //probability of an enemy shooting a bomb
  59. int command; //keyboard input
  60. int score=0; //score: gain +100 when an enemy is destroyed and +50 when a bomb is destroyed
  61. char level='1'; //difficulty level
  62. create_std_bosses();
  63. std::string bossname1=getenv("HOME");
  64. bossname1=bossname1+RECORD_DIR+BOSS_FILE1+".dat";
  65. boss newboss1(1,1,100,9,6,BOSS_FILE1);
  66. newboss1.loadpicture(bossname1.c_str());
  67. std::string bossname2=getenv("HOME");
  68. bossname2=bossname2+RECORD_DIR+BOSS_FILE2+".dat";
  69. boss newboss2(1,1,200,11,5,BOSS_FILE2);
  70. newboss2.loadpicture(bossname2.c_str());
  71. std::string bossname3=getenv("HOME");
  72. bossname3=bossname3+RECORD_DIR+BOSS_FILE3+".dat";
  73. boss newboss3(1,1,300,9,6,BOSS_FILE3);
  74. newboss3.loadpicture(bossname3.c_str());
  75. std::string bossname4=getenv("HOME");
  76. bossname4=bossname4+RECORD_DIR+BOSS_FILE4+".dat";
  77. boss newboss4(1,1,400,11,6,BOSS_FILE4);
  78. newboss4.loadpicture(bossname4.c_str());
  79. std::string bossname5=getenv("HOME");
  80. bossname5=bossname5+RECORD_DIR+BOSS_FILE5+".dat";
  81. boss newboss5(1,1,500,17,5,BOSS_FILE5);
  82. newboss5.loadpicture(bossname5.c_str());
  83. bosses[0]=newboss1;
  84. bosses[1]=newboss2;
  85. bosses[2]=newboss3;
  86. bosses[3]=newboss4;
  87. bosses[4]=newboss5;
  88. boss1=bosses[0];
  89. boss1.alive=true;
  90. player1.weaponclass=1;
  91. //NCURSES STUFF
  92. initscr();
  93. curs_set(0);
  94. noecho();
  95. cbreak();
  96. keypad(stdscr,TRUE);
  97. start_color();
  98. init_pair(0,COLOR_WHITE,COLOR_BLACK);
  99. init_pair(1,COLOR_GREEN,COLOR_BLACK); //PLAYER
  100. init_pair(2,COLOR_RED,COLOR_BLACK); //ENEMY
  101. init_pair(3,COLOR_YELLOW,COLOR_BLACK); //BULLETS-POWERUPS
  102. init_pair(4,COLOR_MAGENTA,COLOR_BLACK); //WALLS
  103. init_pair(5,COLOR_CYAN,COLOR_BLACK); //BOSS
  104. init_pair(6,COLOR_BLUE,COLOR_BLACK); //BOMBS
  105. init_pair(7,COLOR_RED,COLOR_RED);
  106. init_pair(8,COLOR_GREEN,COLOR_GREEN);
  107. init_pair(9,COLOR_BLACK,COLOR_GREEN);
  108. WALLS_NUM=3;
  109. w_vec walls(WALLS_NUM);
  110. Score=newwin(3,10,R/3,C+3);
  111. BossHP=newwin(3,15,R/3-3,C+3);
  112. int i=0;
  113. for(w_vec::iterator it=walls.begin(); it!=walls.end(); ++it, ++i) //creating walls (in a quite symmetric pattern)
  114. 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);
  115. choose_level_bossrush(shootrate,refresh_time,level,commands);
  116. player1.set_commands(commands);
  117. if(atoi(&level)<3)
  118. player1.weaponclass=2;
  119. erase();
  120. if(argc>1)
  121. if(strcmp(argv[1],"--uber-debug")==0){ player1.weaponclass=5; player1.weaponclassrkt=5; player1.rocketlauncher=true; player1.length=MAX_LENGTH; }
  122. 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));
  123. /////////ENTERING MAIN LOOP
  124. while(1)
  125. {
  126. napms(refresh_time); //ncurses sleep function (ms)
  127. timeout(0);
  128. command = getch();
  129. if(command!=ERR) //READING INPUT
  130. command = tolower(command);
  131. if(command == commands[8]){ //q = exit game
  132. endwin();
  133. cout<<lightgreen<<"Game exited correctly."<<none<<endl;
  134. return 1;
  135. }
  136. if(command == commands[4])
  137. pause_game(chflag); //p = pause game
  138. if(chflag==1) //cheats
  139. get_cheat(command,player1,enemies,shootrate);
  140. if(command == commands[0] || command == KEY_LEFT || command == commands[1] || command == KEY_RIGHT || ((command == commands[2]|| command == KEY_UP || command == commands[3] || command == KEY_DOWN) && chflag==1)){ //a or d = move player (if cheats are triggered, also w and s)
  141. for(int i=player1.x;i<player1.x+player1.length;i++) mvaddch(player1.y,i,' ');
  142. player1.next_pos(command);
  143. }
  144. if(command==commands[5] || command==commands[6]) { //spacebar = shoot!
  145. player1.shoot(bullets);
  146. if(player1.rocketlauncher) player1.shootrkt(rockets);
  147. }
  148. for(int i=0;i<boss1.width;i++) mvaddch(boss1.y,boss1.x+i,' ');
  149. for(int j=0;j<boss1.height;j++){
  150. mvaddch(boss1.y+j,boss1.x,' ');
  151. mvaddch(boss1.y+j,boss1.x+boss1.width-1,' ');
  152. }
  153. boss1.next_pos();
  154. if((double)rand()/RAND_MAX<shootrate*10.)
  155. boss1.shoot(bombs);
  156. if(!bullets.empty()) //evaluate new bullets' positions
  157. for(b_list::iterator it=bullets.begin(); it!=bullets.end(); ++it){
  158. mvaddch(it->y,it->x,' ');
  159. it->next_pos();
  160. }
  161. bullets.unique(); //remove duplicates of bullets (i.e. player can't shoot enemies)
  162. if(!rockets.empty()) //evaluate new bullets' positions
  163. for(r_list::iterator it=rockets.begin(); it!=rockets.end(); ++it){
  164. mvaddch(it->y,it->x,' ');
  165. it->next_pos();
  166. }
  167. rockets.unique();
  168. if(!bombs.empty()) //evaluate new bombs' positions
  169. for(b_list::iterator it=bombs.begin(); it!=bombs.end(); ++it){
  170. mvaddch(it->y,it->x,' ');
  171. it->next_pos();
  172. }
  173. bombs.unique(); //remove duplicates of bombs
  174. if(!powerups.empty()) //new powerups' positions
  175. for(b_list::iterator it=powerups.begin(); it!=powerups.end(); ++it){
  176. mvaddch(it->y,it->x,' ');
  177. it->next_pos();
  178. }
  179. powerups.unique(); //FIXME: THIS ONLY WORKS ON CONSECUTIVE ELEMENTS OF THE LIST!! we should at least sort powerups before calling unique()
  180. interactions(player1,bullets,bombs,enemies,walls,powerups,rockets,boss1,score);
  181. if(boss1.health<=0)
  182. if(num<4){
  183. score+=250*atoi(&level)*(num+1);
  184. for(int i=max(0,boss1.y-1);i<min(R-1,boss1.y+boss1.height+1);i++)
  185. for(int j=0;j<C;j++)
  186. mvaddch(i,j,' ');
  187. num++;
  188. mvwprintw(BossHP,0,1," ");
  189. if((double)rand()/RAND_MAX<0.25*num){
  190. if((double)rand()/RAND_MAX<1./POWERUP_RATIO){
  191. if((double)rand()/RAND_MAX<1./ROCKET_RATIO){
  192. powerup newpowerup(boss1.x,boss1.y+1,512);
  193. powerups.push_back(newpowerup);
  194. }
  195. else{
  196. powerup newpowerup(boss1.x,boss1.y+1,128);
  197. powerups.push_back(newpowerup);
  198. }
  199. }
  200. else{
  201. powerup newpowerup(boss1.x,boss1.y+1);
  202. powerups.push_back(newpowerup);
  203. }
  204. }
  205. if(num==4 && player1.weaponclass<3) player1.weaponclass++;
  206. boss1=bosses[num];
  207. boss1.alive=true;
  208. }
  209. ///////ENDGAME CHECKS
  210. if(boss1.health<1 && num==4) //YOU WON!!
  211. {
  212. Victory_bossrush(boss1.name,score,level,chflag);
  213. WINDOW *replay;
  214. replay=newwin(3,25,26,20);
  215. box(replay,ACS_VLINE,ACS_HLINE);
  216. if(playagain(replay)) //playagain() returns true if player wants to play again, false otherwise
  217. {
  218. delwin(replay);
  219. timeout(500);
  220. score=0;
  221. erase();
  222. refresh();
  223. choose_level_bossrush(shootrate, refresh_time,level,commands);
  224. reset(player1, enemies, boss1, bullets, bombs, walls, powerups,rockets, level,chflag); // reset box, player, enemy and deletes all bullets and bombs
  225. resetbosses(bosses,boss1,player1);
  226. num=0;
  227. if(atoi(&level)<3)
  228. player1.weaponclass=2;
  229. erase();
  230. refresh();
  231. continue;
  232. }
  233. else {
  234. endwin();
  235. cout<<lightgreen<<"Game exited correctly."<<none<<endl;
  236. return 0;
  237. }
  238. }
  239. if(gameover(player1,bombs))
  240. {
  241. Defeat(score);
  242. WINDOW* replay;
  243. replay=newwin(3,25,17,20);
  244. box(replay,ACS_VLINE,ACS_HLINE);
  245. if(playagain(replay)) //playagain() returns true if player wants to play again, false otherwise
  246. {
  247. delwin(replay);
  248. timeout(500);
  249. score=0;
  250. erase();
  251. refresh();
  252. choose_level_bossrush(shootrate, refresh_time, level,commands);
  253. reset(player1, enemies, boss1, bullets, bombs, walls, powerups,rockets, level,chflag); // reset box, player, enemy and delete all bullets and bombs
  254. resetbosses(bosses,boss1,player1);
  255. num=0;
  256. if(atoi(&level)<3)
  257. player1.weaponclass=2;
  258. erase();
  259. refresh();
  260. continue;
  261. }
  262. else {
  263. endwin();
  264. cout<<lightgreen<<"Game exited correctly."<<none<<endl;
  265. return 0;
  266. }
  267. }
  268. write_score(Score,score);
  269. if(boss1.alive){
  270. write_bosshp(BossHP,boss1.health,boss1.healthmax,boss1.name);
  271. wrefresh(BossHP);
  272. }
  273. wrefresh(Score);
  274. refresh();
  275. }
  276. ////////END OF MAIN LOOP
  277. endwin();
  278. return 0;
  279. }