示例#1
0
文件: soul_create.c 项目: 0x1p2/rpg-c
int soul_create(soul_t *creature, char *name, char *desc, char p_class, int lvl)
{
	tools("clear", NULL);
	//unsigned long pass;
	char plaintext[64];

	if(name == NULL)
	{
		printf(" Name: ");
		fgets(creature->name, sizeof(creature->name), stdin);	// set p->name to input data.
		creature->name[strcspn(creature->name, "\n")] = 0;	// remove trailing '\n'
		if(!file_exists(creature->name))
		{
			if(PASSWD) {
				printf(" Password: "******" Description: ");
			fgets(creature->desc, sizeof(creature->desc), stdin);	//set p->desc to input data.
			creature->desc[strcspn(creature->desc, "\n")] = 0;	// removetrailing '\n'

			creature->speed = 3.5;	// Generate player speed
			creature->luck = 25;		// Set player's initial luck.
			creature->type = 'p';		// Set player's type. ( p = player, m = mob )
			creature->gold = 500.00;	// Set base gold value player owns.

			class_create(creature, 0, 1);	// Call on selectings the player's class.

			printf("\n Player name: %s\n Description: %s\n  Health: %d\n  Damage: %d\n  Speed: %.1f",
					creature->name, creature->desc, creature->hp, creature->dmg, creature->speed);
			printf(" Primary Skill:\n   %s, %.1f \n", creature->skill->name, creature->skill->val);

			item_init(&creature->objs.bandaid, "Bandage",  25);	// Give player bandages.

			save(creature);		// Save the player.
			getchar();
			tools("pause", NULL);
		} else
			load_profile(creature);	
	} else
	{
		strncpy(creature->name, name, sizeof(creature->name));	// Changed from strcpy (safer now)
		strncpy(creature->desc, desc, sizeof(creature->desc));	// Same as above.
		creature->speed = 4.5;	// Set speed.
		creature->type = 'm';				// Set type to mob ( m = mob )
		creature->gold = (250.00 * ((float)lvl / 2));				// Gold being held.

		
		item_init(&creature->objs.bandaid, "Bandage",  0);	// No bandages for the NPC!

		class_create(creature, p_class, lvl);			// Assign the class.
	}


	return 0;
}
示例#2
0
hash_table_t * duplicate_process_ht(hash_table_t * h, hash_table_t * usage_map) {
	int i;
   item_t * cur;
	fd_item_t * fd_it;
	fd_item_t * fd_it_old;
   hash_table_t * ht = malloc(sizeof(hash_table_t));

   hash_table_init(ht, h->entries, h->op);
   for (i = 0; i < h->entries; i++) {
      cur = h->entry[i].head;
      while ( cur != NULL ) {
			fd_it = new_fd_item();			
			fd_it_old = list_entry(cur, fd_item_t, item);

			fd_it->old_fd = fd_it_old->old_fd;
			item_init(&fd_it->item);

			memcpy(fd_it->fd_map, fd_it_old->fd_map, sizeof(fd_map_t));

			hash_table_insert(ht, &fd_it->old_fd, &fd_it->item);
			increase_fd_usage(usage_map, fd_it_old->fd_map->my_fd);
         cur = cur->next;
      }
   }
   return ht;
}
示例#3
0
int init_config(void)
{
	int id, ret, len = 0;
	void * cfg = (void *)ITEMS_LOWBASE;

	printf("cfg: 0x%p\n", cfg);
	id = boot_device();
	switch(id) {
		case DEV_EEPROM:
		case DEV_FLASH:
			cfg = (void *)(IRAM_BASE_PA + BL_SIZE_FIXED
					- ITEM_SIZE_EMBEDDED);
			len = ITEM_SIZE_EMBEDDED;
			break ;
		case DEV_BND:
			id = DEV_NAND;
		default:
			printf("read items: id=%d\n", id);
			ret = vs_assign_by_id(id, 1);
			if (ret)
			  return ret;
			ret = vs_read(cfg, BL_LOC_CONFIG, ITEM_SIZE_NORMAL, 0);
			if (ret < 0)
			  return ret;
			len = ITEM_SIZE_NORMAL;
	}

	printf("begin init.\n");
	return item_init(cfg, len);
}
示例#4
0
inline fd_usage_t * new_fd_usage() {
	fd_usage_t * fd_usage;

	fd_usage = malloc(sizeof(fd_usage_t));
	item_init(&fd_usage->item);
	fd_usage->usage = 0;
	return fd_usage;
}
示例#5
0
static void
item_free (struct item_s *it)
{
    ASSERT (it != NULL);
    _close (&(it->fd));
    _close (it->pfd + 0);
    _close (it->pfd + 1);
    item_init (it);
    free (it);
}
示例#6
0
item_t * new_process_ht(int32_t pid) {
	process_hash_item_t * p_ht_it = malloc(sizeof(process_hash_item_t));
	p_ht_it->ht = malloc(sizeof(hash_table_t));

	item_init(&p_ht_it->item);
	hash_table_init(p_ht_it->ht, HASH_TABLE_SIZE, &ht_ops_fditem);
	p_ht_it->pid = pid;

	return &p_ht_it->item;
}
示例#7
0
static void null_leave(struct state *st, struct state *next, int id)
{
    part_init();
    shad_init();
    ball_init();
    item_init();
    geom_init();
    gui_init();
    hud_init();
}
示例#8
0
inline fd_item_t * new_fd_item() {
	fd_item_t * fd_item;

	fd_item = malloc(sizeof(fd_item_t));
	item_init(&fd_item->item);
	fd_item->fd_map = malloc(sizeof(fd_map_t));
	memset(fd_item->fd_map->parent_fds, -1, MAX_PARENT_IDS * sizeof(int));
	fd_item->fd_map->last_par_index = -1;
	return fd_item;
}
示例#9
0
static int draw_city(game_t * game)
{
	int i;
	int x,y;
	int num_item;
	int size_pic;
	llist_t * cur_list;
	city_t * cur_city;

	if( item_city != NULL ) {
		free(item_city);
	}

	item_city = NULL;

	num_item = 0;
	for(i=0; i<game->opponent_num+1; i++) {
		cur_list = game->wiz[i].city;
		while(cur_list!=NULL ) {
			cur_city = (city_t*)cur_list->data;

			if(cur_city->side != cur_side) {
				cur_list = cur_list->next;
				continue;
			}

			item_city = (item_t*)realloc(item_city,sizeof(item_t)*(num_item+1));

			x = get_tile_coord_x(cur_city->x, game);
			y = get_tile_coord_y(cur_city->y, game);

			/* City picture are larger than tiles picture */
			x -= (back[CITY_NOWALL].w - TILE_SIZE_W)/2;
			y -= (back[CITY_NOWALL].h - TILE_SIZE_H)/2;

			item_init(&item_city[num_item]);

			size_pic = cur_city->population/10000;
			if(size_pic > 5 ) {
				size_pic = 5;
			}
			item_set_frame(&item_city[num_item],x,y+1,
						   &back[CITY_NOWALL]);
			item_set_frame_normal(&item_city[num_item],size_pic);

			item_set_click_right(&item_city[num_item],cb_city_screen,cur_city);

			num_item++;
			cur_list = cur_list->next;
		}
	}

	return num_item;
}
示例#10
0
static int draw_encounter(game_t * game)
{
	int x,y;
	int num_item;
	llist_t * cur_list;
	encounter_t * cur_encounter;

	if( item_encounter != NULL ) {
		free(item_encounter);
	}

	item_encounter = NULL;

	num_item = 0;

	cur_list = game->encounter;
	while(cur_list!=NULL ) {
		cur_encounter = (encounter_t*)cur_list->data;

		if(cur_encounter->side != cur_side) {
			cur_list = cur_list->next;
			continue;
		}

		item_encounter = (item_t*)realloc(item_encounter,sizeof(item_t)*(num_item+1));

		x = get_tile_coord_x(cur_encounter->x, game);
		y = get_tile_coord_y(cur_encounter->y, game);

		item_init(&item_encounter[num_item]);

		item_set_frame(&item_encounter[num_item],x,y+1,
					   &back[cur_encounter->type]);

		num_item++;
		cur_list = cur_list->next;
	}

	return num_item;
}
示例#11
0
static void
main_init_srv (char **urlv)
{
    int rc, opt;
    struct sockaddr_storage ss;

    for (char **pu = urlv; *pu; ++pu) {
        struct item_s *srv = malloc (sizeof (struct item_s));

        item_init (srv);

        if (!sockaddr_init (SA (&ss), *pu))
            abort ();
        srv->events = EPOLLIN;
        srv->type = SERVER;
        srv->fd = socket (SAFAM (&ss), SOCK_STREAM | O_CLOEXEC | O_NONBLOCK, 0);
        ASSERT (srv->fd >= 0);

        opt = 1;
        setsockopt (srv->fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof (opt));

        rc = bind (srv->fd, SA (&ss), SALEN (&ss));
        ASSERT (rc == 0);
        rc = listen (srv->fd, front_backlog);
        ASSERT (rc == 0);

        struct epoll_event evt;

retry_add:
        evt.data.ptr = srv;
        evt.events = EPOLLIN;
        rc = epoll_ctl (fd_epoll, EPOLL_CTL_ADD, srv->fd, &evt);
        if (rc < 0) {
            if (errno == EINTR)
                goto retry_add;
            ASSERT (rc == 0);
        }
    }
}
示例#12
0
static int draw_tile(game_t * game)
{
	int tile_x,tile_y;
	int index;
	item_t * item_cur;
	int num_tile_w = ORIGINAL_GAME_WIDTH/TILE_SIZE_W ;
	int num_tile_h = ORIGINAL_GAME_HEIGHT/TILE_SIZE_H +2 ;
	int x,y;
	int count_w;
	int count_h;
	int tile_index;

	if( item_tile == NULL ) {
		item_tile = (item_t*)malloc(sizeof(item_t)*game->map->width*game->map->height);
	}

	index=0;
	for( count_w = -num_tile_w/2; count_w < num_tile_w/2; count_w ++) {
		for( count_h=(-num_tile_h/2); count_h<num_tile_h/2; count_h++) {
			tile_y=cur_tile_y+count_h;
			if(tile_y < 0 || tile_y >= game->map->height) {
				continue;
			}
			tile_x=(cur_tile_x+game->map->width+count_w)%game->map->width;
			item_cur = &item_tile[index];
			item_init(item_cur);
			x=orig_x+(count_w*TILE_SIZE_W);
			y=orig_y+(count_h*TILE_SIZE_H);
			tile_index=game->map->side[cur_side].tile[tile_x][tile_y];
			item_set_anim(item_cur,x,y,&tile[tile_index]);
			item_set_click_right(item_cur,cb_move_map,(void*)(((intptr_t)tile_x<<16)+(intptr_t)tile_y));
			index++;
		}
	}

	return index;
}
示例#13
0
extern EOdeque * eo_deque_New(eOsizeitem_t item_size, eOsizecntnr_t capacity,
                              eOres_fp_voidp_uint32_t item_init, uint32_t init_par,  
                              eOres_fp_voidp_voidp_t item_copy, eOres_fp_voidp_t item_clear)
{
    EOdeque *retptr = NULL;
    uint8_t *start = NULL;
    uint8_t *item = NULL;
//    uint32_t pos = 0;
    eOsizecntnr_t i = 0; 
    eOmempool_alignment_t align = eo_mempool_align_08bit;


    // i get the memory for the object. no need to check versus NULL because the memory pool already does it
    retptr = (EOdeque*) eo_mempool_GetMemory(eo_mempool_GetHandle(), eo_mempool_align_32bit, sizeof(EOdeque), 1);



    // now the object has valid memory. i need to initialise it with user-defined data,
    
    retptr->size                = 0;
    retptr->first               = 0;
    retptr->next                = 0;

    eo_errman_Assert(eo_errman_GetHandle(), (0 != item_size), "eo_deque_New(): 0 item_size", s_eobj_ownname, &eo_errman_DescrWrongParamLocal);
    eo_errman_Assert(eo_errman_GetHandle(), (0 != capacity), "eo_deque_New(): 0 capacity", s_eobj_ownname, &eo_errman_DescrWrongParamLocal);

    retptr->item_size           = item_size;
    retptr->sizeofstoreditem    = item_size;
    retptr->capacity            = capacity;
    retptr->item_init_fn        = item_init;
    retptr->item_init_par       = init_par;    
    retptr->item_copy_fn        = item_copy;
    retptr->item_clear_fn       = item_clear;

    // now we get memory for copying objects inside
    if(1 == item_size)
    {
        align = eo_mempool_align_08bit;
    }
    else if(2 == item_size)
    {
        align = eo_mempool_align_16bit;
    }
    else if (item_size <= 4)
    {
        align = eo_mempool_align_32bit;
        retptr->sizeofstoreditem = 4;
    }
    else
    {   // use 8-bytes alignment for everything else
        align = eo_mempool_align_64bit;
        retptr->sizeofstoreditem = (item_size+7)/8;
        retptr->sizeofstoreditem *= 8;
    }
    
    //#warning --> se alloco memoria per n oggetti (eg. di dimensione 6) in modo che ci sia allineamento ad 8, come indicizzo gli oggetti? a 6 oppure a 8?
    // da codice si evince a 6 ... quindi perche' spreco memoria nell'allocazione?
    // se uso dynamic, il heap viene gestito con allineamento a 8. ma quetso vuol dire che per 4 oggetti: 6*4 = 24 che sono tre uint64_t.
    // se uso align_64, anche qui ho array da uint64_t ma per 4 oggetti uso 4*(6+7)/8 = 4*1 = quattro uint64_t .... si spreca perche arrotondo sul item size e non sul totale. 

    // here is the memory from the correct memory pool (or the heap)
    retptr->stored_items  = (void*) eo_mempool_GetMemory(eo_mempool_GetHandle(), align, item_size, capacity);     

    start = (uint8_t*) (retptr->stored_items);
    for(i=0; i<capacity; i++) 
    {
        // cast to uint32_t to tell the reader that index of array start[] can be bigger than max eOsizecntnr_t
        item = &start[(uint32_t)i * item_size];        
        if(NULL != item_init)
        {   // construct each item
            item_init(item, init_par);
        }
        else
        {   // default init
            s_eo_deque_default_init(item, retptr);
        }
    }

    return(retptr);  
}
示例#14
0
文件: item.c 项目: Tatoeba/suggestd
Item *item_new(char const *str, int value) {
	Item *this = malloc(sizeof(Item));
	item_init(this);
	item_set(this, str, value);
	return this;
}
示例#15
0
int main (int argc, char **argv) {
    int c;
    conn *l_conn;
    struct in_addr addr;
    int lock_memory = 0;
    int daemonize = 0;
    int maxcore = 0;
    char *username = 0;
    struct passwd *pw;
    struct sigaction sa;
    struct rlimit rlim;
    char *pid_file = NULL;

    /* init settings */
    settings_init();

    /* process arguments */
    while ((c = getopt(argc, argv, "p:m:Mc:khirvdl:u:P:")) != -1) {
        switch (c) {
        case 'p':
            settings.port = atoi(optarg);
            break;
        case 'm':
            settings.maxbytes = atoi(optarg)*1024*1024;
            break;
        case 'M':
            settings.evict_to_free = 0;
            break;
        case 'c':
            settings.maxconns = atoi(optarg);
            break;
        case 'h':
            usage();
            exit(0);
        case 'i':
            usage_license();
            exit(0);
        case 'k':
            lock_memory = 1;
            break;
        case 'v':
            settings.verbose++;
            break;
        case 'l':
            if (!inet_aton(optarg, &addr)) {
                fprintf(stderr, "Illegal address: %s\n", optarg);
                return 1;
            } else {
                settings.interface = addr;
            }
            break;
        case 'd':
            daemonize = 1;
            break;
        case 'r':
            maxcore = 1;
            break;
        case 'u':
            username = optarg;
            break;
        case 'P':
            pid_file = optarg;
            break;
        default:
            fprintf(stderr, "Illegal argument \"%c\"\n", c);
            return 1;
        }
    }

    if (maxcore) {
        struct rlimit rlim_new;
        /*
         * First try raising to infinity; if that fails, try bringing
         * the soft limit to the hard.
         */
        if (getrlimit(RLIMIT_CORE, &rlim)==0) {
            rlim_new.rlim_cur = rlim_new.rlim_max = RLIM_INFINITY;
            if (setrlimit(RLIMIT_CORE, &rlim_new)!=0) {
                /* failed. try raising just to the old max */
                rlim_new.rlim_cur = rlim_new.rlim_max =
                                        rlim.rlim_max;
                (void) setrlimit(RLIMIT_CORE, &rlim_new);
            }
        }
        /*
         * getrlimit again to see what we ended up with. Only fail if
         * the soft limit ends up 0, because then no core files will be
         * created at all.
         */

        if ((getrlimit(RLIMIT_CORE, &rlim)!=0) || rlim.rlim_cur==0) {
            fprintf(stderr, "failed to ensure corefile creation\n");
            exit(1);
        }
    }

    /*
     * If needed, increase rlimits to allow as many connections
     * as needed.
     */

    if (getrlimit(RLIMIT_NOFILE, &rlim) != 0) {
        fprintf(stderr, "failed to getrlimit number of files\n");
        exit(1);
    } else {
        int maxfiles = settings.maxconns;
        if (rlim.rlim_cur < maxfiles)
            rlim.rlim_cur = maxfiles + 3;
        if (rlim.rlim_max < rlim.rlim_cur)
            rlim.rlim_max = rlim.rlim_cur;
        if (setrlimit(RLIMIT_NOFILE, &rlim) != 0) {
            fprintf(stderr, "failed to set rlimit for open files. Try running as root or requesting smaller maxconns value.\n");
            exit(1);
        }
    }

    /*
     * initialization order: first create the listening socket
     * (may need root on low ports), then drop root if needed,
     * then daemonise if needed, then init libevent (in some cases
     * descriptors created by libevent wouldn't survive forking).
     */

    /* create the listening socket and bind it */
    l_socket = server_socket(settings.port);
    if (l_socket == -1) {
        fprintf(stderr, "failed to listen\n");
        exit(1);
    }

    /* lose root privileges if we have them */
    if (getuid()== 0 || geteuid()==0) {
        if (username==0 || *username=='\0') {
            fprintf(stderr, "can't run as root without the -u switch\n");
            return 1;
        }
        if ((pw = getpwnam(username)) == 0) {
            fprintf(stderr, "can't find the user %s to switch to\n", username);
            return 1;
        }
        if (setgid(pw->pw_gid)<0 || setuid(pw->pw_uid)<0) {
            fprintf(stderr, "failed to assume identity of user %s\n", username);
            return 1;
        }
    }

    /* daemonize if requested */
    /* if we want to ensure our ability to dump core, don't chdir to / */
    if (daemonize) {
        int res;
        res = daemon(maxcore, settings.verbose);
        if (res == -1) {
            fprintf(stderr, "failed to daemon() in order to daemonize\n");
            return 1;
        }
    }


    /* initialize other stuff */
    item_init();
    event_init();
    stats_init();
    assoc_init();
    conn_init();
    slabs_init(settings.maxbytes);

    /* lock paged memory if needed */
    if (lock_memory) {
#ifdef HAVE_MLOCKALL
        mlockall(MCL_CURRENT | MCL_FUTURE);
#else
        fprintf(stderr, "warning: mlockall() not supported on this platform.  proceeding without.\n");
#endif
    }

    /*
     * ignore SIGPIPE signals; we can use errno==EPIPE if we
     * need that information
     */
    sa.sa_handler = SIG_IGN;
    sa.sa_flags = 0;
    if (sigemptyset(&sa.sa_mask) == -1 ||
            sigaction(SIGPIPE, &sa, 0) == -1) {
        perror("failed to ignore SIGPIPE; sigaction");
        exit(1);
    }

    /* create the initial listening connection */
    if (!(l_conn = conn_new(l_socket, conn_listening, EV_READ | EV_PERSIST))) {
        fprintf(stderr, "failed to create listening connection");
        exit(1);
    }

    /* initialise deletion array and timer event */
    deltotal = 200;
    delcurr = 0;
    todelete = malloc(sizeof(item *)*deltotal);
    delete_handler(0,0,0); /* sets up the event */

    /* save the PID in if we're a daemon */
    if (daemonize)
        save_pid(getpid(),pid_file);

    /* enter the loop */
    event_loop(0);

    /* remove the PID file if we're a daemon */
    if (daemonize)
        remove_pidfile(pid_file);

    return 0;
}
示例#16
0
文件: soul_create.c 项目: 0x1p2/rpg-c
int class_create(soul_t *p, char p_class, int lvl)
{
	char opt;	// "Option" for switch statement.
	byte lpctr;	// While variable to be changed to break.

	lpctr = 0;	// 1 will break the while loop.
	opt = p_class;

	tools("clear", NULL);

	while(!lpctr)
	{	
		if(opt != 'a' && opt != 'm' && opt != 'w')
		{
			fflush(stdout);
			printf("[%s, %s]\n", p->name, p->desc);
			printf(" Choose a class: \n\n");
			printf("  [A]rcher\n  [M]age\n  [W]arrior\n\n Option [a/m/w]: ");
			opt = getchar();
			opt = tolower(opt);
		}

		p->objs.arrow.amount = 0;
		p->objs.reagent.amount = 0;

		switch(opt)
		{
			case 'a':	// Archer
				p->stats.dexterity = 13 * lvl;
				p->stats.wisdom = 6 * lvl;
				p->stats.strength = 6 * lvl;
				p->stats.range = 2;
				p->stats.cls = 'a';	// Class = Archer
				p->stats.p = 'd';	// Primary stat (dexterity)
				p->stats.s = 's';	// Secondary stat (strength)
				p->stats.t = 'w';	// Tertiary stat (wisdom)

				//p->attr.pri = &p->attr.dexterity;
				//p->attr.sec = &p->attr.strength;	// Used for stat_gain
				//p->attr.ter = &p->attr.wisdom;
				
				p->skill = &p->bucket.archery;
				strncpy(p->skill->name, "Archery", 7);
				p->skill->val = 50.0;
				

				item_init(&p->objs.arrow, "Arrow", 250);
				p->consumable = &p->objs.arrow;

				lpctr = 1;		// To break out of while.
				break;

			case 'm':	// Mage
				p->stats.dexterity = 5 * lvl;
				p->stats.wisdom = 13 * lvl;
				p->stats.strength = 7 * lvl;
				p->stats.range = 1;
				p->stats.cls = 'm';	// Class = Mage
				p->stats.p = 'w';	// Primary stat (wisdom)
				p->stats.s = 'd';	// Secondary stat (dexterity)
				p->stats.t = 's';	// Tertiary stat (strength)

				//p->attr.pri = &p->attr.wisdom;
				//p->attr.sec = &p->attr.dexterity;	// Used for stat_gain
				//p->attr.ter = &p->attr.strength;

				p->skill = &p->bucket.magery;
				strncpy(p->skill->name, "Magery", 6);
				p->skill->val = 50.0;

				item_init(&p->objs.reagent, "Reagent", 250);
				p->consumable = &p->objs.reagent;

				lpctr = 1;		// To break out of while.
				break;

			case 'w':	// Warrior
				p->stats.dexterity = 7 * lvl;
				p->stats.wisdom = 3 * lvl;
				p->stats.strength = 15 * lvl;
				p->stats.range = 0;
				p->stats.cls = 'w';	// Class = Warrior
				p->stats.p = 's';	// Primary stat (strength)
				p->stats.s = 'd';	// Secondary stat (dexterity)
				p->stats.t = 'w';	// Tertiary stat (wisdom)

				//p->attr.pri = &p->attr.strength;
				//p->attr.sec = &p->attr.dexterity;	// Used for stat_gain
				//p->attr.ter = &p->attr.wisdom;

				p->skill = &p->bucket.fencing;
				strncpy(p->skill->name, "Fencing", 7);
				p->skill->val = 50.0;

				item_init(&p->objs.null, "NULL", 0);
				p->consumable = &p->objs.null;

				lpctr = 1;		// To break out of while.
				break;

			default:
				printf("An error occured: [%c] is not a valid option.\n", opt);
				tools("pause", NULL);
				break;
		}

		p->hp = ((p->stats.strength * 3) + 50);
		p->hp_c = p->hp;
		p->stats.p_lck = 0;	// Set primary skill unlocked (allows stat gain.)
		p->stats.s_lck = 0;	// Set secondary skill unlocked.
		p->stats.t_lck = 0;	// Set tertiary skill unlocked.
	}
	
	tools("clear", NULL);
	return 0;
}
示例#17
0
void screen_play(SDL_Renderer * render,game_t * game)
{
	char gold_buf[128];
	char mana_buf[128];
	SDL_Event event;
	int tile_num;
	int unit_num;
	int city_num;
	int encounter_num;
	int i;
	int j;
	int x;
	LBXAnimation_t * anim_ptr;
	city_t * city;

	local_render = render;

	end_screen = -1;

	selected_group = NULL;

	city = (city_t*)game->wiz[0].city->data;
	cur_tile_x = city->x;
	cur_tile_y = city->y;

	load_font();

	/* Load resource */
	if(anim==NULL) {
		anim = load_graphics(render,"MAIN.LBX");
		if(anim == NULL) {
			exit(EXIT_FAILURE);
		}
	}

	/* Load tiles */
	if(tile==NULL) {
		tile = load_graphics(render,"TERRAIN.LBX");
		if(tile == NULL) {
			exit(EXIT_FAILURE);
		}
	}

	/* Load unit */
	if(unit1==NULL) {
		unit1 = load_graphics(render,"UNITS1.LBX");
		if(unit1 == NULL) {
			exit(EXIT_FAILURE);
		}
	}

	if(unit2==NULL) {
		unit2 = load_graphics(render,"UNITS2.LBX");
		if(unit2 == NULL) {
			exit(EXIT_FAILURE);
		}
	}

	/* Concat unit1 and unit2 */
	if(unit==NULL) {
		j=0;
		anim_ptr = unit1;
		for(i=0; i<2; i++) {
			while(anim_ptr->num_frame!=0) {
				unit = (LBXAnimation_t*)realloc(unit,(j+1)*sizeof(LBXAnimation_t));
				memcpy(&unit[j],anim_ptr,sizeof(LBXAnimation_t));
				anim_ptr++;
				j++;
			}
			anim_ptr= unit2;
		}
	}

	/* Load back */
	if(back==NULL) {
		back = load_graphics(render,"MAPBACK.LBX");
		if(back == NULL) {
			exit(EXIT_FAILURE);
		}
	}

	j=0;
	item_init(&item_ui[j]);
	item_set_frame(&item_ui[j],0,0,&anim[j]);
	j++;
	x=7;
	item_init(&item_ui[j]);
	item_set_frame(&item_ui[j],x,4,&anim[j]);
	item_set_frame_click(&item_ui[j],1);
	j++;
	x+=item_ui[j-1].anim->w + 1;
	item_init(&item_ui[j]);
	item_set_frame(&item_ui[j],x,4,&anim[j]);
	item_set_frame_click(&item_ui[j],1);
	j++;
	x+=item_ui[j-1].anim->w + 1;
	item_init(&item_ui[j]);
	item_set_frame(&item_ui[j],x,4,&anim[j]);
	item_set_frame_click(&item_ui[j],1);
	j++;
	x+=item_ui[j-1].anim->w + 1;
	item_init(&item_ui[j]);
	item_set_frame(&item_ui[j],x,4,&anim[j]);
	item_set_frame_click(&item_ui[j],1);
	j++;
	x+=item_ui[j-1].anim->w + 1;
	item_init(&item_ui[j]);
	item_set_frame(&item_ui[j],x,4,&anim[j]);
	item_set_frame_click(&item_ui[j],1);
	j++;
	x+=item_ui[j-1].anim->w + 1;
	item_init(&item_ui[j]);
	item_set_frame(&item_ui[j],x,4,&anim[j]);
	item_set_frame_click(&item_ui[j],1);
	j++;
	x+=item_ui[j-1].anim->w + 1;
	item_init(&item_ui[j]);
	item_set_frame(&item_ui[j],x,4,&anim[j]);
	item_set_frame_click(&item_ui[j],1);
	item_set_click_left(&item_ui[j],cb_plane,NULL);
	j++;

	/* Main loop */
	while( end_screen == -1) {
		tile_num = draw_tile(game);
		city_num = draw_city(game);
		encounter_num = draw_encounter(game);
		unit_num = draw_unit(game);

		/* Print gold */
		item_init(&item_ui[8]);
		item_set_font(&item_ui[8],font);
		sprintf(gold_buf,"%d GP",game->wiz[0].gold);
		item_set_string(&item_ui[8],gold_buf);
		item_set_frame(&item_ui[8],264,71,NULL);

		/* Print mana */
		item_init(&item_ui[9]);
		item_set_font(&item_ui[9],font);
		sprintf(mana_buf,"%d MP",game->wiz[0].mana);
		item_set_string(&item_ui[9],mana_buf);
		item_set_frame(&item_ui[9],300,71,NULL);

		while (SDL_PollEvent(&event)) {
			if(event.type==SDL_KEYDOWN && event.key.keysym.sym==SDLK_ESCAPE) {
				return;
			}

			/* Move selected_unit */
			if(event.type==SDL_KEYDOWN ) {
				move_unit(game,event.key.keysym.sym);
			}

			sdl_screen_manager(&event);
			sdl_mouse_manager(&event,item_ui,ITM_UI_NUM);
			sdl_mouse_manager(&event,item_city,city_num);
			sdl_mouse_manager(&event,item_encounter,encounter_num);
			sdl_mouse_manager(&event,item_tile,tile_num);
			sdl_mouse_manager(&event,item_unit,unit_num);
			sdl_keyboard_manager(&event);
		}

		SDL_RenderClear(render);

		sdl_blit_item_list(item_tile,tile_num);
		sdl_blit_item_list(item_city,city_num);
		sdl_blit_item_list(item_encounter,encounter_num);
		sdl_blit_item_list(item_back,unit_num);
		sdl_blit_item_list(item_unit,unit_num);
		sdl_blit_item_list(item_ui,ITM_UI_NUM);

		sdl_blit_to_screen();

		sdl_loop_manager();
	}

	return;
}
示例#18
0
ATF_TC_BODY(queue_valid, tc) {
    isc_result_t result;
    item_queue_t queue;
    item_t one, two, three, four, five;
    item_t *p;

    UNUSED(tc);

    ISC_QUEUE_INIT(queue, qlink);

    item_init(&one, 1);
    item_init(&two, 2);
    item_init(&three, 3);
    item_init(&four, 4);
    item_init(&five, 5);

    result = isc_test_begin(NULL, ISC_TRUE);
    ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);

    ATF_CHECK(ISC_QUEUE_EMPTY(queue));

    ISC_QUEUE_POP(queue, qlink, p);
    ATF_CHECK(p == NULL);

    ATF_CHECK(! ISC_QLINK_LINKED(&one, qlink));
    ISC_QUEUE_PUSH(queue, &one, qlink);
    ATF_CHECK(ISC_QLINK_LINKED(&one, qlink));

    ATF_CHECK(! ISC_QUEUE_EMPTY(queue));

    ISC_QUEUE_POP(queue, qlink, p);
    ATF_REQUIRE(p != NULL);
    ATF_CHECK_EQ(p->value, 1);
    ATF_CHECK(ISC_QUEUE_EMPTY(queue));
    ATF_CHECK(! ISC_QLINK_LINKED(p, qlink));

    ISC_QUEUE_PUSH(queue, p, qlink);
    ATF_CHECK(! ISC_QUEUE_EMPTY(queue));
    ATF_CHECK(ISC_QLINK_LINKED(p, qlink));

    ATF_CHECK(! ISC_QLINK_LINKED(&two, qlink));
    ISC_QUEUE_PUSH(queue, &two, qlink);
    ATF_CHECK(ISC_QLINK_LINKED(&two, qlink));

    ATF_CHECK(! ISC_QLINK_LINKED(&three, qlink));
    ISC_QUEUE_PUSH(queue, &three, qlink);
    ATF_CHECK(ISC_QLINK_LINKED(&three, qlink));

    ATF_CHECK(! ISC_QLINK_LINKED(&four, qlink));
    ISC_QUEUE_PUSH(queue, &four, qlink);
    ATF_CHECK(ISC_QLINK_LINKED(&four, qlink));

    ATF_CHECK(! ISC_QLINK_LINKED(&five, qlink));
    ISC_QUEUE_PUSH(queue, &five, qlink);
    ATF_CHECK(ISC_QLINK_LINKED(&five, qlink));

    ISC_QUEUE_POP(queue, qlink, p);
    ATF_REQUIRE(p != NULL);
    ATF_CHECK_EQ(p->value, 1);

    ISC_QUEUE_POP(queue, qlink, p);
    ATF_REQUIRE(p != NULL);
    ATF_CHECK_EQ(p->value, 2);

    ISC_QUEUE_POP(queue, qlink, p);
    ATF_REQUIRE(p != NULL);
    ATF_CHECK_EQ(p->value, 3);

    ISC_QUEUE_POP(queue, qlink, p);
    ATF_REQUIRE(p != NULL);
    ATF_CHECK_EQ(p->value, 4);

    ISC_QUEUE_POP(queue, qlink, p);
    ATF_REQUIRE(p != NULL);
    ATF_CHECK_EQ(p->value, 5);

    ATF_CHECK(ISC_QUEUE_EMPTY(queue));

    ISC_QUEUE_DESTROY(queue);
    isc_test_end();
}
示例#19
0
static int draw_unit(game_t * game)
{
	int i;
	int x,y;
	int num_item;
	group_t * grp_list;
	unit_list_t * ulist;
	unit_t * cur_unit;

	if( item_unit != NULL ) {
		free(item_unit);
	}
	if( item_back != NULL ) {
		free(item_back);
	}

	if(selected_group == NULL ) {
		selected_group = (group_t *)game->wiz[0].group;
		blink_state = 0;
	}

	item_unit = NULL;
	item_back = NULL;

	num_item = 0;
	for(i=0; i<game->opponent_num+1; i++) {
		grp_list = game->wiz[i].group;
		while(grp_list!=NULL ) {
			ulist = (unit_list_t *)grp_list->data;;
			cur_unit = (unit_t*)ulist->data;
			if(cur_unit->side != cur_side) {
				grp_list = grp_list->next;
				continue;
			}

			item_unit = (item_t*)realloc(item_unit,sizeof(item_t)*(num_item+1));
			item_back = (item_t*)realloc(item_back,sizeof(item_t)*(num_item+1));

			x = get_tile_coord_x(cur_unit->x, game);
			y = get_tile_coord_y(cur_unit->y, game);

			item_init(&item_unit[num_item]);
			item_init(&item_back[num_item]);

			/* Blinking selected unit */
			if( selected_group && grp_list == selected_group ) {
				if( SDL_GetTicks() > (blink_time+UNIT_BLINK_TIME) ) {
					blink_state=(blink_state+1)%2;
					blink_time=SDL_GetTicks();
				}

				item_set_frame(&item_back[num_item],x,y,
							   blink_state?&back[game->wiz[i].color + BACK_COLOR]:NULL);

				item_set_frame(&item_unit[num_item],x,y+1,
							   blink_state?&unit[cur_unit->type]:NULL);
			} else {
				item_set_frame(&item_back[num_item],x,y,
							   &back[game->wiz[i].color+BACK_COLOR]);

				item_set_frame(&item_unit[num_item],x,y+1,
							   &unit[cur_unit->type]);

			}
			num_item++;
			grp_list = grp_list->next;
		}
	}

	return num_item;
}