示例#1
0
bool do_creature_pickup(creature_t* creature)
{
  if ((creature->flag & CF_INTELLIGENT) && count_items_at(current_dungeon, creature->pos.x, creature->pos.y) > 0)
  {
    std::vector<item_t*> items = items_at(current_dungeon, creature->pos.x, creature->pos.y);
    for (auto it = items.begin(); it != items.end(); ++it)
    {
      if ((int)(*it)->type < MAX_EQUIP && !creature->equipment[(*it)->type])
      {
        handle_creature_equip(creature, *it);

        remove_item(current_dungeon, *it, false);

        return true;
      }
      else if ((int)(*it)->type >= MAX_EQUIP)
      {
        if ((*it)->type == ITEM_TYPE_WAND)
        {
          creature->inventory.push_back(*it);
          remove_item(current_dungeon, *it, false);
        }
      }

    }
  }

  return false;
}
int remove_item_test(void)
{
  clear_queue();
  insert_item(42);
  _assert(front() == 42);
  _assert(queue_size() == 1);

  _assert(remove_item() == 42);
  _assert(queue_size() == 0);
  _assert(is_empty() == true);

  insert_item(12);
  insert_item(13);
  insert_item(14);

  _assert(front() == 12);
  _assert(rear() == 14);
  _assert(queue_size() == 3);

  _assert(remove_item() == 12);
  _assert(front() == 13);
  _assert(rear() == 14);
  _assert(queue_size() == 2);

  _assert(remove_item() == 13);
  _assert(front() == 14);
  _assert(rear() == 14);
  _assert(queue_size() == 1);

  clear_queue();
  return 0;
}
示例#3
0
/**
 * Removes all references to the given node handle in the gui.
 */
void
nodes_gui_remove_node(const struct nid *node_id)
{
    GtkWidget *clist_nodes;
    gint row;

    clist_nodes = gui_main_window_lookup("clist_nodes");

    /*
     * Make sure node is remove from the "changed" hash table so
     * we don't try an update.
     */
    g_assert(NULL != hs_node_info_changed);
    g_assert(NULL != hs_node_flags_changed);

    remove_item(hs_node_info_changed, node_id);
    remove_item(hs_node_flags_changed, node_id);

	row = gtk_clist_find_row_from_data(GTK_CLIST(clist_nodes),
				deconstify_gpointer(node_id));
    if (row != -1) {
        gtk_clist_remove(GTK_CLIST(clist_nodes), row);
		nid_unref(node_id);
	} else {
        g_warning("%s(): no matching row found", G_STRFUNC);
	}
}
示例#4
0
/** perform stress test on insert and delete in neg cache */
static void stress_test(struct val_neg_cache* neg)
{
	int i;
	if(negverbose)
		printf("negcache test\n");
	for(i=0; i<100; i++) {
		if(random() % 10 < 8)
			add_item(neg);
		else	remove_item(neg);
		check_neg_invariants(neg);
	}
	/* empty it */
	if(negverbose)
		printf("neg stress empty\n");
	while(neg->first) {
		remove_item(neg);
		check_neg_invariants(neg);
	}
	if(negverbose)
		printf("neg stress emptied\n");
	unit_assert(neg->first == NULL);
	/* insert again */
	for(i=0; i<100; i++) {
		if(random() % 10 < 8)
			add_item(neg);
		else	remove_item(neg);
		check_neg_invariants(neg);
	}
}
示例#5
0
int main(int argc, char *argv[])
{
    assert(argc > 1);

    srand(atoi(argv[1]));

    h = maxheap_create(8);
    
    for (int i = 0; i < MAXSIZE; i++)
        vals[i] = -HUGE;

    for (int iters = 0; ; iters++) {
        
        if (iters %100 == 0)
            printf("\r%5d %10d", iters, maxheap_size(h));
        
        double prob = randf();
        
        if (prob < .8) {
            add_item();
            continue;
        }

        if (prob < .99) {
            remove_item();
            continue;
        }

        for (int i = 0; i < sz; i++)
            remove_item();
        //        printf("0");
    }
}
示例#6
0
static inline void
update_row(const void *key, void *value, void *user_data)
{
	struct node_data *data = value;
	time_t *now_ptr = user_data, now = *now_ptr;
	gnet_node_status_t status;

	g_assert(NULL != data);
	g_assert(data->node_id == key);

	if (!guc_node_get_status(data->node_id, &status))
		return;

    /*
     * Update additional info too if it has recorded changes.
     */
    if (remove_item(ht_node_info_changed, data->node_id)) {
        gnet_node_info_t info;

        if (guc_node_fill_info(data->node_id, &info)) {
			nodes_gui_update_node_info(data, &info);
			guc_node_clear_info(&info);
		}
    }

    if (remove_item(ht_node_flags_changed, data->node_id)) {
        gnet_node_flags_t flags;

        if (guc_node_fill_flags(data->node_id, &flags)) {
			nodes_gui_update_node_flags(data, &flags);
		}
    }

	if (status.connect_date)
		data->connected = delta_time(now, status.connect_date);

	if (status.up_date)
		data->uptime = delta_time(now, status.up_date);

	/* Update the status line */
	{	
		const gchar *s;
		size_t size;
		
		s = nodes_gui_common_status_str(&status);
		size = 1 + strlen(s);
		if (size > data->info_size) {
			WFREE_NULL(data->info, data->info_size);
			data->info = wcopy(s, size);
			data->info_size = size;
		} else {
			memcpy(data->info, s, size);
		}
	}

	tree_model_iter_changed(GTK_TREE_MODEL(nodes_model), &data->iter);
}
示例#7
0
/**
 * Update all the nodes at the same time.
 *
 * @bug
 * FIXME: We should remember for every node when it was last
 *        updated and only refresh every node at most once every
 *        second. This information should be kept in a struct pointed
 *        to by the row user_data and should be automatically freed
 *        when removing the row (see upload stats code).
 */
