Esempio n. 1
0
File: client.c Progetto: theZiz/hase
void set_status(pGame game,int status)
{
	int old_status = game->status;
	game->status = status;
	if (game->local == 0)
	{
		pMessage message = NULL;
		numToMessage(&message,"game_id",game->id);
		numToMessage(&message,"admin_pw",game->admin_pw);
		numToMessage(&message,"status",game->status);
		pMessage result = NULL;
		int i;
		for (i = 0; i < 3 && result == NULL;i++)
			result = sendMessage(message,NULL,NULL,0,"set_status.php",hase_url);
		deleteMessage(&message);
		deleteMessage(&result);
	}
	else
	if (status == 1 && old_status != 1)
	{
		int positions[game->player_count];
		int i;
		for (i=0;i<game->player_count;i++)
			positions[i] = i;
		//Shuffle
		for (i=0;i<game->player_count*10;i++)
		{
			int a = spRand()%game->player_count;
			int b = spRand()%game->player_count;
			if (a == b)
				continue;
			int temp = positions[a];
			positions[a] = positions[b];
			positions[b] = temp;
		}
		i = 0;
		pPlayer p = game->local_player;
		while (p)
		{
			p->position_in_game = positions[i];
			p = p->next;
			i++;
		}
	}
}
Esempio n. 2
0
PREFIX spParticleBunchPointer spParticleFromSprite(spSpritePointer sprite,int ( *feedback )( spParticleBunchPointer bunch, Sint32 action, Sint32 extra_data),spParticleBunchPointer* addBunch)
{
	spParticleBunchPointer bunch = spParticleCreate(sprite->momSub->pixelCount,feedback,addBunch);
	SDL_LockSurface(sprite->momSub->surface);
	Uint16* pixel = (Uint16*)sprite->momSub->surface->pixels;
	int line = sprite->momSub->surface->pitch/sprite->momSub->surface->format->BytesPerPixel;
	int x,y,c = 0;
	for (x = sprite->momSub->sx; x < sprite->momSub->sx+sprite->momSub->sw; x++)
		for (y = sprite->momSub->sy; y < sprite->momSub->sy+sprite->momSub->sh; y++)
			if (pixel[x+y*line] != SP_ALPHA_COLOR)
			{
				bunch->particle[c].x = x-sprite->momSub->sx;
				bunch->particle[c].y = y-sprite->momSub->sy;
				bunch->particle[c].data.color = pixel[x+y*line];
				bunch->particle[c].dx = (spRand() & 131071) - SP_ONE; // mod SP_ONE
				bunch->particle[c].dy = (spRand() & 131071) - SP_ONE; // mod SP_ONE
				c++;
			}
	SDL_UnlockSurface(sprite->momSub->surface);	
	return bunch;
}
Esempio n. 3
0
fp_digit spRand(fp_digit lower, fp_digit upper)
{
	// advance the state of the LCG and return the appropriate result

#if BITS_PER_DIGIT == 64
	//we need to do some gymnastics to prevent the potentially 64 bit value
	//of (upper - lower) from being truncated to a 53 bit double

	uint32 n = spBits(upper-lower);

	if (n > 32)
	{
		fp_digit boundary = 4294967296ULL;
		fp_digit l,u;
		l = spRand(lower,boundary-1);
		u = spRand(0,upper>>32);
		return  l + (u << 32);
	}
Esempio n. 4
0
void williams_loop(fact_obj_t *fobj)
{
    //use william's p+1 algorithm 'trials' times on n.
    //we allow multiple trials since we may not always get a p+1 group
    //with our random choice of base
    //expects the input in pp1_obj->gmp_n
    mpz_t d,t;
    int i,it,trials = fobj->pp1_obj.numbases;
    FILE *flog;
    clock_t start, stop;
    double tt;

    //check for trivial cases
    if ((mpz_cmp_ui(fobj->pp1_obj.gmp_n, 1) == 0) || (mpz_cmp_ui(fobj->pp1_obj.gmp_n, 0) == 0))
        return;

    if (mpz_cmp_ui(fobj->pp1_obj.gmp_n, 2) == 0)
        return;

    //open the log file
    flog = fopen(fobj->flogname,"a");
    if (flog == NULL)
    {
        printf("fopen error: %s\n", strerror(errno));
        printf("could not open %s for appending\n",fobj->flogname);
        return;
    }

    //initialize the flag to watch for interrupts, and set the
    //pointer to the function to call if we see a user interrupt
    PP1_ABORT = 0;
    signal(SIGINT,pp1exit);

    //initialize some local args
    mpz_init(d);
    mpz_init(t);

    pp1_init(fobj);

    i=0;
    while (i < trials)
    {
        //watch for an abort
        if (PP1_ABORT)
        {
            print_factors(fobj);
            exit(1);
        }

        start = clock();
        if (is_mpz_prp(fobj->pp1_obj.gmp_n))
        {
            logprint(flog,"prp%d = %s\n", gmp_base10(fobj->pp1_obj.gmp_n),
                     mpz_conv2str(&gstr1.s, 10, fobj->pp1_obj.gmp_n));

            add_to_factor_list(fobj, fobj->pp1_obj.gmp_n);

            stop = clock();
            tt = (double)(stop - start)/(double)CLOCKS_PER_SEC;
            mpz_set_ui(fobj->pp1_obj.gmp_n, 1);
            break;
        }

        fobj->pp1_obj.base = spRand(3,MAX_DIGIT);

        pp1_print_B1_B2(fobj,flog);

        it = pp1_wrapper(fobj);

        //check to see if 'f' is non-trivial
        if ((mpz_cmp_ui(fobj->pp1_obj.gmp_f, 1) > 0)
                && (mpz_cmp(fobj->pp1_obj.gmp_f, fobj->pp1_obj.gmp_n) < 0))
        {
            //non-trivial factor found
            stop = clock();
            tt = (double)(stop - start)/(double)CLOCKS_PER_SEC;

            //check if the factor is prime
            if (is_mpz_prp(fobj->pp1_obj.gmp_f))
            {
                add_to_factor_list(fobj, fobj->pp1_obj.gmp_f);

                if (VFLAG > 0)
                    gmp_printf("pp1: found prp%d factor = %Zd\n",
                               gmp_base10(fobj->pp1_obj.gmp_f),fobj->pp1_obj.gmp_f);

                logprint(flog,"prp%d = %s\n",
                         gmp_base10(fobj->pp1_obj.gmp_f),
                         mpz_conv2str(&gstr1.s, 10, fobj->pp1_obj.gmp_f));
            }
            else
            {
                add_to_factor_list(fobj, fobj->pp1_obj.gmp_f);

                if (VFLAG > 0)
                    gmp_printf("pp1: found c%d factor = %Zd\n",
                               gmp_base10(fobj->pp1_obj.gmp_f),fobj->pp1_obj.gmp_f);

                logprint(flog,"c%d = %s\n",
                         gmp_base10(fobj->pp1_obj.gmp_f),
                         mpz_conv2str(&gstr1.s, 10, fobj->pp1_obj.gmp_f));
            }
            start = clock();

            //reduce input
            mpz_tdiv_q(fobj->pp1_obj.gmp_n, fobj->pp1_obj.gmp_n, fobj->pp1_obj.gmp_f);

            i++;
            break;
        }

        i++;
    }

    fclose(flog);

    pp1_finalize(fobj);
    signal(SIGINT,NULL);
    mpz_clear(d);
    mpz_clear(t);

    return;
}