functions.cpp 50 KB

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