invaders.cpp 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  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 "bullet.hpp"
  8. #include "enemy.hpp"
  9. #include "player.hpp"
  10. #include "boss.hpp"
  11. #include "powerup.hpp"
  12. #include "definitions.hpp"
  13. #include "functions.hpp"
  14. #include "wall.hpp"
  15. using std::cout;
  16. using std::endl;
  17. using std::min;
  18. int main(int argc,char** argv)
  19. {
  20. bool sound = false;
  21. if(argc>1){
  22. if(strcmp(argv[1],"--uber-debug")!=0 && strcmp(argv[1],"--info")!=0 && strcmp(argv[1],"--autowin")!=0 &&
  23. strcmp(argv[1], "--mute")!=0 && strcmp(argv[1], "-m")!=0 && strcmp(argv[1], "--sound")!=0 && strcmp(argv[1], "-s")!=0){
  24. cout<<"Usage: "<<argv[0]<<" [--info|--mute|--sound]"<<endl;
  25. return 1;
  26. }
  27. if(strcmp(argv[1],"--info")==0){
  28. cout<<endl<<"Curses Invaders " VERSION <<endl;
  29. cout<<endl<<"Game developed by Giacomo Parolini (jp) and Enrico Guiraud (blue) in years 2010-2013."<<endl;
  30. cout<<"Source code is available under request to jp@lcm.mi.infn.it (it's quite ugly, I warn you ;-) )"<<endl;
  31. cout<<"Report any bug to the same mail address."<<endl<<endl;
  32. return 1;
  33. }
  34. if(strcmp(argv[1], "--mute")==0 || strcmp(argv[1], "-m")==0) {
  35. sound = false;
  36. } else if(strcmp(argv[1], "--sound")==0 || strcmp(argv[1], "-s")==0) {
  37. sound = true;
  38. }
  39. }
  40. typedef std::list<bullet> b_list;
  41. typedef std::list<enemy> e_list;
  42. typedef std::vector<wall> w_vec;
  43. typedef std::list<rocket> r_list;
  44. srand(time(NULL));
  45. //OBJECTS INIZIALIZATION
  46. player player1; //player is automatically created in [C/2][R]
  47. e_list enemies; //vector of enemies
  48. b_list bullets; //list of player's bullets
  49. b_list bombs; //list of enemies' bombs
  50. b_list powerups; //list of powerups
  51. r_list rockets; //list of rockets
  52. int refresh_time; //how long the program waits before refreshing the screen
  53. int chflag=0; //cheat flag: 0=normal, 1=cheats allowed, 2=special mode activated, 3=both special mode and cheats.
  54. boss boss1;
  55. WINDOW *Score,*BossHP;
  56. int commands[CMD_NUM]={'a','d','w','s','p',' ','l','m','q'}; //0:left,1:right,2:up,3:down,4:pause,5:shoot1,6:shoot2,7:mute,8:quit
  57. //PARAMETERS/UTILITIES
  58. double shootrate; //probability of an enemy shooting a bomb
  59. double poweruprate; //probability of a powerup being dropped
  60. int enemy_num; //number of enemies in the current match
  61. int walls_num; //number of walls
  62. int command; //keyboard input
  63. int score=0; //score: gain +100 when an enemy is destroyed and +50 when a bomb is destroyed
  64. int level=1; //difficulty level
  65. //PTHREAD STUFF
  66. pthread_t thread[THREADS_NUM];
  67. //NCURSES STUFF
  68. initscr();
  69. curs_set(0);
  70. noecho();
  71. nodelay(NULL, true);
  72. cbreak();
  73. keypad(stdscr,TRUE);
  74. start_color();
  75. init_pair(0,COLOR_WHITE,COLOR_BLACK);
  76. init_pair(1,COLOR_GREEN,COLOR_BLACK); //PLAYER
  77. init_pair(2,COLOR_RED,COLOR_BLACK); //ENEMY
  78. init_pair(3,COLOR_YELLOW,COLOR_BLACK); //BULLETS-POWERUPS
  79. init_pair(4,COLOR_MAGENTA,COLOR_BLACK); //WALLS
  80. init_pair(5,COLOR_CYAN,COLOR_BLACK); //BOSS
  81. init_pair(6,COLOR_BLUE,COLOR_BLACK); //BOMBS
  82. init_pair(7,COLOR_RED,COLOR_RED);
  83. init_pair(8,COLOR_GREEN,COLOR_GREEN);
  84. init_pair(9,COLOR_BLACK,COLOR_GREEN);
  85. //////////PROGRAM START: creation of game objects and set of parameters
  86. level = choose_level(commands); // get desired level
  87. setup_level(level, shootrate, poweruprate, refresh_time, boss1, enemy_num, walls_num); // set game parameters and boss
  88. load_enemies(enemies,enemy_num);
  89. player1.set_commands(commands);
  90. erase();
  91. w_vec walls(walls_num);
  92. Score=newwin(3,10,R/3,C+3);
  93. BossHP=newwin(3,15,R/3-3,C+3);
  94. int i=0;
  95. for(w_vec::iterator it=walls.begin(); it!=walls.end(); ++it, ++i) //creating walls (in a quite symmetric pattern)
  96. 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*(int)(walls_num)+1)),2,3);
  97. /////////ENTERING MAIN LOOP
  98. if(sound)
  99. pthread_create(&thread[0],NULL,pmusic,NULL); //executing background music in parallel thread
  100. if(argc>1){
  101. if(strcmp(argv[1],"--uber-debug")==0){ player1.weaponclass=5; player1.weaponclassrkt=5; player1.rocketlauncher=true; player1.length=MAX_LENGTH; }
  102. else if(strcmp(argv[1],"--autowin")==0){ Victory(boss1.name,score,level,0); endwin(); return 1; }
  103. }
  104. while(1)
  105. {
  106. napms(refresh_time); //ncurses sleep function (ms)
  107. timeout(0);
  108. command = getch();
  109. if(command!=ERR) //READING INPUT
  110. command = tolower(command);
  111. if(command == commands[8]){ //q = exit game
  112. endwin();
  113. pkill_music(thread[0]);
  114. cout<<lightgreen<<"Game exited correctly."<<none<<endl;
  115. return 1;
  116. }
  117. if(command == commands[4])
  118. pause_game(chflag); //p = pause game
  119. //=====CHEATS================================================
  120. if(chflag==1 || chflag==3)
  121. get_cheat(command,player1,enemies,shootrate);
  122. if(chflag!=2 && chflag!=3)
  123. get_SpecialMode(command,chflag,boss1,enemies,bullets,bombs,rockets);
  124. //======END CHEATS===============================================
  125. if(command == commands[0] || command == KEY_LEFT || command == commands[1] || command == KEY_RIGHT ||
  126. ((chflag==1 || chflag==3) && (command == commands[2]|| command == KEY_UP || command == commands[3] || command == KEY_DOWN))){ //a or d = move player (if cheats are triggered, also w and s)
  127. //for(int i=player1.x;i<player1.x+player1.length;i++) //mvaddch(player1.y,i,' ');
  128. player1.next_pos(command);
  129. }
  130. if(command==commands[5] || command==commands[6]){ //spacebar = shoot!
  131. if(sound) pthread_create(&thread[4],NULL,pshoot_sound,NULL);
  132. player1.shoot(bullets);
  133. if(player1.rocketlauncher) player1.shootrkt(rockets);
  134. }
  135. if(command==commands[7]){ //mute/unmute
  136. if(sound) pkill_music(thread[0]);
  137. else pthread_create(&thread[0],NULL,pmusic,NULL); // background music
  138. sound=!sound;
  139. }
  140. if(enemyalive(enemies)) //if there's at least one enemy alive:
  141. {
  142. int n_enemies=0;
  143. e_list::const_iterator e_end = enemies.end();
  144. for(e_list::iterator it=enemies.begin(); it!=e_end; ++it)
  145. if(it->alive) n_enemies++; //counts alive enemies
  146. for(e_list::iterator it=enemies.begin(); it!=e_end; ++it)
  147. {
  148. //mvaddch(it->y,it->x,' ');
  149. it->next_pos(); //evaluate new positions
  150. if(it->alive && (double)rand()/RAND_MAX<(shootrate*enemy_num/n_enemies)){ //try a bomb-dropping
  151. if(sound) pthread_create(&thread[1],NULL,penshoot_sound,NULL); // enemy shoot sound
  152. it->shoot(bombs);
  153. }
  154. }
  155. }
  156. else boss1.alive = true; //if no enemy is alive, boss spawns
  157. if(boss1.alive) //if boss is alive, moves and shoots
  158. {
  159. for(int i=0;i<boss1.width;i++) //mvaddch(boss1.y,boss1.x+i,' ');
  160. for(int j=0;j<boss1.height;j++){
  161. //mvaddch(boss1.y+j,boss1.x,' ');
  162. //mvaddch(boss1.y+j,boss1.x+boss1.width-1,' ');
  163. }
  164. boss1.next_pos();
  165. if((double)rand()/RAND_MAX<shootrate*25.){ //boss's shootrate is 25 times the one of normal enemies
  166. if(sound) pthread_create(&thread[1],NULL,penshoot_sound,NULL); // enemy shoot sound
  167. boss1.shoot(bombs);
  168. }
  169. }
  170. if(!bullets.empty()) //evaluate new bullets' positions
  171. for(b_list::iterator it=bullets.begin(); it!=bullets.end(); ++it){
  172. //mvaddch(it->y,it->x,' ');
  173. it->next_pos();
  174. }
  175. bullets.unique(); //remove duplicates of bullets (i.e. player can't shoot enemies)
  176. if(!rockets.empty()) //evaluate new rockets' positions
  177. for(r_list::iterator it=rockets.begin(); it!=rockets.end(); ++it){
  178. //mvaddch(it->y,it->x,' ');
  179. it->next_pos();
  180. }
  181. rockets.unique();
  182. if(!bombs.empty()) //evaluate new bombs' positions
  183. for(b_list::iterator it=bombs.begin(); it!=bombs.end(); ++it){
  184. //mvaddch(it->y,it->x,' ');
  185. it->next_pos();
  186. }
  187. bombs.unique(); //remove duplicates of bombs
  188. if(!powerups.empty()) //new powerups' positions
  189. for(b_list::iterator it=powerups.begin(); it!=powerups.end(); ++it){
  190. //mvaddch(it->y,it->x,' ');
  191. it->next_pos();
  192. }
  193. powerups.unique(); //FIXME: THIS ONLY WORKS ON CONSECUTIVE ELEMENTS OF THE LIST!! we should at least sort powerups before calling unique()
  194. interactions(player1,bullets,bombs,enemies,walls,powerups,rockets,boss1,score,poweruprate);
  195. ///////ENDGAME CHECKS
  196. if(boss1.health<1 || gameover(player1,bombs))
  197. {
  198. if(sound){
  199. pkill_music(thread[0]);
  200. pthread_create(&thread[2],NULL,pwin_theme,NULL);
  201. }
  202. WINDOW *replay;
  203. if(boss1.health<1) // YOU WON
  204. {
  205. Victory(boss1.name,score,level,chflag);
  206. replay=newwin(3,25,26,20);
  207. } else // YOU LOST
  208. {
  209. Defeat(score);
  210. replay=newwin(3,25,17,20);
  211. }
  212. box(replay,ACS_VLINE,ACS_HLINE);
  213. switch(char choice = playagain(replay)) // playagain() returns 'y', 'n' or 'q'
  214. {
  215. case 'y':
  216. case 'n':
  217. delwin(replay);
  218. timeout(500);
  219. score=0;
  220. erase();
  221. refresh();
  222. if(choice=='n')
  223. level = choose_level(commands); // get desired level
  224. setup_level(level, shootrate, poweruprate, refresh_time, boss1, enemy_num, walls_num); // set game parameters and boss
  225. load_enemies(enemies,enemy_num);
  226. reset(player1, enemies, boss1, bullets, bombs, walls, walls_num, powerups, rockets, chflag); // reset box, player, enemy and deletes all bullets and bombs
  227. erase();
  228. refresh();
  229. continue; // Go to the beginning of the main loop
  230. case 'q':
  231. endwin();
  232. cout<<lightgreen<<"Game exited correctly."<<none<<endl;
  233. return 0;
  234. }
  235. }
  236. draw(player1,bullets,bombs,enemies,walls,powerups,rockets,boss1);
  237. write_score(Score,score);
  238. if(boss1.alive){
  239. write_bosshp(BossHP,boss1.health,boss1.healthmax,boss1.name);
  240. wnoutrefresh(BossHP);
  241. }
  242. wnoutrefresh(Score);
  243. refresh();
  244. }
  245. ////////END OF MAIN LOOP
  246. endwin();
  247. return -1; //program never really reaches here, but compiler is happy
  248. }