invaders.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  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. char 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. choose_level(shootrate, refresh_time, boss1, level, ENEMY_NUM, commands); //choose difficulty level and set game parameters and boss
  87. load_enemies(enemies,ENEMY_NUM);
  88. player1.set_commands(commands);
  89. erase();
  90. if(atoi(&level)==1) WALLS_NUM=3;
  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.size()+1))+i*(2*C/(3*walls.size()+1)),2*R/3,(int)min(6,2*C/(3*(int)(walls.size())+1)),2,3);
  97. /////////ENTERING MAIN LOOP
  98. /*if(sound)
  99. bkgd_music = 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,powerups,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) shoot_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 bkgd_music = pthread_create(&thread[0],NULL,pmusic,NULL);
  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) shoot_sound = pthread_create(&thread[1],NULL,penshoot_sound,NULL);
  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) shoot_sound = pthread_create(&thread[1],NULL,penshoot_sound,NULL);
  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);
  195. ///////ENDGAME CHECKS
  196. if(boss1.health<1) //YOU WON!!
  197. {
  198. /*if(sound){
  199. pkill_music(thread[0]);
  200. win_theme = pthread_create(&thread[2],NULL,pwin_theme,NULL);
  201. }*/
  202. Victory(boss1.name,score,level,chflag);
  203. WINDOW *replay;
  204. replay=newwin(3,25,26,20);
  205. box(replay,ACS_VLINE,ACS_HLINE);
  206. if(playagain(replay)) //playagain() returns true if player wants to play again, false otherwise
  207. {
  208. delwin(replay);
  209. timeout(500);
  210. score=0;
  211. erase();
  212. refresh();
  213. choose_level(shootrate, refresh_time, boss1, level, ENEMY_NUM, commands);
  214. load_enemies(enemies,ENEMY_NUM);
  215. reset(player1, enemies, boss1, bullets, bombs, walls, powerups,rockets, level,chflag); // reset box, player, enemy and deletes all bullets and bombs
  216. erase();
  217. refresh();
  218. continue;
  219. }
  220. else {
  221. endwin();
  222. cout<<lightgreen<<"Game exited correctly."<<none<<endl;
  223. return 0;
  224. }
  225. }
  226. if(gameover(player1,bombs))
  227. {
  228. Defeat(score);
  229. WINDOW* replay;
  230. replay=newwin(3,25,17,20);
  231. box(replay,ACS_VLINE,ACS_HLINE);
  232. if(playagain(replay)) //playagain() returns true if player wants to play again, false otherwise
  233. {
  234. delwin(replay);
  235. timeout(500);
  236. score=0;
  237. erase();
  238. refresh();
  239. choose_level(shootrate, refresh_time, boss1, level, ENEMY_NUM, commands);
  240. load_enemies(enemies,ENEMY_NUM);
  241. reset(player1, enemies, boss1, bullets, bombs, walls, powerups,rockets,level,chflag); // reset box, player, enemy and delete all bullets and bombs
  242. erase();
  243. refresh();
  244. continue;
  245. }
  246. else {
  247. endwin();
  248. cout<<lightgreen<<"Game exited correctly."<<none<<endl;
  249. return 0;
  250. }
  251. }
  252. draw(player1,bullets,bombs,enemies,walls,powerups,rockets,boss1);
  253. write_score(Score,score);
  254. if(boss1.alive){
  255. write_bosshp(BossHP,boss1.health,boss1.healthmax,boss1.name);
  256. wrefresh(BossHP);
  257. }
  258. wrefresh(Score);
  259. refresh();
  260. }
  261. ////////END OF MAIN LOOP
  262. endwin();
  263. return -1; //program never really reaches here, but compiler is happy
  264. }