functions.cpp 50 KB

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