void
nodes_gui_update_display(time_t now)
{
	GtkCList *clist;
	GList *l;
	gint row = 0;
    gnet_node_status_t status;

    clist = GTK_CLIST(gui_main_window_lookup("clist_nodes"));
    gtk_clist_freeze(clist);

	for (l = clist->row_list, row = 0; l; l = l->next, row++) {
		const struct nid *node_id = ((GtkCListRow *) l->data)->data;

        guc_node_get_status(node_id, &status);

        /*
         * Update additional info too if it has recorded changes.
         */
        if (remove_item(hs_node_info_changed, node_id)) {
            gnet_node_info_t info;

            guc_node_fill_info(node_id, &info);
            nodes_gui_update_node_info(&info, row);
            guc_node_clear_info(&info);
        }

        if (remove_item(hs_node_flags_changed, node_id)) {
            gnet_node_flags_t flags;

            guc_node_fill_flags(node_id, &flags);
            nodes_gui_update_node_flags(node_id, &flags, -1);
        }

		/*
		 * Don't update times if we've already disconnected.
		 */
		if (status.status == GTA_NODE_CONNECTED) {
	        gtk_clist_set_text(clist, row, c_gnet_connected,
        			short_uptime(delta_time(now, status.connect_date)));

			if (status.up_date)
				gtk_clist_set_text(clist, row, c_gnet_uptime,
					status.up_date ?
						short_uptime(delta_time(now, status.up_date)) : "...");
		}
        gtk_clist_set_text(clist, row, c_gnet_info,
			nodes_gui_common_status_str(&status));
    }
    gtk_clist_thaw(clist);
}
示例#8
0
int main(int argc, const char *argv[])
{
    if(argc!=2)
    {
        perror("\nIncorrect Usage!\n\tUse: Consumer [delay]\n\n\t\tWhere [delay] is sleeping time in secs.");
        exit(EXIT_FAILURE);
    }
    int *shared_buffer = create_shared_mem_buffer();
    int semid = create_semaphore_set();
    
    char item;
    int delay=atoi(argv[1]);
    printf("\n--Configuration-------------------------------\n");
    printf("Delay: %d\n",delay);
    while(1) {
        semop(semid, &downFull, 1);
        semop(semid, &downMutex, 1);
        item = remove_item(semid, shared_buffer);
        //debug_buffer(shared_buffer);
        semop(semid, &upMutex, 1);
        semop(semid, &upEmpty, 1);
        consume_item(item);
        sleep(delay);
    } 
    
    return EXIT_SUCCESS;
}
示例#9
0
// This function is executed by the worker threads
void *worker(void *args)
{
		  int thread_id = (int) args;
		  item_t *this_item;
		  int state;

		  pthread_cleanup_push(cleanup_handler, (void *)thread_id);
		  while(1){
					 printf("Worker %d is waiting for work. \n", thread_id);
					 sem_wait(&work_available);

					 // Obtain item from the queue
					 sem_wait(&work_queue_lock);
					 this_item = remove_item(&work_queue);
					 sem_post(&work_queue_lock);
					
					 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &state); // Disable cancellation in this section of the code
					 printf("Worker %d is busy for %d seconds with work ID %d. \n", thread_id, this_item->work, this_item->id);
					 sleep(this_item->work); // Simulate some processing
					 free((void *)this_item);
					 pthread_setcancelstate(state, &state); // Re-enable cancellation
					 pthread_testcancel(); // Test for cancellation
		  }

		  pthread_cleanup_pop(0);
}
示例#10
0
 void WalletDb::remove_transaction( const TransactionIdType& entry_id )
 {
    const auto rec = lookup_transaction( entry_id );
    if( !rec.valid() ) return;
    remove_item( rec->wallet_entry_index );
    transactions.erase( entry_id );
 }
