functions.cpp 50 KB

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