void* consumer(void* t) { long tid = (long) t; while (1) { while (itemCount == 0); // Spin if nothing in buffer int item = removeItemFromBuffer(); // Once buffer has an item, remove the item from buffer printf("Consumer removed: %d\n", item); itemCount = itemCount - 1; consumeItem(item); } pthread_exit(NULL); }
void consumer(int id,int shmid,int full,int empty,int mutex){ struct sembuf semV,semP; initSemaphoreOperation(&semV,0,0,1); initSemaphoreOperation(&semP,0,0,-1); while(1){ // sleep(1); P(full,semP); P(mutex,semP); int item = consumeItem(shmid); V(mutex,semV); V(empty,semV); printf("\nCONSUMER [%d] consumes = %d\n",id,item); } return; }
//consumer void* consumer(void* arg) { int item; /* Cast the cookie pointer to the right type. */ struct thread_args* p = (struct thread_args*) arg; while (1) { sem_wait(&fillCount); //if the fillCount is 0, meaning the buffer is empty then wait... consumeItem(p->size, p->ptr_wh); //if item was found sem_post(&emptyCount); //up the emptyCount } }
void sellItemToShop(Item* item) { if ( !item ) { return; } if ( item->beatitude < 0 && itemIsEquipped(item, clientnum) ) { messagePlayer(clientnum, language[1124], item->getName()); playSound(90, 64); return; } bool deal = true; if ( stats[clientnum]->PROFICIENCIES[PRO_TRADING] >= CAPSTONE_UNLOCK_LEVEL[PRO_TRADING] ) { //Skill capstone: Can sell anything to any shop. } else { switch ( shopkeepertype ) { case 0: // arms & armor if ( itemCategory(item) != WEAPON && itemCategory(item) != ARMOR && itemCategory(item) != THROWN ) { deal = false; } break; case 1: // hats if ( itemCategory(item) != ARMOR ) { deal = false; } break; case 2: // jewelry if ( itemCategory(item) != RING && itemCategory(item) != AMULET && itemCategory(item) != GEM ) { deal = false; } break; case 3: // bookstore if ( itemCategory(item) != SPELLBOOK && itemCategory(item) != SCROLL && itemCategory(item) != BOOK ) { deal = false; } break; case 4: // potion shop if ( itemCategory(item) != POTION ) { deal = false; } break; case 5: // magicstaffs if ( itemCategory(item) != MAGICSTAFF ) { deal = false; } break; case 6: // food if ( itemCategory(item) != FOOD ) { deal = false; } break; case 7: // tools case 8: // lights if ( itemCategory(item) != TOOL && itemCategory(item) != THROWN ) { deal = false; } break; default: break; } } if ( !deal ) { shopspeech = language[212 + rand() % 3]; shoptimer = ticks - 1; playSound(90, 64); return; } if ( items[item->type].value * .75 <= item->sellValue(clientnum) ) { shopspeech = language[209 + rand() % 3]; } else { shopspeech = language[206 + rand() % 3]; } shoptimer = ticks - 1; newItem(item->type, item->status, item->beatitude, 1, item->appearance, item->identified, shopInv); stats[clientnum]->GOLD += item->sellValue(clientnum); playSound(89, 64); int ocount = item->count; item->count = 1; messagePlayer(clientnum, language[1125], item->description(), item->sellValue(clientnum)); item->count = ocount; if ( multiplayer != CLIENT ) { if ( rand() % 2 ) { if ( item->sellValue(clientnum) <= 1 ) { // selling cheap items does not increase trading past basic if ( stats[clientnum]->PROFICIENCIES[PRO_TRADING] < SKILL_LEVEL_SKILLED ) { players[clientnum]->entity->increaseSkill(PRO_TRADING); } } else { players[clientnum]->entity->increaseSkill(PRO_TRADING); } } } else { strcpy((char*)net_packet->data, "SHPS"); SDLNet_Write32(shopkeeper, &net_packet->data[4]); // send item that was sold to server SDLNet_Write32(item->type, &net_packet->data[8]); SDLNet_Write32(item->status, &net_packet->data[12]); SDLNet_Write32(item->beatitude, &net_packet->data[16]); SDLNet_Write32((Uint32)item->appearance, &net_packet->data[20]); if ( item->identified ) { net_packet->data[24] = 1; } else { net_packet->data[24] = 0; } net_packet->data[25] = clientnum; net_packet->address.host = net_server.host; net_packet->address.port = net_server.port; net_packet->len = 26; sendPacketSafe(net_sock, -1, net_packet, 0); } consumeItem(item); sellitem = NULL; }
void buyItemFromShop(Item* item) { if ( !item ) { return; } if ( stats[clientnum]->GOLD >= item->buyValue(clientnum) ) { if ( items[item->type].value * 1.5 >= item->buyValue(clientnum) ) { shopspeech = language[200 + rand() % 3]; } else { shopspeech = language[197 + rand() % 3]; } shoptimer = ticks - 1; Item* itemToPickup = newItem(item->type, item->status, item->beatitude, 1, item->appearance, item->identified, nullptr); itemPickup(clientnum, itemToPickup); stats[clientnum]->GOLD -= item->buyValue(clientnum); playSound(89, 64); int ocount = item->count; item->count = 1; messagePlayer(clientnum, language[1123], item->description(), item->buyValue(clientnum)); item->count = ocount; if ( multiplayer != CLIENT ) { Entity* entity = uidToEntity(shopkeeper); if (entity) { Stat* shopstats = entity->getStats(); shopstats->GOLD += item->buyValue(clientnum); } if ( rand() % 2 ) { if ( item->buyValue(clientnum) <= 1 ) { // buying cheap items does not increase trading past basic if ( stats[clientnum]->PROFICIENCIES[PRO_TRADING] < SKILL_LEVEL_SKILLED ) { players[clientnum]->entity->increaseSkill(PRO_TRADING); } } else { players[clientnum]->entity->increaseSkill(PRO_TRADING); } } else if ( item->buyValue(clientnum) >= 150 ) { if ( item->buyValue(clientnum) >= 300 || rand() % 2 ) { players[clientnum]->entity->increaseSkill(PRO_TRADING); } } } else { strcpy((char*)net_packet->data, "SHPB"); SDLNet_Write32(shopkeeper, &net_packet->data[4]); // send item that was bought to server SDLNet_Write32(item->type, &net_packet->data[8]); SDLNet_Write32(item->status, &net_packet->data[12]); SDLNet_Write32(item->beatitude, &net_packet->data[16]); SDLNet_Write32((Uint32)item->appearance, &net_packet->data[20]); if ( item->identified ) { net_packet->data[24] = 1; } else { net_packet->data[24] = 0; } net_packet->data[25] = clientnum; net_packet->address.host = net_server.host; net_packet->address.port = net_server.port; net_packet->len = 26; sendPacketSafe(net_sock, -1, net_packet, 0); } consumeItem(item); } else { shopspeech = language[203 + rand() % 3]; shoptimer = ticks - 1; playSound(90, 64); } }