functions.cpp 50 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983
  1. /* This file is part of Invaders.
  2. *
  3. * Copyright (C) 2020 LCM.
  4. * You may use, distribute and modify Invaders under the terms of the
  5. * GPLv3 license, available at <https://www.gnu.org/licenses/\>.
  6. */
  7. #include "functions.hpp"
  8. #include "definitions.hpp"
  9. using std::cout;
  10. using std::cin;
  11. using std::endl;
  12. using std::min;
  13. extern std::mutex m_bullets;
  14. //========RECORD'S METHODS===========
  15. void record::print()
  16. {
  17. printw("%s\t%i\n",nick.c_str(),score);
  18. }
  19. void record::printwin(WINDOW* win)
  20. {
  21. wprintw(win,"%s.......%i",nick.c_str(),score);
  22. }
  23. void record::print(std::ofstream& recordfile)
  24. {
  25. if(!recordfile.is_open())
  26. {
  27. printw("error: record file is not open ");
  28. return;
  29. }
  30. recordfile << std::setw(6) << std::left << nick << " " << score << endl;
  31. }
  32. //=======INDIPENDENT FUNCTIONS=========
  33. void print_title() //CAPITAL LETTERS TITLE!
  34. {
  35. attron(COLOR_PAIR(5));
  36. mvprintw(0,0," __ _ _ _ _ _ _ _ _ _ __");
  37. mvprintw(1,0,"__// |_ |_| |_| | |_ | |\\ | | | |_| |\\ |_ |_| |_ \\\\__");
  38. mvprintw(2,0," \\\\__ _| | | | |_ |_ | | \\| \\/ | | |/ |_ |\\ _| __//");
  39. attron(COLOR_PAIR(0));
  40. }
  41. void print_catchphrase(std::string phrase){
  42. static int head=60;
  43. static int tail=60;
  44. phrase.resize(59,' ');
  45. attron(COLOR_PAIR(4));
  46. if(head<1){
  47. head=60;
  48. }
  49. if(head<tail){
  50. move(3,0);
  51. clrtoeol();
  52. mvprintw(3,head,phrase.c_str());
  53. tail=(head+phrase.size()<60?head+phrase.size():60);
  54. }
  55. else{
  56. move(3,0);
  57. clrtoeol();
  58. for(unsigned int i=0;i<abs(phrase.size()-(60-head));i++)
  59. mvaddch(3,i,phrase[60-head+i]);
  60. for(unsigned int i=head;i<60;i++)
  61. mvaddch(3,i,phrase[i-head]);
  62. tail=(phrase.size()-(60-head)>0?phrase.size()-(60-head):60);
  63. }
  64. head--;
  65. }
  66. std::string choose_phrase(){
  67. std::string phr;
  68. int r=(int)(23*(double)rand()/RAND_MAX);
  69. switch(r){
  70. case 0:
  71. phr="Now with cheats!";
  72. break;
  73. case 1:
  74. phr="20 percent cooler than your 3D-graphics games!";
  75. break;
  76. case 2:
  77. phr="NCurses rulez!";
  78. break;
  79. case 3:
  80. phr="References to people and events are purely random.";
  81. break;
  82. case 4:
  83. phr=">>> Crush <<< Kill >>> Destroy <<< SWAG >>>";
  84. break;
  85. case 5:
  86. phr="Also try Apocalypse 2.0!";
  87. break;
  88. case 6:
  89. phr=")]=>---- # ASCII IS ART!!! # ----<=[(";
  90. break;
  91. case 7:
  92. phr="Still a better program than Isgro's ones...";
  93. break;
  94. case 8:
  95. phr="<< Oh, sul serio, minchia raga, ne'? >>";
  96. break;
  97. case 9:
  98. phr="Cool game, bro, but needs more ponies.";
  99. break;
  100. case 10:
  101. phr="Hit R!!!";
  102. break;
  103. case 11:
  104. phr="Konami Code is still fashion.";
  105. break;
  106. case 12:
  107. phr="Almost completely debugged!";
  108. break;
  109. case 13:
  110. phr="(((( Kill them with WUBZ! ))))";
  111. break;
  112. case 14:
  113. phr="choose_phrase() is best function.";
  114. break;
  115. case 15:
  116. phr="zsh: segmentation fault (core dumped) invaders";
  117. break;
  118. case 16:
  119. phr="Aliens! Aliens everywhere!";
  120. break;
  121. case 17:
  122. phr="Breaks the fourth wall! Breaks the fourth wall!";
  123. break;
  124. case 18:
  125. phr="Them customizable controls!!!";
  126. break;
  127. case 19:
  128. phr=">~~~~~~~~~~~~~~~~~~~~~< You shall be assimilated into Linux.";
  129. break;
  130. case 20:
  131. phr="Did you try inv_bossrush?";
  132. break;
  133. case 21:
  134. phr="Keep calm and FUCKIN' KILL ALIENS.";
  135. break;
  136. case 22:
  137. phr="Read the fucking `man` page!!!";
  138. break;
  139. }
  140. return phr;
  141. }
  142. void print_info(){
  143. WINDOW *info;
  144. info=newwin(20,80,0,0);
  145. box(info,ACS_VLINE,ACS_HLINE);
  146. mvwprintw(info,0,1,"Welcome to Space Invaders!");
  147. wattron(info,COLOR_PAIR(1));
  148. mvwprintw(info,2,1,"About the Game:");
  149. wattron(info,COLOR_PAIR(6));
  150. mvwprintw(info,3,1,"This is a Terminal-playable game where you control a moving cannon base");
  151. mvwprintw(info,4,1,"(");
  152. wattron(info,COLOR_PAIR(1));
  153. wprintw(info,"========");
  154. wattron(info,COLOR_PAIR(6));
  155. wprintw(info,") with the aim to destroy all the enemy Space Invaders");
  156. mvwprintw(info,5,1,"(");
  157. wattron(info,COLOR_PAIR(2));
  158. wprintw(info,"#");
  159. wattron(info,COLOR_PAIR(6));
  160. wprintw(info,") assaulting the Earth. After killing all the Space Invaders, you shall face");
  161. mvwprintw(info,6,1,"the Big Alien Boss, the most powerful of all the alien enemies.");
  162. mvwprintw(info,7,1,"During the game, mind the Powerups (");
  163. wattron(info,COLOR_PAIR(3));
  164. wprintw(info,"+");
  165. wattron(info,COLOR_PAIR(6));
  166. wprintw(info,",");
  167. wattron(info,COLOR_PAIR(1));
  168. wprintw(info,"-");
  169. wattron(info,COLOR_PAIR(6));
  170. wprintw(info,",");
  171. wattron(info,COLOR_PAIR(4));
  172. wprintw(info,"X");
  173. wattron(info,COLOR_PAIR(6));
  174. wprintw(info,") that will increase");
  175. mvwprintw(info,8,1,"your shooting power if you manage to get them.");
  176. wattron(info,COLOR_PAIR(1));
  177. mvwprintw(info,10,1,"Default Controls: ");
  178. wattron(info,COLOR_PAIR(5));
  179. mvwprintw(info,11,1,"<- ");
  180. wattron(info,COLOR_PAIR(6));
  181. wprintw(info," A or Left Arrow");
  182. wattron(info,COLOR_PAIR(5));
  183. mvwprintw(info,12,1,"-> ");
  184. wattron(info,COLOR_PAIR(6));
  185. wprintw(info," D or Right Arrow");
  186. wattron(info,COLOR_PAIR(5));
  187. mvwprintw(info,13,1,"shoot ");
  188. wattron(info,COLOR_PAIR(6));
  189. wprintw(info," spacebar");
  190. wattron(info,COLOR_PAIR(3));
  191. mvwprintw(info,15,10,"Game developed by Giacomo Parolini & Enrico Guiraud (v.4.4)");
  192. wattron(info,COLOR_PAIR(6));
  193. mvwprintw(info,17,14,"Click any key to return to the Start Menu");
  194. wattron(info,COLOR_PAIR(0));
  195. wrefresh(info);
  196. timeout(-1);
  197. if(getch()!=ERR){
  198. delwin(info);
  199. erase();
  200. refresh();
  201. }
  202. }
  203. int choose_level(int* commands)
  204. {
  205. std::string phrase;
  206. phrase=choose_phrase();
  207. char input;
  208. do{
  209. print_title();
  210. print_catchphrase(phrase);
  211. attron(COLOR_PAIR(1));
  212. mvprintw(5,0,"Choose difficulty level:");
  213. attron(COLOR_PAIR(1));
  214. mvprintw(6,0,"1- Easy");
  215. attron(COLOR_PAIR(3));
  216. mvprintw(7,0,"2- Medium");
  217. attron(COLOR_PAIR(2));
  218. mvprintw(8,0,"3- Hard");
  219. attron(COLOR_PAIR(4));
  220. mvprintw(9,0,"4- Impossible");
  221. attron(COLOR_PAIR(5));
  222. mvprintw(10,0,"i- Info");
  223. attron(COLOR_PAIR(6));
  224. mvprintw(11,0,"h- Highscores");
  225. attron(COLOR_PAIR(1));
  226. mvprintw(12,0,"c- Commands");
  227. attron(COLOR_PAIR(2));
  228. mvprintw(13,0,"q- Quit");
  229. refresh();
  230. timeout(80);
  231. input=tolower(getch());
  232. if(!(input=='1' || input=='2' || input=='3' || input=='4' || input=='i' || input=='h' || input=='r' || input =='q') && input!=ERR){
  233. attron(COLOR_PAIR(2));
  234. mvprintw(14,0,"Bad input. Please choose a number within 1 and 4.");
  235. attron(COLOR_PAIR(0));
  236. }
  237. if(input=='i')
  238. print_info();
  239. if(input=='h')
  240. print_scores();
  241. if(input=='r')
  242. phrase=choose_phrase();
  243. if(input=='c')
  244. change_commands(commands);
  245. if(input=='q'){
  246. endwin();
  247. cout<<lightgreen<<"Game exited correctly."<<none<<endl;
  248. exit(1);
  249. }
  250. }while(!(input=='1' || input=='2' || input=='3' || input=='4'));
  251. create_folder(0);
  252. return input - '0';
  253. }
  254. void setup_level(int level, double& shootr, double& poweruprate, int& refresh_t, boss& boss1, int& enemy_num, int& walls_num)
  255. {
  256. switch(level) //setting game parameters
  257. {
  258. case 1:
  259. {
  260. shootr = 0.006;
  261. refresh_t = (int)(0.09*1E3);
  262. boss bosslvl1(1,1,100,9,6,BOSS_FILE1);
  263. boss1 = bosslvl1;
  264. std::string bossname=getenv("HOME");
  265. bossname=bossname+RECORD_DIR+BOSS_FILE1+".dat";
  266. boss1.loadpicture(bossname.c_str());
  267. enemy_num=30;
  268. walls_num = 3;
  269. break;
  270. }
  271. case 2:
  272. {
  273. shootr = 0.009;
  274. refresh_t = (int)(0.09*1E3);
  275. boss bosslvl2(1,1,200,11,5,BOSS_FILE2);
  276. boss1 = bosslvl2;
  277. std::string bossname=getenv("HOME");
  278. bossname=bossname+RECORD_DIR+BOSS_FILE2+".dat";
  279. boss1.loadpicture(bossname.c_str());
  280. enemy_num=40;
  281. walls_num = 2;
  282. break;
  283. }
  284. case 3:
  285. {
  286. shootr = 0.015;
  287. refresh_t = (int)(0.08*1E3);
  288. boss bosslvl3(1,1,300,9,6,BOSS_FILE3);
  289. boss1 = bosslvl3;
  290. std::string bossname=getenv("HOME");
  291. bossname=bossname+RECORD_DIR+BOSS_FILE3+".dat";
  292. boss1.loadpicture(bossname.c_str());
  293. enemy_num=45;
  294. walls_num = 2;
  295. break;
  296. }
  297. case 4:
  298. {
  299. shootr = 0.020;
  300. refresh_t = (int)(0.07*1E3);
  301. boss bosslvl4(1,1,400,11,6,BOSS_FILE4);
  302. boss1 = bosslvl4;
  303. std::string bossname=getenv("HOME");
  304. bossname=bossname+RECORD_DIR+BOSS_FILE4+".dat";
  305. boss1.loadpicture(bossname.c_str());
  306. enemy_num=50;
  307. walls_num = 2;
  308. break;
  309. }
  310. }
  311. poweruprate = (double)AVERAGE_DROP/enemy_num;
  312. }
  313. void change_commands(int* commands){
  314. WINDOW *change;
  315. int cmd;
  316. int line=0;
  317. std::string chcommands[CMD_NUM];
  318. std::string name[CMD_NUM]={"Key Left","Key Right","Key Up","Key Down","Pause","Shoot 1","Shoot 2","Mute/Unmute","Quit"};
  319. change=newwin(CMD_NUM+10,20,0,0);
  320. keypad(change,true);
  321. box(change,ACS_VLINE,ACS_HLINE);
  322. wattron(change,COLOR_PAIR(5));
  323. mvwprintw(change,1,2,"CHANGE CONTROLS");
  324. mvwprintw(change,3,2,"(X) to exit");
  325. do{
  326. for(int i=0;i<CMD_NUM;i++){
  327. if(commands[i]==' ') chcommands[i]="Barra sp.";
  328. else chcommands[i]=commands[i];
  329. if(i==line) wattron(change,COLOR_PAIR(9));
  330. mvwprintw(change,i+5,1,"%s: %s",name[i].c_str(),chcommands[i].c_str());
  331. wattron(change,COLOR_PAIR(1));
  332. }
  333. timeout(0);
  334. cmd=wgetch(change);
  335. switch(cmd){
  336. case KEY_DOWN:
  337. case 's':
  338. if(line<CMD_NUM-1) line++;
  339. break;
  340. case KEY_UP:
  341. case 'w':
  342. if(line>0) line--;
  343. break;
  344. case 'X':
  345. delwin(change);
  346. erase();
  347. refresh();
  348. return;
  349. case '\n':
  350. wattron(change,COLOR_PAIR(2));
  351. mvwprintw(change,line+5,1,"%s: %s",name[line].c_str(),chcommands[line].c_str());
  352. wrefresh(change);
  353. timeout(-1);
  354. cmd=wgetch(change);
  355. commands[line]=tolower(cmd);
  356. wmove(change,line+5,0);
  357. wclrtoeol(change);
  358. box(change,ACS_VLINE,ACS_HLINE);
  359. wattron(change,COLOR_PAIR(1));
  360. break;
  361. }
  362. wrefresh(change);
  363. }while(cmd!='X');
  364. delwin(change);
  365. erase();
  366. refresh();
  367. return;
  368. }
  369. void load_enemies(e_list& enemies,int enemy_num)
  370. {
  371. enemies.clear();
  372. for(int i=0; i<enemy_num; ++i) //loading enemies
  373. {
  374. static int c=0, r=0;
  375. if(c>C-1)
  376. {
  377. c=0;
  378. r++;
  379. if(r>R-2) break;
  380. }
  381. enemy newenemy(c++,r);
  382. if(newenemy.y%2!=0) newenemy.direction=1;
  383. enemies.push_back(newenemy);
  384. }
  385. }
  386. char playagain(WINDOW* replay)
  387. {
  388. char answer;
  389. do
  390. {
  391. wattron(replay,COLOR_PAIR(2));
  392. mvwprintw(replay,1,3,"Play again? [y/n/q] ");
  393. answer=tolower(wgetch(replay));
  394. }while(answer!='y' && answer!='n' && answer!='q');
  395. return answer;
  396. }
  397. bool gameover(player& _player,b_list& bombs) //Checks if a bomb/enemy/boss hit the player.
  398. {
  399. for(b_list::iterator it=bombs.begin(); it!=bombs.end(); ++it)
  400. if((it->x>=_player.x && it->x<_player.x+_player.length) && (it->y==_player.y) && !_player.godmode)
  401. return true;
  402. return false;
  403. }
  404. void reset(player& _player, e_list &enemies, boss& boss1, b_list& bullets, b_list& bombs, w_vec& walls, int walls_num, b_list& powerups, r_list& rockets, int& chflag)
  405. {
  406. _player.x=C/2; //resetting initial position and stats of player1
  407. _player.y=R-1;
  408. _player.weaponclass=0;
  409. _player.length=8;
  410. _player.rocketlauncher=FALSE;
  411. int c=0, r=0;
  412. e_list::const_iterator e_end = enemies.end();
  413. for(e_list::iterator it = enemies.begin(); it!=e_end; ++it) //resetting all the enemies
  414. {
  415. if(c==C)
  416. {
  417. c=0;
  418. r++;
  419. }
  420. it->alive = true;
  421. it->x=c++;
  422. it->y=r;
  423. if(it->y%2==0) it->direction=0;
  424. else it->direction=1;
  425. }
  426. boss1.alive = false;
  427. bullets.clear(); //destroying all bullets/bombs/etc
  428. bombs.clear();
  429. powerups.clear();
  430. rockets.clear();
  431. chflag=0;
  432. walls.resize(walls_num);
  433. for(int i=0;i<walls_num;i++)
  434. walls[i].create((i+1)*(C/(3*walls_num+1))+i*(2*C/(3*walls_num+1)),2*R/3,(int)min(6,2*C/(3*(int)(walls_num)+1)),2,5);
  435. }
  436. bool enemyalive(e_list &enemies)
  437. {
  438. bool isalive = false;
  439. e_list::const_iterator end = enemies.end();
  440. for(e_list::iterator it=enemies.begin(); it!=end; ++it)
  441. if(it->alive==true)
  442. {
  443. isalive = true;
  444. break;
  445. }
  446. return isalive;
  447. }
  448. bool operator<(record record1, record record2)
  449. {
  450. if(record1.score<record2.score) return true;
  451. else return false;
  452. }
  453. void pause_game(int& chflag){
  454. WINDOW *pause;
  455. char c;
  456. std::string cheat;
  457. char Cheat[7];
  458. pause=newwin(5,17,15,15);
  459. box(pause,ACS_VLINE,ACS_HLINE);
  460. wattron(pause,COLOR_PAIR(5));
  461. mvwprintw(pause,1,3,"GAME PAUSED");
  462. mvwprintw(pause,3,2,"(P) to resume");
  463. do{
  464. c=tolower(wgetch(pause));
  465. if(c=='c'){ //cheats!
  466. nocbreak();
  467. wmove(pause,2,5);
  468. echo();
  469. wattron(pause,COLOR_PAIR(4));
  470. wscanw(pause,"%s",Cheat);
  471. cbreak();
  472. noecho();
  473. if(std::strcmp(Cheat,CHEAT_CODE)==0){
  474. if(chflag==0)
  475. chflag=1;
  476. if(chflag==2)
  477. chflag=3;
  478. waddch(pause,'\a');
  479. }
  480. delwin(pause);
  481. erase();
  482. refresh();
  483. return;
  484. }
  485. else if(c=='p'){
  486. delwin(pause);
  487. erase();
  488. refresh();
  489. return;
  490. }
  491. }while(c!='c' && c!='p');
  492. }
  493. void write_score(WINDOW* Score,int score){
  494. wattron(Score,COLOR_PAIR(6));
  495. mvwprintw(Score,1,2,"SCORE");
  496. wattron(Score,COLOR_PAIR(1));
  497. mvwprintw(Score,2,2,"%i",score);
  498. }
  499. void write_bosshp(WINDOW* BossHP,int hp,int hpmax,std::string name){
  500. wattron(BossHP,COLOR_PAIR(3));
  501. mvwprintw(BossHP,0,1,"%s",name.c_str()); //writes boss's name
  502. wattron(BossHP,COLOR_PAIR(8));
  503. int i=0;
  504. for(i=0;i<(int)(15*(double)(hp)/hpmax);i++) mvwaddch(BossHP,1,i,' '); //draws green spaces
  505. i++;
  506. wattron(BossHP,COLOR_PAIR(7));
  507. for(;i<15;i++) mvwaddch(BossHP,1,i,' '); //draws red spaces (total spaces are 15)
  508. if((double)hp/hpmax>.5)
  509. wattron(BossHP,COLOR_PAIR(1));
  510. else if((double)hp/hpmax>.25)
  511. wattron(BossHP,COLOR_PAIR(3));
  512. else
  513. wattron(BossHP,COLOR_PAIR(2));
  514. wmove(BossHP,2,0);
  515. wdeleteln(BossHP);
  516. mvwprintw(BossHP,2,0,"%i / %i HP",hp,hpmax); //writes boss's hp / hpmax (color depends on hp/hpmax ratio)
  517. }
  518. void Victory(std::string name,int score,int level,int chflag){
  519. score+=1500*level;
  520. WINDOW *victory;
  521. victory=newwin(19,32,7,17);
  522. box(victory,ACS_VLINE,ACS_HLINE);
  523. wattron(victory,COLOR_PAIR(1));
  524. mvwprintw(victory,2,4,"YOU DEFEATED %s!",name.c_str());
  525. wattron(victory,COLOR_PAIR(3));
  526. mvwprintw(victory,4,5,"+ %i pts!",level*1500);
  527. wattron(victory,COLOR_PAIR(1));
  528. mvwprintw(victory,4,5,"YOU WON!");
  529. wattron(victory,COLOR_PAIR(3));
  530. mvwprintw(victory,4,5,"Your score is: %i",score);
  531. wrefresh(victory);
  532. if(chflag==0)
  533. refreshrecords(score,victory);
  534. else{
  535. wattron(victory,COLOR_PAIR(4));
  536. mvwprintw(victory,7,3,"I think you used cheats...");
  537. }
  538. wrefresh(victory);
  539. delwin(victory);
  540. }
  541. void Defeat(int score){
  542. WINDOW *defeat;
  543. defeat=newwin(10,32,7,17);
  544. box(defeat,ACS_VLINE,ACS_HLINE);
  545. wattron(defeat,COLOR_PAIR(2));
  546. mvwprintw(defeat,3,10,"GAME OVER!");
  547. wattron(defeat,COLOR_PAIR(3));
  548. mvwprintw(defeat,6,4,"Your score was: %i pts!",score);
  549. wrefresh(defeat);
  550. delwin(defeat);
  551. }
  552. void get_SpecialMode(int c,int& chflag,boss& boss1,e_list& enemies,b_list& bullets,b_list& bombs,r_list& rockets){
  553. if(get_KonamiCode(c)){
  554. activate_combo(boss1,enemies,bullets,bombs,rockets);
  555. if(chflag==0)
  556. chflag=2;
  557. if(chflag==1)
  558. chflag=3;
  559. }
  560. }
  561. bool get_KonamiCode(int c){ //UP,UP,DOWN,DOWN,LEFT,RIGHT,LEFT,RIGHT,B,A
  562. static int combo=0;
  563. switch(combo){
  564. case 0:
  565. case 1:
  566. if(c==KEY_UP) combo++;
  567. else if(c!=ERR) combo=0;
  568. break;
  569. case 2:
  570. case 3:
  571. if(c==KEY_DOWN) combo++;
  572. else if(c!=ERR) combo=0;
  573. break;
  574. case 4:
  575. case 6:
  576. if(c==KEY_LEFT) combo++;
  577. else if(c!=ERR) combo=0;
  578. break;
  579. case 5:
  580. case 7:
  581. if(c==KEY_RIGHT) combo++;
  582. else if(c!=ERR) combo=0;
  583. break;
  584. case 8:
  585. if(c=='b') combo++;
  586. else if(c!=ERR) combo=0;
  587. break;
  588. case 9:
  589. if(c=='a'){
  590. return true;
  591. }
  592. else if(c!=ERR) combo=0;
  593. break;
  594. }
  595. return false;
  596. }
  597. void get_cheat(char command,player& player1,e_list& enemies,double& shootrate){
  598. switch(command){
  599. case '+':
  600. if(player1.weaponclass<5) player1.weaponclass++; //increase weaponclass
  601. break;
  602. case '\\':
  603. if(player1.weaponclass>1) player1.weaponclass--; //decrease weaponclass
  604. break;
  605. case '-':
  606. if(player1.length>2){ //decrease size
  607. mvaddch(player1.y,player1.x+player1.length-1,' ');
  608. player1.length--;
  609. }
  610. break;
  611. case '*':
  612. if(player1.length<MAX_LENGTH) player1.length++; //increase size
  613. break;
  614. case 'n': //nuke enemies
  615. for(e_list::iterator it=enemies.begin();it!=enemies.end();it++){
  616. it->alive=false;
  617. mvaddch(it->y,it->x,' ');
  618. }
  619. break;
  620. case 'r': //activate rocketlauncher and increase weaponclassrkt
  621. if(!(player1.rocketlauncher)) player1.rocketlauncher=TRUE;
  622. else if(player1.weaponclassrkt<MAX_WEAPONCLASS) player1.weaponclassrkt++;
  623. break;
  624. case 'e': //decrease weaponclassrkt and deactivate rocketlauncher
  625. if(player1.weaponclassrkt>0) player1.weaponclassrkt--;
  626. else player1.rocketlauncher=FALSE;
  627. break;
  628. case 'u': //trigger uber mode
  629. player1.rocketlauncher=TRUE;
  630. player1.weaponclass=MAX_WEAPONCLASS;
  631. player1.weaponclassrkt=MAX_WEAPONCLASSRKT;
  632. player1.length=MAX_LENGTH;
  633. break;
  634. case 'g': //trigger/untrigger godmode
  635. player1.godmode=!player1.godmode;
  636. break;
  637. case 'f': //freeze: enemies won't shoot.
  638. shootrate=0;
  639. break;
  640. }
  641. }
  642. void activate_combo(boss& boss1,e_list& enemies,b_list& bullets,b_list& bombs,r_list& rockets){ //if the player performs the Konami Code, the normal boss is replaced with BIG_ALIENBOSS
  643. boss bossextra(1,1,500,17,5,BOSS_FILE5);
  644. boss1 = bossextra;
  645. std::string bossname=getenv("HOME");
  646. bossname=bossname+RECORD_DIR+BOSS_FILE5+".dat";
  647. boss1.loadpicture(bossname.c_str());
  648. for(int i=0;i<7;){
  649. attron(COLOR_PAIR(i++));
  650. mvprintw(15,15,"SPECIAL MODE ACTIVATED!!!");
  651. refresh();
  652. napms(150);
  653. }
  654. attron(COLOR_PAIR(0));
  655. napms(1500);
  656. for(e_list::iterator i=enemies.begin();i!=enemies.end();i++)
  657. i->alive=false;
  658. bullets.clear();
  659. bombs.clear();
  660. rockets.clear();
  661. erase();
  662. }
  663. //=============BOX'S REPLACEMENT FUNCTIONS============================
  664. void draw(player& player1,b_list& bullets,b_list& bombs,e_list& enemies,w_vec& walls,b_list& powerups,r_list& rockets,boss& boss1){
  665. struct point
  666. {
  667. point() { x=y=0; }
  668. point(int X, int Y) { x=X; y=Y; }
  669. int x,y;
  670. };
  671. static std::vector<point> bullets_p, enemies_p, bombs_p, powerups_p, rockets_p, walls_p, player_p, boss_p;
  672. //REDRAW BULLETS
  673. for(auto bullet_p: bullets_p)
  674. mvaddch(bullet_p.y,bullet_p.x,' ');
  675. bullets_p.clear();
  676. attron(COLOR_PAIR(3));
  677. for(auto bullet: bullets)
  678. {
  679. mvaddch(bullet.y,bullet.x,BULLET_SPRITE);
  680. bullets_p.push_back(point(bullet.x,bullet.y));
  681. }
  682. //REDRAW BOMBS
  683. for(auto bomb_p: bombs_p)
  684. mvaddch(bomb_p.y,bomb_p.x,' ');
  685. bombs_p.clear();
  686. attron(COLOR_PAIR(6));
  687. for(auto bomb: bombs)
  688. {
  689. //if(bomb.y<R)
  690. mvaddch(bomb.y,bomb.x,BOMB_SPRITE);
  691. bombs_p.push_back(point(bomb.x,bomb.y));
  692. }
  693. //REDRAW ENEMIES
  694. for(auto enemy_p: enemies_p)
  695. mvaddch(enemy_p.y,enemy_p.x,' ');
  696. enemies_p.clear();
  697. attron(COLOR_PAIR(2));
  698. for(auto enemy: enemies)
  699. {
  700. if(enemy.alive==true)
  701. {
  702. mvaddch(enemy.y,enemy.x,ENEMY_SPRITE);
  703. enemies_p.push_back(point(enemy.x,enemy.y));
  704. }
  705. }
  706. //REDRAW ROCKETS
  707. for(auto rocket_p: rockets_p)
  708. mvaddch(rocket_p.y,rocket_p.x,' ');
  709. rockets_p.clear();
  710. attron(COLOR_PAIR(3));
  711. for(auto rocket: rockets)
  712. {
  713. mvaddch(rocket.y,rocket.x,ROCKET_SPRITE);
  714. rockets_p.push_back(point(rocket.x,rocket.y));
  715. }
  716. //REDRAW POWERUPS
  717. for(auto powerup_p: powerups_p)
  718. mvaddch(powerup_p.y,powerup_p.x,' ');
  719. powerups_p.clear();
  720. for(auto powerup: powerups){
  721. if(powerup.id == 64){
  722. attron(COLOR_PAIR(3));
  723. mvaddch(powerup.y,powerup.x,POWERUP1_SPRITE);
  724. }
  725. else if(powerup.id == 128){
  726. attron(COLOR_PAIR(1));
  727. mvaddch(powerup.y,powerup.x,POWERUP2_SPRITE);
  728. }
  729. else{
  730. attron(COLOR_PAIR(4));
  731. mvaddch(powerup.y,powerup.x,POWERUP3_SPRITE);
  732. }
  733. powerups_p.push_back(point(powerup.x,powerup.y));
  734. }
  735. //REDRAW BOSS
  736. for(auto boss_point: boss_p)
  737. mvaddch(boss_point.y,boss_point.x,' ');
  738. boss_p.clear();
  739. if(boss1.alive)
  740. for(int i=boss1.y; i<boss1.y+boss1.height; i++)
  741. for(int j=boss1.x; j<boss1.x+boss1.width; j++){
  742. if(boss1.health>.5*boss1.healthmax)
  743. attron(COLOR_PAIR(5));
  744. else if(boss1.health>.25*boss1.healthmax)
  745. attron(COLOR_PAIR(3));
  746. else
  747. attron(COLOR_PAIR(2));
  748. mvaddch(i,j,boss1.picture[i-boss1.y][j-boss1.x]);
  749. boss_p.push_back(point(j,i));
  750. }
  751. //REDRAW PLAYER
  752. for(auto play_point: player_p)
  753. mvaddch(play_point.y,play_point.x,' ');
  754. player_p.clear();
  755. if(player1.godmode) attron(COLOR_PAIR(5));
  756. else attron(COLOR_PAIR(1));
  757. for(int i=player1.x;i<player1.x+player1.length;++i)
  758. {
  759. mvaddch(player1.y,i,PLAYER_SPRITE);
  760. player_p.push_back(point(i,player1.y));
  761. }
  762. //REDRAW WALLS
  763. for(auto wall_p: walls_p)
  764. mvaddch(wall_p.y,wall_p.x,' ');
  765. walls_p.clear();
  766. for(std::size_t i=0;i<walls.size();i++)
  767. for(int k=0;k<walls[i].width;k++)
  768. for(int j=0;j<walls[i].height;j++){
  769. if(walls[i].health[j][k]==1) attron(COLOR_PAIR(2));
  770. else attron(COLOR_PAIR(4));
  771. if(walls[i].health[j][k]>0)
  772. {
  773. mvaddch(j+walls[i].y,k+walls[i].x,WALL_SPRITE);
  774. walls_p.push_back(point(k+walls[i].x,j+walls[i].y));
  775. }
  776. }
  777. }
  778. void drop_powerup(int x,int y,b_list& powerups)
  779. {
  780. 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"
  781. if((double)rand()/RAND_MAX<1./ROCKET_RATIO){ //1 powerup out of ROCKET_RATIO*POWERUP_RATIO is a "X" (ID 512)
  782. powerup newpowerup(x,y,512);
  783. powerups.push_back(newpowerup);
  784. }
  785. else{
  786. powerup newpowerup(x,y,128);
  787. powerups.push_back(newpowerup);
  788. }
  789. }
  790. else{
  791. powerup newpowerup(x,y); //otherwise, it is a "+"
  792. powerups.push_back(newpowerup);
  793. }
  794. }
  795. 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, double poweruprate){
  796. bool should_continue;
  797. b_list::iterator it1=bullets.begin();
  798. while(it1!=bullets.end())
  799. {
  800. should_continue = false;
  801. if(it1->y<=0)
  802. { //bullet reached the ceiling
  803. it1 = bullets.erase(it1);
  804. continue;
  805. }
  806. b_list::iterator it2 = bombs.begin();
  807. while(it2!=bombs.end())
  808. {
  809. if(it1->x==it2->x && (it1->y==it2->y || it1->y==it2->y+1)){ //bullet+bomb
  810. it1=bullets.erase(it1);
  811. should_continue = true;
  812. it2=bombs.erase(it2);
  813. score+=50;
  814. break;
  815. }
  816. else
  817. ++it2;
  818. }
  819. if(should_continue) continue;
  820. for(e_list::iterator it3=enemies.begin(); it3!=enemies.end(); ++it3)
  821. if(it1->x==it3->x && it1->y==it3->y && it3->alive){ //bullet+enemy
  822. it1=bullets.erase(it1);
  823. should_continue = true;
  824. it3->alive=false;
  825. if((double)rand()/RAND_MAX<poweruprate) //a powerup is dropped
  826. drop_powerup(it3->x,it3->y+1,powerups);
  827. score+=100;
  828. break;
  829. }
  830. if(should_continue) continue;
  831. if((it1->x>=boss1.x+boss1.width/2-2 && it1->x<=boss1.x+boss1.width/2+2) && boss1.alive){ //bullet+boss
  832. boss1.health--;
  833. if(it1->y==boss1.y) it1=bullets.erase(it1); //bullet gets destroyed only when has reached boss's top
  834. else
  835. {
  836. ++it1;
  837. continue;
  838. }
  839. //(to make the boss lose life more quickly)
  840. }
  841. ++it1;
  842. }
  843. r_list::iterator itr=rockets.begin();
  844. while(itr != rockets.end())
  845. {
  846. should_continue = false;
  847. if(itr->y<=0) //rocket reached the ceiling
  848. {
  849. itr=rockets.erase(itr);
  850. continue;
  851. }
  852. b_list::iterator it2=bombs.begin(); //bomb+rocket
  853. while(it2!=bombs.end())
  854. {
  855. 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
  856. itr=rockets.erase(itr);
  857. should_continue = true;
  858. it2=bombs.erase(it2);
  859. score+=50;
  860. break;
  861. }
  862. else
  863. ++it2;
  864. }
  865. if(should_continue) continue;
  866. e_list::iterator it3 = enemies.begin();
  867. while(it3!=enemies.end())
  868. {
  869. if(itr->x==it3->x && itr->y==it3->y){ //rocket+enemy
  870. itr=rockets.erase(itr);
  871. it3=enemies.erase(it3);
  872. should_continue = true;
  873. break;
  874. }
  875. else
  876. ++it3;
  877. }
  878. if(should_continue) continue;
  879. if((itr->x>=boss1.x+boss1.width/2-2 && itr->x<=boss1.x+boss1.width/2+2) && boss1.alive) //rocket+boss
  880. {
  881. boss1.health--;
  882. if(itr->y==boss1.y)
  883. {
  884. itr=rockets.erase(itr);
  885. continue;
  886. }
  887. }
  888. ++itr;
  889. }
  890. b_list::iterator itb=bombs.begin();
  891. while(itb!=bombs.end()){
  892. if(itb->y>=R) //bomb reached the floor
  893. {
  894. itb=bombs.erase(itb);
  895. continue;
  896. }
  897. if(itb->x>=player1.x && itb->x<player1.x+player1.length && itb->y>player1.y) //bomb+player
  898. {
  899. itb=bombs.erase(itb);
  900. continue;
  901. }
  902. ++itb;
  903. }
  904. b_list::iterator itp=powerups.begin();
  905. while( itp!=powerups.end()){
  906. if(itp->y==player1.y && itp->x>=player1.x && itp->x<player1.x+player1.length){
  907. switch(itp->id){
  908. case 64:
  909. if(player1.weaponclass<MAX_WEAPONCLASS) player1.weaponclass++;
  910. break;
  911. case 128:
  912. if(player1.length>2){
  913. mvaddch(player1.y,player1.x+player1.length-1,' ');
  914. player1.length--;
  915. }
  916. break;
  917. case 512:
  918. if(!(player1.rocketlauncher)) player1.rocketlauncher=TRUE;
  919. else if(player1.weaponclassrkt<MAX_WEAPONCLASSRKT) player1.weaponclassrkt++;
  920. break;
  921. }
  922. itp=powerups.erase(itp);
  923. continue;
  924. }
  925. if(itp->y>=R) //powerup reached the floor
  926. {
  927. itp=powerups.erase(itp);
  928. continue;
  929. }
  930. ++itp;
  931. }
  932. for(size_t i=0;i<walls.size();i++)
  933. for(int j=0;j<walls[i].width;j++)
  934. for(int k=0;k<walls[i].height;k++)
  935. {
  936. if(walls[i].health[k][j]>0)
  937. {
  938. b_list::iterator it1=bullets.begin();
  939. while(it1!=bullets.end())
  940. {
  941. if(it1->x==walls[i].x+j && it1->y==walls[i].y+k){
  942. it1=bullets.erase(it1);
  943. walls[i].health[k][j]--;
  944. }
  945. else
  946. ++it1;
  947. }
  948. b_list::iterator it2=bombs.begin();
  949. while(it2!=bombs.end())
  950. {
  951. if(it2->x==walls[i].x+j && it2->y==walls[i].y+k){
  952. it2=bombs.erase(it2);
  953. walls[i].health[k][j]--;
  954. }
  955. else
  956. ++it2;
  957. }
  958. r_list::iterator it3=rockets.begin();
  959. while(it3!=rockets.end())
  960. {
  961. if(it3->x==walls[i].x+j && it3->y==walls[i].y+k){
  962. it3=rockets.erase(it3);
  963. walls[i].health[k][j]--;
  964. }
  965. else
  966. ++it3;
  967. }
  968. b_list::iterator it4=powerups.begin();
  969. while(it4!=powerups.end())
  970. {
  971. if(it4->x==walls[i].x+j && it4->y==walls[i].y+k){
  972. it4=powerups.erase(it4);
  973. }
  974. else
  975. ++it4;
  976. }
  977. }
  978. }
  979. }
  980. //********** SAVE FUNCTIONS *****************************
  981. void refreshrecords(int newscore,WINDOW* win)
  982. {
  983. r_vec records;
  984. std::string nick;
  985. record newrecord;
  986. std::string local_recpath;
  987. std::string local_dir=getenv("HOME"); //get environment variable $HOME
  988. local_dir+=RECORD_DIR; //we set local_dir and local_recpath: ~/.local/share/invaders/records.dat
  989. local_recpath=local_dir;
  990. local_recpath+=RECORD_FILE;
  991. wattron(win,COLOR_PAIR(5));
  992. mvwprintw(win,6,5,"Please insert your nick");
  993. mvwprintw(win,7,2,"(up to 5 characters): ");
  994. wattron(win,COLOR_PAIR(1));
  995. echo();
  996. char getnick[6];
  997. wscanw(win,"%s",getnick);
  998. nick=getnick;
  999. nick.resize(5,'.');
  1000. noecho();
  1001. std::ifstream recordin(local_recpath.c_str()); //first, check if local_recpath exists; if not, create it.
  1002. if(recordin) //note that local_recpath is actually a symlink to global_recpath
  1003. {
  1004. recordin >> newrecord.nick;
  1005. recordin >> newrecord.score;
  1006. while(!recordin.eof())
  1007. {
  1008. records.push_back(newrecord);
  1009. recordin >> newrecord.nick;
  1010. recordin >> newrecord.score;
  1011. }
  1012. recordin.close();
  1013. newrecord.score=newscore;
  1014. newrecord.nick=nick;
  1015. records.push_back(newrecord);
  1016. sort(records.rbegin(), records.rend());
  1017. wattron(win,COLOR_PAIR(6));
  1018. mvwprintw(win,9,11,"TOP FIVE");
  1019. wattron(win,COLOR_PAIR(0));
  1020. int count=0;
  1021. for(r_vec::iterator it=records.begin(); (it!=records.begin()+5 && it!=records.end()); ++it,++count)
  1022. {
  1023. wmove(win,10+count,9);
  1024. it->printwin(win);
  1025. }
  1026. count=0;
  1027. for(r_vec::iterator it=records.begin(); it!=records.end(); ++it)
  1028. {
  1029. count++;
  1030. if(*it==newrecord)
  1031. {
  1032. mvwprintw(win,16,3,"You're number %i out of %i",count,records.size());
  1033. break;
  1034. }
  1035. }
  1036. std::ofstream recordout(local_recpath.c_str());
  1037. for(r_vec::iterator it=records.begin(); it!=records.end(); ++it)
  1038. it->print(recordout);
  1039. recordout.close();
  1040. }
  1041. else //records.dat not found
  1042. {
  1043. //if local_recpath does not exist, then we must create the local_dir.
  1044. recordin.close();
  1045. recordin.clear();
  1046. create_folder(0);
  1047. //now we check if global_recpath exists.
  1048. std::string global_recpath=GLOBAL_DIR; //global_recpath is GLOBAL_DIR/records.dat
  1049. global_recpath+=RECORD_FILE;
  1050. //we try to open GLOBAL_DIR/records.dat
  1051. std::ifstream globalin(global_recpath.c_str());
  1052. if(!globalin){
  1053. globalin.close();
  1054. globalin.clear();
  1055. create_folder(1); //try to create global_dir (will fail if already
  1056. std::ofstream globalout(global_recpath.c_str()); //exists) and save the global_recpath.
  1057. globalout.close();
  1058. globalout.clear();
  1059. chmod(global_recpath.c_str(),S_IRWXG | S_IRWXU | S_IRWXO);
  1060. }
  1061. else{
  1062. //just save to GLOBAL_DIR/records.dat
  1063. globalin >> newrecord.nick;
  1064. globalin >> newrecord.score;
  1065. while(!globalin.eof())
  1066. {
  1067. records.push_back(newrecord);
  1068. globalin >> newrecord.nick;
  1069. globalin >> newrecord.score;
  1070. }
  1071. globalin.close();
  1072. }
  1073. //finally, create the symlink to global_recpath
  1074. symlink(global_recpath.c_str(),local_recpath.c_str());
  1075. newrecord.score=newscore;
  1076. newrecord.nick=nick;
  1077. records.push_back(newrecord);
  1078. sort(records.rbegin(), records.rend());
  1079. //and save there the records.
  1080. std::ofstream recordout(local_recpath.c_str());
  1081. for(r_vec::iterator it=records.begin(); it!=records.end(); ++it)
  1082. it->print(recordout);
  1083. recordout.close();
  1084. }
  1085. }
  1086. void print_scores(){
  1087. r_vec records;
  1088. std::string recordpath;
  1089. std::string record_dir=getenv("HOME");
  1090. record_dir+=RECORD_DIR;
  1091. recordpath=record_dir;
  1092. recordpath+=RECORD_FILE;
  1093. std::ifstream recordin(recordpath.c_str());
  1094. record newrecord;
  1095. int i=1;
  1096. if(recordin){
  1097. WINDOW *recs;
  1098. recs=newwin(25,30,0,0);
  1099. box(recs,ACS_VLINE,ACS_HLINE);
  1100. wattron(recs,COLOR_PAIR(5));
  1101. mvwprintw(recs,1,1,"======= HIGHSCORES =======");
  1102. wattron(recs,COLOR_PAIR(0));
  1103. recordin >> newrecord.nick;
  1104. recordin >> newrecord.score;
  1105. while(!recordin.eof() && i<21)
  1106. {
  1107. records.push_back(newrecord);
  1108. recordin >> newrecord.nick;
  1109. recordin >> newrecord.score;
  1110. i++;
  1111. }
  1112. recordin.close();
  1113. i=1;
  1114. for(r_vec::iterator it=records.begin(); it!=records.end(); ++it){
  1115. if(i<10)
  1116. mvwprintw(recs,i+2,1,"0%i- ",i);
  1117. else
  1118. mvwprintw(recs,i+2,1,"%i- ",i);
  1119. it->printwin(recs);
  1120. i++;
  1121. }
  1122. wrefresh(recs);
  1123. timeout(-1);
  1124. if(getch()!=ERR){
  1125. delwin(recs);
  1126. erase();
  1127. refresh();
  1128. }
  1129. }
  1130. else{
  1131. WINDOW *recs;
  1132. std::string host=getenv("USER");
  1133. recs=newwin(5,3+host.length()+record_dir.length(),5,5);
  1134. box(recs,ACS_VLINE,ACS_HLINE);
  1135. wattron(recs,COLOR_PAIR(5));
  1136. mvwprintw(recs,1,1,"Save file:");
  1137. mvwprintw(recs,2,1,"%s",record_dir.c_str());
  1138. mvwprintw(recs,3,1,"%s not found.",RECORD_FILE);
  1139. //mvwprintw(recs,4,1,"not found.");
  1140. wrefresh(recs);
  1141. timeout(-1);
  1142. if(getch()!=ERR){
  1143. delwin(recs);
  1144. erase();
  1145. refresh();
  1146. }
  1147. }
  1148. }
  1149. /*//=========== THREADS FUNCTIONS ==============================
  1150. void *pmusic(void *arg){
  1151. while(1){
  1152. 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");
  1153. napms(2000);
  1154. }
  1155. }
  1156. void *pshoot_sound(void *arg){
  1157. system("beep -f 1000 -l 30 -n -f 850 -l 30 2> /dev/null");
  1158. }
  1159. void *penshoot_sound(void *arg){
  1160. system("beep -f 1500 -l 30 -n -f 1000 -l 30 2> /dev/null");
  1161. }
  1162. void *pwin_theme(void *arg){
  1163. system("beep -f 1000 -l 150 -n -f 800 -l 150 -n -f 1000 -l 150 -n -f 1500 -l 600 2> /dev/null");
  1164. }
  1165. void *plose_theme(void *arg){
  1166. system("beep -f 700 -l 150 -n -f 585 -l 150 -n -f 550 -l 150 -n -f 520 -l 600 2> /dev/null");
  1167. }
  1168. void pkill_music(pthread_t thread){
  1169. pthread_cancel(thread);
  1170. system("beep -f 1 2> /dev/null");
  1171. }*/
  1172. //=========BOSSRUSH FUNCTIONS=======================
  1173. int choose_level_bossrush(int* commands)
  1174. {
  1175. std::string phrase;
  1176. phrase=choose_phrase();
  1177. char input;
  1178. do{
  1179. print_title();
  1180. print_catchphrase(phrase);
  1181. attron(COLOR_PAIR(1));
  1182. mvprintw(5,0,"Choose difficulty level:");
  1183. attron(COLOR_PAIR(1));
  1184. mvprintw(6,0,"1- Easy");
  1185. attron(COLOR_PAIR(3));
  1186. mvprintw(7,0,"2- Medium");
  1187. attron(COLOR_PAIR(2));
  1188. mvprintw(8,0,"3- Hard");
  1189. attron(COLOR_PAIR(4));
  1190. mvprintw(9,0,"4- Impossible");
  1191. attron(COLOR_PAIR(5));
  1192. mvprintw(10,0,"i- Info");
  1193. attron(COLOR_PAIR(6));
  1194. mvprintw(11,0,"h- Highscores");
  1195. attron(COLOR_PAIR(1));
  1196. mvprintw(12,0,"c- Commands");
  1197. attron(COLOR_PAIR(2));
  1198. mvprintw(13,0,"q- Quit");
  1199. refresh();
  1200. timeout(80);
  1201. input=tolower(getch());
  1202. if(!(input=='1' || input=='2' || input=='3' || input=='4' || input=='i' || input=='h' || input=='r' || input=='q') && input!=ERR){
  1203. attron(COLOR_PAIR(2));
  1204. mvprintw(14,0,"Bad input. Please choose a number within 1 and 4.");
  1205. attron(COLOR_PAIR(0));
  1206. }
  1207. if(input=='i')
  1208. print_info();
  1209. else if(input=='h')
  1210. print_scores_bossrush();
  1211. else if(input=='r')
  1212. phrase=choose_phrase();
  1213. else if(input=='c')
  1214. change_commands(commands);
  1215. else if(input=='q'){
  1216. endwin();
  1217. cout<<lightgreen<<"Game exited correctly."<<none<<endl;
  1218. exit(1);
  1219. }
  1220. }while(!(input=='1' || input=='2' || input=='3' || input=='4'));
  1221. return input - '0';
  1222. }
  1223. void setup_level_bossrush(int level, double& shootr, double& poweruprate, int& walls_num, int& refresh_t)
  1224. {
  1225. walls_num = 3;
  1226. switch(level) //setting game parameters
  1227. {
  1228. case 1:
  1229. {
  1230. shootr = 0.02;
  1231. refresh_t = (int)(0.07*1E3);
  1232. poweruprate = 0.04;
  1233. break;
  1234. }
  1235. case 2:
  1236. {
  1237. shootr = 0.02;
  1238. refresh_t = (int)(0.07*1E3);
  1239. poweruprate = 0.04;
  1240. break;
  1241. }
  1242. case 3:
  1243. {
  1244. shootr = 0.02;
  1245. refresh_t = (int)(0.07*1E3);
  1246. poweruprate = 0.04;
  1247. break;
  1248. }
  1249. case 4:
  1250. {
  1251. shootr = 0.02;
  1252. refresh_t = (int)(0.07*1E3);
  1253. poweruprate = 0.04;
  1254. walls_num = 2;
  1255. break;
  1256. }
  1257. }
  1258. }
  1259. void resetbosses(boss* Bosses,boss& boss1,player& player1){
  1260. boss* bosses=Bosses;
  1261. bosses->health=bosses->healthmax;
  1262. bosses++;
  1263. bosses->health=bosses->healthmax;
  1264. bosses++;
  1265. bosses->health=bosses->healthmax;
  1266. bosses++;
  1267. bosses->health=bosses->healthmax;
  1268. bosses++;
  1269. bosses->health=bosses->healthmax;
  1270. boss1=*Bosses;
  1271. boss1.alive=true;
  1272. player1.weaponclass=1;
  1273. }
  1274. void Victory_bossrush(std::string name,int score,int level,int chflag){
  1275. score+=1500*level;
  1276. WINDOW *victory;
  1277. victory=newwin(19,32,7,17);
  1278. box(victory,ACS_VLINE,ACS_HLINE);
  1279. wattron(victory,COLOR_PAIR(1));
  1280. mvwprintw(victory,2,5,"YOU DEFEATED %s!",name.c_str());
  1281. wattron(victory,COLOR_PAIR(3));
  1282. mvwprintw(victory,4,5,"+ %i pts!",level*1500);
  1283. wattron(victory,COLOR_PAIR(1));
  1284. mvwprintw(victory,4,5,"YOU WON!");
  1285. wattron(victory,COLOR_PAIR(3));
  1286. mvwprintw(victory,4,5,"Your score is: %i",score);
  1287. wrefresh(victory);
  1288. if(chflag==0)
  1289. refreshrecords_bossrush(score,victory);
  1290. else{
  1291. wattron(victory,COLOR_PAIR(4));
  1292. mvwprintw(victory,7,3,"I think you used cheats...");
  1293. }
  1294. wrefresh(victory);
  1295. delwin(victory);
  1296. }
  1297. void refreshrecords_bossrush(int newscore,WINDOW* win)
  1298. {
  1299. r_vec records;
  1300. std::string nick;
  1301. record newrecord;
  1302. std::string local_recpath;
  1303. std::string local_dir=getenv("HOME"); //get environment variable $HOME
  1304. local_dir+=RECORD_DIR; //we set local_dir and local_recpath: ~/.local/share/invaders/records_bossrush.dat
  1305. local_recpath=local_dir;
  1306. local_recpath+=RECORD_FILE_BOSSRUSH;
  1307. wattron(win,COLOR_PAIR(5));
  1308. mvwprintw(win,6,5,"Please insert your nick");
  1309. mvwprintw(win,7,2,"(up to 5 characters): ");
  1310. wattron(win,COLOR_PAIR(1));
  1311. echo();
  1312. char getnick[6];
  1313. wscanw(win,"%s",getnick);
  1314. nick=getnick;
  1315. nick.resize(5,'.');
  1316. noecho();
  1317. std::ifstream recordin(local_recpath.c_str());
  1318. if(recordin)
  1319. {
  1320. recordin >> newrecord.nick;
  1321. recordin >> newrecord.score;
  1322. while(!recordin.eof())
  1323. {
  1324. records.push_back(newrecord);
  1325. recordin >> newrecord.nick;
  1326. recordin >> newrecord.score;
  1327. }
  1328. recordin.close();
  1329. newrecord.score=newscore;
  1330. newrecord.nick=nick;
  1331. records.push_back(newrecord);
  1332. sort(records.rbegin(), records.rend());
  1333. wattron(win,COLOR_PAIR(6));
  1334. mvwprintw(win,9,11,"TOP FIVE");
  1335. wattron(win,COLOR_PAIR(0));
  1336. int count=0;
  1337. for(r_vec::iterator it=records.begin(); (it!=records.begin()+5 && it!=records.end()); ++it,++count)
  1338. {
  1339. wmove(win,10+count,9);
  1340. it->printwin(win);
  1341. }
  1342. count=0;
  1343. for(r_vec::iterator it=records.begin(); it!=records.end(); ++it)
  1344. {
  1345. count++;
  1346. if(*it==newrecord)
  1347. {
  1348. mvwprintw(win,16,3,"You're number %i out of %i",count,records.size());
  1349. break;
  1350. }
  1351. }
  1352. std::ofstream recordout(local_recpath.c_str());
  1353. for(r_vec::iterator it=records.begin(); it!=records.end(); ++it)
  1354. it->print(recordout);
  1355. recordout.close();
  1356. }
  1357. else //records_bossrush.dat not found
  1358. {
  1359. recordin.close();
  1360. recordin.clear();
  1361. create_folder(0); //this will fail if local_dir already exists (possible if player played bossrush before).
  1362. //now we check if global_recpath exists.
  1363. std::string global_recpath=GLOBAL_DIR; //global_recpath is GLOBAL_DIR/records_bossrush.dat
  1364. global_recpath+=RECORD_FILE_BOSSRUSH;
  1365. //we try to open GLOBAL_DIR/records.dat
  1366. std::ifstream globalin(global_recpath.c_str());
  1367. if(!globalin){
  1368. globalin.close();
  1369. globalin.clear();
  1370. create_folder(1); //try to create global_dir (will fail if already
  1371. std::ofstream globalout(global_recpath.c_str()); //exists) and save the global_recpath.
  1372. globalout.close();
  1373. globalout.clear();
  1374. chmod(global_recpath.c_str(),S_IRWXG | S_IRWXU | S_IRWXO);
  1375. }
  1376. else{
  1377. //just save to GLOBAL_DIR/records_bossrush.dat
  1378. globalin >> newrecord.nick;
  1379. globalin >> newrecord.score;
  1380. while(!globalin.eof())
  1381. {
  1382. records.push_back(newrecord);
  1383. globalin >> newrecord.nick;
  1384. globalin >> newrecord.score;
  1385. }
  1386. globalin.close();
  1387. }
  1388. //finally, create the symlink to global_recpath
  1389. symlink(global_recpath.c_str(),local_recpath.c_str());
  1390. newrecord.score=newscore;
  1391. newrecord.nick=nick;
  1392. records.push_back(newrecord);
  1393. sort(records.rbegin(), records.rend());
  1394. //and save there the records.
  1395. std::ofstream recordout(local_recpath.c_str());
  1396. for(r_vec::iterator it=records.begin(); it!=records.end(); ++it)
  1397. it->print(recordout);
  1398. recordout.close();
  1399. }
  1400. }
  1401. void print_scores_bossrush(){
  1402. r_vec records;
  1403. std::string recordpath;
  1404. std::string record_dir=getenv("HOME");
  1405. record_dir+=RECORD_DIR;
  1406. recordpath=record_dir;
  1407. recordpath+=RECORD_FILE_BOSSRUSH;
  1408. std::ifstream recordin(recordpath.c_str());
  1409. record newrecord;
  1410. int i=1;
  1411. if(recordin){
  1412. WINDOW *recs;
  1413. recs=newwin(25,30,0,0);
  1414. box(recs,ACS_VLINE,ACS_HLINE);
  1415. wattron(recs,COLOR_PAIR(5));
  1416. mvwprintw(recs,1,1,"======= HIGHSCORES =======");
  1417. wattron(recs,COLOR_PAIR(0));
  1418. recordin >> newrecord.nick;
  1419. recordin >> newrecord.score;
  1420. while(!recordin.eof() && i<21)
  1421. {
  1422. records.push_back(newrecord);
  1423. recordin >> newrecord.nick;
  1424. recordin >> newrecord.score;
  1425. i++;
  1426. }
  1427. recordin.close();
  1428. i=1;
  1429. for(r_vec::iterator it=records.begin(); it!=records.end(); ++it){
  1430. if(i<10)
  1431. mvwprintw(recs,i+2,1,"0%i- ",i);
  1432. else
  1433. mvwprintw(recs,i+2,1,"%i- ",i);
  1434. it->printwin(recs);
  1435. i++;
  1436. }
  1437. wrefresh(recs);
  1438. timeout(-1);
  1439. if(getch()!=ERR){
  1440. delwin(recs);
  1441. erase();
  1442. refresh();
  1443. }
  1444. }
  1445. else{
  1446. WINDOW *recs;
  1447. std::string host=getenv("USER");
  1448. recs=newwin(6,25+host.length(),5,5);
  1449. box(recs,ACS_VLINE,ACS_HLINE);
  1450. wattron(recs,COLOR_PAIR(5));
  1451. mvwprintw(recs,1,1,"Save file:");
  1452. mvwprintw(recs,2,1,"%s",record_dir.c_str());
  1453. mvwprintw(recs,3,1,"%s",RECORD_FILE_BOSSRUSH);
  1454. mvwprintw(recs,4,1,"not found.");
  1455. wrefresh(recs);
  1456. timeout(-1);
  1457. if(getch()!=ERR){
  1458. delwin(recs);
  1459. erase();
  1460. refresh();
  1461. }
  1462. }
  1463. }
  1464. void create_readme(){
  1465. std::string recordpath;
  1466. std::string record_dir=getenv("HOME");
  1467. record_dir+=RECORD_DIR;
  1468. recordpath=record_dir;
  1469. recordpath+="README.txt";
  1470. std::ifstream checkread(recordpath.c_str());
  1471. if(!checkread){
  1472. checkread.close();
  1473. checkread.clear();
  1474. std::ofstream readme(recordpath.c_str());
  1475. readme<<"#==Space Invaders=="<<endl;
  1476. readme<<"#"<<endl;
  1477. readme<<"#Original game was created and developed by Giacomo Parolini and Enrico Guiraud (year 2010-2013)"<<endl;
  1478. readme<<"#License: GNU GPL"<<endl;
  1479. readme<<"#"<<endl;
  1480. readme<<"#Game version logs:"<<endl;
  1481. readme<<"# -invaders2.0"<<endl;
  1482. readme<<"# -invaders2.1"<<endl;
  1483. readme<<"# -invaders3.0"<<endl;
  1484. readme<<"# -invaders3.1"<<endl;
  1485. readme<<"# -invaders3.2"<<endl;
  1486. readme<<"# -invaders3.3"<<endl;
  1487. readme<<"# -invaders3.4"<<endl;
  1488. readme<<"# -invaders3.5"<<endl;
  1489. readme<<"# -invaders3.6"<<endl;
  1490. readme<<"# -invaders4.0"<<endl;
  1491. readme<<"# -invaders4.1"<<endl;
  1492. readme<<"# -invaders4.2"<<endl;
  1493. readme<<"# -invaders4.3"<<endl;
  1494. readme<<"# -invaders4.3.5"<<endl;
  1495. readme<<"# -curses_invaders2.0"<<endl;
  1496. readme<<"# -curses_invaders3.0"<<endl;
  1497. readme<<"# -curses_invaders3.5"<<endl;
  1498. readme<<"# -curses_invaders4.0"<<endl;
  1499. readme<<"# -curses_invaders4.1"<<endl;
  1500. readme<<"# -curses_invaders4.2"<<endl;
  1501. readme<<"# -curses_invaders4.3"<<endl;
  1502. readme<<"# -curses_invaders4.4"<<endl;
  1503. readme<<"# -GL_invaders"<<endl;
  1504. readme<<"#"<<endl;
  1505. readme<<"#All versions are written in C++. "<<endl;
  1506. readme<<"#"<<endl;
  1507. 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;
  1508. 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;
  1509. readme<<"#"<<endl;
  1510. readme<<"#System Requirements:"<<endl;
  1511. readme<<"#All versions are Linux x-executable files, so will only run within Linux environment."<<endl;
  1512. readme<<"#However, files CAN be compiled also in MacOS environment."<<endl;
  1513. readme<<"#"<<endl;
  1514. 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;
  1515. readme<<"#"<<endl;
  1516. readme<<"#since v.4.3, game controls are customizable at the beginning of the game."<<endl;
  1517. readme<<"#v.4.4 implements shared highscores."<<endl;
  1518. readme<<"#"<<endl;
  1519. readme<<"#"<<endl;
  1520. readme<<"#CREATING AND EDITING BOSSES (at the moment unavailable: the game doesn't create the scripts automatically)"<<endl;
  1521. 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;
  1522. readme<<"#In order to create a boss, follow these steps:"<<endl;
  1523. readme<<"# *1: create a file named <custom_boss_name>.dat and open it with any text editor (like Gedit, Vim or Notepad)."<<endl;
  1524. 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;
  1525. 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;
  1526. 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;
  1527. readme<<"#"<<endl;
  1528. readme<<"#...and yes: cheats ARE available, just try and find them ;)"<<endl;
  1529. readme.close();
  1530. }
  1531. }
  1532. void create_std_bosses(){
  1533. std::string bosspath;
  1534. std::string boss_dir=getenv("HOME");
  1535. boss_dir+=RECORD_DIR;
  1536. bosspath=boss_dir;
  1537. std::string bosspath1=bosspath+BOSS_FILE1+".dat";
  1538. std::ifstream checkread(bosspath1.c_str());
  1539. if(!checkread){
  1540. std::string newrecordpath=boss_dir+RECORD_FILE; //check if .invaders/records.dat exists
  1541. std::ifstream tryrecord(newrecordpath.c_str());
  1542. if(!tryrecord){ //.invaders/records.dat does not exist, so create the .invaders directory
  1543. tryrecord.close();
  1544. tryrecord.clear();
  1545. std::string newrecordpath2=boss_dir+RECORD_FILE_BOSSRUSH; //check if .invaders/records_bossrush.dat exists
  1546. std::ifstream tryrecord2(newrecordpath2.c_str());
  1547. if(!tryrecord2){
  1548. tryrecord2.close();
  1549. tryrecord2.clear();
  1550. mkdir(boss_dir.c_str(),S_IRWXU|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH);
  1551. create_readme();
  1552. //create_setsize();
  1553. //std::string setsizepath=boss_dir+"setsize.sh";
  1554. //chmod(setsizepath.c_str(),S_IRWXU);
  1555. }
  1556. }
  1557. checkread.close();
  1558. checkread.clear();
  1559. std::ofstream boss1(bosspath1.c_str());
  1560. boss1<<" _____ "<<endl;
  1561. boss1<<"// \\\\"<<endl;
  1562. boss1<<"| 0 0 |"<<endl;
  1563. boss1<<"c L P"<<endl;
  1564. boss1<<"\\ MMM /"<<endl;
  1565. boss1<<" \\_____/ "<<endl;
  1566. boss1.close();
  1567. boss1.clear();
  1568. std::string bosspath2=bosspath+BOSS_FILE2+".dat";
  1569. std::ofstream boss2(bosspath2.c_str());
  1570. boss2<<" _______ "<<endl;
  1571. boss2<<" W _ _ _ W "<<endl;
  1572. boss2<<"|_|o| |o|_|"<<endl;
  1573. boss2<<"c c P"<<endl;
  1574. boss2<<" \\__NNN__/ "<<endl;
  1575. boss2.close();
  1576. boss2.clear();
  1577. std::string bosspath3=bosspath+BOSS_FILE3+".dat";
  1578. std::ofstream boss3(bosspath3.c_str());
  1579. boss3<<" 99999 "<<endl;
  1580. boss3<<" C \\ / D "<<endl;
  1581. boss3<<"C 0 0 D"<<endl;
  1582. boss3<<"C_ ^ _D"<<endl;
  1583. boss3<<" | J | "<<endl;
  1584. boss3<<" \\_/ "<<endl;
  1585. boss3.close();
  1586. boss3.clear();
  1587. std::string bosspath4=bosspath+BOSS_FILE4+".dat";
  1588. std::ofstream boss4(bosspath4.c_str());
  1589. boss4<<" MMMMMMM "<<endl;
  1590. boss4<<" M M "<<endl;
  1591. boss4<<"| == == |"<<endl;
  1592. boss4<<"c L D"<<endl;
  1593. boss4<<" MMM MMM "<<endl;
  1594. boss4<<" MMMMMMM "<<endl;
  1595. boss4.close();
  1596. boss4.clear();
  1597. std::string bosspath5=bosspath+BOSS_FILE5+".dat";
  1598. std::ofstream boss5(bosspath5.c_str());
  1599. boss5<<" \\__ ______ __/ "<<endl;
  1600. boss5<<"\\___ = = ___/ "<<endl;
  1601. boss5<<" | | = = | | "<<endl;
  1602. boss5<<" \\_ _/ "<<endl;
  1603. boss5<<" / || \\ "<<endl;
  1604. boss5.close();
  1605. boss5.clear();
  1606. }
  1607. }
  1608. /*void create_setsize(){
  1609. std::string recordpath;
  1610. std::string record_dir=getenv("HOME");
  1611. record_dir+=RECORD_DIR;
  1612. recordpath=record_dir;
  1613. recordpath+="setsize.sh";
  1614. std::ifstream checkread(recordpath.c_str());
  1615. if(!checkread){
  1616. checkread.close();
  1617. checkread.clear();
  1618. std::ofstream setsize(recordpath.c_str());
  1619. setsize<<"#!/bin/bash"<<endl;
  1620. setsize<<"DEFAULT_R=30"<<endl;
  1621. setsize<<"DEFAULT_C=60"<<endl;
  1622. setsize<<"R=0"<<endl;
  1623. setsize<<"C=0"<<endl;
  1624. setsize<<"INPUT_FILE=definitions.hpp"<<endl;
  1625. setsize<<"trap \"rm $INPUT_FILE.tmp 2> /dev/null; exit -1\" SIGINT SIGTERM"<<endl;
  1626. setsize<<"if [[ $1 == \"default\" ]]; then"<<endl;
  1627. setsize<<"sed s/\"#define R [0-9]*\"/\"#define R $DEFAULT_R\"/ <$INPUT_FILE >$INPUT_FILE.tmp"<<endl;
  1628. setsize<<"sed s/\"#define C [0-9]*\"/\"#define C $DEFAULT_C\"/ <$INPUT_FILE.tmp >$INPUT_FILE"<<endl;
  1629. setsize<<"rm $INPUT_FILE.tmp"<<endl;
  1630. setsize<<"make clean"<<endl;
  1631. setsize<<"make -j4"<<endl;
  1632. setsize<<"exit 0"<<endl;
  1633. setsize<<"elif [[ $# -gt 0 ]]; then echo Usage: $0 \"[default]\"; exit 1"<<endl;
  1634. setsize<<"else"<<endl;
  1635. setsize<<"echo Insert new Row number \\(5-120\\)"<<endl;
  1636. setsize<<"until [[ $R -ge 5 && $R -le 120 ]]; do"<<endl;
  1637. setsize<<" read R"<<endl;
  1638. setsize<<"if [[ $R -lt 5 || $R -gt 120 ]]; then echo Invalid size.; fi"<<endl;
  1639. setsize<<"echo New R will be $R"<<endl;
  1640. setsize<<"done"<<endl;
  1641. setsize<<"echo Insert new Column number \\(5-120\\)"<<endl;
  1642. setsize<<"until [[ $C -ge 5 && $C -le 120 ]]; do"<<endl;
  1643. setsize<<" read C"<<endl;
  1644. setsize<<"if [[ $C -lt 5 || $C -gt 120 ]]; then echo Invalid size.; fi"<<endl;
  1645. setsize<<"echo New C will be $C"<<endl;
  1646. setsize<<"done"<<endl;
  1647. setsize<<"sed s/\"#define R [0-9]*\"/\"#define R $R\"/ <$INPUT_FILE >$INPUT_FILE.tmp"<<endl;
  1648. setsize<<"sed s/\"#define C [0-9]*\"/\"#define C $C\"/ <$INPUT_FILE.tmp >$INPUT_FILE"<<endl;
  1649. setsize<<"rm $INPUT_FILE.tmp"<<endl;
  1650. setsize<<"make clean"<<endl;
  1651. setsize<<"make -j4"<<endl;
  1652. setsize<<"fi"<<endl;
  1653. setsize.close();
  1654. }
  1655. }*/
  1656. void create_folder(int flag){ //0: local, 1: global
  1657. switch(flag){
  1658. case 1:
  1659. if(mkdir(GLOBAL_DIR,S_IRWXU|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH)==0){ //this will fail if local_dir already
  1660. create_readme(); //exists (possible if player played
  1661. create_std_bosses(); //bossrush before).
  1662. //create_setsize();
  1663. }
  1664. break;
  1665. default:
  1666. {
  1667. std::string local_dir=getenv("HOME");
  1668. local_dir+=RECORD_DIR;
  1669. if(mkdir(local_dir.c_str(),S_IRWXU|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH)==0){ //this will fail if local_dir already
  1670. create_readme(); //exists (possible if player played
  1671. create_std_bosses(); //bossrush before).
  1672. //create_setsize();
  1673. }
  1674. }
  1675. }
  1676. }