ularn_wintty.c 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947
  1. /* =============================================================================
  2. * PROGRAM: ularn
  3. * FILENAME: ularn_win.c
  4. *
  5. * DESCRIPTION:
  6. * This module contains all operating system dependant code for input and
  7. * display update.
  8. * Each version of ularn should provide a different implementation of this
  9. * module.
  10. *
  11. * This is the curses based TTY display module.
  12. *
  13. * =============================================================================
  14. * EXPORTED VARIABLES
  15. *
  16. * nonap : Set to true if no time delays are to be used.
  17. * nosignal : Set if ctrl-C is to be trapped to prevent exit.
  18. * enable_scroll : Probably superfluous
  19. * yrepcount : Repeat count for input commands.
  20. *
  21. * =============================================================================
  22. * EXPORTED FUNCTIONS
  23. *
  24. * init_app : Initialise the app
  25. * close_app : Close the app and free resources
  26. * get_normal_input : Get the next command input
  27. * get_prompt_input : Get input in response to a question
  28. * get_password_input : Get a password
  29. * get_num_input : Geta number
  30. * get_dir_input : Get a direction
  31. * set_display : Set the display mode
  32. * UpdateStatus : Update the status display
  33. * UpdateEffects : Update the effects display
  34. * UpdateStatusAndEffects : Update both status and effects display
  35. * ClearText : Clear the text output area
  36. * beep : Make a beep
  37. * Cursor : Set the cursor location
  38. * Printc : Print a single character
  39. * Print : Print a string
  40. * Printf : Print a formatted string
  41. * Standout : Print a string is standout format
  42. * SetFormat : Set the output text format
  43. * ClearEOL : Clear to end of line
  44. * ClearEOPage : Clear to end of page
  45. * show1cell : Show 1 cell on the map
  46. * showplayer : Show the player on the map
  47. * showcell : Show the area around the player
  48. * drawscreen : Redraw the screen
  49. * draws : Redraw a section of the screen
  50. * mapeffect : Draw a directional effect
  51. * magic_effect_frames : Get the number of animation frames in a magic fx
  52. * magic_effect : Draw a frame in a magic fx
  53. * nap : Delay for a specified number of milliseconds
  54. * GetUser : Get the username and user id.
  55. *
  56. * =============================================================================
  57. */
  58. #include <curses.h>
  59. #include <stdarg.h>
  60. #include <stdio.h>
  61. #include "config.h"
  62. #include "header.h"
  63. #include "ularn_game.h"
  64. #include "dungeon.h"
  65. #include "itm.h"
  66. #include "monster.h"
  67. #include "player.h"
  68. #include "ularn_win.h"
  69. //
  70. // player id file
  71. //
  72. #ifndef UNIX
  73. static char *PIDName = LIBDIR "\\vlarn.pid";
  74. #endif
  75. #define FIRST_PID 1001
  76. /* =============================================================================
  77. * Exported variables
  78. */
  79. int nonap = 0;
  80. int nosignal = 0;
  81. char enable_scroll = 0;
  82. int yrepcount = 0;
  83. /* =============================================================================
  84. * Local variables
  85. */
  86. int UseColor = 0;
  87. int CaretActive = 0;
  88. #define M_NONE 0
  89. #define M_SHIFT 1
  90. #define M_CTRL 2
  91. #define M_ASCII 255
  92. #define MAX_KEY_BINDINGS 3
  93. struct KeyCodeType {
  94. int VirtKey;
  95. int ModKey;
  96. };
  97. #define NUM_DIRS 8
  98. static ActionType DirActions[NUM_DIRS] = {
  99. ACTION_MOVE_WEST, ACTION_MOVE_EAST, ACTION_MOVE_SOUTH,
  100. ACTION_MOVE_NORTH, ACTION_MOVE_NORTHEAST, ACTION_MOVE_NORTHWEST,
  101. ACTION_MOVE_SOUTHEAST, ACTION_MOVE_SOUTHWEST};
  102. /* Default keymap */
  103. /* Allow up to MAX_KEY_BINDINGS per action */
  104. static struct KeyCodeType KeyMap[ACTION_COUNT][MAX_KEY_BINDINGS] = {
  105. {{0, 0}, {0, 0}, {0, 0}}, // ACTION_NULL
  106. {{'~', M_ASCII}, {0, 0}, {0, 0}}, // ACTION_DIAG
  107. {{'h', M_ASCII}, {KEY_LEFT, M_ASCII}, {0, 0}}, // ACTION_MOVE_WEST
  108. {{'H', M_ASCII}, {0, 0}, {0, 0}}, // ACTION_RUN_WEST
  109. {{'l', M_ASCII}, {KEY_RIGHT, M_ASCII}, {0, 0}}, // ACTION_MOVE_EAST,
  110. {{'L', M_ASCII}, {0, 0}, {0, 0}}, // ACTION_RUN_EAST,
  111. {{'j', M_ASCII}, {KEY_DOWN, M_ASCII}, {0, 0}}, // ACTION_MOVE_SOUTH,
  112. {{'J', M_ASCII}, {0, 0}, {0, 0}}, // ACTION_RUN_SOUTH,
  113. {{'k', M_ASCII}, {KEY_UP, M_ASCII}, {0, 0}}, // ACTION_MOVE_NORTH,
  114. {{'K', M_ASCII}, {0, 0}, {0, 0}}, // ACTION_RUN_NORTH,
  115. {{'u', M_ASCII}, {KEY_A3, M_ASCII}, {0, 0}}, // ACTION_MOVE_NORTHEAST,
  116. {{'U', M_ASCII}, {0, 0}, {0, 0}}, // ACTION_RUN_NORTHEAST,
  117. {{'y', M_ASCII}, {KEY_A1, M_ASCII}, {0, 0}}, // ACTION_MOVE_NORTHWEST,
  118. {{'Y', M_ASCII}, {0, 0}, {0, 0}}, // ACTION_RUN_NORTHWEST,
  119. {{'n', M_ASCII}, {KEY_C3, M_ASCII}, {0, 0}}, // ACTION_MOVE_SOUTHEAST,
  120. {{'N', M_ASCII}, {0, 0}, {0, 0}}, // ACTION_RUN_SOUTHEAST,
  121. {{'b', M_ASCII}, {KEY_C1, M_ASCII}, {0, 0}}, // ACTION_MOVE_SOUTHWEST,
  122. {{'B', M_ASCII}, {0, 0}, {0, 0}}, // ACTION_RUN_SOUTHWEST,
  123. {{'.', M_ASCII}, {0, 0}, {0, 0}}, // ACTION_WAIT,
  124. {{' ', M_ASCII}, {0, 0}, {0, 0}}, // ACTION_NONE,
  125. {{'w', M_ASCII}, {0, 0}, {0, 0}}, // ACTION_WIELD,
  126. {{'W', M_ASCII}, {0, 0}, {0, 0}}, // ACTION_WEAR,
  127. {{'r', M_ASCII}, {0, 0}, {0, 0}}, // ACTION_READ,
  128. {{'q', M_ASCII}, {0, 0}, {0, 0}}, // ACTION_QUAFF,
  129. {{'d', M_ASCII}, {0, 0}, {0, 0}}, // ACTION_DROP,
  130. {{'c', M_ASCII}, {0, 0}, {0, 0}}, // ACTION_CAST_SPELL,
  131. {{'o', M_ASCII}, {0, 0}, {0, 0}}, // ACTION_OPEN_DOOR
  132. {{'C', M_ASCII}, {0, 0}, {0, 0}}, // ACTION_CLOSE_DOOR,
  133. {{'O', M_ASCII}, {0, 0}, {0, 0}}, // ACTION_OPEN_CHEST
  134. {{'i', M_ASCII}, {0, 0}, {0, 0}}, // ACTION_INVENTORY,
  135. {{'e', M_ASCII}, {0, 0}, {0, 0}}, // ACTION_EAT_COOKIE,
  136. {{'\\', M_ASCII}, {0, 0}, {0, 0}}, // ACTION_LIST_SPELLS,
  137. {{'?', M_ASCII}, {0, 0}, {0, 0}}, // ACTION_HELP,
  138. {{'S', M_ASCII}, {0, 0}, {0, 0}}, // ACTION_SAVE,
  139. {{'Z', M_ASCII}, {0, 0}, {0, 0}}, // ACTION_TELEPORT,
  140. {{'^', M_ASCII}, {0, 0}, {0, 0}}, // ACTION_IDENTIFY_TRAPS,
  141. {{'_', M_ASCII}, {0, 0}, {0, 0}}, // ACTION_BECOME_CREATOR,
  142. {{'+', M_ASCII}, {0, 0}, {0, 0}}, // ACTION_CREATE_ITEM,
  143. {{'-', M_ASCII}, {0, 0}, {0, 0}}, // ACTION_TOGGLE_WIZARD,
  144. {{'`', M_ASCII}, {0, 0}, {0, 0}}, // ACTION_DEBUG_MODE,
  145. {{'T', M_ASCII}, {0, 0}, {0, 0}}, // ACTION_REMOVE_ARMOUR,
  146. {{'g', M_ASCII}, {0, 0}, {0, 0}}, // ACTION_PACK_WEIGHT,
  147. {{'v', M_ASCII}, {0, 0}, {0, 0}}, // ACTION_VERSION,
  148. {{'Q', M_ASCII}, {0, 0}, {0, 0}}, // ACTION_QUIT,
  149. {{18, M_ASCII}, {0, 0}, {0, 0}}, // ACTION_REDRAW_SCREEN,
  150. {{'P', M_ASCII}, {0, 0}, {0, 0}} // ACTION_SHOW_TAX
  151. };
  152. static struct KeyCodeType RunKeyMap = {KEY_B2, M_ASCII};
  153. typedef enum {
  154. C_BLACK,
  155. C_RED,
  156. C_GREEN,
  157. C_YELLOW,
  158. C_BLUE,
  159. C_MAGENTA,
  160. C_CYAN,
  161. C_WHITE,
  162. C_COUNT
  163. } Ularn_Color_Type;
  164. static int ColorPairs[C_COUNT][2] = {
  165. {COLOR_BLACK, COLOR_BLACK}, {COLOR_RED, COLOR_BLACK},
  166. {COLOR_GREEN, COLOR_BLACK}, {COLOR_YELLOW, COLOR_BLACK},
  167. {COLOR_BLUE, COLOR_BLACK}, {COLOR_MAGENTA, COLOR_BLACK},
  168. {COLOR_CYAN, COLOR_BLACK}, {COLOR_WHITE, COLOR_BLACK}};
  169. static int Runkey;
  170. static ActionType Event;
  171. static int GotChar;
  172. static int EventChar;
  173. //
  174. // Characters for tiles
  175. //
  176. int WallTile[16] = {
  177. '#', '#', '#', '#', '#', '#', '#', '#', '#',
  178. '#', '#', '#', '#', '#', '#', '#'
  179. //'#', '-', '|', '+', '-', '-', '+', '-', '|', '+', '|', '|', '+', '-', '|',
  180. //'#'
  181. };
  182. /* Tiles for directional effects */
  183. static int EffectTile[EFFECT_COUNT][9] = {
  184. {'*', '|', '-', '|', '-', '/', '\\', '\\', '/'},
  185. {'*', '|', '-', '|', '-', '/', '\\', '\\', '/'},
  186. {'*', '|', '-', '|', '-', '/', '\\', '\\', '/'},
  187. {'*', '|', '-', '|', '-', '/', '\\', '\\', '/'},
  188. {'*', '|', '-', '|', '-', '/', '\\', '\\', '/'},
  189. };
  190. static int EffectColor[EFFECT_COUNT] = {C_GREEN, C_CYAN, C_RED, C_YELLOW,
  191. C_WHITE};
  192. #define MAX_MAGICFX_FRAME 8
  193. struct MagicEffectDataType {
  194. int Frames; /* Number of frames in the effect */
  195. int Tile[MAX_MAGICFX_FRAME]; /* The primary tile for this frame */
  196. int Color[MAX_MAGICFX_FRAME]; /* Only used for overlay effects */
  197. };
  198. static struct MagicEffectDataType magicfx_tile[MAGIC_COUNT] = {
  199. /* Sparkle */
  200. {8,
  201. {'-', '\\', '|', '/', '-', '\\', '|', '/'},
  202. {C_RED, C_YELLOW, C_GREEN, C_CYAN, C_BLUE, C_MAGENTA, C_RED, C_YELLOW}},
  203. /* Sleep */
  204. {6,
  205. {'z', 'Z', 'z', 'Z', 'z', 'Z', 0, 0},
  206. {C_RED, C_RED, C_GREEN, C_GREEN, C_BLUE, C_BLUE, 0, 0}},
  207. /* Web */
  208. {6,
  209. {'.', 'o', '*', '#', '#', '#', 0, 0},
  210. {C_WHITE, C_WHITE, C_WHITE, C_WHITE, C_GREEN, C_BLUE, 0, 0}},
  211. /* Phantasmal forces */
  212. {6,
  213. {'.', ':', '^', 'A', 'A', 'A', 0, 0},
  214. {C_BLUE, C_BLUE, C_MAGENTA, C_MAGENTA, C_CYAN, C_CYAN, 0, 0}},
  215. /* Cloud kill */
  216. {6,
  217. {'.', 'o', '*', '#', '#', 'O', 0, 0},
  218. {C_GREEN, C_GREEN, C_GREEN, C_GREEN, C_YELLOW, C_GREEN, 0, 0}},
  219. /* Vaporize rock */
  220. {6,
  221. {'.', 'o', '*', '#', '#', 'O', 0, 0},
  222. {C_RED, C_RED, C_RED, C_RED, C_YELLOW, C_RED, 0, 0}},
  223. /* Dehydrate */
  224. {6,
  225. {'.', ':', '|', 'T', '^', '~', 0, 0},
  226. {C_WHITE, C_WHITE, C_WHITE, C_WHITE, C_WHITE, C_WHITE, 0, 0}},
  227. /* Drain life */
  228. {6,
  229. {'#', '*', '|', ':', 'o', '.', 0, 0},
  230. {C_YELLOW, C_YELLOW, C_YELLOW, C_RED, C_RED, C_RED, 0, 0}},
  231. /* Flood */
  232. {6,
  233. {'.', 'o', 'O', 'o', 'O', 'o', 0, 0},
  234. {C_BLUE, C_BLUE, C_BLUE, C_CYAN, C_CYAN, C_BLUE, 0, 0}},
  235. /* Finger of death */
  236. {6,
  237. {'#', '*', '|', ':', 'o', '.', 0, 0},
  238. {C_RED, C_RED, C_MAGENTA, C_MAGENTA, C_BLUE, C_BLUE, 0, 0}},
  239. /* Teleport away */
  240. {6,
  241. {':', '|', 'H', 'H', '|', ':', 0, 0},
  242. {C_CYAN, C_CYAN, C_CYAN, C_BLUE, C_BLUE, C_BLUE, 0, 0}},
  243. /* Magic fire */
  244. {6,
  245. {'.', 'o', '*', '#', '#', 'O', 0, 0},
  246. {C_RED, C_RED, C_RED, C_RED, C_YELLOW, C_RED, 0, 0}},
  247. /* Make wall */
  248. {6,
  249. {'.', ':', 'H', '#', '#', '#', 0, 0},
  250. {C_WHITE, C_WHITE, C_WHITE, C_WHITE, C_RED, C_WHITE, 0, 0}},
  251. /* Summon demon */
  252. {6,
  253. {'.', ':', '^', '8', '8', '8', 0, 0},
  254. {C_MAGENTA, C_MAGENTA, C_RED, C_RED, C_YELLOW, C_YELLOW, 0, 0}},
  255. /* Annihilate (scroll) */
  256. {6,
  257. {'-', '|', '-', '|', '-', '|', 0, 0},
  258. {C_RED, C_RED, C_YELLOW, C_YELLOW, C_GREEN, C_GREEN, 0, 0}}};
  259. //
  260. // Display attributes and colours
  261. //
  262. static int ItemAttr[OCOUNT] = {
  263. 0, 0,
  264. /* Dungeon features */
  265. A_REVERSE, A_REVERSE, A_REVERSE, A_REVERSE, A_REVERSE, A_REVERSE, A_REVERSE,
  266. A_REVERSE, A_REVERSE, A_REVERSE, A_REVERSE, A_REVERSE, A_REVERSE, A_REVERSE,
  267. A_REVERSE, A_REVERSE, 0,
  268. /* gold piles */
  269. 0, 0, 0, 0,
  270. /* eye of larn */
  271. A_STANDOUT,
  272. /* armour */
  273. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  274. /* weapons */
  275. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  276. /* rings */
  277. 0, 0, 0, 0, 0, 0, 0, 0,
  278. /* magic items */
  279. 0, 0, 0, A_REVERSE, A_REVERSE, A_BOLD, 0, A_BOLD, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  280. /* gems */
  281. 0, 0, 0, 0,
  282. /* buildings/entrances */
  283. A_REVERSE, A_REVERSE, A_REVERSE, A_REVERSE, A_REVERSE, A_REVERSE, A_REVERSE,
  284. A_REVERSE, A_REVERSE, A_REVERSE, A_REVERSE,
  285. /* traps */
  286. 0, 0, 0, 0, 0, 0, 0,
  287. /* misc */
  288. 0, 0, 0,
  289. /* drugs */
  290. 0, 0, 0, 0, 0};
  291. static int ItemColor[OCOUNT] = {
  292. C_WHITE, C_WHITE,
  293. /* Dungeon features */
  294. C_WHITE, C_YELLOW, C_YELLOW, C_YELLOW, C_WHITE, C_GREEN, C_WHITE, C_BLUE,
  295. C_WHITE, C_RED, C_WHITE, C_RED, C_WHITE, C_WHITE, C_WHITE, C_WHITE, C_WHITE,
  296. /* gold piles */
  297. C_YELLOW, C_YELLOW, C_YELLOW, C_YELLOW,
  298. /* eye of larn */
  299. C_WHITE,
  300. /* armour */
  301. C_CYAN, C_CYAN, C_WHITE, C_CYAN, C_WHITE, C_CYAN, C_CYAN, C_CYAN, C_WHITE,
  302. C_GREEN,
  303. /* weapons */
  304. C_CYAN, C_WHITE, C_CYAN, C_CYAN, C_WHITE, C_CYAN, C_CYAN, C_CYAN, C_WHITE,
  305. C_WHITE, C_MAGENTA, C_YELLOW,
  306. /* rings */
  307. C_WHITE, C_GREEN, C_BLUE, C_WHITE, C_CYAN, C_RED, C_MAGENTA, C_WHITE,
  308. /* magic items */
  309. C_WHITE, C_WHITE, C_YELLOW, C_CYAN, C_YELLOW, C_CYAN, C_WHITE, C_RED,
  310. C_WHITE, C_WHITE, C_WHITE, C_WHITE, C_WHITE, C_WHITE, C_WHITE, C_WHITE,
  311. C_WHITE,
  312. /* gems */
  313. C_WHITE, C_RED, C_GREEN, C_BLUE,
  314. /* buildings/entrances */
  315. C_WHITE, C_WHITE, C_WHITE, C_WHITE, C_WHITE, C_WHITE, C_WHITE, C_WHITE,
  316. C_WHITE, C_WHITE, C_WHITE,
  317. /* traps */
  318. C_RED, C_RED, C_RED, C_RED, C_RED, C_RED, C_RED,
  319. /* misc */
  320. C_WHITE, C_WHITE, C_WHITE,
  321. /* drugs */
  322. C_WHITE, C_WHITE, C_WHITE, C_WHITE, C_WHITE};
  323. static int MonstColor[MONST_COUNT] = {
  324. C_WHITE, C_WHITE, C_GREEN, C_GREEN, C_WHITE, C_WHITE, C_RED,
  325. C_GREEN, C_GREEN, C_RED, C_RED, C_RED, C_CYAN, C_GREEN,
  326. C_WHITE, C_BLUE, C_RED, C_WHITE, C_WHITE, C_RED, C_RED,
  327. C_CYAN, C_GREEN, C_YELLOW, C_WHITE, C_WHITE, C_GREEN, C_WHITE,
  328. C_CYAN, C_YELLOW, C_RED, C_MAGENTA, C_WHITE, C_YELLOW, C_YELLOW,
  329. C_YELLOW, C_YELLOW, C_RED, C_WHITE, C_WHITE, C_WHITE, C_CYAN,
  330. C_GREEN, C_YELLOW, C_MAGENTA, C_GREEN, C_BLUE, C_BLUE, C_MAGENTA,
  331. C_GREEN, C_MAGENTA, C_RED, C_YELLOW, C_CYAN, C_YELLOW, C_GREEN,
  332. C_RED, C_WHITE, C_YELLOW, C_GREEN, C_CYAN, C_BLUE, C_RED,
  333. C_MAGENTA, C_CYAN, C_MAGENTA};
  334. //
  335. // Current display mode
  336. //
  337. DisplayModeType CurrentDisplayMode = DISPLAY_TEXT;
  338. WINDOW *MapWindow;
  339. WINDOW *StatusWindow;
  340. WINDOW *EffectsWindow;
  341. WINDOW *MessageWindow;
  342. WINDOW *TextWindow;
  343. // =============================================================================
  344. // Text mode stuff
  345. //
  346. /* XXX trn default is 80 */
  347. #define LINE_LENGTH 80
  348. //
  349. // Messages
  350. //
  351. #define MAX_MSG_LINES 5
  352. static FormatType CurrentMsgFormat;
  353. static int MsgCursorX = 1;
  354. static int MsgCursorY = 1;
  355. //
  356. // Text
  357. //
  358. /* XXX trn default is 24, 80 */
  359. #define MAX_TEXT_LINES 24
  360. #define TEXT_LINE_LENGTH 80
  361. static FormatType CurrentTextFormat;
  362. static int TextCursorX = 1;
  363. static int TextCursorY = 1;
  364. //
  365. // Generalised text buffer
  366. // Top left corner is x=1, y=1
  367. //
  368. static FormatType CurrentFormat;
  369. static int CursorX = 1;
  370. static int CursorY = 1;
  371. static int MaxLine;
  372. //
  373. // The monster to use for showing mimics. Changes every 10 turns.
  374. //
  375. static int mimicmonst = MIMIC;
  376. /* =============================================================================
  377. * Local functions
  378. */
  379. /* =============================================================================
  380. * FUNCTION: SetWallTiles
  381. *
  382. * DESCRIPTION:
  383. * Set the wall tiles to the ACS line drawing values
  384. *
  385. * PARAMETERS:
  386. *
  387. * None
  388. *
  389. * RETURN VALUE:
  390. *
  391. * None.
  392. */
  393. static void SetWallTiles(void) {
  394. WallTile[0] = ACS_BLOCK;
  395. WallTile[1] = ACS_HLINE;
  396. WallTile[2] = ACS_VLINE;
  397. WallTile[3] = ACS_URCORNER;
  398. WallTile[4] = ACS_HLINE;
  399. WallTile[5] = ACS_HLINE;
  400. WallTile[6] = ACS_ULCORNER;
  401. WallTile[7] = ACS_TTEE;
  402. WallTile[8] = ACS_VLINE;
  403. WallTile[9] = ACS_LRCORNER;
  404. WallTile[10] = ACS_VLINE;
  405. WallTile[11] = ACS_RTEE;
  406. WallTile[12] = ACS_LLCORNER;
  407. WallTile[13] = ACS_BTEE;
  408. WallTile[14] = ACS_LTEE;
  409. WallTile[15] = ACS_PLUS;
  410. }
  411. /* =============================================================================
  412. * FUNCTION: SetCursesAttr
  413. *
  414. * DESCRIPTION:
  415. * Set curses text display attributes for the indicated format.
  416. *
  417. * PARAMETERS:
  418. *
  419. * Format : The text format.
  420. *
  421. * RETURN VALUE:
  422. *
  423. * None.
  424. */
  425. static void SetCursesAttr(FormatType Format) {
  426. int BaseAttr;
  427. #ifdef W32_TTY
  428. BaseAttr = A_BOLD;
  429. #else
  430. BaseAttr = 0;
  431. #endif
  432. switch (Format) {
  433. case FORMAT_NORMAL:
  434. if (UseColor)
  435. wattrset(TextWindow, BaseAttr | COLOR_PAIR(C_WHITE));
  436. else
  437. wattrset(TextWindow, A_NORMAL);
  438. break;
  439. case FORMAT_STANDOUT:
  440. if (UseColor)
  441. wattrset(TextWindow, BaseAttr | COLOR_PAIR(C_RED));
  442. else
  443. wattrset(TextWindow, A_STANDOUT);
  444. break;
  445. case FORMAT_STANDOUT2:
  446. if (UseColor)
  447. wattrset(TextWindow, BaseAttr | COLOR_PAIR(C_GREEN));
  448. else
  449. wattrset(TextWindow, A_BOLD);
  450. break;
  451. case FORMAT_STANDOUT3:
  452. if (UseColor)
  453. wattrset(TextWindow, BaseAttr | COLOR_PAIR(C_BLUE));
  454. else
  455. wattrset(TextWindow, A_UNDERLINE);
  456. break;
  457. default:
  458. break;
  459. }
  460. }
  461. /* =============================================================================
  462. * FUNCTION: setup_colour_pairs
  463. *
  464. * DESCRIPTION:
  465. * Setup the colour pairs used by curses.
  466. *
  467. * PARAMETERS:
  468. *
  469. * None.
  470. *
  471. * RETURN VALUE:
  472. *
  473. * None.
  474. */
  475. static void setup_colour_pairs(void) {
  476. short i;
  477. for (i = 0; i < C_COUNT; i++)
  478. init_pair(i, ColorPairs[i][0], ColorPairs[i][1]);
  479. #ifdef W32_TTY
  480. //
  481. // PD Curses on windoze doesn't seem to do reverse video correctly!
  482. // Need to set up the reverse colour combinations
  483. //
  484. for (i = 0; i < C_COUNT; i++)
  485. init_pair(C_COUNT + i, ColorPairs[i][1], ColorPairs[i][0]);
  486. #endif
  487. }
  488. /*
  489. * Repaint flag to force redraw of everything, not just deltas
  490. */
  491. static int Repaint = 0;
  492. /* =============================================================================
  493. * FUNCTION: PaintStatus
  494. *
  495. * DESCRIPTION:
  496. * Paint the status area.
  497. *
  498. * PARAMETERS:
  499. *
  500. * None.
  501. *
  502. * RETURN VALUE:
  503. *
  504. * None.
  505. */
  506. static void PaintStatus(void) {
  507. char Line[81];
  508. char Buf[81];
  509. int i;
  510. #ifdef W32_TTY
  511. wattrset(StatusWindow, A_BOLD | COLOR_PAIR(C_WHITE));
  512. #else
  513. wattrset(StatusWindow, COLOR_PAIR(C_WHITE));
  514. #endif
  515. if (Repaint) {
  516. wclear(StatusWindow);
  517. for (i = 0; i < 80; i++) {
  518. mvwaddch(StatusWindow, 0, i, ' ');
  519. mvwaddch(StatusWindow, 1, i, ' ');
  520. }
  521. }
  522. //
  523. // Build the top status line
  524. //
  525. Line[0] = 0;
  526. /* Spells */
  527. if (c[SPELLMAX] > 99)
  528. sprintf(Buf, "Spells:%3ld(%3ld)", c[SPELLS], c[SPELLMAX]);
  529. else
  530. sprintf(Buf, "Spells:%3ld(%2ld) ", c[SPELLS], c[SPELLMAX]);
  531. strcat(Line, Buf);
  532. /* AC, WC */
  533. sprintf(Buf, " AC: %-3ld WC: %-3ld Level", c[AC], c[WCLASS]);
  534. strcat(Line, Buf);
  535. /* Level */
  536. if (c[LEVEL] > 99)
  537. sprintf(Buf, "%3ld", c[LEVEL]);
  538. else
  539. sprintf(Buf, " %-2ld", c[LEVEL]);
  540. strcat(Line, Buf);
  541. /* Exp, class */
  542. sprintf(Buf, " Exp: %-9ld %s", c[EXPERIENCE], class[c[LEVEL] - 1]);
  543. strcat(Line, Buf);
  544. mvwaddstr(StatusWindow, 0, 0, Line);
  545. //
  546. // Format the second line of the status
  547. //
  548. sprintf(Buf, "%ld (%ld)", c[HP], c[HPMAX]);
  549. sprintf(Line,
  550. "HP: %11s STR=%-2ld INT=%-2ld WIS=%-2ld CON=%-2ld DEX=%-2ld "
  551. "CHA=%-2ld LV:",
  552. Buf, c[STRENGTH] + c[STREXTRA], c[INTELLIGENCE], c[WISDOM],
  553. c[CONSTITUTION], c[DEXTERITY], c[CHARISMA]);
  554. if ((level == 0) || (wizard))
  555. c[TELEFLAG] = 0;
  556. if (c[TELEFLAG])
  557. strcat(Line, " ?");
  558. else
  559. strcat(Line, levelname[level]);
  560. sprintf(Buf, " Gold: %-8ld", c[GOLD]);
  561. strcat(Line, Buf);
  562. mvwaddstr(StatusWindow, 1, 0, Line);
  563. wrefresh(StatusWindow);
  564. //
  565. // Mark all character values as displayed.
  566. //
  567. c[TMP] = c[STRENGTH] + c[STREXTRA];
  568. for (i = 0; i < 100; i++)
  569. cbak[i] = c[i];
  570. }
  571. /* Effects strings */
  572. static struct bot_side_def {
  573. int typ;
  574. char *string;
  575. } bot_data[] = {
  576. {STEALTH, " Stealth "}, {UNDEADPRO, " Undead Pro "},
  577. {SPIRITPRO, " Spirit Pro "}, {CHARMCOUNT, " Charm "},
  578. {TIMESTOP, " Time Stop "}, {HOLDMONST, " Hold Monst "},
  579. {GIANTSTR, " Giant Str "}, {FIRERESISTANCE, " Fire Resit "},
  580. {DEXCOUNT, " Dexterity "}, {STRCOUNT, " Strength "},
  581. {SCAREMONST, " Scare "}, {HASTESELF, " Haste Self "},
  582. {CANCELLATION, " Cancel "}, {INVISIBILITY, " Invisible "},
  583. {ALTPRO, " Protect 3 "}, {PROTECTIONTIME, " Protect 2 "},
  584. {WTW, " Wall-Walk "}};
  585. /* =============================================================================
  586. * FUNCTION: PaintEffects
  587. *
  588. * DESCRIPTION:
  589. * Paint the effects display.
  590. *
  591. * PARAMETERS:
  592. *
  593. * None.
  594. *
  595. * RETURN VALUE:
  596. *
  597. * None.
  598. */
  599. static void PaintEffects(void) {
  600. int i, idx;
  601. int WasSet;
  602. int IsSet;
  603. #ifdef W32_TTY
  604. wattrset(StatusWindow, A_BOLD | COLOR_PAIR(C_WHITE));
  605. #else
  606. wattrset(StatusWindow, COLOR_PAIR(C_WHITE));
  607. #endif
  608. if (Repaint)
  609. wclear(EffectsWindow);
  610. for (i = 0; i < 17; i++) {
  611. idx = bot_data[i].typ;
  612. WasSet = (cbak[idx] != 0);
  613. IsSet = (c[idx] != 0);
  614. if ((Repaint) || (IsSet != WasSet)) {
  615. if (IsSet)
  616. mvwaddstr(EffectsWindow, i, 0, bot_data[i].string);
  617. else
  618. mvwaddstr(EffectsWindow, i, 0, " ");
  619. }
  620. cbak[idx] = c[idx];
  621. }
  622. wrefresh(EffectsWindow);
  623. }
  624. /* =============================================================================
  625. * FUNCTION: GetTile
  626. *
  627. * DESCRIPTION:
  628. * Get the tile to be displayed for a location on the map.
  629. *
  630. * PARAMETERS:
  631. *
  632. * x : The x coordinate for the tile
  633. *
  634. * y : The y coordiante for the tile
  635. *
  636. * TileId : This is set to the tile to be displayed for (x, y).
  637. *
  638. * Attr : The curses attribute for this tile
  639. *
  640. * ColorPair : The colour pair for this tile
  641. *
  642. * RETURN VALUE:
  643. *
  644. * None.
  645. */
  646. static void GetTile(int x, int y, int *TileId, int *Attr, int *ColorPair) {
  647. MonsterIdType k;
  648. *Attr = 0;
  649. if ((x == playerx) && (y == playery) && (c[BLINDCOUNT] == 0)) {
  650. //
  651. // This is the square containing the player and the players isn't
  652. // blind, so return the player tile.
  653. //
  654. *TileId = '@';
  655. *ColorPair = C_RED;
  656. return;
  657. }
  658. //
  659. // Work out what is here
  660. //
  661. if (know[x][y] == OUNKNOWN) {
  662. //
  663. // The player doesn't know what is at this position.
  664. //
  665. *TileId = objnamelist[OUNKNOWN];
  666. *Attr = ItemAttr[(int)know[x][y]];
  667. *ColorPair = C_BLACK;
  668. } else {
  669. k = mitem[x][y].mon;
  670. if (k != 0) {
  671. if ((c[BLINDCOUNT] == 0) && (((stealth[x][y] & STEALTH_SEEN) != 0) ||
  672. ((stealth[x][y] & STEALTH_AWAKE) != 0))) {
  673. //
  674. // There is a monster here and the player is not blind and the
  675. // monster is seen or awake.
  676. //
  677. if (k == MIMIC) {
  678. if ((gtime % 10) == 0)
  679. while ((mimicmonst = rnd(MAXMONST)) == INVISIBLESTALKER)
  680. ;
  681. *TileId = monstnamelist[mimicmonst];
  682. *ColorPair = MonstColor[mimicmonst];
  683. } else if ((k == INVISIBLESTALKER) && (c[SEEINVISIBLE] == 0)) {
  684. *TileId = objnamelist[(int)know[x][y]];
  685. *Attr = ItemAttr[(int)know[x][y]];
  686. *ColorPair = ItemColor[(int)know[x][y]];
  687. } else if ((k >= DEMONLORD) && (k <= LUCIFER) && (c[EYEOFLARN] == 0)) {
  688. /* demons are invisible if not have the eye */
  689. *TileId = objnamelist[(int)know[x][y]];
  690. *Attr = ItemAttr[(int)know[x][y]];
  691. *ColorPair = ItemColor[(int)know[x][y]];
  692. } else {
  693. *TileId = monstnamelist[k];
  694. *ColorPair = MonstColor[k];
  695. }
  696. } /* can see monster */
  697. else {
  698. /*
  699. * The monster at this location is not known to the player, so show
  700. * the tile for the item at this location
  701. */
  702. *TileId = objnamelist[(int)know[x][y]];
  703. *Attr = ItemAttr[(int)know[x][y]];
  704. *ColorPair = ItemColor[(int)know[x][y]];
  705. }
  706. } /* monster here */
  707. else {
  708. k = know[x][y];
  709. *TileId = objnamelist[k];
  710. *Attr = ItemAttr[(int)know[x][y]];
  711. *ColorPair = ItemColor[(int)know[x][y]];
  712. }
  713. }
  714. /* Handle walls */
  715. if (*TileId == objnamelist[OWALL])
  716. *TileId = WallTile[iarg[x][y]];
  717. #ifdef W32_TTY
  718. if (*Attr & A_REVERSE) {
  719. /* If reverse on win32/pdcurses then use the reverse color pair */
  720. *Attr = (*Attr & (~A_REVERSE));
  721. *ColorPair += C_COUNT;
  722. } else
  723. /* PD Curses also needs bold to make bright colours on win32 */
  724. *Attr |= A_BOLD;
  725. #endif
  726. }
  727. /* =============================================================================
  728. * FUNCTION: PaintMap
  729. *
  730. * DESCRIPTION:
  731. * Repaint the map.
  732. *
  733. * PARAMETERS:
  734. *
  735. * None.
  736. *
  737. * RETURN VALUE:
  738. *
  739. * None.
  740. */
  741. static void PaintMap(void) {
  742. int x, y;
  743. int TileId;
  744. int Attr;
  745. int Color;
  746. if (Repaint) {
  747. wclear(MapWindow);
  748. for (y = 0; y < MAXY; y++) {
  749. for (x = 0; x < MAXX; x++) {
  750. GetTile(x, y, &TileId, &Attr, &Color);
  751. if (UseColor)
  752. wattrset(MapWindow, Attr | COLOR_PAIR(Color));
  753. else
  754. wattrset(MapWindow, Attr);
  755. mvwaddch(MapWindow, y, x, TileId);
  756. }
  757. }
  758. }
  759. wrefresh(MapWindow);
  760. }
  761. /* =============================================================================
  762. * FUNCTION: PaintTextWindow
  763. *
  764. * DESCRIPTION:
  765. * Repaint the window in text mode.
  766. *
  767. * PARAMETERS:
  768. *
  769. * None.
  770. *
  771. * RETURN VALUE:
  772. *
  773. * None.
  774. */
  775. static void PaintTextWindow(void) {
  776. touchwin(TextWindow);
  777. wrefresh(TextWindow);
  778. }
  779. /* =============================================================================
  780. * FUNCTION: PaintMapWindow
  781. *
  782. * DESCRIPTION:
  783. * Repaint the window in map mode
  784. *
  785. * PARAMETERS:
  786. *
  787. * None.
  788. *
  789. * RETURN VALUE:
  790. *
  791. * None.
  792. */
  793. static void PaintMapWindow(void) {
  794. PaintStatus();
  795. PaintEffects();
  796. PaintTextWindow();
  797. PaintMap();
  798. showplayer();
  799. }
  800. /* =============================================================================
  801. * FUNCTION: PaintWindow
  802. *
  803. * DESCRIPTION:
  804. * Repaint the window.
  805. *
  806. * PARAMETERS:
  807. *
  808. * None.
  809. *
  810. * RETURN VALUE:
  811. *
  812. * None.
  813. */
  814. static void PaintWindow(void) {
  815. Repaint = 1;
  816. if (CurrentDisplayMode == DISPLAY_MAP)
  817. PaintMapWindow();
  818. else
  819. PaintTextWindow();
  820. Repaint = 0;
  821. }
  822. /* =============================================================================
  823. * Exported functions
  824. */
  825. /* =============================================================================
  826. * FUNCTION: init_app
  827. */
  828. int init_app(void) {
  829. int x, y;
  830. /* Initialise curses app */
  831. initscr();
  832. cbreak();
  833. noecho();
  834. nonl();
  835. intrflush(stdscr, FALSE);
  836. // meta(stdscr, TRUE);
  837. start_color();
  838. setup_colour_pairs();
  839. keypad(stdscr, TRUE);
  840. /* Create windows */
  841. MapWindow = newwin(17, 67, 0, 0);
  842. StatusWindow = newwin(2, 80, 17, 0);
  843. EffectsWindow = newwin(17, 13, 0, 67);
  844. MessageWindow = newwin(5, 80, 19, 0);
  845. SetWallTiles();
  846. /* Start colour mode for slowlaris */
  847. wattrset(stdscr, A_STANDOUT | COLOR_PAIR(C_RED));
  848. mvwaddch(stdscr, 0, 0, '*');
  849. touchwin(stdscr);
  850. wrefresh(stdscr);
  851. UseColor = has_colors();
  852. refresh();
  853. //
  854. // Clear the text buffers
  855. //
  856. TextWindow = MessageWindow;
  857. SetCursesAttr(FORMAT_NORMAL);
  858. for (y = 0; y < MAX_MSG_LINES; y++) {
  859. for (x = 0; x < LINE_LENGTH; x++)
  860. mvwaddch(TextWindow, y, x, ' ');
  861. }
  862. wrefresh(TextWindow);
  863. TextWindow = stdscr;
  864. SetCursesAttr(FORMAT_NORMAL);
  865. for (y = 0; y < MAX_TEXT_LINES; y++) {
  866. for (x = 0; x < LINE_LENGTH; x++)
  867. mvwaddch(TextWindow, y, x, ' ');
  868. }
  869. wrefresh(TextWindow);
  870. return 1;
  871. }
  872. /* =============================================================================
  873. * FUNCTION: close_app
  874. */
  875. void close_app(void) {
  876. delwin(MessageWindow);
  877. delwin(EffectsWindow);
  878. delwin(StatusWindow);
  879. delwin(MapWindow);
  880. nl();
  881. echo();
  882. nocbreak();
  883. endwin();
  884. }
  885. /* =============================================================================
  886. * FUNCTION: get_normal_input
  887. */
  888. ActionType get_normal_input(void) {
  889. int idx;
  890. int got_dir;
  891. int Found;
  892. ActionType Action;
  893. int i;
  894. Event = ACTION_NULL;
  895. Runkey = 0;
  896. while (Event == ACTION_NULL) {
  897. wrefresh(MapWindow);
  898. EventChar = getch();
  899. GotChar = 1;
  900. //
  901. // Decide the event corresponding to the key pressed
  902. //
  903. /* Decode key press as a ULarn Action */
  904. /*
  905. * Check ASCII key bindings if no virtual key matches and
  906. * got a valid ASCII char
  907. */
  908. Found = 0;
  909. if (!Found && GotChar) {
  910. Action = ACTION_NULL;
  911. while ((Action < ACTION_COUNT) && (!Found)) {
  912. for (i = 0; i < MAX_KEY_BINDINGS; i++) {
  913. if (KeyMap[Action][i].ModKey == M_ASCII) {
  914. /* ASCII key binding */
  915. if (EventChar == KeyMap[Action][i].VirtKey)
  916. Found = 1;
  917. }
  918. }
  919. if (!Found)
  920. Action++;
  921. }
  922. }
  923. if (Found)
  924. Event = Action;
  925. else {
  926. /* check run key */
  927. if ((EventChar == RunKeyMap.VirtKey) && (RunKeyMap.ModKey == M_ASCII))
  928. Runkey = 1;
  929. }
  930. //
  931. // Clear enhanced interface events in enhanced interface is not active
  932. //
  933. if (!enhance_interface) {
  934. if ((Event == ACTION_OPEN_DOOR) || (Event == ACTION_OPEN_CHEST))
  935. Event = ACTION_NULL;
  936. }
  937. }
  938. if (Runkey) {
  939. idx = 0;
  940. got_dir = 0;
  941. while ((idx < NUM_DIRS) && (!got_dir)) {
  942. if (DirActions[idx] == Event)
  943. got_dir = 1;
  944. else
  945. idx++;
  946. }
  947. if (got_dir)
  948. /* modify into a run event */
  949. Event = Event + 1;
  950. }
  951. return Event;
  952. }
  953. /* =============================================================================
  954. * FUNCTION: get_prompt_input
  955. */
  956. char get_prompt_input(char *prompt, char *answers, int ShowCursor) {
  957. char *ch;
  958. Print(prompt);
  959. if (ShowCursor)
  960. CaretActive = 1;
  961. //
  962. // Process events until a character in answers has been pressed.
  963. //
  964. GotChar = 0;
  965. while (!GotChar) {
  966. wrefresh(TextWindow);
  967. EventChar = getch();
  968. GotChar = 1;
  969. if (GotChar) {
  970. //
  971. // Search for the input character in the answers string
  972. //
  973. ch = strchr(answers, EventChar);
  974. if (ch == NULL) {
  975. //
  976. // Not an answer we want
  977. //
  978. GotChar = 0;
  979. }
  980. }
  981. }
  982. if (ShowCursor)
  983. CaretActive = 0;
  984. return EventChar;
  985. }
  986. /* =============================================================================
  987. * FUNCTION: get_password_input
  988. */
  989. void get_password_input(char *password, int Len) {
  990. char ch;
  991. char inputchars[256];
  992. int Pos;
  993. int value;
  994. /* get the printable characters on this system */
  995. Pos = 0;
  996. for (value = 0; value < 256; value++) {
  997. if (isprint(value)) {
  998. inputchars[Pos] = (char)value;
  999. Pos++;
  1000. }
  1001. }
  1002. /* add CR, BS and null terminator */
  1003. inputchars[Pos++] = '\010';
  1004. inputchars[Pos++] = '\015';
  1005. inputchars[Pos] = '\0';
  1006. Pos = 0;
  1007. do {
  1008. ch = get_prompt_input("", inputchars, 1);
  1009. if (isprint((int)ch) && (Pos < Len)) {
  1010. password[Pos] = ch;
  1011. Pos++;
  1012. Printc('*');
  1013. } else if (ch == '\010') {
  1014. //
  1015. // Backspace
  1016. //
  1017. if (Pos > 0) {
  1018. CursorX--;
  1019. Printc(' ');
  1020. CursorX--;
  1021. Pos--;
  1022. wmove(TextWindow, CursorY - 1, CursorX - 1);
  1023. wrefresh(TextWindow);
  1024. }
  1025. }
  1026. } while (ch != '\015');
  1027. password[Pos] = 0;
  1028. }
  1029. #if 0
  1030. /* =============================================================================
  1031. * FUNCTION: get_string_input
  1032. */
  1033. static void get_string_input(char *string, int Len)
  1034. {
  1035. char ch;
  1036. char inputchars[256];
  1037. int Pos;
  1038. int value;
  1039. /* get the printable characters on this system */
  1040. Pos = 0;
  1041. for (value = 0; value < 256; value++) {
  1042. if (isprint(value)) {
  1043. inputchars[Pos] = (char)value;
  1044. Pos++;
  1045. }
  1046. }
  1047. /* add CR, BS and null terminator */
  1048. inputchars[Pos++] = '\010';
  1049. inputchars[Pos++] = '\015';
  1050. inputchars[Pos] = '\0';
  1051. Pos = 0;
  1052. do{
  1053. ch = get_prompt_input("", inputchars, 1);
  1054. if (isprint((int)ch) && (Pos < Len)) {
  1055. string[Pos] = ch;
  1056. Pos++;
  1057. Printc(ch);
  1058. }else if (ch == '\010') {
  1059. //
  1060. // Backspace
  1061. //
  1062. if (Pos > 0) {
  1063. CursorX--;
  1064. Printc(' ');
  1065. CursorX--;
  1066. Pos--;
  1067. wmove(TextWindow, CursorY - 1, CursorX - 1);
  1068. wrefresh(TextWindow);
  1069. }
  1070. }
  1071. } while (ch != '\015');
  1072. string[Pos] = 0;
  1073. }
  1074. #endif
  1075. /* =============================================================================
  1076. * FUNCTION: get_num_input
  1077. */
  1078. int get_num_input(int defval) {
  1079. char ch;
  1080. int Pos = 0;
  1081. int value = 0;
  1082. int neg = 0;
  1083. do {
  1084. ch = get_prompt_input("", "-*0123456789\010\015", 1);
  1085. if ((ch == '-') && (Pos == 0)) {
  1086. //
  1087. // Minus
  1088. //
  1089. neg = 1;
  1090. Printc(ch);
  1091. Pos++;
  1092. }
  1093. if (ch == '*')
  1094. return defval;
  1095. else if (ch == '\010') {
  1096. //
  1097. // Backspace
  1098. //
  1099. if (Pos > 0) {
  1100. if ((Pos == 1) && neg)
  1101. neg = 0;
  1102. else
  1103. value = value / 10;
  1104. CursorX--;
  1105. Printc(' ');
  1106. CursorX--;
  1107. Pos--;
  1108. wmove(TextWindow, CursorY - 1, CursorX - 1);
  1109. wrefresh(TextWindow);
  1110. }
  1111. } else if ((ch >= '0') && (ch <= '9')) {
  1112. //
  1113. // digit
  1114. //
  1115. value = value * 10 + (ch - '0');
  1116. Printc(ch);
  1117. Pos++;
  1118. }
  1119. } while (ch != '\015');
  1120. if (Pos == 0)
  1121. return defval;
  1122. else {
  1123. if (neg)
  1124. value = -value;
  1125. return value;
  1126. }
  1127. }
  1128. /* =============================================================================
  1129. * FUNCTION: get_dir_input
  1130. */
  1131. ActionType get_dir_input(char *prompt, int ShowCursor) {
  1132. int got_dir;
  1133. int idx;
  1134. //
  1135. // Display the prompt at the current position
  1136. //
  1137. Print(prompt);
  1138. //
  1139. // Show the cursor if required
  1140. //
  1141. if (ShowCursor)
  1142. CaretActive = 1;
  1143. Event = ACTION_NULL;
  1144. got_dir = 0;
  1145. while (!got_dir) {
  1146. get_normal_input();
  1147. idx = 0;
  1148. while ((idx < NUM_DIRS) && (!got_dir)) {
  1149. if (DirActions[idx] == Event)
  1150. got_dir = 1;
  1151. else
  1152. idx++;
  1153. }
  1154. }
  1155. if (ShowCursor)
  1156. CaretActive = 0;
  1157. return Event;
  1158. }
  1159. /* =============================================================================
  1160. * FUNCTION: UpdateStatus
  1161. */
  1162. void UpdateStatus(void) {
  1163. if (CurrentDisplayMode == DISPLAY_TEXT)
  1164. /* Don't redisplay if in text mode */
  1165. return;
  1166. PaintStatus();
  1167. }
  1168. /* =============================================================================
  1169. * FUNCTION: UpdateEffects
  1170. */
  1171. void UpdateEffects(void) {
  1172. if (CurrentDisplayMode == DISPLAY_TEXT)
  1173. /* Don't redisplay if in text mode */
  1174. return;
  1175. PaintEffects();
  1176. }
  1177. /* =============================================================================
  1178. * FUNCTION: UpdateStatusAndEffects
  1179. */
  1180. void UpdateStatusAndEffects(void) {
  1181. if (CurrentDisplayMode == DISPLAY_TEXT)
  1182. /* Don't redisplay if in text mode */
  1183. return;
  1184. //
  1185. // Do effects first as update status will mark all effects as current
  1186. //
  1187. PaintEffects();
  1188. PaintStatus();
  1189. }
  1190. /* =============================================================================
  1191. * FUNCTION: set_display
  1192. */
  1193. void set_display(DisplayModeType Mode) {
  1194. //
  1195. // Save the current settings
  1196. //
  1197. if (CurrentDisplayMode == DISPLAY_MAP) {
  1198. MsgCursorX = CursorX;
  1199. MsgCursorY = CursorY;
  1200. CurrentMsgFormat = CurrentFormat;
  1201. } else if (CurrentDisplayMode == DISPLAY_TEXT) {
  1202. TextCursorX = CursorX;
  1203. TextCursorY = CursorY;
  1204. CurrentTextFormat = CurrentFormat;
  1205. }
  1206. CurrentDisplayMode = Mode;
  1207. //
  1208. // Set the text buffer settings for the new display mode
  1209. //
  1210. if (CurrentDisplayMode == DISPLAY_MAP) {
  1211. CursorX = MsgCursorX;
  1212. CursorY = MsgCursorY;
  1213. CurrentFormat = CurrentMsgFormat;
  1214. MaxLine = MAX_MSG_LINES;
  1215. TextWindow = MessageWindow;
  1216. } else if (CurrentDisplayMode == DISPLAY_TEXT) {
  1217. CursorX = TextCursorX;
  1218. CursorY = TextCursorY;
  1219. CurrentFormat = CurrentTextFormat;
  1220. MaxLine = MAX_TEXT_LINES;
  1221. TextWindow = stdscr;
  1222. }
  1223. PaintWindow();
  1224. }
  1225. /* =============================================================================
  1226. * FUNCTION: IncCursorY
  1227. *
  1228. * DESCRIPTION:
  1229. * Increae the cursor y position, scrolling the text window if requried.
  1230. *
  1231. * PARAMETERS:
  1232. *
  1233. * Count : The number of lines to increase the cursor y position
  1234. *
  1235. * RETURN VALUE:
  1236. *
  1237. * None.
  1238. */
  1239. static void IncCursorY(int Count) {
  1240. /*int Scroll;*/
  1241. int inc;
  1242. int x;
  1243. inc = Count;
  1244. /*Scroll = 0;*/
  1245. while (inc > 0) {
  1246. CursorY = CursorY + 1;
  1247. if (CursorY > MaxLine) {
  1248. /*Scroll = 1;*/
  1249. CursorY--;
  1250. scrollok(TextWindow, 1);
  1251. scroll(TextWindow);
  1252. scrollok(TextWindow, 0);
  1253. SetCursesAttr(FORMAT_NORMAL);
  1254. for (x = 0; x < LINE_LENGTH; x++)
  1255. mvwaddch(TextWindow, CursorY - 1, x, ' ');
  1256. }
  1257. inc--;
  1258. }
  1259. wrefresh(TextWindow);
  1260. }
  1261. /* =============================================================================
  1262. * FUNCTION: IncCursorX
  1263. *
  1264. * DESCRIPTION:
  1265. * Increase the cursor x position, handling line wrap.
  1266. *
  1267. * PARAMETERS:
  1268. *
  1269. * Count : The amount to increase the cursor x position.
  1270. *
  1271. * RETURN VALUE:
  1272. *
  1273. * None.
  1274. */
  1275. static void IncCursorX(int Count) {
  1276. CursorX = CursorX + Count;
  1277. if (CursorX > LINE_LENGTH) {
  1278. CursorX = 1;
  1279. IncCursorY(1);
  1280. }
  1281. }
  1282. /* =============================================================================
  1283. * FUNCTION: ClearText
  1284. */
  1285. void ClearText(void) {
  1286. int x, y;
  1287. //
  1288. // Clear the text buffer
  1289. //
  1290. SetCursesAttr(FORMAT_NORMAL);
  1291. for (y = 0; y < MaxLine; y++) {
  1292. for (x = 0; x < LINE_LENGTH; x++)
  1293. mvwaddch(TextWindow, y, x, ' ');
  1294. }
  1295. wrefresh(TextWindow);
  1296. CursorX = 1;
  1297. CursorY = 1;
  1298. }
  1299. /* =============================================================================
  1300. * FUNCTION: beep
  1301. */
  1302. void UlarnBeep(void) {
  1303. //
  1304. // Play a beep
  1305. //
  1306. if (!nobeep) {
  1307. #ifdef W32_TTY
  1308. //
  1309. // beep seems to cause problems under windows, so put a bell
  1310. //
  1311. putch(7);
  1312. #else
  1313. beep();
  1314. #endif
  1315. }
  1316. }
  1317. /* =============================================================================
  1318. * FUNCTION: Cursor
  1319. */
  1320. void MoveCursor(int x, int y) {
  1321. CursorX = x;
  1322. CursorY = y;
  1323. }
  1324. static int RefreshEachChar = 1;
  1325. /* =============================================================================
  1326. * FUNCTION: Printc
  1327. */
  1328. void Printc(char c) {
  1329. int incx;
  1330. switch (c) {
  1331. case '\t':
  1332. incx = ((((CursorX - 1) / 8) + 1) * 8 + 1) - CursorX;
  1333. IncCursorX(incx);
  1334. break;
  1335. case '\n':
  1336. CursorX = 1;
  1337. IncCursorY(1);
  1338. break;
  1339. case '\015':
  1340. break;
  1341. default:
  1342. SetCursesAttr(CurrentFormat);
  1343. mvwaddch(TextWindow, CursorY - 1, CursorX - 1, c);
  1344. if (RefreshEachChar)
  1345. wrefresh(TextWindow);
  1346. IncCursorX(1);
  1347. break;
  1348. }
  1349. }
  1350. /* =============================================================================
  1351. * FUNCTION: Print
  1352. */
  1353. void Print(char *string) {
  1354. int Len;
  1355. int pos;
  1356. if (string == NULL)
  1357. return;
  1358. Len = strlen(string);
  1359. if (Len == 0)
  1360. return;
  1361. RefreshEachChar = 0;
  1362. for (pos = 0; pos < Len; pos++)
  1363. Printc(string[pos]);
  1364. RefreshEachChar = 1;
  1365. wrefresh(TextWindow);
  1366. }
  1367. /* =============================================================================
  1368. * FUNCTION: Printf
  1369. */
  1370. void Printf(char *fmt, ...) {
  1371. char buf[2048];
  1372. va_list argptr;
  1373. va_start(argptr, fmt);
  1374. vsprintf(buf, fmt, argptr);
  1375. va_end(argptr);
  1376. Print(buf);
  1377. }
  1378. /* =============================================================================
  1379. * FUNCTION: Standout
  1380. */
  1381. void Standout(char *String) {
  1382. CurrentFormat = FORMAT_STANDOUT;
  1383. Print(String);
  1384. CurrentFormat = FORMAT_NORMAL;
  1385. }
  1386. /* =============================================================================
  1387. * FUNCTION: SetFormat
  1388. */
  1389. void SetFormat(FormatType format) { CurrentFormat = format; }
  1390. /* =============================================================================
  1391. * FUNCTION: ClearToEOL
  1392. */
  1393. void ClearToEOL(void) {
  1394. int x;
  1395. for (x = CursorX; x <= LINE_LENGTH; x++) {
  1396. SetCursesAttr(FORMAT_NORMAL);
  1397. mvwaddch(TextWindow, CursorY - 1, x - 1, ' ');
  1398. }
  1399. }
  1400. /* =============================================================================
  1401. * FUNCTION: ClearToEOPage
  1402. */
  1403. void ClearToEOPage(int x, int y) {
  1404. int tx, ty;
  1405. SetCursesAttr(FORMAT_NORMAL);
  1406. for (tx = x; tx <= LINE_LENGTH; tx++)
  1407. mvwaddch(TextWindow, y - 1, tx - 1, ' ');
  1408. for (ty = y + 1; ty <= MaxLine; ty++)
  1409. for (tx = 1; tx <= LINE_LENGTH; tx++)
  1410. mvwaddch(TextWindow, ty - 1, tx - 1, ' ');
  1411. }
  1412. /* =============================================================================
  1413. * FUNCTION: show1cell
  1414. */
  1415. void show1cell(int x, int y) {
  1416. int TileId;
  1417. int Attr;
  1418. int Color;
  1419. /* see nothing if blind */
  1420. if (c[BLINDCOUNT])
  1421. return;
  1422. /* we end up knowing about it */
  1423. know[x][y] = item[x][y];
  1424. if (mitem[x][y].mon != MONST_NONE)
  1425. stealth[x][y] |= STEALTH_SEEN;
  1426. GetTile(x, y, &TileId, &Attr, &Color);
  1427. wattrset(MapWindow, Attr | COLOR_PAIR(Color));
  1428. mvwaddch(MapWindow, y, x, TileId);
  1429. wrefresh(MapWindow);
  1430. }
  1431. /* =============================================================================
  1432. * FUNCTION: showplayer
  1433. */
  1434. void showplayer(void) {
  1435. int TileId;
  1436. int Attr;
  1437. int Color;
  1438. //
  1439. // Determine if we need to scroll the map
  1440. //
  1441. if (c[BLINDCOUNT] == 0) {
  1442. TileId = '@';
  1443. #ifdef W32_TTY
  1444. Attr = A_BOLD;
  1445. #else
  1446. Attr = 0;
  1447. #endif
  1448. Color = C_RED;
  1449. } else
  1450. GetTile(playerx, playery, &TileId, &Attr, &Color);
  1451. wattrset(MapWindow, Attr | COLOR_PAIR(Color));
  1452. mvwaddch(MapWindow, playery, playerx, TileId);
  1453. wmove(MapWindow, playery, playerx);
  1454. wrefresh(MapWindow);
  1455. }
  1456. /* =============================================================================
  1457. * FUNCTION: showcell
  1458. */
  1459. void showcell(int x, int y) {
  1460. int minx, maxx;
  1461. int miny, maxy;
  1462. int mx, my;
  1463. int TileId;
  1464. int Attr;
  1465. int Color;
  1466. /*
  1467. * Decide how much the player knows about around him/her.
  1468. */
  1469. if (c[AWARENESS]) {
  1470. minx = x - 3;
  1471. maxx = x + 3;
  1472. miny = y - 3;
  1473. maxy = y + 3;
  1474. } else {
  1475. minx = x - 1;
  1476. maxx = x + 1;
  1477. miny = y - 1;
  1478. maxy = y + 1;
  1479. }
  1480. if (c[BLINDCOUNT]) {
  1481. minx = x;
  1482. maxx = x;
  1483. miny = y;
  1484. maxy = y;
  1485. }
  1486. /*
  1487. * Limit the area to the map extents
  1488. */
  1489. if (minx < 0)
  1490. minx = 0;
  1491. if (maxx > MAXX - 1)
  1492. maxx = MAXX - 1;
  1493. if (miny < 0)
  1494. miny = 0;
  1495. if (maxy > MAXY - 1)
  1496. maxy = MAXY - 1;
  1497. for (my = miny; my <= maxy; my++) {
  1498. for (mx = minx; mx <= maxx; mx++) {
  1499. if ((mx == playerx) && (my == playery))
  1500. know[mx][my] = item[mx][my];
  1501. else if ((know[mx][my] != item[mx][my]) || /* item changed */
  1502. ((mx == lastpx) && (my == lastpy)) || /* last player pos */
  1503. ((mitem[mx][my].mon != MONST_NONE) && /* unseen monster */
  1504. ((stealth[mx][my] & STEALTH_SEEN) == 0))) {
  1505. //
  1506. // Only draw areas not already known (and hence displayed)
  1507. //
  1508. know[mx][my] = item[mx][my];
  1509. if (mitem[mx][my].mon != MONST_NONE)
  1510. stealth[mx][my] |= STEALTH_SEEN;
  1511. GetTile(mx, my, &TileId, &Attr, &Color);
  1512. wattrset(MapWindow, Attr | COLOR_PAIR(Color));
  1513. mvwaddch(MapWindow, my, mx, TileId);
  1514. } // if not known
  1515. }
  1516. }
  1517. showplayer();
  1518. }
  1519. /* =============================================================================
  1520. * FUNCTION: drawscreen
  1521. */
  1522. void drawscreen(void) { PaintWindow(); }
  1523. /* =============================================================================
  1524. * FUNCTION: draws
  1525. */
  1526. void draws(int minx, int miny, int maxx, int maxy) {
  1527. (void)minx;
  1528. (void)miny;
  1529. (void)maxx;
  1530. (void)maxy;
  1531. PaintWindow();
  1532. }
  1533. /* =============================================================================
  1534. * FUNCTION: mapeffect
  1535. */
  1536. void mapeffect(int x, int y, DirEffectsType effect, int dir) {
  1537. wattrset(MapWindow, A_NORMAL | COLOR_PAIR(EffectColor[effect]));
  1538. mvwaddch(MapWindow, y, x, EffectTile[effect][dir]);
  1539. wmove(MapWindow, 0, 0);
  1540. touchwin(MapWindow);
  1541. wrefresh(MapWindow);
  1542. }
  1543. /* =============================================================================
  1544. * FUNCTION: magic_effect_frames
  1545. */
  1546. int magic_effect_frames(MagicEffectsType fx) { return magicfx_tile[fx].Frames; }
  1547. /* =============================================================================
  1548. * FUNCTION: magic_effect
  1549. */
  1550. void magic_effect(int x, int y, MagicEffectsType fx, int frame) {
  1551. wattrset(MapWindow, COLOR_PAIR(magicfx_tile[fx].Color[frame]));
  1552. mvwaddch(MapWindow, y, x, magicfx_tile[fx].Tile[frame]);
  1553. wmove(MapWindow, 0, 0);
  1554. touchwin(MapWindow);
  1555. wrefresh(MapWindow);
  1556. }
  1557. /* =============================================================================
  1558. * FUNCTION: nap
  1559. */
  1560. void nap(int delay) {
  1561. #ifdef UNIX
  1562. usleep(delay * 1000);
  1563. #else
  1564. napms(delay);
  1565. #endif
  1566. }
  1567. /* =============================================================================
  1568. * FUNCTION: GetUser
  1569. */
  1570. void GetUser(char *username, int *uid) {
  1571. #ifdef UNIX
  1572. *uid = getuid();
  1573. strcpy(username, getenv("USER"));
  1574. #else
  1575. FILE *fp;
  1576. char TmpName[80];
  1577. int TmpPid;
  1578. int Found;
  1579. int n;
  1580. if (username[0] == 0) {
  1581. //
  1582. // Name is not yet specified, so ask player for the name
  1583. //
  1584. Print("Who are you? ");
  1585. get_string_input(username, USERNAME_LENGTH);
  1586. if (strlen(username) == 0)
  1587. strcpy(username, "Anon");
  1588. }
  1589. /* get the Player Id */
  1590. fp = fopen(PIDName, "rb");
  1591. if (fp == NULL) {
  1592. /* Need to create the PID file. */
  1593. fp = fopen(PIDName, "wb");
  1594. if (fp != NULL) {
  1595. *uid = FIRST_PID;
  1596. fwrite(username, USERNAME_LENGTH + 1, 1, fp);
  1597. fwrite(uid, sizeof(int), 1, fp);
  1598. fclose(fp);
  1599. }
  1600. } else {
  1601. /* search the PID file for this player id */
  1602. Found = 0;
  1603. TmpPid = FIRST_PID;
  1604. while (!feof(fp) && !Found) {
  1605. n = fread(TmpName, USERNAME_LENGTH + 1, 1, fp);
  1606. if (n == 1)
  1607. n = fread(&TmpPid, sizeof(int), 1, fp);
  1608. if (n == 1) {
  1609. if (strcmp(TmpName, username) == 0) {
  1610. *uid = TmpPid;
  1611. Found = 1;
  1612. }
  1613. }
  1614. }
  1615. fclose(fp);
  1616. if (!Found) {
  1617. *uid = TmpPid + 1;
  1618. fp = fopen(PIDName, "ab");
  1619. if (fp != NULL) {
  1620. fwrite(username, USERNAME_LENGTH + 1, 1, fp);
  1621. fwrite(uid, sizeof(int), 1, fp);
  1622. fclose(fp);
  1623. }
  1624. }
  1625. }
  1626. #endif
  1627. }