bossrush.cpp 9.2 KB

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