123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186 |
- #include <stdlib.h>
- #include "main.h"
- #include "Character.h"
- chr CreateCharacter( struct Game* g ){
- chr C = NULL;
- C = malloc(sizeof(character));
- if(C) {
- wrefresh(g->win);
- C->pos = NULL;
- wrefresh(g->win);
- C->pos = CreateCoords();
- wrefresh(g->win);
- C->context = g;
- wrefresh(g->win);
- ResetCharacter(C);
- wrefresh(g->win);
- return C;
- } else {
- fprintf( L, "Error when trying to allocate memory in CreateCharacter.\n" );
- exit(1);
- }
- }
- void PurgeCharacter( chr c ){
- ResetCharacter(c);
- if( c->pos ) {
- DESTROY(c->pos);
- }
- }
- void ResetCharacter( chr c ){
- int i = 0;
- for( i = 0; i < nattrs; ++i ) {
- attr* attr_hash = Attrs + i;
- SetStat( c, attr_hash->str, 0 );
- }
- }
- void CopyCharacter( chr sink, chr source ){
- int i = 0;
- for( i = 0; i < nattrs; ++i ) {
- attr* attr_hash = Attrs + i;
- SetStat( sink, attr_hash->str, GetStat( source, attr_hash->str ) );
- }
- }
- void SetStat( chr c, char* stat, int newval ){
- switch( AttrHash( stat ) ) {
- case ISPC:
- c->ispc = newval;
- break;
- case POS_X:
- mvwprintw(c->context->arena, c->pos->y, c->pos->x, " " );
- c->pos->x = newval;
- mvwprintw(c->context->arena, c->pos->y, c->pos->x, "%c", c->rep );
- break;
- case POS_Y:
- mvwprintw(c->context->arena, c->pos->y, c->pos->x, " " );
- c->pos->y = newval;
- mvwprintw(c->context->arena, c->pos->y, c->pos->x, "%c", c->rep );
- break;
- case ALERT:
- c->alert = newval;
- break;
- case ALERTNESS:
- c->alertness = newval;
- break;
- case FATIGUE:
- c->fatigue = newval;
- break;
- case EXHAUSTED:
- c->exhausted = newval;
- break;
- case HEALTH:
- c->health = newval;
- break;
- case WIN:
- c->win = newval;
- break;
- case ID:
- c->id = newval;
- break;
- case REP:
- c->rep = newval;
- break;
- }
- }
- int GetStat( chr c, char* stat ){
- switch( AttrHash( stat ) ) {
- case ISPC:
- return c->ispc;
- case POS_X:
- return c->pos->x;
- case POS_Y:
- return c->pos->y;
- case ALERT:
- return c->alert;
- case ALERTNESS:
- return c->alertness;
- case FATIGUE:
- return c->fatigue;
- case EXHAUSTED:
- return c->exhausted;
- case HEALTH:
- return c->health;
- case WIN:
- return c->win;
- case ID:
- return c->id;
- case REP:
- return c->rep;
- }
- }
- /*! Convert name of an attribute into its key value.
- * Permits switch to work on attributes given by name. */
- int AttrHash( char* a ) {
- int i;
- for( i = 0; i < nattrs; ++i ) {
- attr* attr_pair = Attrs + i;
- if( strcmp( attr_pair->str, a ) == 0 ) return attr_pair->val;
- }
- return BAD_KEY;
- }
- void FilePrint( chr c ) {
- if( c == NULL ) return;
- attr* attr_pair = Attrs;
- fprintf( L, "%s = %d", attr_pair->str, GetStat( c, attr_pair->str ) );
- int i;
- for( i=1; i < nattrs; ++i ) {
- attr* attr_pair = Attrs + i;
- fprintf( L, ", %s = %d", attr_pair->str, GetStat( c, attr_pair->str ) );
- }
- fprintf( L, "\n" );
- }
- void CursesPrint( chr c ) {
- if( !c ) return;
- int attrcheck = c->alert;
- int dist_cat = 0;
- WINDOW* w = c->context->arena;
- wattron(w, GetCharAttr(c, distance( c->context->pc->pos, c->pos )));
- if( dist_cat <= 1 ) mvwprintw( w, c->pos->y, c->pos->x, "%c", c->rep );
- wstandend(w);
- wrefresh(w);
- }
- void init_pc( chr c ) {
- wrefresh(c->context->win);
- c->ispc = TRUE;
- wrefresh(c->context->win);
- c->pos->x = 1;
- wrefresh(c->context->win);
- c->pos->y = 1;
- wrefresh(c->context->win);
- c->rep = '@';
- wrefresh(c->context->win);
- c->fatigue = 0;
- wrefresh(c->context->win);
- c->exhausted = FALSE;
- wrefresh(c->context->win);
- c->win = FALSE;
- wrefresh(c->context->win);
- c->health = max_health;
- wrefresh(c->context->win);
- }
- void init_npc( chr c ) {
- c->ispc = FALSE;
- int max_y, max_x;
- getmaxyx( c->context->arena, max_y, max_x );
- c->pos->x = randint(2, max_x-1);
- c->pos->y = randint(2, max_y-1);
- while( blocked( c->context->arena, c->pos->y, c->pos->x ) ) {
- c->pos->x = randint(2, max_x - 1 );
- c->pos->y = randint(2, max_y - 1 );
- }
- c->rep = 'X';
- c->alert = FALSE;
- c->alertness = 0;
- c->health = max_health;
- }
|