invaders.cpp 11 KB

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