bossrush.cpp 9.8 KB

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