invaders.cpp 10 KB

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