Exemple #1
0
/**
 * Add charge(s) of rain to given container, possibly contaminating it.
 */
void item::add_rain_to_container(bool acid, int charges)
{
    if( charges <= 0) {
        return;
    }
    item ret( acid ? "water_acid" : "water", calendar::turn );
    const long capa = get_remaining_capacity_for_liquid( ret );
    if (contents.empty()) {
        // This is easy. Just add 1 charge of the rain liquid to the container.
        if (!acid) {
            // Funnels aren't always clean enough for water. // todo; disinfectant squeegie->funnel
            ret.poison = one_in(10) ? 1 : 0;
        }
        ret.charges = std::min<long>( charges, capa );
        put_in(ret);
    } else {
        // The container already has a liquid.
        item &liq = contents[0];
        long orig = liq.charges;
        long added = std::min<long>( charges, capa );
        if (capa > 0 ) {
            liq.charges += added;
        }

        if (liq.typeId() == ret.typeId() || liq.typeId() == "water_acid_weak") {
            // The container already contains this liquid or weakly acidic water.
            // Don't do anything special -- we already added liquid.
        } else {
            // The rain is different from what's in the container.
            // Turn the container's liquid into weak acid with a probability
            // based on its current volume.

            // If it's raining acid and this container started with 7
            // charges of water, the liquid will now be 1/8th acid or,
            // equivalently, 1/4th weak acid (the rest being water). A
            // stochastic approach gives the liquid a 1 in 4 (or 2 in
            // liquid.charges) chance of becoming weak acid.
            const bool transmute = x_in_y(2 * added, liq.charges);

            if (transmute) {
                item transmuted("water_acid_weak", 0);
                transmuted.charges = liq.charges;
                contents[0] = transmuted;
            } else if (liq.typeId() == "water") {
                // The container has water, and the acid rain didn't turn it
                // into weak acid. Poison the water instead, assuming 1
                // charge of acid would act like a charge of water with poison 5.
                long total_poison = liq.poison * (orig) + (5 * added);
                liq.poison = total_poison / liq.charges;
                long leftover_poison = total_poison - liq.poison * liq.charges;
                if (leftover_poison > rng(0, liq.charges)) {
                    liq.poison++;
                }
            }
        }
    }
}
Exemple #2
0
struct vector *
make_unique(struct vector *arr, struct closure *fun, struct svalue *skipnum)
{
    struct vector *res, *ret;
    struct unique *head, *nxt, *nxt2;
    
    int cnt, ant, cnt2;
    
    if (arr->size < 1)
	return allocate_array(0);

    head = 0; 
    ant = 0; 
    INCREF(arr->ref);
    for(cnt = 0; cnt < arr->size; cnt++) 
    {
	if (arr->item[cnt].type == T_OBJECT)
	{
            push_svalue(&arr->item[cnt]);
            (void)call_var(1, fun);

	    if ((!sp) || (sp->type != skipnum->type) || !equal_svalue(sp, skipnum)) 
	    {
		if (sp) 
		{
		    ant = put_in(&head, sp, &(arr->item[cnt]));
		}
	    }

            pop_stack();
	}
    }
    DECREF(arr->ref);
    ret = allocate_array(ant);
    
    for (cnt = ant - 1; cnt >= 0; cnt--) /* Reverse to compensate put_in */
    {
	ret->item[cnt].type = T_POINTER;
	ret->item[cnt].u.vec = res = allocate_array(head->count);
	nxt2 = head;
	head = head->next;
	cnt2 = 0;
	while (nxt2) 
	{
	    assign_svalue_no_free (&res->item[cnt2++], nxt2->val);
	    free_svalue(&nxt2->mark);
	    nxt = nxt2->same;
/*	    tmpfree((char *) nxt2); */
	    nxt2 = nxt;
	}
	if (!head) 
	    break; /* It shouldn't but, to avoid skydive just in case */
    }
    return ret;
}
bool path_preview_handle::start_drag (QPointF local_pos, QTransform transform)
{
  path_nearest_point nearest_calc;
  put_in (m_edit_operation, m_item);

  m_drag_it.reset (new svg_path_geom_iterator);
  nearest_calc.get_nearest_point (transform.map (local_pos), m_item->full_transform () * transform,
                                  m_edit_operation->get_svg_path ()->get_geom (), &m_drag_t, m_drag_it.get ());

  if (m_drag_t < 0)
    {
      m_drag_it.reset ();
      m_edit_operation.reset ();
      return false;
    }

  m_prev_drag = m_drag_start = local_pos;
  return true;
}
int main(int argc, char **argv)
{
    int status = 0;
    int child_pid = 0;
    int child_count = 0;
    int read_count = 0;
    int write_count = 0;
    int empty = 0;
    int full = 0;
    void *shm_addr = NULL;
    int shm_id = 0;
    int sem_id = 0;

    shm_id = safe_shmget();
    sem_id = safe_semget();

    while ( child_count < 5 )
    {

        child_pid = fork();
        if ( child_pid < 0 )    // error
        {
            perror("fork");
            exit(-1);
        }
        else if ( child_pid == 0 ) // child
        {
            shm_id = safe_shmget();
            sem_id = safe_semget();
            struct buffer msg;

            shm_addr = shmat(shm_id, NULL, 0);
            if ( shm_addr == ( (void *) -1 ))
            {
                perror("shmat");
                exit(-1);
            }

            if ( child_count < 2 )  // producer
            {
                while( write_count < 6 )
                {
                    if(is_full(shm_addr))
                    {
                        simple_sleep();
                        continue;
                    }

                    p(sem_id); 
                    fprintf(stdout, "child %d get into critical with empty count:%d\n", child_count, get_empty(shm_addr));
                    report(shm_addr);
                    do
                    {
                        if(is_full(shm_addr))
                        {
                            break;
                        }
                        bzero(&msg, sizeof(msg));
                        snprintf((char *)msg.msg, 128, "child %d is putting msg %d\n", child_count, write_count); 
                        fprintf(stdout, "%s", msg.msg);
                        put_in(shm_addr, &msg);
                        write_count++;
                    }while(0);
                    report(shm_addr);
                    fprintf(stdout, "child %d get out of critical with empty count:%d\n", child_count, get_empty(shm_addr));
                    v(sem_id); 

                }

            }
            else                    // consumer
            {
                while ( read_count < 4 )
                {
                    if ( is_empty(shm_addr) )
                    {
                        simple_sleep();
                        continue;
                    }
                    
                    p(sem_id);
                    fprintf(stdout, "child %d get into critical with empty count:%d\n", child_count, get_empty(shm_addr));
                    report(shm_addr);
                    do
                    {
                        if ( is_empty(shm_addr) )
                        {
                            break;
                        }
                        bzero(&msg, sizeof(msg));
                        read_out(shm_addr, &msg);
                        fprintf(stdout, "child %d read out msg: %s\n", child_count, msg.msg);
                        read_count++;
                    }while(0);
                    report(shm_addr);
                    fprintf(stdout, "child %d get out of critical with empty count:%d\n", child_count, get_empty(shm_addr));
                    v(sem_id);
                }
            }

            exit(0);
        }
        else                    //parent
        {
            child_count = child_count + 1;
            continue;
        }
    }

    for ( child_count=0; child_count<5; child_count++ )
    {
        wait();
    }
    safe_shmdel(shm_id);
    safe_semdel(sem_id);

    return 0;
}
Exemple #5
0
// Add charge(s) of rain to given container, possibly contaminating it
void item::add_rain_to_container(bool acid, int charges)
{
    if( charges <= 0) {
        return;
    }
    const char *typeId = acid ? "water_acid" : "water";
    int max = dynamic_cast<it_container*>(type)->contains;
    int orig = 0;
    int added = charges;
    if (contents.empty()) {
        // This is easy. Just add 1 charge of the rain liquid to the container.
        item ret(item_controller->find_template(typeId), 0);
        if (!acid) {
            // Funnels aren't always clean enough for water. // todo; disinfectant squeegie->funnel
            ret.poison = one_in(10) ? 1 : 0;
        }
        ret.charges = ( charges > max ? max : charges );
        put_in(ret);
    } else {
        // The container already has a liquid.
        item &liq = contents[0];
        orig = liq.charges;
        max -= liq.charges;
        added = ( charges > max ? max : charges );
        if (max > 0 ) {
            liq.charges += added;
        }

        if (liq.typeId() == typeId || liq.typeId() == "water_acid_weak") {
            // The container already contains this liquid or weakly acidic water.
            // Don't do anything special -- we already added liquid.
        } else {
            // The rain is different from what's in the container.
            // Turn the container's liquid into weak acid with a probability
            // based on its current volume.

            // If it's raining acid and this container started with 7
            // charges of water, the liquid will now be 1/8th acid or,
            // equivalently, 1/4th weak acid (the rest being water). A
            // stochastic approach gives the liquid a 1 in 4 (or 2 in
            // liquid.charges) chance of becoming weak acid.
            const bool transmute = x_in_y(2*added, liq.charges);

            if (transmute) {
                item transmuted(item_controller->find_template("water_acid_weak"), 0);
                transmuted.charges = liq.charges;
                contents[0] = transmuted;
            } else if (liq.typeId() == "water") {
                // The container has water, and the acid rain didn't turn it
                // into weak acid. Poison the water instead, assuming 1
                // charge of acid would act like a charge of water with poison 5.
                int total_poison = liq.poison * (orig) + (5*added);
                liq.poison = total_poison / liq.charges;
                int leftover_poison = total_poison - liq.poison * liq.charges;
                if (leftover_poison > rng(0, liq.charges)) {
                    liq.poison++;
                }
            }
        }
    }
}