bossrush.cpp 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  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 main(int argc,char** argv)
  30. {
  31. if(argc>1){
  32. if(strcmp(argv[1],"--uber-debug")!=0 && strcmp(argv[1],"--info")!=0){
  33. cout<<"Usage: "<<argv[0]<<" [--info]"<<endl;
  34. return 1;
  35. }
  36. if(strcmp(argv[1],"--info")==0){
  37. cout<<endl<<"Curses Invaders 4.4 - Bossrush"<<endl;
  38. cout<<endl<<"Game developed by Giacomo Parolini (jp) and Enrico Guiraud (blue) in years 2010-2013."<<endl;
  39. cout<<"Source code is available under request to jp@lcm.mi.infn.it (it's quite ugly, I warn you ;-) )"<<endl;
  40. cout<<"Report any bug to the same mail address."<<endl<<endl;
  41. return 1;
  42. }
  43. }
  44. typedef std::list<bullet> b_list;
  45. typedef std::vector<wall> w_vec;
  46. typedef std::list<rocket> r_list;
  47. srand(time(NULL));
  48. //OBJECTS INIZIALIZATION
  49. player player1; //player is automatically created in [C/2][R]
  50. boss bosses[5]; //vector of bosses
  51. b_list bullets; //list of player's bullets
  52. b_list bombs; //list of enemies' bombs
  53. b_list powerups; //list of powerups
  54. r_list rockets; //list of rockets
  55. boss boss1;
  56. std::list<enemy> enemies;
  57. int refresh_time=100; //how long the program waits before refreshing the screen
  58. int chflag=0; //cheat flag: 0=normal, 1=cheats allowed.
  59. int num=0;
  60. int commands[CMD_NUM]={'a','d','w','s','p',' ','l','m','q'};
  61. WINDOW *Score,*BossHP;
  62. //PARAMETERS/UTILITIES
  63. double shootrate; //probability of an enemy shooting a bomb
  64. double poweruprate; //probability of a powerup being dropped
  65. int walls_num; //number of walls
  66. int command; //keyboard input
  67. int score=0; //score: gain +100 when an enemy is destroyed and +50 when a bomb is destroyed
  68. int level=1; //difficulty level
  69. create_std_bosses();
  70. std::string bossname1=getenv("HOME");
  71. bossname1=bossname1+RECORD_DIR+BOSS_FILE1+".dat";
  72. boss newboss1(1,1,100,9,6,BOSS_FILE1);
  73. newboss1.loadpicture(bossname1.c_str());
  74. std::string bossname2=getenv("HOME");
  75. bossname2=bossname2+RECORD_DIR+BOSS_FILE2+".dat";
  76. boss newboss2(1,1,200,11,5,BOSS_FILE2);
  77. newboss2.loadpicture(bossname2.c_str());
  78. std::string bossname3=getenv("HOME");
  79. bossname3=bossname3+RECORD_DIR+BOSS_FILE3+".dat";
  80. boss newboss3(1,1,300,9,6,BOSS_FILE3);
  81. newboss3.loadpicture(bossname3.c_str());
  82. std::string bossname4=getenv("HOME");
  83. bossname4=bossname4+RECORD_DIR+BOSS_FILE4+".dat";
  84. boss newboss4(1,1,400,11,6,BOSS_FILE4);
  85. newboss4.loadpicture(bossname4.c_str());
  86. std::string bossname5=getenv("HOME");
  87. bossname5=bossname5+RECORD_DIR+BOSS_FILE5+".dat";
  88. boss newboss5(1,1,500,17,5,BOSS_FILE5);
  89. newboss5.loadpicture(bossname5.c_str());
  90. bosses[0]=newboss1;
  91. bosses[1]=newboss2;
  92. bosses[2]=newboss3;
  93. bosses[3]=newboss4;
  94. bosses[4]=newboss5;
  95. boss1=bosses[0];
  96. boss1.alive=true;
  97. player1.weaponclass=1;
  98. //NCURSES STUFF
  99. initscr();
  100. curs_set(0);
  101. noecho();
  102. cbreak();
  103. keypad(stdscr,TRUE);
  104. start_color();
  105. init_pair(0,COLOR_WHITE,COLOR_BLACK);
  106. init_pair(1,COLOR_GREEN,COLOR_BLACK); //PLAYER
  107. init_pair(2,COLOR_RED,COLOR_BLACK); //ENEMY
  108. init_pair(3,COLOR_YELLOW,COLOR_BLACK); //BULLETS-POWERUPS
  109. init_pair(4,COLOR_MAGENTA,COLOR_BLACK); //WALLS
  110. init_pair(5,COLOR_CYAN,COLOR_BLACK); //BOSS
  111. init_pair(6,COLOR_BLUE,COLOR_BLACK); //BOMBS
  112. init_pair(7,COLOR_RED,COLOR_RED);
  113. init_pair(8,COLOR_GREEN,COLOR_GREEN);
  114. init_pair(9,COLOR_BLACK,COLOR_GREEN);
  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, poweruprate, walls_num, 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,poweruprate);
  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, poweruprate, walls_num, refresh_time);
  242. reset(player1, enemies, boss1, bullets, bombs, walls, walls_num, powerups, rockets, 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. }