invaders.cpp 10 KB

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