functions.cpp 50 KB

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