mapgen.c 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095
  1. /*
  2. mapgen.c
  3. tile <ascii> <mapto> <top> <right> <bottom> <left>
  4. ":"<mapline>
  5. num := <hexnum> | <decnum> | <asciinum>
  6. asciinum := "'" <char>
  7. hexnum := "$" <hexdigit> { <hexdigit> }
  8. decnum := <decdigit> { <decdigit> }
  9. decdigit := "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9"
  10. hexdigit := "a" | "b" | "c" | "d" | "e" | "f" | "A" | "B" | "C" | "D" | "E" | "F" | <decdigit>
  11. The value 0 for "top", "right", "bottom" or "left" means match any.
  12. */
  13. #include <stdio.h>
  14. #include <stdlib.h>
  15. #include <stdint.h>
  16. #include <string.h>
  17. #include <sys/types.h>
  18. #include <sys/stat.h>
  19. #include <unistd.h>
  20. #include <assert.h>
  21. #include "u8g2.h"
  22. #include "ugl.h"
  23. extern void u8g2_SetupBuffer_TGA(u8g2_t *u8g2, const u8g2_cb_t *u8g2_cb);
  24. extern void tga_save(const char *name);
  25. extern int tga_init(uint16_t w, uint16_t h);
  26. extern u8x8_display_info_t u8x8_tga_info;
  27. const uint8_t scrollosprites[6642] U8G2_FONT_SECTION("scrollosprites") =
  28. "\323\0\3\3\5\5\5\6\6\20\20\0\0\17\0\17\1\4\11U\31\331 \6\0@\30\6!$\316"
  29. "\305\20>\60\16\211\243\320A\326\220,\16\220H\342\0ID<\31\217cs\330\60:\207\315\201r("
  30. "\0\42$\316\305\20>,\16\212\243\320A\223\304\220(\16\212\305\1\21:$\34\245\203f\341\71l\16"
  31. "\233\3\345P\0#\37\255\305\20>\240\16\70\213\344\0\221\34B\11\207\250\223\324\71h\16\232\203\346\240"
  32. "\71L\16\4$\35\214I\21\236\65\34\213\320\42\21\233\304&\261\211(!\361D\226\24\11\306\222\243\66"
  33. "\0%\32\314\311\20&\35`\16\206Eb;\200xx\30\22'\224!\35BG&\2&\27kI\21"
  34. "\26E\26y\70XB\262\231p$,YD\304\362$\12'$\316\305\20\346A\30:\304\42\7Y\34"
  35. "|\220\306\1\321\203T\365 \215\3\242\7\221D*\12\35d\207\31\0((\316\305\20N\344\20\211L"
  36. "\16\221P\344\20I\307!\16\210\36\244q@\364 G\214\34\42\221\311!\22\212\34\42\351P\0)\35"
  37. "\313\315\20>@\16\240\332H\242\241H\70\22U\16\223C\314J\7H.\222K\344\60*\34\316\305\20"
  38. "\306C\34*\71X\16\226\203\345`\71<\324\21\336QBJ\241C\345p+%\356E\20\246Y-\16"
  39. "\213\304DA\71($\225F\342\260HD,\221\331\346\60\363\341@\71TB\207PX\14,'\316\305"
  40. "\20F\70\32\211\303\302uH\64\34\7\4c\304H(\30\15e\11\305d\301\70$\32\216\3\254qX"
  41. "$\32\16-&\316\305\20>\60\35\22:\330\342\220`$\30\11\206\62\246\5c\301\244\214\221`$\30"
  42. "\207\304\16\246\70$\30\7\2.*\316\305\20F\34\32\11Fb\321H:(\222\16\212d\7D\262\211"
  43. "\42Y\204\307\220L\32\221D\304\63\71\340\30+E\342\320\0/&\316\305\20.\35\42\233\6c\211\321"
  44. "P\60\32\211F\307\261\210\34\60\222\311\1\221\220\70\24\207\245C\242B\71\204\12\60\36\314I\20\236\365"
  45. "\20<\4#\224\340!\250x\210Z\303\261\303Db\241Xd\42q\60\6\61)\317A\20\256\35r\210"
  46. "\3\16q@\204\222z\10\5\225d\207\220\320$\11\205)\221\303Pb\13IlQ\221,%\30\212\0"
  47. "\62*\317E\20\246\35r\10\5\17\241`\204\22\12\36BA\245\340!\24.\235e\222\310A\42\211\324"
  48. "\42\222HY\42\222\203\202Q\0\63+\356I\20>\64h\32\35B\261C(\26\241\204b\207PL("
  49. "\212\35BA\223,L:T$\23\251D\226\70\221\305\16\241\10\0\64(\356I\20\226\35d\7\34\302"
  50. "\251\221\210$\61b\11\206H\321p\34`\7\204c\221\303\204bI\221X\244\42\71$\30\5\65*\16"
  51. "J\20>L\16\225\32C\207\360!\24\213P\62\35B\61\241(v\10\5M\262\60\211d\221X%\42"
  52. "\71D$\7\10\205\0\66%\315I\20\236\371\20=\204R(\241\320!\24\22\212B\207P\314$\12S"
  53. "H\207\210Q\16\10\305l\241C(\0\67%\354I\20\316D\62;\304\221\16\61\11Et\30\261D\16"
  54. "\221\230mBY\221L\224$\24\221D&\222\12E\0\70\25hQ\20\26\315\24\221\204Lj\224\304\203"
  55. "$B$\206B\0\71\42\255I\20V\34*\36\35\206\207h\204\22=\204B\63Q$\62\21\316a\207"
  56. "\350!*J\215\205\202\0:\34\314I\20\236\365\20\264\4#\306\71H\221\22\244X\224e\207\250\365\70"
  57. "\223\303\202\0;#\316E\20\246\35p\10\37\302\22\345Cx\64\215Xb\343\311\341 \71DD\61Y"
  58. "\360\60<L\205\62\0<,\356E\20\246\35\20\216\306!\301\70$\30\11F\202qH\60\16\11\306!"
  59. "A\261\60b\211\305a\221P\70$\12\207J\42Z\204\22\3=$\316E\20\246\35p\210\36\206\22\212"
  60. "\360\60\212\220(\21\211EL\207\10\245\207\231\304\42\265CDrP*\0>)\317A\20\256\35r\10"
  61. "\37\304\22J\342A\24\211\220F\22\211MJ\223\304\202\244\320!\24:\310B\22[T$KJ\213\0"
  62. "?+\317E\20\246\35r\10\305\16\223\230\204\42\211\35&)$JHbI\246]g\222\310A\42\211"
  63. "\324\42\222HY\42\222\303Ba\0@+\357E\20>\70j\232\35B\261\303$&\241Hb\207I\12"
  64. "\211\22\222X\222iBa\355\60\224LdQY\352D\26<\204\42\0A'\356I\20\226\35d\7\34"
  65. "\302\251\221\210$\61b\11\206H\321p\34`\7\204c\221\303\204bI\264HErP\62\0B)\17"
  66. "F\20>P\16\26\33c\207\360a\22\223P\62\35&)$JHbI\246\11\205\65\22Qb\7\251"
  67. "\203\324!B!\0C%\316E\20\246\35p\210\36&!\11E\22:L$$JDbI\245)\226"
  68. "\330$\266\70,\24\264\305\16\241\0D'\356E\20V$\42I=\304Q\17C\11Ev(\65I,"
  69. "\42\21I\22\22\212B\303\231\304\42\265C\354\200C\14\0E\30\212M\20\216P\22\42\205\214\21I\320"
  70. "(\222Rb\301X\204\314j\2F \253I\20^\70&\224\35D\207H\12%\205\62\211L\42*\246"
  71. "\71\344\20;\304DiI!\0G#\316E\20\246\35p\210^\204\222ci\42\241\221$\42\71\210$"
  72. "\24\16\17B\211\35b\207\210\344\240T\0H%\316E\20\246\35p\210\36F\221\211d\22\71\24G\323"
  73. "\210%\66\236\34\16\222CD\24\223\5\17\303\303T(\3I.\360A\20\256\35\24\216\3\342\220h\34"
  74. "\26\14\5C\241\71\214\22\207E\222\302\241\30\221\32\42E\343\260\244p(D\246\206DqPD\22\5"
  75. "J\42\255E\20\246\371\20=D\17\321\220(z\10FD\222\220\304\205R\211\260E$\221\231$-$"
  76. "\24\1K\36\314I\20\236\65T\34I#\243`)(\33\36\242F\261\344p\261P,\62\221\70\30\3"
  77. "L\64\17F\20>\70\16\220FM\263C\34p\10\5C\242\304p(\70\32\205C\243\10%\244\42\11"
  78. "GT\42\24\231$R\213HB\322\210$I\26\222\4c\21\0M&\354I\20\246\325\22;\4\303\261"
  79. "\70$\24\42\245\210$\261\10%\32\14\256\204\224(\241\10%)\222\30\12\13E\0N\63\16F\20\206"
  80. "\34*\264\305\16\241\220)\22:\204D\341X$\16\11EB\244\224\210H\22\22\31)\224\30\205\42\216"
  81. "H\42\222\230$E\22\233\10C\207\30\0O+\356I\20\246\35`\7\34\302\251qH\60D\12FD"
  82. "\222h\204\22\7\4\343\200\10%\26\71L(\226\24\211E*\222C\202Q\0P%\317E\20\246\35r"
  83. "\10\37\246\22\212\360@,\21%\226\240\210\22\14\11\305\207PLR\223\216B\303\203\34`\4Q!\216"
  84. "E\20\226P,\224\3\312\221\210\362A\26\7\214\242\227\340!\22\213\134\242\227\350%\30\241\210\0R\36"
  85. "N\305\21V\34\22\13\5C\21\221\22I\251\42\222\34\36\42\222C\304B\11e\221C\3S \314\311"
  86. "\20\16)j\12\36C\226X\250|\210\34D\7I\304\134\212EL\301cH\222\32\251\0T,\356E"
  87. "\20V\210\24\214Xb!S\222)\26\42\205$!Qdr\230\304$\212\23\312$\24\22\205$\241,"
  88. "\241\34\343\220h\70\33\0U'\356E\20\336\34V\7E\346\240h\34\22\222\243\304AtH\70\16\260"
  89. "C\254\24\11\227\212X\42\12\207\42\311\221\10\0V\37\216E\20\356\371 =\10G\61\212\312\212J>"
  90. "I\262EF\242\231\251R\212\215#I\26\0W%\356E\20> \16\252\3\346\260\203\345p:\324!"
  91. "\346C\360 \214P\302\207p$\24\11\247\3\202q\20\25\0X)\356E\20\246X\30\11F\202\207\351"
  92. "!\24\223h;D\244\64\311$\24\211\34E\207\322\241\64\222Md\341H\60\16\11\306\1Y-\356E"
  93. "\20\246X\30\261\4\17\223X\204\222x\210\204\210\222\310D$\212\220D\241\12%d\24\35\42#\311l"
  94. "$\21\216B\62RH\26IZ&\356E\20\226\304\42\212\34\42\261\303\354P\232PF\207Jd\70Y"
  95. "\232\224H\347\203\360\360P\241,\221$\243H\2[(\357E\20\266$\16\272\3&\325\303\324$\225\314"
  96. "A\242Ix\224\26\42\311b\345\330\241h\207D\330&\223\234\242\221\24\0\134,\357E\20Fd\42\11"
  97. "\305$\7\351\304xI\264C\326\202#J\220\24\13EJ\243\220\71t\60\35\342\240\12MB\311\42\211"
  98. "FR\0]*\15J\20f(\16\241C&r\300!x\20N\306Ca\35r\20\36\42\241\203D\22"
  99. "\271Hb!\321,\65\24\213CbA\0^+\356E\20>,\216\42\207\312\16Q[\354\20\13\35b"
  100. "\241H\61\24\7\204\222\304\241\210\34\20\212\206\42\241\70,T\216\320$\241\0_*\356E\20N\34\22"
  101. "\15\247\312\42qXDH\212\310L\243\342\210B\31I\316\244\10K\344\62\221\204(\321Hd$\212h"
  102. "\231\0`*\356E\20\246\371\60;\224\206\225\221\350\20\11E\16\221P\344\20\11E\16!\321Ah\71"
  103. "\224H\262\230\34\64\213\210$*\23\2a \216E\20\256\35`\211\36\302Bq$\24\11\247F\202\221"
  104. "\330\204\62\42R$\207\210L(N\3b-\356E\20n\60\16\20\305A!\71\220\16\261\3&\21i"
  105. "$e\32\7\10#\261\210\60\16\220\206\203\222`D\222R\212\10kqX\14\0c(\316E\20\346$"
  106. "\62\215\3\344PY\60&\207\226&\222\310!\42\211\34\42\21\311!B\71D(\207\10\35\61\34\25\213"
  107. "\0d(\356E\20> \216\315\16\70Dd\21JDv\10\305\302\241\210\34\42\11\35b\241CLr"
  108. "\210I\16q\14\321\260p\4e!\356E\20\246\35b\207\330a\342\211\213H\342K\311\64\12\207&\207"
  109. "\10\345\20\221\35\302B\351p\4f\22\20B\20\206\344\377RG\262\374\377%\216$\371Rg\32\20B"
  110. "\20\206\344\377RG\222L.+\227\225\313\34<\331\377\22G\222|)h\33\20B\20\206\344\377RG"
  111. "\262\203N\226\223\345d\71YN\226\223%\216$\371Ri-\20B\20\206($\241\210E\242\70\262H"
  112. "\34\32\251\312\1r\320\70Z\34E*\241a\35:\7\211e\21\222DJ\222\203\304\62\71\204\4j-"
  113. "\20B\20\206($\241\210E\242\330\34Z\224Ej\304\32u*\13VG\307\311!\16\71D\244\207\310"
  114. "$t\10\11\357\260\222L\16!\1k-\20B\20\206\244D\221\134(\221\203\355 \21\36dB\243d"
  115. "B\232\215+\223\226\320\205v\220\10\17\63\311a\64:\210\204\27\231\34B\2l'\20B\20\306\303\34"
  116. "Ar\250H\16\25u\210v\210\246\212\246\212\246\212\246\212\246\212v\210\312\241\42\71T\344\10\17\3m"
  117. ":\20B\20\306\303\34Ar\250H\342\260\210$\16\213Hb\221\244\210$\24\311E\22\213$E$\241"
  118. "H.\222X$)\42\11Er\221\304\42I\21I\34\26\221\34*r\204\207\1n'\20B\20\306\303"
  119. "\34Ar\250\210\16#\331!&\64J\251b]CQa\242,\34\23\305!!I\34\26\221#<\14"
  120. "o\61\20B\20\306\303\34Ar\250H\306\23\311\322D\62I\232H&I\23\311$i\42\231$M$"
  121. "\223\244\211di\42\31O$\343\211\344P\221#<\14p!\316\305\20\306C\34*\207\312\241rP\35"
  122. "T\7\25)EJ\221r\210P\16\21\312!B\71\34\10q#\356\305\20>\214\16\243\303\250\24*\205"
  123. "J\71D(\207\10\345\20!V\210uP\35$\207\312\241\207C\0r%\316\305\20^\204\22\234P&"
  124. ",\207\10\345\20\241\34\42\224C\204R:P\344\300P\204r\210P\16\21v\204\7s\15\304\355\20\216"
  125. "\344P\341\213$)\30t&\354I\20V\60\16\11\311\1\242\60\35b\225D\206\223Lr@H,\211"
  126. "\34\42r\330\301r\230\304a\221\303\4\0u)\320\301\20\346!\16\210C\242Aa,\30\12F\204\254"
  127. "R:\264\16\243\310!\222\311a\242r\210\310\344\20\351a\16\70\4\1v\24\212M\21\226\355\20\71\30"
  128. "\17\207\360\341\17\22IE$\11w\31\316\305\20\266\370\360A\16;\34\352\220\303\37\346\310r\250\34*"
  129. "\207\212\1x\17\350P\22\216\345p\220\330a\42\211P\0y\22\216E\21\306\377\357\10\17\221IJ\313"
  130. "\245r\207\6z$\356E\20\306?\304\241\222t\210$Ez\70\310\241\22I\312D%er\70\310\241"
  131. "\22J\26\11%\313\341\20{\32\316\305\20\316\241r\370\377r\250\304\241\221C\35I\16\21\311!\42\71"
  132. "D\2|\32\314\311\20\236Mb\241X\346\260\310ar\370\203\35z\250C\345\20:D\0}\26\220A"
  133. "\21\306\377\37\352h\221|\221\344\247H\276H\362S$?~\12\10R\20\306\377\377\203\0\23\310Q"
  134. "\21\306\377!\16\212\244H\62ER$\231\42\31\200\32\314Q\20\306\377\207\341!\22\71\210\42\207H\344"
  135. " \212\34\42\221\203\360\20\4\201\33\314A\20\306\377\203\360\20\11\35$\221C$t\220D\16\221\320!"
  136. "x\10\36\2\202\31\314Q\21\306!x\10\36\376\17rp$\27I\236\42\271H\362\24\311\3\203\27\314"
  137. "A\21\346!x\370\230\203#\271H\362\24\311E\222\247H\36\204\37\14R\20\306!x\10\36\376\37"
  138. "\206\207H\344 \212\34\42\221\203(r\210D\16\302C\20\0\205\36\14B\20\346!x\370\30\36\42"
  139. "\241\203$r\210\204\16\222\310!\22:\4\17\301C\0\206\42\320A\20\306\377\37\212\207h\350\20\211H"
  140. "\42\207P\322!\22\221D\16\241\244C$\361\20\207\34\202\0\207\33\320A\21\346!\16\71\4\17\377?"
  141. "\324\321\42\371\42\311O\221|\221\344\247H~\210'\20B\20\346!\16\71\4\17\377?\24\17\321\320!"
  142. "\22\221D\16\241\244C$\42\211\34BI\207H\342!\16\71\4\1\211\13g\320\23V\70\22\221\205\0"
  143. "\212\16\307\320\22\246,\62\235CD!\31\0\213\16\246T\23\226(\42\31IB\61\21\0\214 \316\305"
  144. "\20.\35r\210\36f\207\322\241r\370P\254LB\221\241D\35$\207\312\241r \25\0\215\32\314\311"
  145. "\20\256\34D\7\330\21\255\207\330a\216r\210\35&\207:\232\34&\5\216\42\256\305\20^$\24\211\3"
  146. "\202\341\341\70\30\7\245RB\21J\310\24\246\3\345P\71T\16\225\3\251\0\217\24\214I\21\226\22I"
  147. "\35\257\42\21I\35\257\42\21I\11\0\220!\316\305\20\246\35p\10\37\204\207!\35\66\61J\16\341C"
  148. "Lr\220P\16\227\203\205t\231H\214\4\221\23\212\315\20\36\325v\10\35\42\207\323\321\213dF\65\1"
  149. "\222\36\214\311\20\256(\34\212D#\212\21J,bI\71D\42\207Q\376\64\221L\351\0\33\0\223\35"
  150. "\214\311\20FD\313\341\201\16>\214\42\226PD$\11\15G\303\321D\62\245\3l\0\224\34\216\305\20"
  151. "\306;\70\26\7\305\342\240\70\370p\210\261M\326\230#\351\240X\34\66\4\225#\256E\21n$\16\225"
  152. "\3\347\60:@B\11\212H\261\330\60\24\23\317\302\22alb)\35\210\7!\0\226 \314\305\20>"
  153. "$\16\224\4Ct\10\35`\12\215\204\243a\214\32\241Z\345 \71H\16\222\303\0\227!\256E\21."
  154. "\35\26\212\203\42\222\70@(\214\34\42Q;\242\310$\264C\254\221\10%Q\35`\4\230$\316\305\20"
  155. ">L\16\215\304\201\241\70\250\16\231\303\346\200\320\34\60\221\3\350\240;\340\20\276\3\354\220:\10\0\231"
  156. "#\316\305\20^\35J\221\203\352\260\71t\16\21\311\1\63\351P\70\216\315\1\241\71$\62\207\315\201r"
  157. "(\0\232#\314\305\20>\200\16\10J\203\221`J\64\224J\212\12c\21R:$\224\227`H\24\14"
  158. "\7\343\0\12\0\233\37\254I\21>D\16J\226\305!\241X-D\212\235b\246Xd\226\16\210\305\1"
  159. "\301h\270\12\234\30-\305\21\316!\42\71DN\23Z\34D\231\234&\207\312\241r\250\20\235#n\305"
  160. "\21\346$\62\215\3\204q\200\60\16\20\306\1\245\11\345\20\241\34\42\224C\204r\210P\16\21\12\0\236"
  161. "#\316\305\20\246\35\20N\22E\305\62\211EB>\34(\207J\344\60\221\331\42\311\221\20EB;\214"
  162. "\215\0\237 \216\305\20\356\34\32\31\206B\264\71H(\26E$\61:\350\30:\204(\242QUr\7"
  163. "\24\1\240\27\14I\22\316\34R\7\304\342\200\330Av\220\5\17\301Hd\16\1\241\30\15E\22\246\34"
  164. "\26\212\3F\341\320\201t\230\214b\263P,\22\224\3\242\27k\311\20^U&\214M\253i\222\10e"
  165. "\22\71\204\16\17\246\33\0\243\37\256\305\20^\35r\7\330A\224Z\360\66\271C\352\220h\34R\252D"
  166. "\17\221\312\35R\207\0\244\26J\315\21\226\315\22\71D,\22\312h\351&\21\245\204c&\0\245\27J"
  167. "\315\21\246-R\212PRH\22\232d(\21F\302\241h\314\10\246\27J\315\21\226-\62\212DJ\222"
  168. "\222\244$)\211frHr\314\4\247\30\214I\21n\371,\22\213\244\21\362XQ$\207\310!\42\331"
  169. "\270\16\21\250\17\314\310\22>\214\16\71\230\16v\10\35\6\251\27k\311\21V\365v\20\225D\224\210\210"
  170. "\62\211\20\17\17\224\303\354\2\252\30K\311\21\216QV\21\306h\242\310\201\26I\241C(Y\16\224\303"
  171. "\0\253$\316\305\20>L\16\245\303\350 \71P\16\242C(\322\211\34\20!EMQ\211(L\213\3"
  172. "dq\20\35\2\254#\316\305\20\16\35\24\224\3\302\322\70D\26\207\205b\323H,\24\25N\345\320\71"
  173. "\354\360Ar\250\35F\0\255%\316\305\20>\70\16\231\304!u\350x&-\311!\223\340LZ\22\306"
  174. "&\321\240\34\24\222CbqHH\16\2\256\37\216\305\20>L,\222\10%\25Y\304\32\261\36\244\7"
  175. "aez\220\306(\62\321DJ\7\1\257\37\254\311\20&\371\20\223\225\42\42\212\322uB\233D\16\25"
  176. "\313\350\24;D\202\222X\34\26\1\260&\316\305\20.\35&\221C\354\200\203\220b\212X\16\222\203\344"
  177. "\20\23\31#!Ih\26\22\305\1\301\70(\35(\7\261\27\212M\21\36\325v\10Z&\241\313Ar"
  178. "\230\34$\207\313!f\2\262\31L\311\21\336\34\62\221\36D\207\213\354 ;\310&\241C$t\70\214"
  179. "\4\263\26\212M\21\226\34\62\207\210%\25\222\371\360\201r\10\35b&\0\264\31\256E\21>@\16\224"
  180. "C\345 \241pBY\361\277L$\26\251\35D\5\265\42\314\311\20fl$\221\214F\21I\204|\70"
  181. "D*\7\213\344\60\233\220\351 \71H\22\207d\7E\1\266\33n\305\21>\70\16\230\210)b\312\224"
  182. "B\235P\17\322C\364\20\266\3\352 \0\267\37\314\311\20\356\34R\224T$\323\221TZ\207\314\61\204"
  183. "e\341I\34\42\211\303\342\300\70\4\0\270\27kI\21\336\351\360\60\246\211$\222\211t\16\231\3\350\0"
  184. ":D\12\271\42\315\305\20>H\16\234\3$T\312\320|\10^\202\21R\60$\213\3c\323 -<"
  185. "\221C\344 \0\272$\314\311\20> \16\222\203\310\207\330D\222\242t\270H&\222I\344\20I\207\204"
  186. "&I\241IRL\222\325\6\273(\316\305\20F\34\355|\20\36&\241\303\360@<DD\61Y$\64"
  187. "\13EB\223\244Hh\222$\233$\205$\322\240\304\10\274\32\214\311\20\236U\250\355\60\221X\312\207\7"
  188. "\313a\22\71D\322!\261C\10\0\275#\316\305\20.\35\244\16\261N(\223\242\351\20\22\35B\223C"
  189. "Dr\250\35\246\207\70\204\16\12\306AT\0\276\22h\321\20\26M$\262\321\202\221!\307H\60F\2"
  190. "\277\35\254\311\20&\35 \22\333\1t@\60:\214\215#t\0\35B\207\314A\21\261\320\6\300\32\314"
  191. "\311\20&\35 \22\33%\24I\34F\207\230\257_#\65\241H\16\221\2\301\27J\315\21>D\246\66"
  192. "\211\3\304\22!E\64\211H&QIx\16\302&\316\305\20>\214\16\252C(q\0)L\12\223\302"
  193. "\244\240d\24\225\210\342\0\311\34$\7F\346\0\231\34 \207\2\303 \216E\21>,\16\215\304a\351"
  194. "\220\240\34\20\233FBSId*\242\312\246B\251\34*\207\2\304$\316\305\20\366!l\211\3Hq"
  195. "\210,\16\212\305\1\241\344Q$:\24\316as\330\34\66\207\315\201r(\0\305 \316\305\20>T\16"
  196. "\234\303\346\260\71P\16\213C\347\260\71P\16\213C\347\260\71l\16\224C\1\306\27\214I\21v\371z"
  197. ",\5i\61\232l\64\273\226\351\200\71H\16\3\307 \316\305\20>@\16\14E\304\301i\70u\34+"
  198. "G\354\0\253\210:\223\316as\330\34(\207\2\310\35\214I\21\326!v\230\34D\7\231\271\42\254\10"
  199. "\353\0:d\16\212\304!\261C\10\0\311\27\314\311\20\206\34b<\230\256\257\25aEh\276\276Fj"
  200. "B\33\0\312&\316\305\20\206\34v\230FI\261\340$\32\214\304\1\301\210\70\24\241\3b\343\250\64\24"
  201. "\314\26\207\205\342\300\210\34*\313(\316\305\20>H\42\207\205\342\220P\34\224\42\16\305\242\241\344P\252"
  202. "(U\42\212J&q\310$\16\213\310\201rh\34\4\314\23J\315\21\226\355\20\71|\20\12c\352\220"
  203. "\261D(\1\315\27L\311\21\326!v\30\35F\207\321a\24\207\204\342\220\20/D\2\316\31N\305\21"
  204. ">L\16\233\304!\224\70\240\16\261\3\316\207\70\200L$\23\5\317&\316\305\20>(\22\7F\322A"
  205. "\221\244H(bI:D\322\202\226\20\345\61d\241\5o\24;$\30\7\321A\0\320%\216E\21>"
  206. "(\42\7M\344\20\311$\42\233TD\222\312hR\21\5'\243 EF\11\306!\301\70\210\16\2\321"
  207. "\27N\305\21N\251;\32\211H\42\213\244\223Pdb:\224\16\304\2\322\32LI\22\6EB\207F"
  208. "\16\21\311!\22\71L\210\207\340!x\10\36\202\4\323\30\356D\22\316\241\22\207V(\207H(r\210"
  209. "H\42\224I(\62\246\2\324\33nE\21\236P*\241\310\306\223\342\201t(\331Fs\200\35\20\216\222"
  210. "\210K\0\325\31L\311\21\6\361\20\221\34\202\224\341l\64\213\311B\207\321a\216r\10\1\326\32l\311"
  211. "\21&\35\20\214\21/\241\310Er\20\35\16\23\22Y|\230\35B\0\327\37\314\311\20\246\325z\10\206"
  212. "cqH(\16\11\305!\261p\64\30\234H&\304C\360\134\207\10\330\30kM\21\316\32\205D\251L"
  213. "d\252e:\200\16\210\210c\302h\15\0\331\37\254\311\20\216X\22\62\311ar\330\34\242B\21\216\242"
  214. "\245`)X\12Fd\321`\34@\4\332\20\214H\23FP\70\42M\222\42)\207\11\0\333\21\256\304"
  215. "\22\316\341\60\233\211b\263,\261\340l\2\334\33\216E\21\246YM\16Q\233\204HJ$%\222\22I"
  216. "\211\66!\311\301\312F\0\335\32\214I\21\256\34D\7X\17\261\303\344P\244C\350\20:\204\16\241C"
  217. "\210\0\336\26kI\21\326Av\10^\215\267C\250\62\251]#sP\34\2\337\15J\315\21\36\231\355"
  218. "\360C\215\314\6\340\22J\315\21\206\270\70\31\315\254d\252m\64\31\226\5\341\23L\311\21\16\211r\370"
  219. "\203\345\60;D\355\0:H\12\342\35\256E\21\266\34*\7\322a\324\303\345P;L\17\321\303\360\60"
  220. "+\225\306\23\71L\0\343%\316\305\20\266\34 \207\210B\246\340!z\30\36F\221\303Dr\230\204\16"
  221. "\303\303\364\20\14\231Br\210\34 \6\344\21\212M\21N\361\20\265\226\253\35\213\226C\254\10\345 \316"
  222. "\305\20.\35r\210\36fD\322x\62\223\231H&\222M\66\31\217\210\264\303\364\20\207P\1\346\35\256"
  223. "E\21\266\34H\207\321Av\210H\16\30\215GS\22\221D;\224ZL\207\203\1\347\35\253I\21>"
  224. "H\16\240\332\16\261Rl\30\13\307b\222X\314\22;\4%t\210\34\2\350%\316\305\20v\34\24\23"
  225. "\5E\24\231\350\20\12\35\204\7\312\341@\222\234b\22J\34`\7I\344\240\230Xl\4\351#\256\305"
  226. "\20VT\30I\223\316a\221\344`\70\222\16\233\303\42\351\300\70$\16\214\244\303\346\260H:\60\16\352"
  227. "\35\312\315\20\246\34\20\12\207\242\243\340(V\12\225\42\266\233d(\207\310!\311\61\23\0\353\23\252\315"
  228. "\20\336\315\261\365p\240\222\327\1r\200\34\22\7\354!\316\305\20.\35r\210\36fD\322x\62\223\231"
  229. "\222L\21\221IF\221\235f\305\71t\16\236\4\1\355$\316\305\20>\240\16\71\37\244\7i\244\22\215"
  230. "\3\42\243h\244T\232\304A\261\70l&\7\322a\241\70P\12\356\36\316\305\20\366!|\210\303\346\260"
  231. "Qm\34\33\307\16\221\340\301\216\3\35\32\207\306\241T\0\357\33\312\315\20\226\355\20\71<\204D\63m"
  232. "\221\321\220\30\221$\205C\22\65+\15\0\360 \316\305\20\306\34r>\10+\245\332\250\32\252C\352\220"
  233. ":\244\24\64\221\332\206\263\210P\222\16\15\361$\314\311\20\316a\22\207I\250\22\252\204*\241J\250\22"
  234. "\252\204\32\211\14c\21Y\64\22\212\3Bq\220\24\0\362\35\312\315\20\306\203%\34\12\207\302\261`\64"
  235. "\24\16E\203\261\220(\205\22:D\16\7\3\0\0\0";
  236. struct tile_struct
  237. {
  238. int ascii;
  239. int map_to;
  240. int condition[4];
  241. int item_index; /* reference into item_list */
  242. };
  243. #define TILE_MAX 4096
  244. struct tile_struct tile_list[TILE_MAX];
  245. int tile_cnt = 0;
  246. #define ITEM_IDENTIFIER_MAX 256
  247. /* global item templates */
  248. struct item_struct
  249. {
  250. char name[ITEM_IDENTIFIER_MAX];
  251. uint8_t fg_tile; /* for tga output and inital tile, bg-tile is set via tile_list */
  252. uint16_t init_proc;
  253. uint16_t hit_proc;
  254. uint16_t step_proc;
  255. };
  256. struct item_struct item_list[256];
  257. int item_cnt;
  258. #define MAP_SIZE_X 1024
  259. #define MAP_SIZE_Y 1024
  260. #define MAP_LINE_MAX 4096
  261. #define PHASE_MAPDATA 0
  262. #define PHASE_MAPSTRUCT 1
  263. int map_phase;
  264. uint8_t map[MAP_SIZE_Y][MAP_SIZE_X];
  265. uint8_t map2[MAP_SIZE_Y][MAP_SIZE_X];
  266. int map_curr_line = 0;
  267. char map_name[MAP_LINE_MAX];
  268. long map_width = 0;
  269. long map_height = 0;
  270. uint16_t map_init_code_pos;
  271. FILE *map_fp;
  272. char map_line[MAP_LINE_MAX];
  273. FILE *out_fp;
  274. /*============================================*/
  275. int item_get_idx_by_name(const char *name)
  276. {
  277. int i;
  278. for( i = 0; i < item_cnt; i++ )
  279. {
  280. if ( strcmp(item_list[i].name, name) == 0 )
  281. return i;
  282. }
  283. return -1;
  284. }
  285. int item_add(const char *name)
  286. {
  287. if ( item_cnt < 256 )
  288. {
  289. strcpy(item_list[item_cnt].name, name);
  290. item_cnt++;
  291. return item_cnt-1;
  292. }
  293. return -1;
  294. }
  295. void item_write(FILE *fp)
  296. {
  297. int i;
  298. fprintf(fp, "item_template_t item_template_list[] = {\n");
  299. for( i = 0; i < item_cnt;i++ )
  300. {
  301. fprintf(fp, " /* %-20s */ { /* init= */ %4u, /* hit= */%4u, /* step= */ %4u, /* fg= */ 0x%02x}", item_list[i].name, item_list[i].init_proc, item_list[i].hit_proc, item_list[i].step_proc, item_list[i].fg_tile);
  302. if ( i != item_cnt-1 )
  303. fprintf(fp, ",\n");
  304. else
  305. fprintf(fp, "\n");
  306. }
  307. fprintf(fp, "};\n\n");
  308. }
  309. /*============================================*/
  310. static void skip_space(const char **s)
  311. {
  312. for(;;)
  313. {
  314. if ( **s == '#' )
  315. {
  316. while( **s != '\0' )
  317. (*s)++;
  318. break;
  319. }
  320. if ( **s == '\0' )
  321. break;
  322. if ( **s > ' ' )
  323. break;
  324. (*s)++;
  325. }
  326. }
  327. static long get_dec(const char **s)
  328. {
  329. long v = 0;
  330. for(;;)
  331. {
  332. if ( (**s) >= '0' && (**s) <= '9' )
  333. {
  334. v*=10;
  335. v+= (**s)-'0';
  336. (*s)++;
  337. }
  338. else
  339. {
  340. break;
  341. }
  342. }
  343. skip_space(s);
  344. return v;
  345. }
  346. static long get_hex(const char **s)
  347. {
  348. long v = 0;
  349. for(;;)
  350. {
  351. if ( (**s) >= '0' && (**s) <= '9' )
  352. {
  353. v*=16;
  354. v+= (**s)-'0';
  355. (*s)++;
  356. }
  357. else if ( (**s) >= 'a' && (**s) <= 'f' )
  358. {
  359. v*=16;
  360. v+= (**s)-'a'+10;
  361. (*s)++;
  362. }
  363. else if ( (**s) >= 'A' && (**s) <= 'F' )
  364. {
  365. v*=16;
  366. v+= (**s)-'A'+10;
  367. (*s)++;
  368. }
  369. else
  370. {
  371. break;
  372. }
  373. }
  374. skip_space(s);
  375. return v;
  376. }
  377. static long get_ascii(const char **s)
  378. {
  379. long v = 0;
  380. v = **s;
  381. (*s)++;
  382. skip_space(s);
  383. return v;
  384. }
  385. static long get_num(const char **s)
  386. {
  387. if ( (**s) == '$' )
  388. {
  389. (*s)++;
  390. return get_hex(s);
  391. }
  392. if ( (**s) == '\'' )
  393. {
  394. (*s)++;
  395. return get_ascii(s);
  396. }
  397. return get_dec(s);
  398. }
  399. static const char *get_identifier(const char **s)
  400. {
  401. static char buf[MAP_LINE_MAX];
  402. int c;
  403. int i = 0;
  404. buf[0] = '\0';
  405. for(;;)
  406. {
  407. c = **s;
  408. if ( c < 'A' )
  409. break;
  410. if ( i >= MAP_LINE_MAX-2 )
  411. break;
  412. buf[i++] = c;
  413. buf[i] = '\0';
  414. (*s)++;
  415. }
  416. skip_space(s);
  417. return buf;
  418. }
  419. /*============================================*/
  420. int get_tile_idx_by_ascii(int ascii)
  421. {
  422. int i;
  423. for( i = 0; i < tile_cnt; i++ )
  424. {
  425. if ( tile_list[i].ascii == ascii )
  426. return i;
  427. }
  428. return -1;
  429. }
  430. /*============================================*/
  431. /* map a tile from map[][] to map2[][] */
  432. /* called by map_all_tile */
  433. int map_tile(int x, int y)
  434. {
  435. int ascii, i, j;
  436. int cond[4];
  437. int is_condition_match;
  438. //int is_simple_match;
  439. int condition_match_cnt;
  440. int condition_match_max;
  441. int i_best;
  442. /* get the ascii version */
  443. ascii = map[y][x];
  444. cond[0] = 32;
  445. cond[1] = 32;
  446. cond[2] = 32;
  447. cond[3] = 32;
  448. if ( y > 0 ) cond[0] = map[y-1][x];
  449. if ( x+1 < map_width ) cond[1] = map[y][x+1];
  450. if ( y+1 < map_height ) cond[2] = map[y+1][x];
  451. if ( x > 0 ) cond[3] = map[y][x-1];
  452. /* find matching tile */
  453. condition_match_max = -1;
  454. i_best = -1;
  455. for( i = 0; i < tile_cnt; i++ )
  456. {
  457. if ( tile_list[i].ascii == ascii )
  458. {
  459. is_condition_match = 1;
  460. //is_simple_match = 1;
  461. condition_match_cnt = 0;
  462. for( j = 0; j < 4; j++ )
  463. {
  464. if ( tile_list[i].condition[j] != 0 )
  465. {
  466. //is_simple_match = 0;
  467. if ( tile_list[i].condition[j] != cond[j] )
  468. {
  469. is_condition_match = 0;
  470. }
  471. else
  472. {
  473. condition_match_cnt++;
  474. }
  475. }
  476. }
  477. if ( is_condition_match )
  478. {
  479. if ( condition_match_max < condition_match_cnt )
  480. {
  481. condition_match_max = condition_match_cnt;
  482. i_best = i;
  483. }
  484. }
  485. }
  486. }
  487. if ( i_best < 0 )
  488. {
  489. printf("no tile mapping found for '%c' (x=%d, y=%d)\n", ascii, x, y);
  490. return 0;
  491. }
  492. //printf("tile mapping '%c' --> $%02x (x=%d, y=%d)\n", ascii, tile_list[i_best].map_to, x, y);
  493. map2[y][x] = tile_list[i_best].map_to;
  494. return 1;
  495. }
  496. int map_all_tiles(void)
  497. {
  498. int x, y;
  499. for( y = 0; y < map_height; y++ )
  500. for( x = 0; x < map_width; x++ )
  501. if ( map_tile(x,y) == 0 )
  502. return 0;
  503. return 1;
  504. }
  505. /*============================================*/
  506. void clear_map(void)
  507. {
  508. int x, y;
  509. for( y = 0; y < MAP_SIZE_Y; y++ )
  510. for( x = 0; x < MAP_SIZE_X; x++ )
  511. map[y][x] =32;
  512. map_curr_line = 0;
  513. }
  514. void write_map(void)
  515. {
  516. int x, y;
  517. if ( map_phase == PHASE_MAPDATA )
  518. {
  519. if ( out_fp != NULL )
  520. {
  521. fprintf(out_fp, "unsigned char map_%s[%ld] = \n", map_name, map_height*map_width);
  522. for( y = 0; y < map_height; y++ )
  523. {
  524. fprintf(out_fp, " \"");
  525. for( x = 0; x < map_width; x++ )
  526. {
  527. fprintf(out_fp, "\\x%02x", map2[y][x]);
  528. }
  529. fprintf(out_fp, "\"");
  530. if ( y+1 < map_height )
  531. fprintf(out_fp, "\n");
  532. else
  533. fprintf(out_fp, ";\n");
  534. }
  535. fprintf(out_fp, "\n");
  536. }
  537. }
  538. }
  539. void write_map_struct(void)
  540. {
  541. if ( map_phase == PHASE_MAPSTRUCT )
  542. {
  543. if ( out_fp != NULL )
  544. {
  545. fprintf(out_fp, " { ");
  546. fprintf(out_fp, "map_%s, ", map_name);
  547. fprintf(out_fp, "item_onmap_%s, ", map_name);
  548. fprintf(out_fp, "/* map init code */ %u, ", map_init_code_pos);
  549. fprintf(out_fp, "ITEM_ONMAP_%s_CNT, ", map_name);
  550. fprintf(out_fp, "/* width= */ %ld, ", map_width);
  551. fprintf(out_fp, "/* height= */ %ld ", map_height);
  552. fprintf(out_fp, " },");
  553. fprintf(out_fp, "\n");
  554. }
  555. }
  556. }
  557. void write_tga_map(const char *filename)
  558. {
  559. static u8g2_t u8g2;
  560. int x, y, i;
  561. unsigned tile;
  562. if ( map_phase != PHASE_MAPDATA )
  563. return;
  564. u8x8_tga_info.tile_width = map_width*2;
  565. u8x8_tga_info.tile_height = map_height*2;
  566. u8x8_tga_info.pixel_width = map_width*16;
  567. u8x8_tga_info.pixel_height = map_height*16;
  568. u8g2_SetupBuffer_TGA(&u8g2, &u8g2_cb_r0);
  569. u8x8_InitDisplay(u8g2_GetU8x8(&u8g2));
  570. u8x8_SetPowerSave(u8g2_GetU8x8(&u8g2), 0);
  571. u8g2_SetFont(&u8g2, scrollosprites);
  572. u8g2_FirstPage(&u8g2);
  573. do
  574. {
  575. for( y = 0; y < map_height; y++ )
  576. {
  577. for( x = 0; x < map_width; x++ )
  578. {
  579. tile = map2[y][x];
  580. /* check if there is a fg_tile, if so, use that tile instead */
  581. for( i = 0; i < tile_cnt; i++ )
  582. {
  583. if ( tile_list[i].item_index >= 0 )
  584. {
  585. if ( tile_list[i].ascii == map[y][x] )
  586. {
  587. tile = item_list[tile_list[i].item_index].fg_tile;
  588. }
  589. }
  590. }
  591. u8g2_DrawGlyph(&u8g2, x*16, y*16+16, tile);
  592. }
  593. }
  594. } while( u8g2_NextPage(&u8g2) );
  595. tga_save(filename);
  596. }
  597. static void write_item_onmap(void)
  598. {
  599. int x, y, i;
  600. int is_first = 1;
  601. int cnt = 0;
  602. /*
  603. pseudo code:
  604. with all tiles on the map
  605. if the tile is connected to an item
  606. output an item onmap entry
  607. */
  608. if ( map_phase == PHASE_MAPDATA )
  609. {
  610. if ( out_fp != NULL )
  611. {
  612. fprintf(out_fp, "item_onmap_t item_onmap_%s[] = {\n", map_name);
  613. for( y = 0; y < map_height; y++ )
  614. {
  615. for( x = 0; x < map_width; x++ )
  616. {
  617. for( i = 0; i < tile_cnt; i++ )
  618. {
  619. if ( tile_list[i].item_index >= 0 )
  620. {
  621. if ( tile_list[i].ascii == map[y][x] )
  622. {
  623. if ( is_first == 0 )
  624. {
  625. fprintf(out_fp, ",\n");
  626. }
  627. is_first = 0;
  628. fprintf(out_fp, " /* %-20s */ ", item_list[tile_list[i].item_index].name);
  629. fprintf(out_fp, "{ /*x=*/ %2d, /*y=*/ %2d, /*idx=*/ %2d, %2d}", x, y, tile_list[i].item_index, 0);
  630. cnt++;
  631. }
  632. }
  633. }
  634. }
  635. }
  636. fprintf(out_fp, "\n};\n");
  637. fprintf(out_fp, "#define ITEM_ONMAP_%s_CNT %d\n\n", map_name, cnt);
  638. }
  639. }
  640. }
  641. int map_read_tile(const char **s, int is_overwrite, int item_index)
  642. {
  643. long ascii;
  644. int idx, i;
  645. ascii = get_num(s);
  646. if ( is_overwrite )
  647. {
  648. for( idx = 0; idx < tile_cnt; idx++ )
  649. {
  650. if ( tile_list[idx].ascii == ascii )
  651. break;
  652. }
  653. }
  654. else
  655. {
  656. idx = tile_cnt;
  657. }
  658. if ( idx < tile_cnt )
  659. {
  660. for( i = 0; i < 4; i++ )
  661. {
  662. tile_list[idx].condition[i] = 0;
  663. }
  664. }
  665. else
  666. {
  667. if ( tile_cnt >= TILE_MAX )
  668. {
  669. printf("max number of tiles reached\n");
  670. return 0;
  671. }
  672. idx = tile_cnt;
  673. tile_cnt++;
  674. }
  675. tile_list[idx].ascii = ascii;
  676. tile_list[idx].item_index = item_index;
  677. tile_list[idx].map_to = get_num(s);
  678. for( i = 0; i < 4; i++ )
  679. {
  680. tile_list[idx].condition[i] = get_num(s);
  681. }
  682. //printf("[%d] tile %c: ", idx, (int)ascii);
  683. //printf("map to $%02x\n", tile_list[idx].map_to);
  684. return 1;
  685. }
  686. /*============================================*/
  687. /* read one row of the game map */
  688. int map_read_row(const char **s)
  689. {
  690. int x = 0;
  691. //printf("line %d\n", map_curr_line);
  692. while ( **s >= ' ' )
  693. {
  694. if ( x > map_width )
  695. {
  696. printf("map '%s': Row '%d' too long\n", map_name, map_curr_line);
  697. return 0;
  698. }
  699. //printf("%d ", **s);
  700. map[map_curr_line][x] = **s;
  701. (*s)++;
  702. x++;
  703. }
  704. map_curr_line++;
  705. return 1;
  706. }
  707. /*============================================*/
  708. /* read a command of the map file */
  709. int map_read_map_cmd(const char **s)
  710. {
  711. /* get new map */
  712. strcpy(map_name, get_identifier(s));
  713. map_width = get_num(s);
  714. map_height = get_num(s);
  715. printf("map '%s' (%ld x %ld)\n", map_name, map_width, map_height);
  716. map_init_code_pos = 0;
  717. clear_map();
  718. return 1;
  719. }
  720. int is_inside_proc;
  721. int is_inside_map;
  722. int map_read_line(const char **s)
  723. {
  724. const char *id;
  725. if ( is_inside_proc != 0 )
  726. {
  727. if ( uglReadLine(s) == 0 )
  728. is_inside_proc = 0;
  729. return 1;
  730. }
  731. skip_space(s);
  732. if ( **s == '#' ) /* comment (hmm handled by skip_space) */
  733. return 1;
  734. if ( **s == '\0' ) /* empty line */
  735. return 1;
  736. if ( **s == ':' )
  737. {
  738. (*s)++;
  739. return map_read_row(s);
  740. }
  741. id = get_identifier(s);
  742. if ( strcmp(id, "tile") == 0 )
  743. {
  744. return map_read_tile(s, 0, -1);
  745. }
  746. else if ( strcmp(id, "thing") == 0 )
  747. {
  748. return map_read_tile(s, 1, -1);
  749. }
  750. else if ( strcmp(id, "itemkey") == 0 )
  751. {
  752. const char *id;
  753. int idx;
  754. id = get_identifier(s);
  755. idx = item_get_idx_by_name(id);
  756. if ( idx < 0 )
  757. {
  758. printf("code line %d, item '%s' unknown.\n", ugl_current_input_line, id);
  759. }
  760. else
  761. {
  762. return map_read_tile(s, 1, idx);
  763. }
  764. }
  765. else if ( strcmp(id, "item") == 0 )
  766. {
  767. const char *id;
  768. int idx;
  769. id = get_identifier(s);
  770. idx = item_get_idx_by_name(id);
  771. if ( idx >= 0 )
  772. {
  773. printf("code line %d, item '%s' already exists.\n", ugl_current_input_line, id);
  774. }
  775. else
  776. {
  777. if ( item_add(id) < 0 )
  778. {
  779. printf("code line %d, item '%s': Too many items.\n", ugl_current_input_line, id);
  780. }
  781. else
  782. {
  783. idx = item_get_idx_by_name(id);
  784. assert( idx >= 0 );
  785. item_list[idx].fg_tile = get_num(s);
  786. }
  787. }
  788. return 1;
  789. }
  790. else if ( strcmp(id, "mapinit") == 0 )
  791. {
  792. map_init_code_pos = uglStartNamelessProc(0);
  793. is_inside_proc = 1;
  794. return 1;
  795. }
  796. else if ( strcmp(id, "iteminit") == 0 )
  797. {
  798. const char *id;
  799. int idx;
  800. uint16_t code_pos;
  801. id = get_identifier(s);
  802. idx = item_get_idx_by_name(id);
  803. code_pos = uglStartNamelessProc(0);
  804. if ( idx < 0 )
  805. {
  806. printf("code line %d, item '%s' not found.\n", ugl_current_input_line, id);
  807. }
  808. else
  809. {
  810. item_list[idx].init_proc= code_pos;
  811. }
  812. is_inside_proc = 1;
  813. return 1;
  814. }
  815. else if ( strcmp(id, "itemhit") == 0 )
  816. {
  817. const char *id;
  818. int idx;
  819. uint16_t code_pos;
  820. id = get_identifier(s);
  821. idx = item_get_idx_by_name(id);
  822. code_pos = uglStartNamelessProc(0);
  823. if ( idx < 0 )
  824. {
  825. printf("code line %d, item '%s' not found.\n", ugl_current_input_line, id);
  826. }
  827. else
  828. {
  829. item_list[idx].hit_proc= code_pos;
  830. }
  831. is_inside_proc = 1;
  832. return 1;
  833. }
  834. else if ( strcmp(id, "itemstep") == 0 )
  835. {
  836. const char *id;
  837. int idx;
  838. uint16_t code_pos;
  839. id = get_identifier(s);
  840. idx = item_get_idx_by_name(id);
  841. code_pos = uglStartNamelessProc(0);
  842. if ( idx < 0 )
  843. {
  844. printf("code line %d, item '%s' not found.\n", ugl_current_input_line, id);
  845. }
  846. else
  847. {
  848. item_list[idx].step_proc= code_pos;
  849. }
  850. is_inside_proc = 1;
  851. return 1;
  852. }
  853. else if ( strcmp(id, "map") == 0 )
  854. {
  855. is_inside_map = 1;
  856. return map_read_map_cmd(s);
  857. }
  858. else if ( strcmp(id, "endmap") == 0 )
  859. {
  860. /* write existing map: once a map has been read, transform it to the target */
  861. if ( map_width > 0 && map_height > 0 )
  862. {
  863. if ( map_all_tiles() )
  864. {
  865. char buf[128];
  866. write_map();
  867. write_item_onmap();
  868. sprintf(buf, "%s.tga", map_name);
  869. write_tga_map(buf);
  870. write_map_struct();
  871. }
  872. }
  873. is_inside_map = 0;
  874. return 1;
  875. }
  876. else
  877. {
  878. printf("code line %d, map line %d: unkown command '%s'\n", ugl_current_input_line, map_curr_line, id);
  879. }
  880. return 1;
  881. }
  882. int map_read_fp(void)
  883. {
  884. const char *s;
  885. ugl_InitBytecode();
  886. if ( map_phase == PHASE_MAPDATA )
  887. ugl_is_suppress_log = 0;
  888. if ( map_phase == PHASE_MAPSTRUCT )
  889. ugl_is_suppress_log = 1;
  890. ugl_current_input_line = 1;
  891. tile_cnt = 0;
  892. item_cnt = 0;
  893. for(;;)
  894. {
  895. if ( fgets(map_line, MAP_LINE_MAX, map_fp) == NULL )
  896. break;
  897. s = &(map_line[0]);
  898. if ( map_read_line(&s) == 0 )
  899. {
  900. if ( is_inside_proc )
  901. printf("endproc missing\n");
  902. if ( is_inside_map )
  903. printf("endmap missing\n");
  904. return 0;
  905. }
  906. ugl_current_input_line++;
  907. }
  908. ugl_ResolveSymbols();
  909. return 1;
  910. }
  911. int map_read_filename(const char *name, int phase)
  912. {
  913. map_phase = phase;
  914. map_fp = fopen(name, "r");
  915. if ( map_fp == NULL )
  916. return 0;
  917. printf("file '%s'\n", name);
  918. if ( map_read_fp() == 0 )
  919. return fclose(map_fp), 0;
  920. fclose(map_fp);
  921. return 1;
  922. }
  923. int main(int argc, char **argv)
  924. {
  925. const char *filename = NULL;
  926. const char *outfile = NULL;
  927. if ( argc <= 1 )
  928. {
  929. printf("%s [options] mapfile\n", argv[0]);
  930. printf(" -o c-file-name\n");
  931. exit(1);
  932. }
  933. argv++;
  934. argc--;
  935. while( argc > 0 )
  936. {
  937. if ( strcmp(*argv, "-o" ) == 0 )
  938. {
  939. argv++;
  940. argc--;
  941. if ( argc <= 0 )
  942. {
  943. printf("output file missing\n");
  944. exit(1);
  945. }
  946. outfile = argv[0];
  947. }
  948. else
  949. {
  950. filename = argv[0];
  951. }
  952. argv++;
  953. argc--;
  954. }
  955. clear_map();
  956. if ( filename != NULL )
  957. {
  958. out_fp= NULL;
  959. if ( outfile != NULL )
  960. {
  961. out_fp = fopen(outfile, "w");
  962. printf("output file %s\n", outfile);
  963. fprintf(out_fp, "/* %s generated by mapgen, olikraus@gmail.com */\n", outfile);
  964. fprintf(out_fp, "\n");
  965. fprintf(out_fp, "#include \"map.h\"\n");
  966. fprintf(out_fp, "\n");
  967. }
  968. map_read_filename(filename, PHASE_MAPDATA);
  969. ugl_WriteBytecodeCArray(out_fp, "map_code");
  970. //write_item_onmap();
  971. item_write(out_fp);
  972. if ( out_fp != NULL )
  973. {
  974. fprintf(out_fp, "map_t map_list[] = {\n");
  975. map_read_filename(filename, PHASE_MAPSTRUCT);
  976. fprintf(out_fp, "};\n");
  977. fprintf(out_fp, "\n");
  978. }
  979. if ( out_fp != NULL )
  980. fclose(out_fp);
  981. }
  982. }