home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / games / volume9 / wanderer3 / part01 / edit.c next >
C/C++ Source or Header  |  1990-05-21  |  12KB  |  546 lines

  1. #include "wand_head.h"
  2.  
  3. extern char *playscreen();
  4. extern void helpme();
  5. extern int inform_me();
  6.  
  7. extern int debug_disp;
  8. extern char *edit_screen;
  9. extern char screen[NOOFROWS][ROWLEN+1];
  10. extern char *edit_memory;
  11. extern char *memory_end;
  12. extern char screen_name[61];
  13.  
  14. static char *inst[] = { "    O  Boulder",
  15.             "  < >  Arrows",
  16.             "    ^  Balloon",
  17.             "    :  Earth",
  18.             "    !  Landmine",
  19.             "    *  Treasure",
  20.             "  / \\  Deflectors",
  21.             "    +  Cage",
  22.             "_ = #  Rock (# indestructable)",
  23.             "    T  Teleport",
  24.             "    A  Arrival (1 max)",
  25.             "    X  Exit (always 1)",
  26.             "    @  Start (always 1)",
  27.             "    M  Big Monster (1 max)",
  28.             "    S  Baby Monster",
  29.             "    -  Alternative space",
  30.             "    C  Time Capsule",
  31.             "    ~  Thingy",
  32.             "    B  Bomb",
  33.             NULL };
  34.  
  35. check_legality()
  36. /*
  37.    this will check: baby monster vs cage numbers,
  38.             teleport, large monster, exit and player numbers
  39.             hanging boulders/arrows etc
  40. */
  41. {
  42. int ercount,cages,hanging,bmons,tele,arrival,you,mons,exits;
  43. int x, y;
  44. char buf[80];
  45. ercount = hanging = cages = bmons = tele = arrival = you = mons = exits = 0;
  46.  
  47. move(20,0);
  48. addstr("Checking screen legality..."); refresh();
  49. for( x = 0 ; x < ROWLEN ; x++)
  50.     for( y = 0 ; y < NOOFROWS ; y++) {
  51.     move(y+1,x+1); addch('?'); refresh();
  52.     switch(screen[y][x]) {
  53.       case '+': cages++; break;
  54.       case 'S': bmons++; break;
  55.       case 'T': tele++; break;
  56.       case 'A': arrival++; break;
  57.       case '@': you++; break;
  58.       case 'M': mons++; break;
  59.       case 'X': exits++; break;
  60.       case '-':
  61.       case ' ': if((screen[y-1][x] == 'O')&&(y>0)) hanging++;
  62.                 if((screen[y+1][x] == '^')&&(y<NOOFROWS)) hanging++;
  63.                 if((screen[y][x-1] == '>')&&(x>0)) hanging++;
  64.                 if((screen[y][x+1] == '<')&&(x<ROWLEN)) hanging++;
  65.             break;
  66.       default : break;
  67.     }
  68.     move(y+1,x+1); addch(screen[y][x]);
  69.     }
  70. move(20,0); addstr("                         ");
  71. if(cages != bmons) {
  72.     ercount++;
  73.     if( cages < bmons )
  74.         sprintf(buf,"++++ Warning: %d cage(s), %d baby monster(s).",cages,bmons);
  75.     else
  76.         sprintf(buf,"**** Cage imbalance: %d cage(s), %d baby monster(s).",cages,bmons);
  77.     if(inform_me(buf,1)) return;
  78. }
  79. if(tele > 1) {
  80.     ercount++;
  81.     if(inform_me("++++ Warning: Too many teleports",1)) return;
  82. }
  83. if(arrival > ((tele>0)?1:0)) {
  84.     ercount++;
  85.     if(tele == 0) {
  86.         if(inform_me("**** No arrivals without teleports.",1)) return;
  87.     } else if(inform_me("**** Too many arrivals.",1)) return;
  88. }
  89. if(( arrival == 0 ) && ( tele > 0 )) {
  90.     ercount++;
  91.     if(inform_me("**** No arrival for teleport.",1)) return;
  92. }
  93. if( you != 1 ) {
  94.     ercount++;
  95.         if( you == 0 ) {
  96.         if(inform_me("**** No start position.",1)) return;
  97.     } else if(inform_me("**** Too many start positions.",1))return;
  98. }
  99. if( mons > 1 ) {
  100.     ercount++;
  101.     if(inform_me("**** Too many monsters.",1))return;
  102. }
  103. if( exits != 1 ) {
  104.     ercount++;
  105.     if( exits == 0 ) {
  106.         if(inform_me("**** No exit to screen.",1))return;
  107.     } else if(inform_me("++++ Warning: Too many exits.",1))return;
  108. }
  109. if( hanging > 0 ) {
  110.     sprintf(buf,"++++ Warning: %d hanging boulders/arrows/balloons.",hanging);
  111.     if(inform_me(buf,1)) return;
  112. }
  113. ercount += hanging;
  114. move(19,0);
  115. if( ercount == 0 ) inform_me("---- Screen OK.",0);
  116. else {
  117.     sprintf(buf,"---- Total errors: %d",ercount);
  118.     inform_me(buf,0);
  119. }
  120. refresh();
  121. }
  122.  
  123. void readstring(str,size)
  124. char *str;
  125. int size;
  126. {
  127.   int count = 0;
  128.   char ch;
  129.   for(;;) {
  130.     ch = getch();
  131.     if( ch == '\n' ) {
  132.     *str = '\0';
  133.     break;
  134.     }
  135.     if(( ch == '\010') || ( ch == '\177' )) {
  136.     if(count == 0)
  137.         continue;
  138.         str--; count--;
  139.     addstr("\b \b");
  140.     refresh();
  141.     continue;
  142.     }
  143.     if(count == size) {
  144.     printf("\007");
  145.     continue;
  146.     }
  147.     addch(ch);
  148.     *str = ch;
  149.     str++; count++;
  150.     refresh();
  151.   }
  152. }
  153.  
  154. clearbottom()
  155. {
  156. move(20,0);
  157. addstr("                                                                            \n");
  158. addstr("                                                                            \n");
  159. addstr("                                                                            ");
  160. }
  161.  
  162. /* Print instructions around the screen */
  163. void instruct()
  164. {
  165. int loop;
  166. for(loop = 0; inst[loop] ; loop++)
  167.     {
  168.     move(loop,45);
  169.     addstr(inst[loop]);
  170.     }
  171. move(21,0);
  172. addstr("c: change name, m: change moves, q: save and exit, n/p: play game\n");
  173. addstr("Press '?' for full editor instructions. Use wanderer keys to move.");
  174. }
  175.  
  176. void noins()
  177. {
  178. int loop;
  179. for(loop =0; inst[loop] ; loop++)
  180.     {
  181.     move(loop,45);
  182.     addstr("                              ");
  183.     }
  184. clearbottom();
  185. }
  186.  
  187. /* save and restore screen data */
  188. screen_save(maxmoves)
  189. int maxmoves;
  190. {
  191. char file[90];
  192. char *oldname;
  193. int y;
  194.  
  195. clearbottom();
  196. move(20,0);
  197. addstr("Filename to write to? :");
  198. if(edit_screen)
  199.     addstr(edit_screen);
  200. else
  201.     addstr("./screen");
  202. move(20,23); refresh();
  203. addstr("                                   "); move(20,23);
  204. readstring(file,89);
  205. move(20,0);
  206. addstr("                                                                           "); refresh();
  207. oldname = edit_screen;
  208. if( file[0] ) edit_screen = file;
  209. for(y = 0; y<=NOOFROWS;y++)        /* make sure screen written */
  210.     if(screen[y][ROWLEN-1] == ' ') /* correctly...             */
  211.     screen[y][ROWLEN-1] = '-';
  212. wscreen(0,maxmoves);
  213. for(y = 0; y<=NOOFROWS;y++)
  214.     if(screen[y][ROWLEN-1] == '-')
  215.     screen[y][ROWLEN-1] = ' ';
  216. edit_screen = oldname;
  217. }
  218.  
  219. screen_read(maxmoves)
  220. int *maxmoves;
  221. {
  222. static char file[90];
  223. int y;
  224.  
  225. clearbottom();
  226. move(20,0);
  227. addstr("Filename to read from? :");
  228. addstr("Forget it");
  229. move(20,24); refresh();
  230. addstr("                                   "); move(20,24);
  231. readstring(file,89);
  232. move(20,0);
  233. addstr("                                                                           "); refresh();
  234. if( ! file[0] ) return;
  235. edit_screen = file;
  236. rscreen(0,maxmoves);
  237. for(y = 0; y<=NOOFROWS;y++)
  238.     if(screen[y][ROWLEN-1] == '-')
  239.     screen[y][ROWLEN-1] = ' ';
  240. }
  241.  
  242. /* save and restore edit memory data */
  243. edit_save()
  244. {
  245. char file[90];
  246. int i = 0,fd;
  247.  
  248. clearbottom();
  249. move(20,0);
  250. addstr("Filename to save? :"); refresh();
  251. readstring(file,89);
  252. move(20,0); addstr("                                                                           "); refresh();
  253. if(( fd = open(file,O_WRONLY|O_CREAT|O_TRUNC,0644)) == -1 ) {
  254.     inform_me("File cannot be opened for writing.",0);
  255. } else {
  256.     i = write(fd, edit_memory, (int)(memory_end - edit_memory));
  257.     if( i < 0 ) inform_me("Write error on file.",0);
  258. }
  259. instruct();
  260. }
  261.  
  262. edit_restore()
  263. {
  264. char file[90];
  265. int i = 0,fd;
  266.  
  267. clearbottom();
  268. move(20,0);
  269. addstr("Filename to load? :"); refresh();
  270. readstring(file,89);
  271. move(20,0); addstr("                                                                           "); refresh();
  272. move(19,0);
  273. if(( fd = open(file,O_RDONLY)) == -1 ) {
  274.     inform_me("File cannot be opened for reading.",0);
  275. } else {
  276.     i = read(fd, edit_memory, EMSIZE);
  277.     if( i < 0 ) inform_me("Read error on file.",0);
  278.     if( i == 0 ) inform_me("File empty.",0);
  279.     if( i > 0 ) {
  280.     sprintf(file,"Read in %d moves.",i);
  281.     inform_me(file,0);
  282.     memory_end = edit_memory + i;
  283.     }
  284. }
  285. instruct();
  286. }
  287.  
  288. /* Actual edit function */
  289.  
  290. void editscreen(num,score,bell,maxmoves,keys)
  291. int  num, maxmoves,
  292.      *bell,
  293.      *score;
  294. char keys[10];
  295. {
  296. int  mmbkup,x,y,sx=0,sy=0,quit=0,nx,ny,nosave =0;
  297. char (*frow)[ROWLEN+1] = screen,
  298.      ch;
  299. char buffer[50];
  300. char *howdead;
  301. char *storage;
  302.  
  303. if((storage = (char *) malloc(sizeof(screen)))== NULL) {
  304.     addstr("OOps... cant malloc a backup screen!\n\n");
  305.     return;
  306. }
  307.  
  308. for(x=0;x<=ROWLEN;x++)
  309.     for(y=0;y<NOOFROWS;y++)
  310.     {
  311.         if(screen[y][x] == '@')
  312.         {
  313.         sx = x;
  314.         sy = y;
  315.         }
  316.         if(screen[y][x] == '-')
  317.             screen[y][x] = ' ';
  318.         };
  319. x=sx;
  320. y=sy;
  321. if(maxmoves != 0)
  322. (void) sprintf(buffer,"Moves   : %d        ",maxmoves);
  323. else
  324. (void) strcpy(buffer,"Moves   : Unlimited");
  325. debug_disp=1;
  326. map(frow);
  327. move(18,0);
  328. addstr(buffer);
  329. move(19,0);
  330. addstr("                                                                          ");
  331. move(19,0);
  332. addstr("Name    : ");
  333. addstr(screen_name);
  334.  
  335. /* ACTUAL EDIT FUNCTION */
  336.  
  337. instruct();
  338. while(!quit)
  339. {
  340. move(y+1,x+1);
  341. refresh();
  342. ch = (char)getchar();
  343.  
  344. nx=x;
  345. ny=y;
  346.  
  347. if(ch == keys[3]||ch == keys[2]||ch == keys[1]||ch == keys[0])
  348.     {
  349.     if(ch == keys[3])
  350.         nx++;
  351.     if(ch == keys[2])
  352.         nx--;
  353.     if(ch == keys[1])
  354.         ny++;
  355.     if(ch == keys[0])
  356.             ny--;
  357.     }
  358. else if(ch == 'q')
  359.     {
  360.     clearbottom();
  361.     break;
  362.     }
  363. else if(ch == 'x')
  364.     {
  365.     clearbottom();
  366.     move(20,0);
  367.     addstr("You will lose any changes made this session - are you sure? (y/n)");
  368.     refresh();
  369.     ch = getch();
  370.     if(ch != 'y')
  371.     {
  372.     noins();
  373.     instruct();
  374.     refresh();
  375.         }
  376.     else
  377.     {
  378.     nosave = 1;
  379.     addstr("\n");
  380.     refresh();
  381.     break;
  382.         }
  383.     }
  384. else if(ch == 'm')              /* change to number of moves for the screen */
  385.     {
  386.     clearbottom();
  387.     move(21,0);
  388.     addstr("How many moves for this screen? :");
  389.     refresh();echo();
  390.     scanf("%d",&maxmoves);noecho();
  391.     if(maxmoves < 0 ) maxmoves = 0;
  392.     if(maxmoves != 0)
  393.         (void) sprintf(buffer,"Moves   : %d        ",maxmoves);
  394.     else
  395.         (void) strcpy(buffer,"Moves   : Unlimited ");
  396.     instruct();
  397.     move(18,0);
  398.     addstr(buffer);
  399.     refresh();            /* for some reason, this seems to add a '.' to */
  400.               /* the map... Ive no idea why yet... */
  401.     }
  402. else if(ch == 'c') {    /* change name */
  403.     clearbottom();
  404.     move(21,0);
  405.     addstr("New name: ");
  406.     refresh();
  407.     readstring(screen_name,58);
  408.     screen_name[61] = '\0';
  409.     instruct();
  410.     move(19,0);
  411.     addstr("                                                                          ");
  412.     move(19,0);
  413.     addstr("Name    : ");
  414.     addstr(screen_name);
  415.     refresh();
  416. }
  417. else if(ch == 'p' || ch == 'n')       /* play the game (test) */
  418.     {
  419.     noins();
  420.     mmbkup = maxmoves;
  421.     bcopy(screen,storage,sizeof(screen));
  422.     if(ch == 'p')
  423.         {
  424.         debug_disp = 0;
  425.         clear();
  426.         }
  427.     *score = 0;
  428.     howdead = playscreen(&num,score,bell,maxmoves,keys);
  429.     move(20,0);
  430.     if(howdead!=0)
  431.         addstr(howdead);
  432.     else
  433.         addstr("DONE!");
  434.     printw("; hit any key to continue\n");
  435.     refresh();
  436.     ch = (char)getchar();
  437.     clear();
  438.     bcopy(storage,screen,sizeof(screen));
  439.     maxmoves = mmbkup;
  440.     debug_disp = 1;
  441.     map(frow);
  442.     if(maxmoves != 0)
  443.         (void) sprintf(buffer,"Moves   : %d        \n",maxmoves);
  444.     else
  445.         (void) strcpy(buffer,"Moves   : Unlimited\n");
  446.     move(18,0);
  447.     addstr(buffer);
  448.     addstr("Name    : ");
  449.     addstr(screen_name);
  450.     instruct();
  451.     }
  452. else if(ch == 18) {  /* ctrl r -- read memory data */
  453.     edit_restore();
  454.     }
  455. else if( ch == 23) { /* ctrl w -- write memory data */
  456.     edit_save();
  457.     }
  458. else if( ch == 7 ) { /* ctrl g -- read screen */
  459.     screen_read(&maxmoves);
  460.     map(frow);
  461.     instruct();
  462.     }
  463. else if( ch == 16 ) { /* ctrl p -- write screen */
  464.     screen_save(maxmoves);
  465.     instruct();
  466.     }
  467. else if( ch == 12 ) { /* ctrl l -- redraw screen  */
  468.     clear();
  469.     map(frow);
  470.     if(maxmoves != 0)
  471.     (void) sprintf(buffer,"Moves   : %d        \n",maxmoves);
  472.     else
  473.     (void) strcpy(buffer,"Moves   : Unlimited\n");
  474.     move(18,0);
  475.     addstr(buffer);
  476.     addstr("Name    : ");
  477.     addstr(screen_name);
  478.     instruct();
  479.     }
  480. else if( ch == 'L' ) {
  481.     clearbottom();
  482.     check_legality();
  483.     instruct();
  484.     }
  485. else if(ch == '?' ) {
  486.     helpme(0);
  487.     if(maxmoves != 0)
  488.     (void) sprintf(buffer,"Moves   : %d        \n",maxmoves);
  489.     else
  490.     (void) strcpy(buffer,"Moves   : Unlimited\n");
  491.     map(frow);
  492.     }
  493. else if((ch == 127)||(ch == 8)) {  /* delete key */
  494.     if(--nx < 0)
  495.     {
  496.     nx = ROWLEN -1;
  497.     if(--ny < 0 ) ny = NOOFROWS -1;
  498.         }
  499.     screen[ny][nx] = ' ';
  500.     move(ny+1,nx+1);
  501.     addch(' ');
  502.     }
  503. else
  504.     {
  505.     if(ch >= 'a' && ch <= 'z') ch = ch - 'a' + 'A';
  506.     if(ch == '"') ch = (char)getchar();
  507.     if( ! (ch < ' ') )
  508.     {
  509.         screen[y][x] = ch;
  510.        move(y+1,x+1);
  511.         addch(ch);
  512.         nx++;
  513.         }
  514.     }
  515. if(nx < 0)
  516.     {
  517.     nx = ROWLEN-1;
  518.     ny--;
  519.     }
  520. if(nx >= ROWLEN)
  521.     {
  522.     nx = 0;
  523.     ny++;
  524.     }
  525. if(ny < 0) ny = NOOFROWS-1;
  526. if(ny >= NOOFROWS) ny = 0;
  527. move(ny+1,nx+1);
  528. x=nx;
  529. y=ny;
  530. }
  531.  
  532. noins();
  533. move(20,0);
  534. refresh();
  535.  
  536. if(! nosave)
  537.     {
  538.     for(y = 0; y<=NOOFROWS;y++) /* certain editors - eg ded - have a */
  539.                                 /* habit of truncating trailing spaces*/
  540.                             /* so this should stop them! */
  541.         if(screen[y][ROWLEN-1] == ' ')
  542.         screen[y][ROWLEN-1] = '-';
  543.     wscreen(num,maxmoves);
  544.     }
  545. }
  546.