save.y 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. %{
  2. #include <stdlib.h>
  3. #include <stdio.h>
  4. #include "main.h"
  5. #include "Game.h"
  6. #include "Creature.h"
  7. #include "Character.h"
  8. #include "Curses.h"
  9. #include "Map.h"
  10. #include "save.h"
  11. game G;
  12. chr c;
  13. block b;
  14. int n;
  15. %}
  16. %union{
  17. char *str;
  18. int num;
  19. };
  20. %token <num> NUM
  21. %token <str> CH STAT
  22. %token MAP
  23. %%
  24. save:
  25. | save object
  26. | object
  27. ;
  28. object:
  29. character
  30. | map
  31. ;
  32. character:
  33. type '{' stats '}' { LoadCharacter(G,c); ResetCharacter(c); }
  34. ;
  35. type:
  36. CH { c->ispc = (strcmp( $1, "pc" ) == 0); }
  37. ;
  38. stats:
  39. stats ',' stat
  40. | stat
  41. ;
  42. stat:
  43. STAT '=' NUM { SetStat( c, $1, $3 ); }
  44. | STAT '=' pos
  45. ;
  46. pos:
  47. '(' NUM ',' NUM ')' { SetStat( c, "y", $2 ); SetStat( c, "x", $4 ); }
  48. ;
  49. map:
  50. MAP '{' blocks '}'
  51. ;
  52. blocks:
  53. blocks ',' block
  54. | block
  55. ;
  56. block:
  57. '(' NUM ',' NUM ',' NUM ',' NUM ')' { b->h = $2; b->w = $4; b->y = $6; b->x = $8; LoadBlock(G, b); }
  58. ;
  59. %%
  60. void yyerror() {}
  61. game LoadGame() {
  62. n=0;
  63. fprintf( L, "Creating game.\n" );
  64. G = CreateGame();
  65. fprintf( L, "Creating pc.\n" );
  66. c = CreateCharacter(G);
  67. b = CreateBlock(0, 0, 0, 0);
  68. fprintf( L, "Opening saved game file.\n" );
  69. if( ( yyin = fopen( "game.save", "r" ) ) == NULL ) return NULL;
  70. fprintf( L, "Parsing saved game data.\n" );
  71. yyparse();
  72. box(G->arena, 0, 0);
  73. wrefresh(G->arena);
  74. fprintf( L, "Closing saved game file.\n" );
  75. fclose( yyin );
  76. if(c) {
  77. PurgeCharacter(c);
  78. DESTROY(c);
  79. }
  80. if(b) {
  81. DESTROY(b);
  82. }
  83. return G;
  84. }