123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309 |
- /* This file is part of Invaders.
- *
- * Copyright (C) 2020 LCM.
- * You may use, distribute and modify Invaders under the terms of the
- * GPLv3 license, available at <https://www.gnu.org/licenses/\>.
- */
- #include "bullet.hpp"
- #include "enemy.hpp"
- #include "player.hpp"
- #include "boss.hpp"
- #include "powerup.hpp"
- #include "definitions.hpp"
- #include "functions.hpp"
- #include "wall.hpp"
- using std::cout;
- using std::endl;
- using std::min;
- int main(int argc,char** argv)
- {
- if(argc>1){
- if(strcmp(argv[1],"--uber-debug")!=0 && strcmp(argv[1],"--info")!=0 && strcmp(argv[1],"--autowin")!=0){
- cout<<"Usage: "<<argv[0]<<" [--info]"<<endl;
- return 1;
- }
-
- if(strcmp(argv[1],"--info")==0){
- cout<<endl<<"Curses Invaders 4.4"<<endl;
- cout<<endl<<"Game developed by Giacomo Parolini (jp) and Enrico Guiraud (blue) in years 2010-2013."<<endl;
- cout<<"Source code is available under request to jp@lcm.mi.infn.it (it's quite ugly, I warn you ;-) )"<<endl;
- cout<<"Report any bug to the same mail address."<<endl<<endl;
- return 1;
- }
- }
-
- typedef std::list<bullet> b_list;
- typedef std::list<enemy> e_list;
- typedef std::vector<wall> w_vec;
- typedef std::list<rocket> r_list;
- srand(time(NULL));
-
- //OBJECTS INIZIALIZATION
- player player1; //player is automatically created in [C/2][R]
- e_list enemies; //vector of enemies
- b_list bullets; //list of player's bullets
- b_list bombs; //list of enemies' bombs
- b_list powerups; //list of powerups
- r_list rockets; //list of rockets
- int refresh_time; //how long the program waits before refreshing the screen
- int chflag=0; //cheat flag: 0=normal, 1=cheats allowed, 2=special mode activated, 3=both special mode and cheats.
- boss boss1;
- WINDOW *Score,*BossHP;
- 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
-
- //PARAMETERS/UTILITIES
- double shootrate; //probability of an enemy shooting a bomb
- double poweruprate; //probability of a powerup being dropped
- int enemy_num; //number of enemies in the current match
- int walls_num; //number of walls
- int command; //keyboard input
- int score=0; //score: gain +100 when an enemy is destroyed and +50 when a bomb is destroyed
- int level=1; //difficulty level
- //bool sound=false;
-
- /*//PTHREAD STUFF
- pthread_t thread[THREADS_NUM];
- int bkgd_music; //#0
- int enshoot_sound; //#1
- int win_theme; //#2
- int lose_theme; //#3
- int shoot_sound; //#4
-
- if(argc>1){ //./invaders -m/m/mute -> the game starts with no sounds.
- if(strcmp(argv[1],"-s")==0 || strcmp(argv[1],"s")==0 || strcmp(argv[1],"sound")==0)
- sound=true;
- else{
- cout<<"usage: "<<argv[0]<<" [-s]/[s]/[sound]"<<endl;
- return -1;
- }
- }*/
-
- //NCURSES STUFF
- initscr();
- curs_set(0);
- noecho();
- nodelay(NULL, true);
- cbreak();
- keypad(stdscr,TRUE);
- start_color();
- init_pair(0,COLOR_WHITE,COLOR_BLACK);
- init_pair(1,COLOR_GREEN,COLOR_BLACK); //PLAYER
- init_pair(2,COLOR_RED,COLOR_BLACK); //ENEMY
- init_pair(3,COLOR_YELLOW,COLOR_BLACK); //BULLETS-POWERUPS
- init_pair(4,COLOR_MAGENTA,COLOR_BLACK); //WALLS
- init_pair(5,COLOR_CYAN,COLOR_BLACK); //BOSS
- init_pair(6,COLOR_BLUE,COLOR_BLACK); //BOMBS
- init_pair(7,COLOR_RED,COLOR_RED);
- init_pair(8,COLOR_GREEN,COLOR_GREEN);
- init_pair(9,COLOR_BLACK,COLOR_GREEN);
-
- //////////PROGRAM START: creation of game objects and set of parameters
-
- level = choose_level(commands); // get desired level
- setup_level(level, shootrate, poweruprate, refresh_time, boss1, enemy_num, walls_num, commands); // set game parameters and boss
- load_enemies(enemies,enemy_num);
- player1.set_commands(commands);
- erase();
-
- w_vec walls(walls_num);
-
- Score=newwin(3,10,R/3,C+3);
- BossHP=newwin(3,15,R/3-3,C+3);
-
- int i=0;
- for(w_vec::iterator it=walls.begin(); it!=walls.end(); ++it, ++i) //creating walls (in a quite symmetric pattern)
- 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);
- /////////ENTERING MAIN LOOP
-
- /*if(sound)
- bkgd_music = pthread_create(&thread[0],NULL,pmusic,NULL); //executing background music in parallel thread*/
-
- if(argc>1){
- if(strcmp(argv[1],"--uber-debug")==0){ player1.weaponclass=5; player1.weaponclassrkt=5; player1.rocketlauncher=true; player1.length=MAX_LENGTH; }
- else if(strcmp(argv[1],"--autowin")==0){ Victory(boss1.name,score,level,0); endwin(); return 1; }
- }
-
- while(1)
- {
- napms(refresh_time); //ncurses sleep function (ms)
- timeout(0);
- command = getch();
-
- if(command!=ERR) //READING INPUT
- command = tolower(command);
-
- if(command == commands[8]){ //q = exit game
- endwin();
- //pkill_music(thread[0]);
- cout<<lightgreen<<"Game exited correctly."<<none<<endl;
- return 1;
- }
- if(command == commands[4])
- pause_game(chflag); //p = pause game
- //=====CHEATS================================================
-
- if(chflag==1 || chflag==3)
- get_cheat(command,player1,enemies,shootrate);
- if(chflag!=2 && chflag!=3)
- get_SpecialMode(command,chflag,boss1,enemies,bullets,bombs,powerups,rockets);
-
- //======END CHEATS===============================================
-
- if(command == commands[0] || command == KEY_LEFT || command == commands[1] || command == KEY_RIGHT ||
- ((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)
- //for(int i=player1.x;i<player1.x+player1.length;i++) //mvaddch(player1.y,i,' ');
- player1.next_pos(command);
- }
-
- if(command==commands[5] || command==commands[6]){ //spacebar = shoot!
- //if(sound) shoot_sound = pthread_create(&thread[4],NULL,pshoot_sound,NULL);
- player1.shoot(bullets);
- if(player1.rocketlauncher) player1.shootrkt(rockets);
- }
-
- /*if(command==commands[7]){ //mute/unmute
- if(sound) pkill_music(thread[0]);
- else bkgd_music = pthread_create(&thread[0],NULL,pmusic,NULL);
- sound=!sound;
- }*/
-
- if(enemyalive(enemies)) //if there's at least one enemy alive:
- {
- int n_enemies=0;
- e_list::const_iterator e_end = enemies.end();
-
- for(e_list::iterator it=enemies.begin(); it!=e_end; ++it)
- if(it->alive) n_enemies++; //counts alive enemies
-
- for(e_list::iterator it=enemies.begin(); it!=e_end; ++it)
- {
- //mvaddch(it->y,it->x,' ');
- it->next_pos(); //evaluate new positions
- if(it->alive && (double)rand()/RAND_MAX<(shootrate*enemy_num/n_enemies)){ //try a bomb-dropping
- //if(sound) shoot_sound = pthread_create(&thread[1],NULL,penshoot_sound,NULL);
- it->shoot(bombs);
- }
- }
- }
- else boss1.alive = true; //if no enemy is alive, boss spawns
-
- if(boss1.alive) //if boss is alive, moves and shoots
- {
- for(int i=0;i<boss1.width;i++) //mvaddch(boss1.y,boss1.x+i,' ');
- for(int j=0;j<boss1.height;j++){
- //mvaddch(boss1.y+j,boss1.x,' ');
- //mvaddch(boss1.y+j,boss1.x+boss1.width-1,' ');
- }
- boss1.next_pos();
- if((double)rand()/RAND_MAX<shootrate*25.){ //boss's shootrate is 25 times the one of normal enemies
- //if(sound) shoot_sound = pthread_create(&thread[1],NULL,penshoot_sound,NULL);
- boss1.shoot(bombs);
- }
- }
-
- if(!bullets.empty()) //evaluate new bullets' positions
- for(b_list::iterator it=bullets.begin(); it!=bullets.end(); ++it){
- //mvaddch(it->y,it->x,' ');
- it->next_pos();
- }
-
- bullets.unique(); //remove duplicates of bullets (i.e. player can't shoot enemies)
- if(!rockets.empty()) //evaluate new rockets' positions
- for(r_list::iterator it=rockets.begin(); it!=rockets.end(); ++it){
- //mvaddch(it->y,it->x,' ');
- it->next_pos();
- }
- rockets.unique();
- if(!bombs.empty()) //evaluate new bombs' positions
- for(b_list::iterator it=bombs.begin(); it!=bombs.end(); ++it){
- //mvaddch(it->y,it->x,' ');
- it->next_pos();
- }
-
- bombs.unique(); //remove duplicates of bombs
- if(!powerups.empty()) //new powerups' positions
- for(b_list::iterator it=powerups.begin(); it!=powerups.end(); ++it){
- //mvaddch(it->y,it->x,' ');
- it->next_pos();
- }
-
- powerups.unique(); //FIXME: THIS ONLY WORKS ON CONSECUTIVE ELEMENTS OF THE LIST!! we should at least sort powerups before calling unique()
-
- interactions(player1,bullets,bombs,enemies,walls,powerups,rockets,boss1,score,poweruprate);
- ///////ENDGAME CHECKS
- if(boss1.health<1 || gameover(player1,bombs))
- {
- /*if(sound){
- pkill_music(thread[0]);
- win_theme = pthread_create(&thread[2],NULL,pwin_theme,NULL);
- }*/
- WINDOW *replay;
- if(boss1.health<1) // YOU WON
- {
- Victory(boss1.name,score,level,chflag);
- replay=newwin(3,25,26,20);
- } else // YOU LOST
- {
- Defeat(score);
- replay=newwin(3,25,17,20);
- }
- box(replay,ACS_VLINE,ACS_HLINE);
- switch(char choice = playagain(replay)) // playagain() returns 'y', 'n' or 'q'
- {
- case 'y':
- case 'n':
- delwin(replay);
- timeout(500);
- score=0;
- erase();
- refresh();
- if(choice=='n')
- level = choose_level(commands); // get desired level
- setup_level(level, shootrate, poweruprate, refresh_time, boss1, enemy_num, walls_num, commands); // set game parameters and boss
- load_enemies(enemies,enemy_num);
- reset(player1, enemies, boss1, bullets, bombs, walls, walls_num, powerups, rockets, chflag); // reset box, player, enemy and deletes all bullets and bombs
- erase();
- refresh();
- continue; // Go to the beginning of the main loop
- case 'q':
- endwin();
- cout<<lightgreen<<"Game exited correctly."<<none<<endl;
- return 0;
- }
- }
- draw(player1,bullets,bombs,enemies,walls,powerups,rockets,boss1);
- write_score(Score,score);
-
- if(boss1.alive){
- write_bosshp(BossHP,boss1.health,boss1.healthmax,boss1.name);
- wnoutrefresh(BossHP);
- }
-
- wnoutrefresh(Score);
- refresh();
- }
- ////////END OF MAIN LOOP
- endwin();
-
- return -1; //program never really reaches here, but compiler is happy
- }
|