123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965 |
- #include "functions.hpp"
- #include "definitions.hpp"
- using std::cout;
- using std::cin;
- using std::endl;
- using std::min;
- extern std::mutex m_bullets;
- //========RECORD'S METHODS===========
-
- void record::print()
- {
- printw("%s\t%i\n",nick.c_str(),score);
-
- }
- void record::printwin(WINDOW* win)
- {
- wprintw(win,"%s.......%i",nick.c_str(),score);
-
- }
- void record::print(std::ofstream& recordfile)
- {
- if(!recordfile.is_open())
- {
- printw("error: record file is not open ");
- return;
- }
- recordfile << std::setw(6) << std::left << nick << " " << score << endl;
- }
-
- //=======INDIPENDENT FUNCTIONS=========
- void print_title() //CAPITAL LETTERS TITLE!
- {
- attron(COLOR_PAIR(5));
- mvprintw(0,0," __ _ _ _ _ _ _ _ _ _ __");
- mvprintw(1,0,"__// |_ |_| |_| | |_ | |\\ | | | |_| |\\ |_ |_| |_ \\\\__");
- mvprintw(2,0," \\\\__ _| | | | |_ |_ | | \\| \\/ | | |/ |_ |\\ _| __//");
- attron(COLOR_PAIR(0));
- }
- void print_catchphrase(std::string phrase){
- static int head=60;
- static int tail=60;
- phrase.resize(59,' ');
-
- attron(COLOR_PAIR(4));
- if(head<1){
- head=60;
- }
- if(head<tail){
- move(3,0);
- clrtoeol();
- mvprintw(3,head,phrase.c_str());
- tail=(head+phrase.size()<60?head+phrase.size():60);
- }
- else{
- move(3,0);
- clrtoeol();
- for(unsigned int i=0;i<abs(phrase.size()-(60-head));i++)
- mvaddch(3,i,phrase[60-head+i]);
- for(unsigned int i=head;i<60;i++)
- mvaddch(3,i,phrase[i-head]);
- tail=(phrase.size()-(60-head)>0?phrase.size()-(60-head):60);
- }
- head--;
-
-
-
- }
-
- std::string choose_phrase(){
- std::string phr;
- int r=(int)(23*(double)rand()/RAND_MAX);
- switch(r){
- case 0:
- phr="Now with cheats!";
- break;
- case 1:
- phr="20 percent cooler than your 3D-graphics games!";
- break;
- case 2:
- phr="NCurses rulez!";
- break;
- case 3:
- phr="References to people and events are purely random.";
- break;
- case 4:
- phr=">>> Crush <<< Kill >>> Destroy <<< SWAG >>>";
- break;
- case 5:
- phr="Also try Apocalypse 2.0!";
- break;
- case 6:
- phr=")]=>---- # ASCII IS ART!!! # ----<=[(";
- break;
- case 7:
- phr="Still a better program than Isgro's ones...";
- break;
- case 8:
- phr="<< Oh, sul serio, minchia raga, ne'? >>";
- break;
- case 9:
- phr="Cool game, bro, but needs more ponies.";
- break;
- case 10:
- phr="Hit R!!!";
- break;
- case 11:
- phr="Konami Code is still fashion.";
- break;
- case 12:
- phr="Almost completely debugged!";
- break;
- case 13:
- phr="(((( Kill them with WUBZ! ))))";
- break;
- case 14:
- phr="choose_phrase() is best function.";
- break;
- case 15:
- phr="zsh: segmentation fault (core dumped) invaders";
- break;
- case 16:
- phr="Aliens! Aliens everywhere!";
- break;
- case 17:
- phr="Breaks the fourth wall! Breaks the fourth wall!";
- break;
- case 18:
- phr="Them customizable controls!!!";
- break;
- case 19:
- phr=">~~~~~~~~~~~~~~~~~~~~~< You shall be assimilated into Linux.";
- break;
- case 20:
- phr="Did you try inv_bossrush?";
- break;
- case 21:
- phr="Keep calm and FUCKIN' KILL ALIENS.";
- break;
- case 22:
- phr="Read the fucking `man` page!!!";
- break;
- }
- return phr;
- }
- void print_info(){
- WINDOW *info;
- info=newwin(20,80,0,0);
- box(info,ACS_VLINE,ACS_HLINE);
- mvwprintw(info,0,1,"Welcome to Space Invaders!");
- wattron(info,COLOR_PAIR(1));
- mvwprintw(info,2,1,"About the Game:");
- wattron(info,COLOR_PAIR(6));
- mvwprintw(info,3,1,"This is a Terminal-playable game where you control a moving cannon base");
- mvwprintw(info,4,1,"(");
- wattron(info,COLOR_PAIR(1));
- wprintw(info,"========");
- wattron(info,COLOR_PAIR(6));
- wprintw(info,") with the aim to destroy all the enemy Space Invaders");
- mvwprintw(info,5,1,"(");
- wattron(info,COLOR_PAIR(2));
- wprintw(info,"#");
- wattron(info,COLOR_PAIR(6));
- wprintw(info,") assaulting the Earth. After killing all the Space Invaders, you shall face");
- mvwprintw(info,6,1,"the Big Alien Boss, the most powerful of all the alien enemies.");
- mvwprintw(info,7,1,"During the game, mind the Powerups (");
- wattron(info,COLOR_PAIR(3));
- wprintw(info,"+");
- wattron(info,COLOR_PAIR(6));
- wprintw(info,",");
- wattron(info,COLOR_PAIR(1));
- wprintw(info,"-");
- wattron(info,COLOR_PAIR(6));
- wprintw(info,",");
- wattron(info,COLOR_PAIR(4));
- wprintw(info,"X");
- wattron(info,COLOR_PAIR(6));
- wprintw(info,") that will increase");
- mvwprintw(info,8,1,"your shooting power if you manage to get them.");
- wattron(info,COLOR_PAIR(1));
- mvwprintw(info,10,1,"Default Controls: ");
- wattron(info,COLOR_PAIR(5));
- mvwprintw(info,11,1,"<- ");
- wattron(info,COLOR_PAIR(6));
- wprintw(info," A or Left Arrow");
- wattron(info,COLOR_PAIR(5));
- mvwprintw(info,12,1,"-> ");
- wattron(info,COLOR_PAIR(6));
- wprintw(info," D or Right Arrow");
- wattron(info,COLOR_PAIR(5));
- mvwprintw(info,13,1,"shoot ");
- wattron(info,COLOR_PAIR(6));
- wprintw(info," spacebar");
- wattron(info,COLOR_PAIR(3));
- mvwprintw(info,15,10,"Game developed by Giacomo Parolini & Enrico Guiraud (v.4.4)");
- wattron(info,COLOR_PAIR(6));
- mvwprintw(info,17,14,"Click any key to return to the Start Menu");
- wattron(info,COLOR_PAIR(0));
-
- wrefresh(info);
- timeout(-1);
- if(getch()!=ERR){
- delwin(info);
- erase();
- refresh();
- }
- }
- int choose_level(int* commands)
- {
- std::string phrase;
- phrase=choose_phrase();
- char input;
-
- do{
- print_title();
- print_catchphrase(phrase);
- attron(COLOR_PAIR(1));
- mvprintw(5,0,"Choose difficulty level:");
- attron(COLOR_PAIR(1));
- mvprintw(6,0,"1- Easy");
- attron(COLOR_PAIR(3));
- mvprintw(7,0,"2- Medium");
- attron(COLOR_PAIR(2));
- mvprintw(8,0,"3- Hard");
- attron(COLOR_PAIR(4));
- mvprintw(9,0,"4- Impossible");
- attron(COLOR_PAIR(5));
- mvprintw(10,0,"i- Info");
- attron(COLOR_PAIR(6));
- mvprintw(11,0,"h- Highscores");
- attron(COLOR_PAIR(1));
- mvprintw(12,0,"c- Commands");
- attron(COLOR_PAIR(2));
- mvprintw(13,0,"q- Quit");
-
- refresh();
- timeout(80);
-
- input=tolower(getch());
-
- if(!(input=='1' || input=='2' || input=='3' || input=='4' || input=='i' || input=='h' || input=='r' || input =='q') && input!=ERR){
- attron(COLOR_PAIR(2));
- mvprintw(14,0,"Bad input. Please choose a number within 1 and 4.");
- attron(COLOR_PAIR(0));
- }
-
- if(input=='i')
- print_info();
-
- if(input=='h')
- print_scores();
- if(input=='r')
- phrase=choose_phrase();
-
- if(input=='c')
- change_commands(commands);
-
- if(input=='q'){
- endwin();
- cout<<lightgreen<<"Game exited correctly."<<none<<endl;
- exit(1);
- }
-
- }while(!(input=='1' || input=='2' || input=='3' || input=='4'));
-
- create_folder(0);
- return input - '0';
- }
- void setup_level(int level, double& shootr, int& refresh_t, boss& boss1, int& enemy_num, int* commands)
- {
- switch(level) //setting game parameters
- {
- case 1:
- {
- shootr = 0.006;
- refresh_t = (int)(0.09*1E3);
- boss bosslvl1(1,1,100,9,6,BOSS_FILE1);
- boss1 = bosslvl1;
- std::string bossname=getenv("HOME");
- bossname=bossname+RECORD_DIR+BOSS_FILE1+".dat";
- boss1.loadpicture(bossname.c_str());
- enemy_num=30;
- break;
- }
- case 2:
- {
- shootr = 0.009;
- refresh_t = (int)(0.09*1E3);
- boss bosslvl2(1,1,200,11,5,BOSS_FILE2);
- boss1 = bosslvl2;
- std::string bossname=getenv("HOME");
- bossname=bossname+RECORD_DIR+BOSS_FILE2+".dat";
- boss1.loadpicture(bossname.c_str());
- enemy_num=40;
- break;
- }
- case 3:
- {
- shootr = 0.015;
- refresh_t = (int)(0.08*1E3);
- boss bosslvl3(1,1,300,9,6,BOSS_FILE3);
- boss1 = bosslvl3;
- std::string bossname=getenv("HOME");
- bossname=bossname+RECORD_DIR+BOSS_FILE3+".dat";
- boss1.loadpicture(bossname.c_str());
- enemy_num=45;
- break;
- }
- case 4:
- {
- shootr = 0.020;
- refresh_t = (int)(0.07*1E3);
- boss bosslvl4(1,1,400,11,6,BOSS_FILE4);
- boss1 = bosslvl4;
- std::string bossname=getenv("HOME");
- bossname=bossname+RECORD_DIR+BOSS_FILE4+".dat";
- boss1.loadpicture(bossname.c_str());
- enemy_num=50;
- break;
- }
- }
- }
- void change_commands(int* commands){
- WINDOW *change;
- int cmd;
- int line=0;
- std::string chcommands[CMD_NUM];
-
- std::string name[CMD_NUM]={"Key Left","Key Right","Key Up","Key Down","Pause","Shoot 1","Shoot 2","Mute/Unmute","Quit"};
- change=newwin(CMD_NUM+10,20,0,0);
- keypad(change,true);
- box(change,ACS_VLINE,ACS_HLINE);
- wattron(change,COLOR_PAIR(5));
- mvwprintw(change,1,2,"CHANGE CONTROLS");
- mvwprintw(change,3,2,"(X) to exit");
-
- do{
- for(int i=0;i<CMD_NUM;i++){
- if(commands[i]==' ') chcommands[i]="Barra sp.";
- else chcommands[i]=commands[i];
- if(i==line) wattron(change,COLOR_PAIR(9));
- mvwprintw(change,i+5,1,"%s: %s",name[i].c_str(),chcommands[i].c_str());
- wattron(change,COLOR_PAIR(1));
- }
- timeout(0);
- cmd=wgetch(change);
-
- switch(cmd){
- case KEY_DOWN:
- case 's':
- if(line<CMD_NUM-1) line++;
- break;
- case KEY_UP:
- case 'w':
- if(line>0) line--;
- break;
- case 'X':
- delwin(change);
- erase();
- refresh();
- return;
- case '\n':
- wattron(change,COLOR_PAIR(2));
- mvwprintw(change,line+5,1,"%s: %s",name[line].c_str(),chcommands[line].c_str());
- wrefresh(change);
- timeout(-1);
- cmd=wgetch(change);
- commands[line]=tolower(cmd);
- wmove(change,line+5,0);
- wclrtoeol(change);
- box(change,ACS_VLINE,ACS_HLINE);
- wattron(change,COLOR_PAIR(1));
- break;
- }
-
- wrefresh(change);
-
- }while(cmd!='X');
-
- delwin(change);
- erase();
- refresh();
- return;
-
- }
-
- void load_enemies(e_list& enemies,int enemy_num)
- {
- enemies.clear();
- for(int i=0; i<enemy_num; ++i) //loading enemies
- {
- static int c=0, r=0;
- if(c>C-1)
- {
- c=0;
- r++;
- if(r>R-2) break;
- }
-
- enemy newenemy(c++,r);
- if(newenemy.y%2!=0) newenemy.direction=1;
- enemies.push_back(newenemy);
- }
- }
-
- char playagain(WINDOW* replay)
- {
- char answer;
-
- do
- {
- wattron(replay,COLOR_PAIR(2));
- mvwprintw(replay,1,3,"Play again? [y/n/q] ");
- answer=tolower(wgetch(replay));
- }while(answer!='y' && answer!='n' && answer!='q');
-
- return answer;
- }
- bool gameover(player& _player,b_list& bombs) //Checks if a bomb/enemy/boss hit the player.
- {
- for(b_list::iterator it=bombs.begin(); it!=bombs.end(); ++it)
- if((it->x>=_player.x && it->x<_player.x+_player.length) && (it->y==_player.y) && !_player.godmode)
- return true;
-
- return false;
- }
- void reset(player& _player, e_list &enemies, boss& boss1, b_list& bullets, b_list& bombs, w_vec& walls, b_list& powerups, r_list& rockets, char level,int& chflag)
- {
-
- _player.x=C/2; //resetting initial position and stats of player1
- _player.y=R-1;
- _player.weaponclass=0;
- _player.length=8;
- _player.rocketlauncher=FALSE;
-
- int c=0, r=0;
- e_list::const_iterator e_end = enemies.end();
- for(e_list::iterator it = enemies.begin(); it!=e_end; ++it) //resetting all the enemies
- {
- if(c==C)
- {
- c=0;
- r++;
- }
- it->alive = true;
- it->x=c++;
- it->y=r;
- if(it->y%2==0) it->direction=0;
- else it->direction=1;
- }
-
- boss1.alive = false;
-
- bullets.clear(); //destroying all bullets/bombs/etc
- bombs.clear();
- powerups.clear();
- rockets.clear();
- chflag=0;
-
- WALLS_NUM=2; //resetting walls
-
- if(atoi(&level)==1) WALLS_NUM=3;
-
- walls.resize(WALLS_NUM);
- for(int i=0;i<walls.size();i++)
- walls[i].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,5);
-
- }
-
- bool enemyalive(e_list &enemies)
- {
- bool isalive = false;
- e_list::const_iterator end = enemies.end();
-
- for(e_list::iterator it=enemies.begin(); it!=end; ++it)
- if(it->alive==true)
- {
- isalive = true;
- break;
- }
-
- return isalive;
- }
- bool operator<(record record1, record record2)
- {
- if(record1.score<record2.score) return true;
- else return false;
- }
- void pause_game(int& chflag){
- WINDOW *pause;
- char c;
- std::string cheat;
- char Cheat[7];
- pause=newwin(5,17,15,15);
- box(pause,ACS_VLINE,ACS_HLINE);
- wattron(pause,COLOR_PAIR(5));
- mvwprintw(pause,1,3,"GAME PAUSED");
- mvwprintw(pause,3,2,"(P) to resume");
- do{
- c=tolower(wgetch(pause));
- if(c=='c'){ //cheats!
- nocbreak();
- wmove(pause,2,5);
- echo();
- wattron(pause,COLOR_PAIR(4));
- wscanw(pause,"%s",Cheat);
- cbreak();
- noecho();
- if(std::strcmp(Cheat,CHEAT_CODE)==0){
- if(chflag==0)
- chflag=1;
- if(chflag==2)
- chflag=3;
- waddch(pause,'\a');
- }
- delwin(pause);
- erase();
- refresh();
- return;
- }
- else if(c=='p'){
- delwin(pause);
- erase();
- refresh();
- return;
- }
- }while(c!='c' && c!='p');
- }
- void write_score(WINDOW* Score,int score){
- wattron(Score,COLOR_PAIR(6));
- mvwprintw(Score,1,2,"SCORE");
- wattron(Score,COLOR_PAIR(1));
- mvwprintw(Score,2,2,"%i",score);
- }
- void write_bosshp(WINDOW* BossHP,int hp,int hpmax,std::string name){
- wattron(BossHP,COLOR_PAIR(3));
- mvwprintw(BossHP,0,1,"%s",name.c_str()); //writes boss's name
-
- wattron(BossHP,COLOR_PAIR(8));
- int i=0;
- for(i=0;i<(int)(15*(double)(hp)/hpmax);i++) mvwaddch(BossHP,1,i,' '); //draws green spaces
- i++;
-
- wattron(BossHP,COLOR_PAIR(7));
- for(;i<15;i++) mvwaddch(BossHP,1,i,' '); //draws red spaces (total spaces are 15)
-
- if((double)hp/hpmax>.5)
- wattron(BossHP,COLOR_PAIR(1));
- else if((double)hp/hpmax>.25)
- wattron(BossHP,COLOR_PAIR(3));
- else
- wattron(BossHP,COLOR_PAIR(2));
-
- wmove(BossHP,2,0);
- wdeleteln(BossHP);
- mvwprintw(BossHP,2,0,"%i / %i HP",hp,hpmax); //writes boss's hp / hpmax (color depends on hp/hpmax ratio)
- }
- void Victory(std::string name,int score,char level,int chflag){
- score+=1500*atoi(&level);
- WINDOW *victory;
- victory=newwin(19,32,7,17);
- box(victory,ACS_VLINE,ACS_HLINE);
- wattron(victory,COLOR_PAIR(1));
- mvwprintw(victory,2,4,"YOU DEFEATED %s!",name.c_str());
- wattron(victory,COLOR_PAIR(3));
- mvwprintw(victory,4,5,"+ %i pts!",atoi(&level)*1500);
- wattron(victory,COLOR_PAIR(1));
- mvwprintw(victory,4,5,"YOU WON!");
- wattron(victory,COLOR_PAIR(3));
- mvwprintw(victory,4,5,"Your score is: %i",score);
- wrefresh(victory);
-
- if(chflag==0)
- refreshrecords(score,victory);
- else{
- wattron(victory,COLOR_PAIR(4));
- mvwprintw(victory,7,3,"I think you used cheats...");
- }
-
- wrefresh(victory);
-
- delwin(victory);
- }
- void Defeat(int score){
- WINDOW *defeat;
- defeat=newwin(10,32,7,17);
- box(defeat,ACS_VLINE,ACS_HLINE);
- wattron(defeat,COLOR_PAIR(2));
- mvwprintw(defeat,3,10,"GAME OVER!");
- wattron(defeat,COLOR_PAIR(3));
- mvwprintw(defeat,6,4,"Your score was: %i pts!",score);
-
- wrefresh(defeat);
- delwin(defeat);
- }
- void get_SpecialMode(int c,int& chflag,boss& boss1,e_list& enemies,b_list& bullets,b_list& bombs,b_list& powerups,r_list& rockets){
- if(get_KonamiCode(c)){
- activate_combo(boss1,enemies,bullets,bombs,powerups,rockets);
- if(chflag==0)
- chflag=2;
- if(chflag==1)
- chflag=3;
- }
- }
-
- bool get_KonamiCode(int c){ //UP,UP,DOWN,DOWN,LEFT,RIGHT,LEFT,RIGHT,B,A
- static int combo=0;
- switch(combo){
- case 0:
- case 1:
- if(c==KEY_UP) combo++;
- else if(c!=ERR) combo=0;
- break;
- case 2:
- case 3:
- if(c==KEY_DOWN) combo++;
- else if(c!=ERR) combo=0;
- break;
- case 4:
- case 6:
- if(c==KEY_LEFT) combo++;
- else if(c!=ERR) combo=0;
- break;
- case 5:
- case 7:
- if(c==KEY_RIGHT) combo++;
- else if(c!=ERR) combo=0;
- break;
- case 8:
- if(c=='b') combo++;
- else if(c!=ERR) combo=0;
- break;
- case 9:
- if(c=='a'){
- return true;
- }
- else if(c!=ERR) combo=0;
- break;
- }
-
- return false;
- }
- void get_cheat(char command,player& player1,e_list& enemies,double& shootrate){
- switch(command){
- case '+':
- if(player1.weaponclass<5) player1.weaponclass++; //increase weaponclass
- break;
- case '\\':
- if(player1.weaponclass>1) player1.weaponclass--; //decrease weaponclass
- break;
- case '-':
- if(player1.length>2){ //decrease size
- mvaddch(player1.y,player1.x+player1.length-1,' ');
- player1.length--;
- }
- break;
- case '*':
- if(player1.length<MAX_LENGTH) player1.length++; //increase size
- break;
- case 'n': //nuke enemies
- for(e_list::iterator it=enemies.begin();it!=enemies.end();it++){
- it->alive=false;
- mvaddch(it->y,it->x,' ');
- }
- break;
- case 'r': //activate rocketlauncher and increase weaponclassrkt
- if(!(player1.rocketlauncher)) player1.rocketlauncher=TRUE;
- else if(player1.weaponclassrkt<MAX_WEAPONCLASS) player1.weaponclassrkt++;
- break;
- case 'e': //decrease weaponclassrkt and deactivate rocketlauncher
- if(player1.weaponclassrkt>0) player1.weaponclassrkt--;
- else player1.rocketlauncher=FALSE;
- break;
- case 'u': //trigger uber mode
- player1.rocketlauncher=TRUE;
- player1.weaponclass=MAX_WEAPONCLASS;
- player1.weaponclassrkt=MAX_WEAPONCLASSRKT;
- player1.length=MAX_LENGTH;
- break;
- case 'g': //trigger/untrigger godmode
- player1.godmode=!player1.godmode;
- break;
- case 'f': //freeze: enemies won't shoot.
- shootrate=0;
- break;
- }
- }
- void activate_combo(boss& boss1,e_list& enemies,b_list& bullets,b_list& bombs,b_list& powerups,r_list& rockets){ //if the player performs the Konami Code, the normal boss is replaced with BIG_ALIENBOSS
- boss bossextra(1,1,500,17,5,BOSS_FILE5);
- boss1 = bossextra;
- std::string bossname=getenv("HOME");
- bossname=bossname+RECORD_DIR+BOSS_FILE5+".dat";
- boss1.loadpicture(bossname.c_str());
- for(int i=0;i<7;){
- attron(COLOR_PAIR(i++));
- mvprintw(15,15,"SPECIAL MODE ACTIVATED!!!");
- refresh();
- napms(150);
- }
-
- attron(COLOR_PAIR(0));
- napms(1500);
- for(e_list::iterator i=enemies.begin();i!=enemies.end();i++)
- i->alive=false;
- bullets.clear();
- bombs.clear();
- rockets.clear();
-
- erase();
- }
- //=============BOX'S REPLACEMENT FUNCTIONS============================
- void draw(player& player1,b_list& bullets,b_list& bombs,e_list& enemies,w_vec& walls,b_list& powerups,r_list& rockets,boss& boss1){
-
- struct point
- {
- point() { x=y=0; }
- point(int X, int Y) { x=X; y=Y; }
- int x,y;
- };
-
- static std::vector<point> bullets_p, enemies_p, bombs_p, powerups_p, rockets_p, walls_p, player_p, boss_p;
-
- //REDRAW BULLETS
- for(auto bullet_p: bullets_p)
- mvaddch(bullet_p.y,bullet_p.x,' ');
- bullets_p.clear();
-
- attron(COLOR_PAIR(3));
- for(auto bullet: bullets)
- {
- mvaddch(bullet.y,bullet.x,BULLET_SPRITE);
- bullets_p.push_back(point(bullet.x,bullet.y));
- }
-
- //REDRAW BOMBS
- for(auto bomb_p: bombs_p)
- mvaddch(bomb_p.y,bomb_p.x,' ');
- bombs_p.clear();
-
- attron(COLOR_PAIR(6));
- for(auto bomb: bombs)
- {
- //if(bomb.y<R)
- mvaddch(bomb.y,bomb.x,BOMB_SPRITE);
- bombs_p.push_back(point(bomb.x,bomb.y));
- }
-
- //REDRAW ENEMIES
- for(auto enemy_p: enemies_p)
- mvaddch(enemy_p.y,enemy_p.x,' ');
- enemies_p.clear();
-
- attron(COLOR_PAIR(2));
- for(auto enemy: enemies)
- {
- if(enemy.alive==true)
- {
- mvaddch(enemy.y,enemy.x,ENEMY_SPRITE);
- enemies_p.push_back(point(enemy.x,enemy.y));
- }
- }
-
- //REDRAW ROCKETS
- for(auto rocket_p: rockets_p)
- mvaddch(rocket_p.y,rocket_p.x,' ');
- rockets_p.clear();
-
- attron(COLOR_PAIR(3));
- for(auto rocket: rockets)
- {
- mvaddch(rocket.y,rocket.x,ROCKET_SPRITE);
- rockets_p.push_back(point(rocket.x,rocket.y));
- }
-
-
- //REDRAW POWERUPS
- for(auto powerup_p: powerups_p)
- mvaddch(powerup_p.y,powerup_p.x,' ');
- powerups_p.clear();
-
- for(auto powerup: powerups){
- if(powerup.id == 64){
- attron(COLOR_PAIR(3));
- mvaddch(powerup.y,powerup.x,POWERUP1_SPRITE);
- }
- else if(powerup.id == 128){
- attron(COLOR_PAIR(1));
- mvaddch(powerup.y,powerup.x,POWERUP2_SPRITE);
- }
- else{
- attron(COLOR_PAIR(4));
- mvaddch(powerup.y,powerup.x,POWERUP3_SPRITE);
- }
- powerups_p.push_back(point(powerup.x,powerup.y));
- }
-
- //REDRAW BOSS
- for(auto boss_point: boss_p)
- mvaddch(boss_point.y,boss_point.x,' ');
- boss_p.clear();
-
- if(boss1.alive)
- for(int i=boss1.y; i<boss1.y+boss1.height; i++)
- for(int j=boss1.x; j<boss1.x+boss1.width; j++){
- if(boss1.health>.5*boss1.healthmax)
- attron(COLOR_PAIR(5));
- else if(boss1.health>.25*boss1.healthmax)
- attron(COLOR_PAIR(3));
- else
- attron(COLOR_PAIR(2));
- mvaddch(i,j,boss1.picture[i-boss1.y][j-boss1.x]);
- boss_p.push_back(point(j,i));
- }
-
-
- //REDRAW PLAYER
- for(auto play_point: player_p)
- mvaddch(play_point.y,play_point.x,' ');
- player_p.clear();
-
- if(player1.godmode) attron(COLOR_PAIR(5));
- else attron(COLOR_PAIR(1));
- for(int i=player1.x;i<player1.x+player1.length;++i)
- {
- mvaddch(player1.y,i,PLAYER_SPRITE);
- player_p.push_back(point(i,player1.y));
- }
-
-
- //REDRAW WALLS
- for(auto wall_p: walls_p)
- mvaddch(wall_p.y,wall_p.x,' ');
- walls_p.clear();
-
- for(int i=0;i<walls.size();i++)
- for(int k=0;k<walls[i].width;k++)
- for(int j=0;j<walls[i].height;j++){
- if(walls[i].health[j][k]==1) attron(COLOR_PAIR(2));
- else attron(COLOR_PAIR(4));
- if(walls[i].health[j][k]>0)
- {
- mvaddch(j+walls[i].y,k+walls[i].x,WALL_SPRITE);
- walls_p.push_back(point(k+walls[i].x,j+walls[i].y));
- }
- }
-
- }
- void drop_powerup(int x,int y,b_list& powerups)
- {
- if((double)rand()/RAND_MAX<1./POWERUP_RATIO){ //1 powerup out of POWERUP_RATIO is either a "-" (with ID 128 instead of 64) or a "X"
- if((double)rand()/RAND_MAX<1./ROCKET_RATIO){ //1 powerup out of ROCKET_RATIO*POWERUP_RATIO is a "X" (ID 512)
- powerup newpowerup(x,y,512);
- powerups.push_back(newpowerup);
- }
- else{
- powerup newpowerup(x,y,128);
- powerups.push_back(newpowerup);
- }
- }
- else{
- powerup newpowerup(x,y); //otherwise, it is a "+"
- powerups.push_back(newpowerup);
- }
- }
- void interactions(player& player1,b_list& bullets,b_list& bombs,e_list& enemies,w_vec& walls,b_list& powerups,r_list& rockets,boss& boss1,int& score){
- bool should_continue;
-
- b_list::iterator it1=bullets.begin();
- while(it1!=bullets.end())
- {
- should_continue = false;
-
- if(it1->y<=0)
- { //bullet reached the ceiling
- it1 = bullets.erase(it1);
- continue;
- }
-
- b_list::iterator it2 = bombs.begin();
- while(it2!=bombs.end())
- {
- if(it1->x==it2->x && (it1->y==it2->y || it1->y==it2->y+1)){ //bullet+bomb
- it1=bullets.erase(it1);
- should_continue = true;
- it2=bombs.erase(it2);
- score+=50;
- break;
- }
- else
- ++it2;
- }
- if(should_continue) continue;
-
- for(e_list::iterator it3=enemies.begin(); it3!=enemies.end(); ++it3)
- if(it1->x==it3->x && it1->y==it3->y && it3->alive){ //bullet+enemy
- it1=bullets.erase(it1);
- should_continue = true;
- it3->alive=false;
- if((double)rand()/RAND_MAX<(double)AVERAGE_DROP/ENEMY_NUM) //a powerup is dropped (AVERAGE_DROP is the mean number of powerups dropped per play in Normal mode)
- drop_powerup(it3->x,it3->y+1,powerups);
- score+=100;
- break;
- }
- if(should_continue) continue;
-
-
-
- if((it1->x>=boss1.x+boss1.width/2-2 && it1->x<=boss1.x+boss1.width/2+2) && boss1.alive){ //bullet+boss
- boss1.health--;
- if(it1->y==boss1.y) it1=bullets.erase(it1); //bullet gets destroyed only when has reached boss's top
- else
- {
- ++it1;
- continue;
- }
- //(to make the boss lose life more quickly)
- }
-
- ++it1;
- }
-
- r_list::iterator itr=rockets.begin();
- while(itr != rockets.end())
- {
- should_continue = false;
-
- if(itr->y<=0) //rocket reached the ceiling
- {
- itr=rockets.erase(itr);
- continue;
- }
-
- b_list::iterator it2=bombs.begin(); //bomb+rocket
- while(it2!=bombs.end())
- {
- if((itr->x==it2->x && itr->y==it2->y) || (itr->v>0 && (itr->x==it2->x-1 && itr->y==it2->y+1)) || (itr->v<0 && (itr->x==it2->x+1 && itr->y==it2->y+1))){ //rocket+bomb
- itr=rockets.erase(itr);
- should_continue = true;
- it2=bombs.erase(it2);
- score+=50;
- break;
- }
- else
- ++it2;
- }
- if(should_continue) continue;
-
- e_list::iterator it3 = enemies.begin();
- while(it3!=enemies.end())
- {
- if(itr->x==it3->x && itr->y==it3->y){ //rocket+enemy
- itr=rockets.erase(itr);
- it3=enemies.erase(it3);
- should_continue = true;
- break;
- }
- else
- ++it3;
- }
- if(should_continue) continue;
-
- if((itr->x>=boss1.x+boss1.width/2-2 && itr->x<=boss1.x+boss1.width/2+2) && boss1.alive) //rocket+boss
- {
- boss1.health--;
- if(itr->y==boss1.y)
- {
- itr=rockets.erase(itr);
- continue;
- }
- }
-
- ++itr;
- }
-
- b_list::iterator itb=bombs.begin();
- while(itb!=bombs.end()){
- if(itb->y>=R) //bomb reached the floor
- {
- itb=bombs.erase(itb);
- continue;
- }
-
- if(itb->x>=player1.x && itb->x<player1.x+player1.length && itb->y>player1.y) //bomb+player
- {
- itb=bombs.erase(itb);
- continue;
- }
-
- ++itb;
- }
-
- b_list::iterator itp=powerups.begin();
- while( itp!=powerups.end()){
- if(itp->y==player1.y && itp->x>=player1.x && itp->x<player1.x+player1.length){
- switch(itp->id){
- case 64:
- if(player1.weaponclass<MAX_WEAPONCLASS) player1.weaponclass++;
- break;
- case 128:
- if(player1.length>2){
- mvaddch(player1.y,player1.x+player1.length-1,' ');
- player1.length--;
- }
- break;
- case 512:
- if(!(player1.rocketlauncher)) player1.rocketlauncher=TRUE;
- else if(player1.weaponclassrkt<MAX_WEAPONCLASSRKT) player1.weaponclassrkt++;
- break;
- }
- itp=powerups.erase(itp);
- continue;
- }
-
- if(itp->y>=R) //powerup reached the floor
- {
- itp=powerups.erase(itp);
- continue;
- }
-
- ++itp;
- }
-
- for(int i=0;i<walls.size();i++)
- for(int j=0;j<walls[i].width;j++)
- for(int k=0;k<walls[i].height;k++)
- {
- if(walls[i].health[k][j]>0)
- {
- b_list::iterator it1=bullets.begin();
- while(it1!=bullets.end())
- {
- if(it1->x==walls[i].x+j && it1->y==walls[i].y+k){
- it1=bullets.erase(it1);
- walls[i].health[k][j]--;
- }
- else
- ++it1;
- }
-
- b_list::iterator it2=bombs.begin();
- while(it2!=bombs.end())
- {
- if(it2->x==walls[i].x+j && it2->y==walls[i].y+k){
- it2=bombs.erase(it2);
- walls[i].health[k][j]--;
- }
- else
- ++it2;
- }
-
- r_list::iterator it3=rockets.begin();
- while(it3!=rockets.end())
- {
- if(it3->x==walls[i].x+j && it3->y==walls[i].y+k){
- it3=rockets.erase(it3);
- walls[i].health[k][j]--;
- }
- else
- ++it3;
- }
-
- b_list::iterator it4=powerups.begin();
- while(it4!=powerups.end())
- {
- if(it4->x==walls[i].x+j && it4->y==walls[i].y+k){
- it4=powerups.erase(it4);
- }
- else
- ++it4;
- }
- }
- }
- }
- //********** SAVE FUNCTIONS *****************************
- void refreshrecords(int newscore,WINDOW* win)
- {
- r_vec records;
- std::string nick;
- record newrecord;
-
- std::string local_recpath;
- std::string local_dir=getenv("HOME"); //get environment variable $HOME
- local_dir+=RECORD_DIR; //we set local_dir and local_recpath: ~/.local/share/invaders/records.dat
- local_recpath=local_dir;
- local_recpath+=RECORD_FILE;
- wattron(win,COLOR_PAIR(5));
- mvwprintw(win,6,5,"Please insert your nick");
- mvwprintw(win,7,2,"(up to 5 characters): ");
- wattron(win,COLOR_PAIR(1));
- echo();
- char getnick[6];
- wscanw(win,"%s",getnick);
- nick=getnick;
- nick.resize(5,'.');
-
- noecho();
-
- std::ifstream recordin(local_recpath.c_str()); //first, check if local_recpath exists; if not, create it.
-
- if(recordin) //note that local_recpath is actually a symlink to global_recpath
- {
- recordin >> newrecord.nick;
- recordin >> newrecord.score;
- while(!recordin.eof())
- {
- records.push_back(newrecord);
- recordin >> newrecord.nick;
- recordin >> newrecord.score;
- }
- recordin.close();
-
- newrecord.score=newscore;
- newrecord.nick=nick;
-
- records.push_back(newrecord);
- sort(records.rbegin(), records.rend());
-
- wattron(win,COLOR_PAIR(6));
- mvwprintw(win,9,11,"TOP FIVE");
- wattron(win,COLOR_PAIR(0));
- int count=0;
- for(r_vec::iterator it=records.begin(); (it!=records.begin()+5 && it!=records.end()); ++it,++count)
- {
- wmove(win,10+count,9);
- it->printwin(win);
- }
-
- count=0;
- for(r_vec::iterator it=records.begin(); it!=records.end(); ++it)
- {
- count++;
- if(*it==newrecord)
- {
- mvwprintw(win,16,3,"You're number %i out of %i",count,records.size());
- break;
- }
- }
-
- std::ofstream recordout(local_recpath.c_str());
- for(r_vec::iterator it=records.begin(); it!=records.end(); ++it)
- it->print(recordout);
- recordout.close();
- }
- else //records.dat not found
- {
- //if local_recpath does not exist, then we must create the local_dir.
-
- recordin.close();
- recordin.clear();
-
- create_folder(0);
-
- //now we check if global_recpath exists.
-
- std::string global_recpath=GLOBAL_DIR; //global_recpath is GLOBAL_DIR/records.dat
- global_recpath+=RECORD_FILE;
-
- //we try to open GLOBAL_DIR/records.dat
- std::ifstream globalin(global_recpath.c_str());
-
- if(!globalin){
- globalin.close();
- globalin.clear();
- create_folder(1); //try to create global_dir (will fail if already
- std::ofstream globalout(global_recpath.c_str()); //exists) and save the global_recpath.
-
- globalout.close();
- globalout.clear();
-
- chmod(global_recpath.c_str(),S_IRWXG | S_IRWXU | S_IRWXO);
- }
-
- else{
- //just save to GLOBAL_DIR/records.dat
- globalin >> newrecord.nick;
- globalin >> newrecord.score;
- while(!globalin.eof())
- {
- records.push_back(newrecord);
- globalin >> newrecord.nick;
- globalin >> newrecord.score;
- }
- globalin.close();
-
-
- }
-
- //finally, create the symlink to global_recpath
-
- symlink(global_recpath.c_str(),local_recpath.c_str());
-
- newrecord.score=newscore;
- newrecord.nick=nick;
-
- records.push_back(newrecord);
- sort(records.rbegin(), records.rend());
-
- //and save there the records.
-
- std::ofstream recordout(local_recpath.c_str());
- for(r_vec::iterator it=records.begin(); it!=records.end(); ++it)
- it->print(recordout);
- recordout.close();
-
- }
- }
- void print_scores(){
- r_vec records;
- std::string recordpath;
- std::string record_dir=getenv("HOME");
- record_dir+=RECORD_DIR;
- recordpath=record_dir;
- recordpath+=RECORD_FILE;
- std::ifstream recordin(recordpath.c_str());
- record newrecord;
- int i=1;
-
-
- if(recordin){
- WINDOW *recs;
- recs=newwin(25,30,0,0);
- box(recs,ACS_VLINE,ACS_HLINE);
- wattron(recs,COLOR_PAIR(5));
- mvwprintw(recs,1,1,"======= HIGHSCORES =======");
- wattron(recs,COLOR_PAIR(0));
- recordin >> newrecord.nick;
- recordin >> newrecord.score;
- while(!recordin.eof() && i<21)
- {
- records.push_back(newrecord);
- recordin >> newrecord.nick;
- recordin >> newrecord.score;
- i++;
- }
- recordin.close();
-
- i=1;
-
- for(r_vec::iterator it=records.begin(); it!=records.end(); ++it){
- if(i<10)
- mvwprintw(recs,i+2,1,"0%i- ",i);
- else
- mvwprintw(recs,i+2,1,"%i- ",i);
- it->printwin(recs);
- i++;
- }
-
- wrefresh(recs);
- timeout(-1);
- if(getch()!=ERR){
- delwin(recs);
- erase();
- refresh();
- }
- }
- else{
- WINDOW *recs;
- std::string host=getenv("USER");
- recs=newwin(5,3+host.length()+record_dir.length(),5,5);
- box(recs,ACS_VLINE,ACS_HLINE);
- wattron(recs,COLOR_PAIR(5));
- mvwprintw(recs,1,1,"Save file:");
- mvwprintw(recs,2,1,"%s",record_dir.c_str());
- mvwprintw(recs,3,1,"%s not found.",RECORD_FILE);
- //mvwprintw(recs,4,1,"not found.");
- wrefresh(recs);
- timeout(-1);
- if(getch()!=ERR){
- delwin(recs);
- erase();
- refresh();
- }
- }
- }
- /*//=========== THREADS FUNCTIONS ==============================
- void *pmusic(void *arg){
- while(1){
- system("beep 330 -l 150 -n -f 1 -l 40 -n -f 494 -l 159 -n -f 1 -l 40 -n -f 660 -l 150 -n -f 1 -l 40 -n -f 590 -l 150 -n -f 660 -l 150 -n -f 494 -l 100 -n -f 494 -l 100 -n -f 523 -l 150 -n -f 1 -l 40 -n -f 440 -l 150 -n -f 1 -l 40 -n -f 494 -l 150 -n -f 1 -l 40 -n -f 392 -l 100 -n -f 392 -l 100 -n -f 440 -l 150 -n -f 370 -l 150 -n -f 1 -l 40 -n -f 392 -l 150 -n -f 1 -l 40 -n -f 330 -l 100 -n -f 330 -l 100 -n -f 370 -l 150 -n -f 1 -l 40 -n -f 294 -l 150 -n -f 1 -l 40 -n -f 330 -l 150 -n -f 247 -l 100 -n -f 247 -l 100 -n -f 261 -l 150 -n -f 1 -l 40 -n -f 311 -l 150 -n -f 1 -l 40 -n -f 330 -l 150 -n -f 1 -l 40 -n -f 247 -l 100 -n -f 247 -l 100 -n -f 262 -l 150 -n -f 1 -l 40 -n -f 370 -l 150 -n -f 1 -l 40 -n -f 330 -l 150 -n -f 1 -l 40 -n -f 494 -l 159 -n -f 1 -l 40 -n -f 660 -l 150 -n -f 1 -l 40 -n -f 590 -l 150 -n -f 660 -l 150 -n -f 494 -l 100 -n -f 494 -l 100 -n -f 523 -l 150 -n -f 1 -l 40 -n -f 440 -l 150 -n -f 1 -l 40 -n -f 494 -l 150 -n -f 1 -l 40 -n -f 392 -l 100 -n -f 392 -l 100 -n -f 440 -l 150 -n -f 370 -l 150 -n -f 1 -l 40 -n -f 392 -l 150 -n -f 1 -l 40 -n -f 330 -l 100 -n -f 330 -l 100 -n -f 370 -l 150 -n -f 1 -l 40 -n -f 294 -l 150 -n -f 1 -l 40 -n -f 330 -l 150 -n -f 247 -l 100 -n -f 247 -l 100 -n -f 261 -l 150 -n -f 1 -l 40 -n -f 311 -l 150 -n -f 1 -l 40 -n -f 330 -l 150 -n -f 1 -l 40 -n -f 247 -l 100 -n -f 247 -l 100 -n -f 262 -l 150 -n -f 1 -l 40 -n -f 370 -l 150 -n -f 1 -l 40 -n -f 330 -l 150 -n -f 1 -l 40 2> /dev/null");
- napms(2000);
- }
- }
- void *pshoot_sound(void *arg){
- system("beep -f 1000 -l 30 -n -f 850 -l 30 2> /dev/null");
- }
- void *penshoot_sound(void *arg){
- system("beep -f 1500 -l 30 -n -f 1000 -l 30 2> /dev/null");
- }
- void *pwin_theme(void *arg){
- system("beep -f 1000 -l 150 -n -f 800 -l 150 -n -f 1000 -l 150 -n -f 1500 -l 600 2> /dev/null");
- }
- void *plose_theme(void *arg){
- system("beep -f 700 -l 150 -n -f 585 -l 150 -n -f 550 -l 150 -n -f 520 -l 600 2> /dev/null");
- }
- void pkill_music(pthread_t thread){
- pthread_cancel(thread);
- system("beep -f 1 2> /dev/null");
- }*/
-
- //=========BOSSRUSH FUNCTIONS=======================
- int choose_level_bossrush(int* commands)
- {
- std::string phrase;
- phrase=choose_phrase();
- char input;
-
- do{
- print_title();
- print_catchphrase(phrase);
- attron(COLOR_PAIR(1));
- mvprintw(5,0,"Choose difficulty level:");
- attron(COLOR_PAIR(1));
- mvprintw(6,0,"1- Easy");
- attron(COLOR_PAIR(3));
- mvprintw(7,0,"2- Medium");
- attron(COLOR_PAIR(2));
- mvprintw(8,0,"3- Hard");
- attron(COLOR_PAIR(4));
- mvprintw(9,0,"4- Impossible");
- attron(COLOR_PAIR(5));
- mvprintw(10,0,"i- Info");
- attron(COLOR_PAIR(6));
- mvprintw(11,0,"h- Highscores");
- attron(COLOR_PAIR(1));
- mvprintw(12,0,"c- Commands");
- attron(COLOR_PAIR(2));
- mvprintw(13,0,"q- Quit");
-
- refresh();
- timeout(80);
-
- input=tolower(getch());
-
- if(!(input=='1' || input=='2' || input=='3' || input=='4' || input=='i' || input=='h' || input=='r' || input=='q') && input!=ERR){
- attron(COLOR_PAIR(2));
- mvprintw(14,0,"Bad input. Please choose a number within 1 and 4.");
- attron(COLOR_PAIR(0));
- }
-
- if(input=='i')
- print_info();
-
- else if(input=='h')
- print_scores_bossrush();
-
- else if(input=='r')
- phrase=choose_phrase();
-
- else if(input=='c')
- change_commands(commands);
-
- else if(input=='q'){
- endwin();
- cout<<lightgreen<<"Game exited correctly."<<none<<endl;
- exit(1);
- }
-
- }while(!(input=='1' || input=='2' || input=='3' || input=='4'));
-
- return input - '0';
- }
- void setup_level_bossrush(int level, double& shootr, int& refresh_t)
- {
- switch(level) //setting game parameters
- {
- case 1:
- {
- shootr = 0.006;
- refresh_t = (int)(0.09*1E3);
- }
- case 2:
- {
- shootr = 0.009;
- refresh_t = (int)(0.085*1E3);
- }
- case 3:
- {
- shootr = 0.015;
- refresh_t = (int)(0.08*1E3);
- }
- case 4:
- {
- shootr = 0.020;
- refresh_t = (int)(0.07*1E3);
- }
- }
-
- }
- void resetbosses(boss* Bosses,boss& boss1,player& player1){
- boss* bosses=Bosses;
- bosses->health=bosses->healthmax;
- bosses++;
- bosses->health=bosses->healthmax;
- bosses++;
- bosses->health=bosses->healthmax;
- bosses++;
- bosses->health=bosses->healthmax;
- bosses++;
- bosses->health=bosses->healthmax;
- boss1=*Bosses;
- boss1.alive=true;
- player1.weaponclass=1;
- }
- void Victory_bossrush(std::string name,int score,char level,int chflag){
- score+=1500*atoi(&level);
- WINDOW *victory;
- victory=newwin(19,32,7,17);
- box(victory,ACS_VLINE,ACS_HLINE);
- wattron(victory,COLOR_PAIR(1));
- mvwprintw(victory,2,5,"YOU DEFEATED %s!",name.c_str());
- wattron(victory,COLOR_PAIR(3));
- mvwprintw(victory,4,5,"+ %i pts!",atoi(&level)*1500);
- wattron(victory,COLOR_PAIR(1));
- mvwprintw(victory,4,5,"YOU WON!");
- wattron(victory,COLOR_PAIR(3));
- mvwprintw(victory,4,5,"Your score is: %i",score);
- wrefresh(victory);
-
- if(chflag==0)
- refreshrecords_bossrush(score,victory);
- else{
- wattron(victory,COLOR_PAIR(4));
- mvwprintw(victory,7,3,"I think you used cheats...");
- }
-
- wrefresh(victory);
-
- delwin(victory);
- }
- void refreshrecords_bossrush(int newscore,WINDOW* win)
- {
- r_vec records;
- std::string nick;
- record newrecord;
- std::string local_recpath;
- std::string local_dir=getenv("HOME"); //get environment variable $HOME
- local_dir+=RECORD_DIR; //we set local_dir and local_recpath: ~/.local/share/invaders/records_bossrush.dat
- local_recpath=local_dir;
- local_recpath+=RECORD_FILE_BOSSRUSH;
- wattron(win,COLOR_PAIR(5));
- mvwprintw(win,6,5,"Please insert your nick");
- mvwprintw(win,7,2,"(up to 5 characters): ");
- wattron(win,COLOR_PAIR(1));
- echo();
- char getnick[6];
- wscanw(win,"%s",getnick);
- nick=getnick;
- nick.resize(5,'.');
-
- noecho();
-
- std::ifstream recordin(local_recpath.c_str());
-
- if(recordin)
- {
- recordin >> newrecord.nick;
- recordin >> newrecord.score;
- while(!recordin.eof())
- {
- records.push_back(newrecord);
- recordin >> newrecord.nick;
- recordin >> newrecord.score;
- }
- recordin.close();
-
- newrecord.score=newscore;
- newrecord.nick=nick;
-
- records.push_back(newrecord);
- sort(records.rbegin(), records.rend());
-
- wattron(win,COLOR_PAIR(6));
- mvwprintw(win,9,11,"TOP FIVE");
- wattron(win,COLOR_PAIR(0));
- int count=0;
- for(r_vec::iterator it=records.begin(); (it!=records.begin()+5 && it!=records.end()); ++it,++count)
- {
- wmove(win,10+count,9);
- it->printwin(win);
- }
-
- count=0;
- for(r_vec::iterator it=records.begin(); it!=records.end(); ++it)
- {
- count++;
- if(*it==newrecord)
- {
- mvwprintw(win,16,3,"You're number %i out of %i",count,records.size());
- break;
- }
- }
-
- std::ofstream recordout(local_recpath.c_str());
- for(r_vec::iterator it=records.begin(); it!=records.end(); ++it)
- it->print(recordout);
- recordout.close();
- }
- else //records_bossrush.dat not found
- {
- recordin.close();
- recordin.clear();
-
- create_folder(0); //this will fail if local_dir already exists (possible if player played bossrush before).
-
- //now we check if global_recpath exists.
-
- std::string global_recpath=GLOBAL_DIR; //global_recpath is GLOBAL_DIR/records_bossrush.dat
- global_recpath+=RECORD_FILE_BOSSRUSH;
-
- //we try to open GLOBAL_DIR/records.dat
- std::ifstream globalin(global_recpath.c_str());
-
- if(!globalin){
- globalin.close();
- globalin.clear();
- create_folder(1); //try to create global_dir (will fail if already
- std::ofstream globalout(global_recpath.c_str()); //exists) and save the global_recpath.
-
- globalout.close();
- globalout.clear();
-
- chmod(global_recpath.c_str(),S_IRWXG | S_IRWXU | S_IRWXO);
- }
-
- else{
- //just save to GLOBAL_DIR/records_bossrush.dat
- globalin >> newrecord.nick;
- globalin >> newrecord.score;
- while(!globalin.eof())
- {
- records.push_back(newrecord);
- globalin >> newrecord.nick;
- globalin >> newrecord.score;
- }
- globalin.close();
-
-
- }
-
- //finally, create the symlink to global_recpath
-
- symlink(global_recpath.c_str(),local_recpath.c_str());
-
- newrecord.score=newscore;
- newrecord.nick=nick;
-
- records.push_back(newrecord);
- sort(records.rbegin(), records.rend());
-
- //and save there the records.
-
- std::ofstream recordout(local_recpath.c_str());
- for(r_vec::iterator it=records.begin(); it!=records.end(); ++it)
- it->print(recordout);
- recordout.close();
- }
- }
- void print_scores_bossrush(){
- r_vec records;
- std::string recordpath;
- std::string record_dir=getenv("HOME");
- record_dir+=RECORD_DIR;
- recordpath=record_dir;
- recordpath+=RECORD_FILE_BOSSRUSH;
- std::ifstream recordin(recordpath.c_str());
- record newrecord;
- int i=1;
-
-
- if(recordin){
- WINDOW *recs;
- recs=newwin(25,30,0,0);
- box(recs,ACS_VLINE,ACS_HLINE);
- wattron(recs,COLOR_PAIR(5));
- mvwprintw(recs,1,1,"======= HIGHSCORES =======");
- wattron(recs,COLOR_PAIR(0));
- recordin >> newrecord.nick;
- recordin >> newrecord.score;
- while(!recordin.eof() && i<21)
- {
- records.push_back(newrecord);
- recordin >> newrecord.nick;
- recordin >> newrecord.score;
- i++;
- }
- recordin.close();
-
- i=1;
-
- for(r_vec::iterator it=records.begin(); it!=records.end(); ++it){
- if(i<10)
- mvwprintw(recs,i+2,1,"0%i- ",i);
- else
- mvwprintw(recs,i+2,1,"%i- ",i);
- it->printwin(recs);
- i++;
- }
-
- wrefresh(recs);
- timeout(-1);
- if(getch()!=ERR){
- delwin(recs);
- erase();
- refresh();
- }
- }
- else{
- WINDOW *recs;
- std::string host=getenv("USER");
- recs=newwin(6,25+host.length(),5,5);
- box(recs,ACS_VLINE,ACS_HLINE);
- wattron(recs,COLOR_PAIR(5));
- mvwprintw(recs,1,1,"Save file:");
- mvwprintw(recs,2,1,"%s",record_dir.c_str());
- mvwprintw(recs,3,1,"%s",RECORD_FILE_BOSSRUSH);
- mvwprintw(recs,4,1,"not found.");
- wrefresh(recs);
- timeout(-1);
- if(getch()!=ERR){
- delwin(recs);
- erase();
- refresh();
- }
- }
- }
- void create_readme(){
- std::string recordpath;
- std::string record_dir=getenv("HOME");
- record_dir+=RECORD_DIR;
- recordpath=record_dir;
- recordpath+="README.txt";
- std::ifstream checkread(recordpath.c_str());
- if(!checkread){
- checkread.close();
- checkread.clear();
- std::ofstream readme(recordpath.c_str());
-
- readme<<"#==Space Invaders=="<<endl;
- readme<<"#"<<endl;
- readme<<"#Original game was created and developed by Giacomo Parolini and Enrico Guiraud (year 2010-2013)"<<endl;
- readme<<"#License: GNU GPL"<<endl;
- readme<<"#"<<endl;
- readme<<"#Game version logs:"<<endl;
- readme<<"# -invaders2.0"<<endl;
- readme<<"# -invaders2.1"<<endl;
- readme<<"# -invaders3.0"<<endl;
- readme<<"# -invaders3.1"<<endl;
- readme<<"# -invaders3.2"<<endl;
- readme<<"# -invaders3.3"<<endl;
- readme<<"# -invaders3.4"<<endl;
- readme<<"# -invaders3.5"<<endl;
- readme<<"# -invaders3.6"<<endl;
- readme<<"# -invaders4.0"<<endl;
- readme<<"# -invaders4.1"<<endl;
- readme<<"# -invaders4.2"<<endl;
- readme<<"# -invaders4.3"<<endl;
- readme<<"# -invaders4.3.5"<<endl;
- readme<<"# -curses_invaders2.0"<<endl;
- readme<<"# -curses_invaders3.0"<<endl;
- readme<<"# -curses_invaders3.5"<<endl;
- readme<<"# -curses_invaders4.0"<<endl;
- readme<<"# -curses_invaders4.1"<<endl;
- readme<<"# -curses_invaders4.2"<<endl;
- readme<<"# -curses_invaders4.3"<<endl;
- readme<<"# -curses_invaders4.4"<<endl;
- readme<<"# -GL_invaders"<<endl;
- readme<<"#"<<endl;
- readme<<"#All versions are written in C++. "<<endl;
- readme<<"#"<<endl;
- readme<<"#curses_invaders versions were developed by Giacomo Parolini and differ from the previous ones for exploiting library <ncurses.h> for graphic terminal management."<<endl;
- readme<<"#GL_invaders versions were developed by Enrico Guiraud and exploit GL libraries such as <glutm/window.h> and <glt/project.h> for graphics."<<endl;
- readme<<"#"<<endl;
- readme<<"#System Requirements:"<<endl;
- readme<<"#All versions are Linux x-executable files, so will only run within Linux environment."<<endl;
- readme<<"#However, files CAN be compiled also in MacOS environment."<<endl;
- readme<<"#"<<endl;
- readme<<"#curses_invaders versions require the library <ncurses.h> and may not work correctly on some terminals. Versions 3.0 and following also contain executable \"bossrush\"."<<endl;
- readme<<"#"<<endl;
- readme<<"#since v.4.3, game controls are customizable at the beginning of the game."<<endl;
- readme<<"#v.4.4 implements shared highscores."<<endl;
- readme<<"#"<<endl;
- readme<<"#"<<endl;
- readme<<"#CREATING AND EDITING BOSSES (at the moment unavailable: the game doesn't create the scripts automatically)"<<endl;
- readme<<"#curses_invaders4.0 and following also come with the scripts checkboss.sh and setboss.sh, written by Enrico Guiraud. These scripts are used to edit the five game bosses, allowing the player to create his custom bosses. "<<endl;
- readme<<"#In order to create a boss, follow these steps:"<<endl;
- readme<<"# *1: create a file named <custom_boss_name>.dat and open it with any text editor (like Gedit, Vim or Notepad)."<<endl;
- readme<<"# *2: \"draw\" your boss in ASCII-style. You don't have to pay attention of the rows' lengths, as checkboss.sh will fix them automatically. Just be sure the lines and the columns fit the game board (game board's dimensions are written in definitions.hpp as \"R\" and \"C\")."<<endl;
- readme<<"# *3: save your file.dat and launch either checkboss.sh (if you just want to adjust your boss's dimensions) or directly setboss.sh (to add the boss to the game). Syntax of checkboss.sh is \"./checkboss.sh <bossname.dat>\" or \"./checkboss <bossname>\". Syntax of setboss.sh is \"./setboss.sh <bossname> <level>\"; the script will substitute the boss of the <level>-th game level (1=easy, 2=medium, 3=hard, 4=impossible, 5=special boss) with your custom boss. Notice that setboss.sh needs you to pass the argument WITHOUT the \".dat\"."<<endl;
- readme<<"# *4: play to test your new boss! If you want to restore the original boss, just run setboss.sh once again passing the original boss as argument."<<endl;
- readme<<"#"<<endl;
- readme<<"#...and yes: cheats ARE available, just try and find them ;)"<<endl;
-
- readme.close();
- }
- }
- void create_std_bosses(){
- std::string bosspath;
- std::string boss_dir=getenv("HOME");
- boss_dir+=RECORD_DIR;
- bosspath=boss_dir;
- std::string bosspath1=bosspath+BOSS_FILE1+".dat";
- std::ifstream checkread(bosspath1.c_str());
- if(!checkread){
-
- std::string newrecordpath=boss_dir+RECORD_FILE; //check if .invaders/records.dat exists
- std::ifstream tryrecord(newrecordpath.c_str());
-
- if(!tryrecord){ //.invaders/records.dat does not exist, so create the .invaders directory
- tryrecord.close();
- tryrecord.clear();
-
- std::string newrecordpath2=boss_dir+RECORD_FILE_BOSSRUSH; //check if .invaders/records_bossrush.dat exists
- std::ifstream tryrecord2(newrecordpath2.c_str());
-
- if(!tryrecord2){
- tryrecord2.close();
- tryrecord2.clear();
- mkdir(boss_dir.c_str(),S_IRWXU|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH);
- create_readme();
- //create_setsize();
- //std::string setsizepath=boss_dir+"setsize.sh";
- //chmod(setsizepath.c_str(),S_IRWXU);
- }
- }
-
- checkread.close();
- checkread.clear();
- std::ofstream boss1(bosspath1.c_str());
-
- boss1<<" _____ "<<endl;
- boss1<<"// \\\\"<<endl;
- boss1<<"| 0 0 |"<<endl;
- boss1<<"c L P"<<endl;
- boss1<<"\\ MMM /"<<endl;
- boss1<<" \\_____/ "<<endl;
-
- boss1.close();
- boss1.clear();
-
- std::string bosspath2=bosspath+BOSS_FILE2+".dat";
- std::ofstream boss2(bosspath2.c_str());
-
- boss2<<" _______ "<<endl;
- boss2<<" W _ _ _ W "<<endl;
- boss2<<"|_|o| |o|_|"<<endl;
- boss2<<"c c P"<<endl;
- boss2<<" \\__NNN__/ "<<endl;
-
- boss2.close();
- boss2.clear();
-
- std::string bosspath3=bosspath+BOSS_FILE3+".dat";
- std::ofstream boss3(bosspath3.c_str());
-
- boss3<<" 99999 "<<endl;
- boss3<<" C \\ / D "<<endl;
- boss3<<"C 0 0 D"<<endl;
- boss3<<"C_ ^ _D"<<endl;
- boss3<<" | J | "<<endl;
- boss3<<" \\_/ "<<endl;
-
- boss3.close();
- boss3.clear();
-
- std::string bosspath4=bosspath+BOSS_FILE4+".dat";
- std::ofstream boss4(bosspath4.c_str());
-
- boss4<<" MMMMMMM "<<endl;
- boss4<<" M M "<<endl;
- boss4<<"| == == |"<<endl;
- boss4<<"c L D"<<endl;
- boss4<<" MMM MMM "<<endl;
- boss4<<" MMMMMMM "<<endl;
-
- boss4.close();
- boss4.clear();
-
- std::string bosspath5=bosspath+BOSS_FILE5+".dat";
- std::ofstream boss5(bosspath5.c_str());
-
- boss5<<" \\__ ______ __/ "<<endl;
- boss5<<"\\___ = = ___/ "<<endl;
- boss5<<" | | = = | | "<<endl;
- boss5<<" \\_ _/ "<<endl;
- boss5<<" / || \\ "<<endl;
-
- boss5.close();
- boss5.clear();
-
- }
- }
- /*void create_setsize(){
- std::string recordpath;
- std::string record_dir=getenv("HOME");
- record_dir+=RECORD_DIR;
- recordpath=record_dir;
- recordpath+="setsize.sh";
- std::ifstream checkread(recordpath.c_str());
- if(!checkread){
- checkread.close();
- checkread.clear();
- std::ofstream setsize(recordpath.c_str());
-
- setsize<<"#!/bin/bash"<<endl;
- setsize<<"DEFAULT_R=30"<<endl;
- setsize<<"DEFAULT_C=60"<<endl;
- setsize<<"R=0"<<endl;
- setsize<<"C=0"<<endl;
- setsize<<"INPUT_FILE=definitions.hpp"<<endl;
- setsize<<"trap \"rm $INPUT_FILE.tmp 2> /dev/null; exit -1\" SIGINT SIGTERM"<<endl;
- setsize<<"if [[ $1 == \"default\" ]]; then"<<endl;
- setsize<<"sed s/\"#define R [0-9]*\"/\"#define R $DEFAULT_R\"/ <$INPUT_FILE >$INPUT_FILE.tmp"<<endl;
- setsize<<"sed s/\"#define C [0-9]*\"/\"#define C $DEFAULT_C\"/ <$INPUT_FILE.tmp >$INPUT_FILE"<<endl;
- setsize<<"rm $INPUT_FILE.tmp"<<endl;
- setsize<<"make clean"<<endl;
- setsize<<"make -j4"<<endl;
- setsize<<"exit 0"<<endl;
- setsize<<"elif [[ $# -gt 0 ]]; then echo Usage: $0 \"[default]\"; exit 1"<<endl;
- setsize<<"else"<<endl;
- setsize<<"echo Insert new Row number \\(5-120\\)"<<endl;
- setsize<<"until [[ $R -ge 5 && $R -le 120 ]]; do"<<endl;
- setsize<<" read R"<<endl;
- setsize<<"if [[ $R -lt 5 || $R -gt 120 ]]; then echo Invalid size.; fi"<<endl;
- setsize<<"echo New R will be $R"<<endl;
- setsize<<"done"<<endl;
- setsize<<"echo Insert new Column number \\(5-120\\)"<<endl;
- setsize<<"until [[ $C -ge 5 && $C -le 120 ]]; do"<<endl;
- setsize<<" read C"<<endl;
- setsize<<"if [[ $C -lt 5 || $C -gt 120 ]]; then echo Invalid size.; fi"<<endl;
- setsize<<"echo New C will be $C"<<endl;
- setsize<<"done"<<endl;
- setsize<<"sed s/\"#define R [0-9]*\"/\"#define R $R\"/ <$INPUT_FILE >$INPUT_FILE.tmp"<<endl;
- setsize<<"sed s/\"#define C [0-9]*\"/\"#define C $C\"/ <$INPUT_FILE.tmp >$INPUT_FILE"<<endl;
- setsize<<"rm $INPUT_FILE.tmp"<<endl;
- setsize<<"make clean"<<endl;
- setsize<<"make -j4"<<endl;
- setsize<<"fi"<<endl;
-
- setsize.close();
- }
- }*/
- void create_folder(int flag){ //0: local, 1: global
- switch(flag){
- case 1:
- if(mkdir(GLOBAL_DIR,S_IRWXU|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH)==0){ //this will fail if local_dir already
- create_readme(); //exists (possible if player played
- create_std_bosses(); //bossrush before).
- //create_setsize();
- }
- break;
- default:
- {
- std::string local_dir=getenv("HOME");
- local_dir+=RECORD_DIR;
- if(mkdir(local_dir.c_str(),S_IRWXU|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH)==0){ //this will fail if local_dir already
- create_readme(); //exists (possible if player played
- create_std_bosses(); //bossrush before).
- //create_setsize();
- }
- }
- }
- }
|