bossrush.cpp 9.5 KB

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