Character.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. #include <stdlib.h>
  2. #include "main.h"
  3. #include "Character.h"
  4. chr CreateCharacter( struct Game* g ){
  5. chr C = NULL;
  6. C = malloc(sizeof(character));
  7. if(C) {
  8. wrefresh(g->win);
  9. C->pos = NULL;
  10. wrefresh(g->win);
  11. C->pos = CreateCoords();
  12. wrefresh(g->win);
  13. C->context = g;
  14. wrefresh(g->win);
  15. ResetCharacter(C);
  16. wrefresh(g->win);
  17. return C;
  18. } else {
  19. fprintf( L, "Error when trying to allocate memory in CreateCharacter.\n" );
  20. exit(1);
  21. }
  22. }
  23. void PurgeCharacter( chr c ){
  24. ResetCharacter(c);
  25. if( c->pos ) {
  26. DESTROY(c->pos);
  27. }
  28. }
  29. void ResetCharacter( chr c ){
  30. int i = 0;
  31. for( i = 0; i < nattrs; ++i ) {
  32. attr* attr_hash = Attrs + i;
  33. SetStat( c, attr_hash->str, 0 );
  34. }
  35. }
  36. void CopyCharacter( chr sink, chr source ){
  37. int i = 0;
  38. for( i = 0; i < nattrs; ++i ) {
  39. attr* attr_hash = Attrs + i;
  40. SetStat( sink, attr_hash->str, GetStat( source, attr_hash->str ) );
  41. }
  42. }
  43. void SetStat( chr c, char* stat, int newval ){
  44. switch( AttrHash( stat ) ) {
  45. case ISPC:
  46. c->ispc = newval;
  47. break;
  48. case POS_X:
  49. mvwprintw(c->context->arena, c->pos->y, c->pos->x, " " );
  50. c->pos->x = newval;
  51. mvwprintw(c->context->arena, c->pos->y, c->pos->x, "%c", c->rep );
  52. break;
  53. case POS_Y:
  54. mvwprintw(c->context->arena, c->pos->y, c->pos->x, " " );
  55. c->pos->y = newval;
  56. mvwprintw(c->context->arena, c->pos->y, c->pos->x, "%c", c->rep );
  57. break;
  58. case ALERT:
  59. c->alert = newval;
  60. break;
  61. case ALERTNESS:
  62. c->alertness = newval;
  63. break;
  64. case FATIGUE:
  65. c->fatigue = newval;
  66. break;
  67. case EXHAUSTED:
  68. c->exhausted = newval;
  69. break;
  70. case HEALTH:
  71. c->health = newval;
  72. break;
  73. case WIN:
  74. c->win = newval;
  75. break;
  76. case ID:
  77. c->id = newval;
  78. break;
  79. case REP:
  80. c->rep = newval;
  81. break;
  82. }
  83. }
  84. int GetStat( chr c, char* stat ){
  85. switch( AttrHash( stat ) ) {
  86. case ISPC:
  87. return c->ispc;
  88. case POS_X:
  89. return c->pos->x;
  90. case POS_Y:
  91. return c->pos->y;
  92. case ALERT:
  93. return c->alert;
  94. case ALERTNESS:
  95. return c->alertness;
  96. case FATIGUE:
  97. return c->fatigue;
  98. case EXHAUSTED:
  99. return c->exhausted;
  100. case HEALTH:
  101. return c->health;
  102. case WIN:
  103. return c->win;
  104. case ID:
  105. return c->id;
  106. case REP:
  107. return c->rep;
  108. }
  109. }
  110. /*! Convert name of an attribute into its key value.
  111. * Permits switch to work on attributes given by name. */
  112. int AttrHash( char* a ) {
  113. int i;
  114. for( i = 0; i < nattrs; ++i ) {
  115. attr* attr_pair = Attrs + i;
  116. if( strcmp( attr_pair->str, a ) == 0 ) return attr_pair->val;
  117. }
  118. return BAD_KEY;
  119. }
  120. void FilePrint( chr c ) {
  121. if( c == NULL ) return;
  122. attr* attr_pair = Attrs;
  123. fprintf( L, "%s = %d", attr_pair->str, GetStat( c, attr_pair->str ) );
  124. int i;
  125. for( i=1; i < nattrs; ++i ) {
  126. attr* attr_pair = Attrs + i;
  127. fprintf( L, ", %s = %d", attr_pair->str, GetStat( c, attr_pair->str ) );
  128. }
  129. fprintf( L, "\n" );
  130. }
  131. void CursesPrint( chr c ) {
  132. if( !c ) return;
  133. int attrcheck = c->alert;
  134. int dist_cat = 0;
  135. WINDOW* w = c->context->arena;
  136. wattron(w, GetCharAttr(c, distance( c->context->pc->pos, c->pos )));
  137. if( dist_cat <= 1 ) mvwprintw( w, c->pos->y, c->pos->x, "%c", c->rep );
  138. wstandend(w);
  139. wrefresh(w);
  140. }
  141. void init_pc( chr c ) {
  142. wrefresh(c->context->win);
  143. c->ispc = TRUE;
  144. wrefresh(c->context->win);
  145. c->pos->x = 1;
  146. wrefresh(c->context->win);
  147. c->pos->y = 1;
  148. wrefresh(c->context->win);
  149. c->rep = '@';
  150. wrefresh(c->context->win);
  151. c->fatigue = 0;
  152. wrefresh(c->context->win);
  153. c->exhausted = FALSE;
  154. wrefresh(c->context->win);
  155. c->win = FALSE;
  156. wrefresh(c->context->win);
  157. c->health = max_health;
  158. wrefresh(c->context->win);
  159. }
  160. void init_npc( chr c ) {
  161. c->ispc = FALSE;
  162. int max_y, max_x;
  163. getmaxyx( c->context->arena, max_y, max_x );
  164. c->pos->x = randint(2, max_x-1);
  165. c->pos->y = randint(2, max_y-1);
  166. while( blocked( c->context->arena, c->pos->y, c->pos->x ) ) {
  167. c->pos->x = randint(2, max_x - 1 );
  168. c->pos->y = randint(2, max_y - 1 );
  169. }
  170. c->rep = 'X';
  171. c->alert = FALSE;
  172. c->alertness = 0;
  173. c->health = max_health;
  174. }