bossrush.cpp 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  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. double poweruprate; //probability of a powerup being dropped
  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. WALLS_NUM=3;
  116. w_vec walls(WALLS_NUM);
  117. Score=newwin(3,10,R/3,C+3);
  118. BossHP=newwin(3,15,R/3-3,C+3);
  119. int i=0;
  120. for(w_vec::iterator it=walls.begin(); it!=walls.end(); ++it, ++i) //creating walls (in a quite symmetric pattern)
  121. 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);
  122. level = choose_level_bossrush(commands);
  123. setup_level_bossrush(level, shootrate, poweruprate, refresh_time);
  124. player1.set_commands(commands);
  125. if(level<3)
  126. player1.weaponclass=2;
  127. erase();
  128. if(argc>1)
  129. if(strcmp(argv[1],"--uber-debug")==0){ player1.weaponclass=5; player1.weaponclassrkt=5; player1.rocketlauncher=true; player1.length=MAX_LENGTH; }
  130. 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));
  131. /////////ENTERING MAIN LOOP
  132. while(1)
  133. {
  134. napms(refresh_time); //ncurses sleep function (ms)
  135. timeout(0);
  136. command = getch();
  137. if(command!=ERR) //READING INPUT
  138. command = tolower(command);
  139. if(command == commands[8]){ //q = exit game
  140. endwin();
  141. cout<<lightgreen<<"Game exited correctly."<<none<<endl;
  142. return 1;
  143. }
  144. if(command == commands[4])
  145. pause_game(chflag); //p = pause game
  146. if(chflag==1) //cheats
  147. get_cheat(command,player1,enemies,shootrate);
  148. 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)
  149. for(int i=player1.x;i<player1.x+player1.length;i++) mvaddch(player1.y,i,' ');
  150. player1.next_pos(command);
  151. }
  152. if(command==commands[5] || command==commands[6]) { //spacebar = shoot!
  153. player1.shoot(bullets);
  154. if(player1.rocketlauncher) player1.shootrkt(rockets);
  155. }
  156. for(int i=0;i<boss1.width;i++) mvaddch(boss1.y,boss1.x+i,' ');
  157. for(int j=0;j<boss1.height;j++){
  158. mvaddch(boss1.y+j,boss1.x,' ');
  159. mvaddch(boss1.y+j,boss1.x+boss1.width-1,' ');
  160. }
  161. boss1.next_pos();
  162. if((double)rand()/RAND_MAX<shootrate*10.)
  163. boss1.shoot(bombs);
  164. if(!bullets.empty()) //evaluate new bullets' positions
  165. for(b_list::iterator it=bullets.begin(); it!=bullets.end(); ++it){
  166. mvaddch(it->y,it->x,' ');
  167. it->next_pos();
  168. }
  169. bullets.unique(); //remove duplicates of bullets (i.e. player can't shoot enemies)
  170. if(!rockets.empty()) //evaluate new bullets' positions
  171. for(r_list::iterator it=rockets.begin(); it!=rockets.end(); ++it){
  172. mvaddch(it->y,it->x,' ');
  173. it->next_pos();
  174. }
  175. rockets.unique();
  176. if(!bombs.empty()) //evaluate new bombs' positions
  177. for(b_list::iterator it=bombs.begin(); it!=bombs.end(); ++it){
  178. mvaddch(it->y,it->x,' ');
  179. it->next_pos();
  180. }
  181. bombs.unique(); //remove duplicates of bombs
  182. if(!powerups.empty()) //new powerups' positions
  183. for(b_list::iterator it=powerups.begin(); it!=powerups.end(); ++it){
  184. mvaddch(it->y,it->x,' ');
  185. it->next_pos();
  186. }
  187. powerups.unique(); //FIXME: THIS ONLY WORKS ON CONSECUTIVE ELEMENTS OF THE LIST!! we should at least sort powerups before calling unique()
  188. interactions(player1,bullets,bombs,enemies,walls,powerups,rockets,boss1,score,poweruprate);
  189. if(boss1.health<=0)
  190. if(num<4){
  191. score+=250*level*(num+1);
  192. for(int i=max(0,boss1.y-1);i<min(R-1,boss1.y+boss1.height+1);i++)
  193. for(int j=0;j<C;j++)
  194. mvaddch(i,j,' ');
  195. num++;
  196. mvwprintw(BossHP,0,1," ");
  197. if((double)rand()/RAND_MAX<0.25*num){
  198. if((double)rand()/RAND_MAX<1./POWERUP_RATIO){
  199. if((double)rand()/RAND_MAX<1./ROCKET_RATIO){
  200. powerup newpowerup(boss1.x,boss1.y+1,512);
  201. powerups.push_back(newpowerup);
  202. }
  203. else{
  204. powerup newpowerup(boss1.x,boss1.y+1,128);
  205. powerups.push_back(newpowerup);
  206. }
  207. }
  208. else{
  209. powerup newpowerup(boss1.x,boss1.y+1);
  210. powerups.push_back(newpowerup);
  211. }
  212. }
  213. if(num==4 && player1.weaponclass<3) player1.weaponclass++;
  214. boss1=bosses[num];
  215. boss1.alive=true;
  216. }
  217. ///////ENDGAME CHECKS
  218. if((boss1.health<1 && num==4) || gameover(player1,bombs))
  219. {
  220. WINDOW *replay;
  221. if (boss1.health<1 && num==4)
  222. {
  223. Victory_bossrush(boss1.name,score,level,chflag);
  224. replay=newwin(3,25,26,20);
  225. } else
  226. {
  227. Defeat(score);
  228. replay=newwin(3,25,17,20);
  229. }
  230. box(replay,ACS_VLINE,ACS_HLINE);
  231. switch(char choice = playagain(replay)) // playagain() returns 'y'/'n'/'q'
  232. {
  233. case 'y':
  234. case 'n':
  235. delwin(replay);
  236. timeout(500);
  237. score=0;
  238. erase();
  239. refresh();
  240. if(choice=='n')
  241. level = choose_level_bossrush(commands);
  242. setup_level_bossrush(level, shootrate, poweruprate, refresh_time);
  243. reset(player1, enemies, boss1, bullets, bombs, walls, powerups,rockets, level,chflag); // reset box, player, enemy and deletes all bullets and bombs
  244. resetbosses(bosses,boss1,player1);
  245. num=0;
  246. if(level<3)
  247. player1.weaponclass=2;
  248. erase();
  249. refresh();
  250. continue; // Go to the beginning of the main loop
  251. case 'q':
  252. endwin();
  253. cout<<lightgreen<<"Game exited correctly."<<none<<endl;
  254. return 0;
  255. }
  256. }
  257. write_score(Score,score);
  258. if(boss1.alive){
  259. write_bosshp(BossHP,boss1.health,boss1.healthmax,boss1.name);
  260. wrefresh(BossHP);
  261. }
  262. wrefresh(Score);
  263. refresh();
  264. }
  265. ////////END OF MAIN LOOP
  266. endwin();
  267. return 0;
  268. }