bossrush.cpp 11 KB

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