invaders.cpp 10 KB

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