示例#11
0
void *consumer(void *param) {
	buffer_item item;

	while (1) {
		sleep(rand() % 10); //sleep for random period of time

		printf("Thread %d waiting for a full space\n", syscall(SYS_gettid));
		fflush(stdout);
		sem_wait(&full); //get full lock
		

		printf("Thread %d waiting for critical section\n", syscall(SYS_gettid));
		fflush(stdout);
		pthread_mutex_lock(&mutex); //get mutex lock
		

		printf("Thread %d has entered critical section\n", syscall(SYS_gettid));
		fflush(stdout);

		if (remove_item(&item)){
			fprintf(stderr, "Consumer report error condition\n");
		}else{
			printf("consumer consumed %d\n", item);
			fflush(stdout);
		}
		
		pthread_mutex_unlock(&mutex); //release mutex lock
		printf("Thread %d has exited critical section\n", syscall(SYS_gettid));
		fflush(stdout);

		sem_post(&empty); //signal empty		
		printf("Thread %d has signaled for an empty space\n", syscall(SYS_gettid));
		fflush(stdout);
	}
}
示例#12
0
文件: history.c 项目: Sorikairo/42sh
int		builtin_history(t_cmd *cmd, t_system *sys)
{
  t_history	*tmp;

  if (cmd->args[1] == NULL)
    {
      tmp = sys->history;
      while (tmp->next != NULL)
	{
	  my_printf("%s\n", tmp->cmd);
	  tmp = tmp->next;
	}
    }
  else if (utl_strcmp(cmd->args[1], "-c") == 0)
    {
      while (sys->history != NULL)
	{
	  tmp = sys->history;
	  sys->history = sys->history->next;
	  free(tmp->cmd);
	}
    }
  else if (utl_strcmp(cmd->args[1], "-d") == 0)
    remove_item(cmd, sys);
  return (0);
}
示例#13
0
int main(){
    Item* listptr;
    int i;
    listptr = new_item(0);
    for (i=1; i < 6; i++){
        listptr=insert_front(listptr, i);
    }
    for (i=0; i < 6; i++){
        printf("i %d Item %d\n", i, get(listptr, i));
    }
    listptr = remove_item(listptr, 3);
    for (i=0; i <= 5; i++){
        printf("i %d Item %d\n", i, get(listptr, i));
    }
    printf("Index for 3 %d\n", get_index(listptr, 3));
    
    for (i=1; i < 6; i++){
        listptr=insert_back(listptr, i);
    }
    for (i=0; i < 12; i++){
        printf("i %d Item %d\n", i, get(listptr, i));
    }
    set_item(listptr, 9, 15);
    for (i=0; i < 12; i++){
        printf("i %d Item %d\n", i, get(listptr, i));
    }
    set_item(listptr, 10, 15);
    free_all(listptr);
}
示例#14
0
static int get_numeric_item(lua_State* L, int idx, const char* name,
                            unsigned* val)
{
  lua_getfield(L, idx, name);
  int t = lua_type(L, -1);
  double d;
  switch (t) {
  case LUA_TNUMBER:
    d = lua_tonumber(L, -1);
    if (d < 0) {
      lua_pushfstring(L, "%s must be set to a positive number", name);
      return 1;
    }
    *val = (unsigned)d;
    break;
  case LUA_TNIL:
    break; // use the default
  default:
    lua_pushfstring(L, "%s must be set to a number", name);
    return 1;
    break;
  }
  remove_item(L, idx, name);

  return 0;
}
示例#15
0
//===========================================================================
void MessageManager_Register(HWND hwnd, const UINT* messages, bool add)
{
    UINT msg;
    while (0 != (msg = *messages++))
    {
        struct MsgMap *mm = (struct MsgMap *)assoc(msgs, (void*)msg);
        if (mm) {
            if (remove_assoc(&mm->winmap, hwnd))
                --mm->count;

        } else if (add) {
            mm = c_new(struct MsgMap);
            mm->msg = msg;
            append_node (&msgs, mm);
            // these are the messages that expect return values and
            // are handled differently in 'MessageManager_Send()'
            mm->send_mode = BB_DRAGTODESKTOP == msg || BB_GETBOOL == msg;
        }

        if (add) {
            struct winmap *w = c_new(struct winmap);
            w->hwnd = hwnd;
            cons_node(&mm->winmap, w);
            ++mm->count;
            dbg_msg("add", hwnd, msg);

        } else if (mm) {
            if (NULL == mm->winmap)
                remove_item(&msgs, mm);
            dbg_msg("del", hwnd, msg);
        }
    }
示例#16
0
std::list<item> inventory::reduce_stack_internal(const Locator& locator, int quantity)
{
    int pos = 0;
    std::list<item> ret;
    for (invstack::iterator iter = items.begin(); iter != items.end(); ++iter)
    {
        if (item_matches_locator(iter->front(), locator, pos))
        {
            if(quantity >= iter->size() || quantity < 0)
            {
                ret = *iter;
                items.erase(iter);
            }
            else
            {
                for(int i = 0 ; i < quantity ; i++)
                {
                    ret.push_back(remove_item(&iter->front()));
                }
            }
            break;
        }
        ++pos;
    }
    return ret;
}
示例#17
0
 void wallet_db::remove_transaction( const transaction_id_type& record_id )
 {
    const auto rec = lookup_transaction( record_id );
    if( !rec.valid() ) return;
    remove_item( rec->wallet_record_index );
    transactions.erase( record_id );
 }
示例#18
0
/*********************************************************************
** Function:         use_item()
** Description:      Use an item in the bag at a certain index
** Parameters:       integer item_num (index), Player*
** Pre-Conditions:   Item index msut be valid.
** Post-Conditions:  Item will be used; if depleted afterwards, then
**                   removed from the belt.
********************************************************************/
void DivingBelt::use_item(int item_num, Player* player) {
  Item* item = items[item_num];
  item->use(player);
  if (item->is_depleted()) {
    remove_item(item_num);
  }
}
示例#19
0
void meltingkey(int in,int cn)
{
	int sprite;

        if (cn) return;
	if (!it[in].carried) return;	// can only use if item is carried
	
	it[in].drdata[1]++;
	if (it[in].drdata[1]>=it[in].drdata[0]) {
		if (it[in].carried) log_char(it[in].carried,LOG_SYSTEM,0,"Your %s melted away.",it[in].name);
		if (ch[cn].flags&CF_PLAYER) dlog(cn,in,"dropped because it melted");
		remove_item(in);
		destroy_item(in);
		return;
	}
	sprite=50494+it[in].drdata[1]*5/it[in].drdata[0];

	if (it[in].sprite!=sprite) {
		it[in].sprite=sprite;
		if (it[in].carried) {
			ch[it[in].carried].flags|=CF_ITEMS;
			if (sprite==50495) {
				log_char(it[in].carried,LOG_SYSTEM,0,"Your %s starts to melt.",it[in].name);
			}
		}
	}
			
	call_item(it[in].driver,in,0,ticker+TICKS*10);
}
示例#20
0
static int load_sandbox_defaults(lua_State* L,
                                 const char* key,
                                 hs_sandbox_config* cfg)
{
  lua_getglobal(L, key);
  if (!lua_istable(L, -1)) {
    lua_pushfstring(L, "%s must be a table", key);
    return 1;
  }
  if (get_numeric_item(L, 1, cfg_sb_output, &cfg->output_limit)) return 1;
  if (get_numeric_item(L, 1, cfg_sb_memory, &cfg->memory_limit)) return 1;
  if (get_numeric_item(L, 1, cfg_sb_instruction, &cfg->instruction_limit)) {
    return 1;
  }
  if (get_numeric_item(L, 1, cfg_sb_ticker_interval, &cfg->ticker_interval)) {
    return 1;
  }
  if (get_bool_item(L, 1, cfg_sb_preserve, &cfg->preserve_data)) return 1;

  if (check_for_unknown_options(L, 1, key)) return 1;

  remove_item(L, LUA_GLOBALSINDEX, key);

  return 0;
}
示例#21
0
static void
host_lookup_callback(const gchar *hostname, gpointer key)
{
	const struct nid *node_id = key;
	gnet_node_info_t info;
	struct node_data *data;
	host_addr_t addr;
	guint16 port;

	if (!ht_pending_lookups)
		goto finish;

	if (!remove_item(ht_pending_lookups, node_id))
		goto finish;

	data = find_node(node_id);
	if (!data)
		goto finish;

	guc_node_fill_info(node_id, &info);
	g_assert(node_id == info.node_id);
	
	addr = info.addr;
	port = info.port;
	guc_node_clear_info(&info);

	WFREE_NULL(data->host, data->host_size);
	
	if (hostname) {
		const gchar *host;
		gchar *to_free;

		if (utf8_is_valid_string(hostname)) {
			to_free = NULL;
			host = hostname;
		} else {
			to_free = locale_to_utf8_normalized(hostname, UNI_NORM_GUI);
			host = to_free;
		}
		
		data->host_size = w_concat_strings(&data->host,
							host, " (",
							host_addr_port_to_string(addr, port), ")",
							(void *) 0);

		G_FREE_NULL(to_free);
	} else {
		statusbar_gui_warning(10,
			_("Reverse lookup for %s failed"), host_addr_to_string(addr));
		data->host_size = w_concat_strings(&data->host,
							host_addr_port_to_string(addr, port),
							(void *) 0);
	}

finish:
	nid_unref(node_id);
}
示例#22
0
item inventory::remove_item_by_letter(char ch)
{
 for (int i = 0; i < items.size(); i++) {
  if (items[i][0].invlet == ch)
   return remove_item(i);
 }

 return nullitem;
}
示例#23
0
void oxy_potion(int in,int cn)
{
	if (!cn) return;
	if (!it[in].carried) return;
	
	add_spell(cn,IDR_OXYGEN,TICKS*60,"nonomagic_spell");
	remove_item(in);
	destroy_item(in);
	ch[cn].flags|=CF_ITEMS;
}
示例#24
0
int EditPopup::update(Track *track, Edit *edit)
{
	this->edit = edit;
	this->track = track;

	if(track->data_type == TRACK_VIDEO && !resize_option)
	{
		add_item(resize_option = new EditPopupResize(mwindow, this));
		add_item(matchsize_option = new EditPopupMatchSize(mwindow, this));
	}
	else
	if(track->data_type == TRACK_AUDIO && resize_option)
	{
		remove_item(resize_option);
		remove_item(matchsize_option);
		resize_option = 0;
		matchsize_option = 0;
	}
	return 0;
}
示例#25
0
void CmpInventory::process_message(const MsgObjectDestroyed& msg)
{
	if(m_items.find(msg.object_id()) != m_items.end())
	{
		// An item contained in the inventory has been destroyed: we need to remove it from the inventory.
		// Note that the item still exists until after processing of destroyed messages has finished (see
		// the implementation in ObjectManager), so there is no danger of accessing the item after it has
		// already been physically destroyed.
		remove_item(msg.object_id());
	}
}
示例#26
0
文件: umenu.c 项目: inemajo/umenu
void
cleanup_exit(int i)
{
  while (l.nb_items)
    remove_item(&l, l.nb_items-1);
  free(l.items);
  free(l.nb_colsw);

  tcsetattr(STDIN_FILENO, TCSANOW, &oldt);
  _exit(i);
}
示例#27
0
void underwater_berry(int in,int cn)
{
	if (!cn) return;

	if (!(ch[cn].flags&CF_PLAYER)) return;

	if (!add_spell(cn,IDR_OXYGEN,TICKS*30,"nonomagic_spell")) return;

	remove_item(in);
	destroy_item(in);
}
示例#28
0
item inventory::remove_item_by_type(itype_id type)
{
    for (invstack::iterator iter = items.begin(); iter != items.end(); ++iter)
    {
        if (iter->front().type->id == type)
        {
            return remove_item(iter);
        }
    }
    return nullitem;
}
示例#29
0
item inventory::remove_item_by_letter(char ch)
{
 for (unsigned int i = 0; i < items.size(); i++) {
  if (items[i][0].invlet == ch) {
   if (items[i].size() > 1)
    items[i][1].invlet = ch;
   return remove_item(i);
  }
 }

 return nullitem;
}
示例#30
0
文件: project4.c 项目: amdonham/C
void *initConsumer(void *t){
	buffer_item item = 0;
	long id = (long)t;
	while(1){
		if(shouldExit){pthread_exit(0);}
		//Sleep random period of time
		sleep(rand()%3);
		//Remove an item
		if(remove_item(&item,id)){printf("Consumer consumed %d on thread %lu\n\n\n",item,id);}
		else{printf("Error in consumption on thread %lu\n",id);}
	}
}