Beispiel #1
0
void update() {
	if (!danmakux.state_paused && danmakux.state_playing) {
		danmakux.framesPast++;
		runFunction(&danmakux.level, "update");
		for (std::list<PlayerBullet>::iterator i = danmakux.playerbullets.begin(); i != danmakux.playerbullets.end(); i++) {
			plblt_update(&(*i));
		}
		for (std::list<Enemy>::iterator i = danmakux.enemies.begin(); i != danmakux.enemies.end(); i++) {
			if (!(*i).dead) enem_update(&(*i));
			for (std::list<EnemyBullet>::iterator j = (*i).bullets.begin(); j != (*i).bullets.end(); j++) {
				enblt_update(&(*j));
			}
		}
		for (std::list<Item>::iterator i = danmakux.items.begin(); i != danmakux.items.end(); i++) {
			item_update(&(*i));
		}
		plyr_update(danmakux.player);
		if (danmakux.bombsRemain > 0 && danmakux.fireBomb == true) {
			danmakux.fireBomb = false;
			//bomb
		}
	}
	else if (danmakux.state_paused) {
		for (std::list<MenuItem>::iterator i = danmakux.pauseitems.begin(); i != danmakux.pauseitems.end(); i++) {
			menuitem_update(&(*i));
		}
	}
	else if (danmakux.state_menu) {
		for (std::list<MenuItem>::iterator i = danmakux.menuitems.begin(); i != danmakux.menuitems.end(); i++) {
			menuitem_update(&(*i));
		}
	}
}
Beispiel #2
0
void complete_nread(conn *c) {
    item *it = c->item;
    int comm = c->item_comm;
    item *old_it;
    time_t now = time(0);

    stats.set_cmds++;

    while(1) {
        if (strncmp(ITEM_data(it) + it->nbytes - 2, "\r\n", 2) != 0) {
            out_string(c, "CLIENT_ERROR bad data chunk");
            break;
        }

        old_it = assoc_find(ITEM_key(it));

        if (old_it && settings.oldest_live &&
                old_it->time <= settings.oldest_live) {
            item_unlink(old_it);
            old_it = 0;
        }

        if (old_it && old_it->exptime && old_it->exptime < now) {
            item_unlink(old_it);
            old_it = 0;
        }

        if (old_it && comm==NREAD_ADD) {
            item_update(old_it);
            out_string(c, "NOT_STORED");
            break;
        }

        if (!old_it && comm == NREAD_REPLACE) {
            out_string(c, "NOT_STORED");
            break;
        }

        if (old_it && (old_it->it_flags & ITEM_DELETED) && (comm == NREAD_REPLACE || comm == NREAD_ADD)) {
            out_string(c, "NOT_STORED");
            break;
        }

        if (old_it) {
            item_replace(old_it, it);
        } else item_link(it);

        c->item = 0;
        out_string(c, "STORED");
        return;
    }

    item_free(it);
    c->item = 0;
    return;
}
Beispiel #3
0
int process_touch_command ( char *key, size_t nkey, int32_t exptime_int )
{
    item *it;
    it = item_touch (key, nkey, realtime (exptime_int));
    if ( it )
    {
        item_update (it);
        item_remove (it);
        return true;
    }
    else
    {
        //item_remove(it);
        return false;
    }
}
Beispiel #4
0
int process_get_command ( char *key, size_t nkey, char *dst, int *len )
{
    item *it;
    char *src = NULL;
    it = item_get (key, nkey);
    if ( it )
    {
        item_update (it);
        src = ITEM_data (it);
        *len = it->nbytes - 2;
        memcpy (dst, src, it->nbytes);
        item_remove (it);
        return true;
    }
    else
    {
        //item_remove(it);
        return false;
    }
}
Beispiel #5
0
// process a memcached get(s) command. (we don't support CAS).
void process_get_command(conn *c, token_t *tokens, size_t ntokens,
                                bool return_cas) {
	char *key;
	size_t nkey;
	int i = 0;
	item *it;
	token_t *key_token = &tokens[KEY_TOKEN];
	char *suffix;

	assert(c != NULL);

	if (config.alloc && c->mem_blob == NULL) {
		long size = config.alloc_mean + gsl_ran_gaussian(c->thread->r, config.alloc_stddev);
		size = size <= 0 ? 10 : size;
		if (config.verbose > 0) {
			fprintf(stderr, "allocated blob: %ld\n", size);
		}

		c->mem_blob = malloc(sizeof(char) * size);
		c->mem_free_delay = 0;

		if (config.rtt_delay) {
			double r = config.rtt_mean + gsl_ran_gaussian(c->thread->r, config.rtt_stddev);
			if (r >= config.rtt_cutoff) {
				int wait = r / 100;
				if (config.verbose > 0) {
					fprintf(stderr, "delay: %d\n", wait);
				}
				c->mem_free_delay = wait;
				conn_set_state(c, conn_mwrite);
			}
		}
	}

	// process the whole command line, (only part of it may be tokenized right now)
	do {
		// process all tokenized keys at this stage.
		while(key_token->length != 0) {
			key = key_token->value;
			nkey = key_token->length;

			if(nkey > KEY_MAX_LENGTH) {
				error_response(c, "CLIENT_ERROR bad command line format");
				return;
			}

			// lookup key-value.
			it = item_get(key, nkey);
			
			// hit.
			if (it) {
				if (i >= c->isize && !conn_expand_items(c)) {
					item_remove(it);
					break;
				}

				// Construct the response. Each hit adds three elements to the
				// outgoing data list:
				//   "VALUE <key> <flags> <data_length>\r\n"
				//   "<data>\r\n"
				// The <data> element is stored on the connection item list, not on
				// the iov list.
				if (!conn_add_iov(c, "VALUE ", 6) != 0 ||
				    !conn_add_iov(c, ITEM_key(it), it->nkey) != 0 ||
				    !conn_add_iov(c, ITEM_suffix(it), it->nsuffix + it->nbytes) != 0) {
					item_remove(it);
					break;
				}

				if (config.verbose > 1) {
					fprintf(stderr, ">%d sending key %s\n", c->sfd, key);
				}

				// add item to remembered list (i.e., we've taken ownership of them
				// through refcounting and later must release them once we've
				// written out the iov associated with them).
				item_update(it);
				*(c->ilist + i) = it;
				i++;
			}

			key_token++;
		}

		/*
		 * If the command string hasn't been fully processed, get the next set
		 * of tokens.
		 */
		if(key_token->value != NULL) {
			ntokens = tokenize_command(key_token->value, tokens, MAX_TOKENS);
			key_token = tokens;
		}

	} while(key_token->value != NULL);

	c->icurr = c->ilist;
	c->ileft = i;

	if (config.verbose > 1) {
		fprintf(stderr, ">%d END\n", c->sfd);
	}

	// If the loop was terminated because of out-of-memory, it is not reliable
	// to add END\r\n to the buffer, because it might not end in \r\n. So we
	// send SERVER_ERROR instead.
	if (key_token->value != NULL || !conn_add_iov(c, "END\r\n", 5) != 0) {
		error_response(c, "SERVER_ERROR out of memory writing get response");
	} else {
		conn_set_state(c, conn_mwrite);
	}
}
Beispiel #6
0
void process_command(conn *c, char *command) {

    int comm = 0;
    int incr = 0;

    /*
     * for commands set/add/replace, we build an item and read the data
     * directly into it, then continue in nread_complete().
     */

    if (settings.verbose > 1)
        fprintf(stderr, "<%d %s\n", c->sfd, command);

    /* All incoming commands will require a response, so we cork at the beginning,
       and uncork at the very end (usually by means of out_string)  */
    set_cork(c, 1);

    if ((strncmp(command, "add ", 4) == 0 && (comm = NREAD_ADD)) ||
            (strncmp(command, "set ", 4) == 0 && (comm = NREAD_SET)) ||
            (strncmp(command, "replace ", 8) == 0 && (comm = NREAD_REPLACE))) {

        char key[251];
        int flags;
        time_t expire;
        int len, res;
        item *it;

        res = sscanf(command, "%*s %250s %u %ld %d\n", key, &flags, &expire, &len);
        if (res!=4 || strlen(key)==0 ) {
            out_string(c, "CLIENT_ERROR bad command line format");
            return;
        }
        expire = realtime(expire);
        it = item_alloc(key, flags, expire, len+2);
        if (it == 0) {
            out_string(c, "SERVER_ERROR out of memory");
            /* swallow the data line */
            c->write_and_go = conn_swallow;
            c->sbytes = len+2;
            return;
        }

        c->item_comm = comm;
        c->item = it;
        c->rcurr = ITEM_data(it);
        c->rlbytes = it->nbytes;
        c->state = conn_nread;
        return;
    }

    if ((strncmp(command, "incr ", 5) == 0 && (incr = 1)) ||
            (strncmp(command, "decr ", 5) == 0)) {
        char temp[32];
        unsigned int value;
        item *it;
        unsigned int delta;
        char key[251];
        int res;
        char *ptr;
        time_t now = time(0);

        res = sscanf(command, "%*s %250s %u\n", key, &delta);
        if (res!=2 || strlen(key)==0 ) {
            out_string(c, "CLIENT_ERROR bad command line format");
            return;
        }

        it = assoc_find(key);
        if (it && (it->it_flags & ITEM_DELETED)) {
            it = 0;
        }
        if (it && it->exptime && it->exptime < now) {
            item_unlink(it);
            it = 0;
        }

        if (!it) {
            out_string(c, "NOT_FOUND");
            return;
        }

        ptr = ITEM_data(it);
        while (*ptr && (*ptr<'0' && *ptr>'9')) ptr++;

        value = atoi(ptr);

        if (incr)
            value+=delta;
        else {
            if (delta >= value) value = 0;
            else value-=delta;
        }

        sprintf(temp, "%u", value);
        res = strlen(temp);
        if (res + 2 > it->nbytes) { /* need to realloc */
            item *new_it;
            new_it = item_alloc(ITEM_key(it), it->flags, it->exptime, res + 2 );
            if (new_it == 0) {
                out_string(c, "SERVER_ERROR out of memory");
                return;
            }
            memcpy(ITEM_data(new_it), temp, res);
            memcpy(ITEM_data(new_it) + res, "\r\n", 2);
            item_replace(it, new_it);
        } else { /* replace in-place */
            memcpy(ITEM_data(it), temp, res);
            memset(ITEM_data(it) + res, ' ', it->nbytes-res-2);
        }
        out_string(c, temp);
        return;
    }

    if (strncmp(command, "get ", 4) == 0) {

        char *start = command + 4;
        char key[251];
        int next;
        int i = 0;
        item *it;
        time_t now = time(0);

        while(sscanf(start, " %250s%n", key, &next) >= 1) {
            start+=next;
            stats.get_cmds++;
            it = assoc_find(key);
            if (it && (it->it_flags & ITEM_DELETED)) {
                it = 0;
            }
            if (settings.oldest_live && it &&
                    it->time <= settings.oldest_live) {
                item_unlink(it);
                it = 0;
            }
            if (it && it->exptime && it->exptime < now) {
                item_unlink(it);
                it = 0;
            }

            if (it) {
                if (i >= c->isize) {
                    item **new_list = realloc(c->ilist, sizeof(item *)*c->isize*2);
                    if (new_list) {
                        c->isize *= 2;
                        c->ilist = new_list;
                    } else break;
                }
                stats.get_hits++;
                it->refcount++;
                item_update(it);
                *(c->ilist + i) = it;
                i++;
            } else stats.get_misses++;
        }
        c->icurr = c->ilist;
        c->ileft = i;
        if (c->ileft) {
            c->ipart = 0;
            c->state = conn_mwrite;
            c->ibytes = 0;
            return;
        } else {
            out_string(c, "END");
            return;
        }
    }

    if (strncmp(command, "delete ", 7) == 0) {
        char key[251];
        item *it;
        int res;
        time_t exptime = 0;

        res = sscanf(command, "%*s %250s %ld", key, &exptime);
        it = assoc_find(key);
        if (!it) {
            out_string(c, "NOT_FOUND");
            return;
        }

        if (exptime == 0) {
            item_unlink(it);
            out_string(c, "DELETED");
            return;
        }

        if (delcurr >= deltotal) {
            item **new_delete = realloc(todelete, sizeof(item *) * deltotal * 2);
            if (new_delete) {
                todelete = new_delete;
                deltotal *= 2;
            } else {
                /*
                 * can't delete it immediately, user wants a delay,
                 * but we ran out of memory for the delete queue
                 */
                out_string(c, "SERVER_ERROR out of memory");
                return;
            }
        }

        exptime = realtime(exptime);

        it->refcount++;
        /* use its expiration time as its deletion time now */
        it->exptime = exptime;
        it->it_flags |= ITEM_DELETED;
        todelete[delcurr++] = it;
        out_string(c, "DELETED");
        return;
    }

    if (strncmp(command, "stats", 5) == 0) {
        process_stat(c, command);
        return;
    }

    if (strcmp(command, "flush_all") == 0) {
        settings.oldest_live = time(0);
        out_string(c, "OK");
        return;
    }

    if (strcmp(command, "version") == 0) {
        out_string(c, "VERSION " VERSION);
        return;
    }

    if (strcmp(command, "quit") == 0) {
        c->state = conn_closing;
        return;
    }

    if (strncmp(command, "slabs reassign ", 15) == 0) {
        int src, dst;
        char *start = command+15;
        if (sscanf(start, "%u %u\r\n", &src, &dst) == 2) {
            int rv = slabs_reassign(src, dst);
            if (rv == 1) {
                out_string(c, "DONE");
                return;
            }
            if (rv == 0) {
                out_string(c, "CANT");
                return;
            }
            if (rv == -1) {
                out_string(c, "BUSY");
                return;
            }
        }
        out_string(c, "CLIENT_ERROR bogus command");
        return;
    }

    out_string(c, "ERROR");
    return;
}
void process_command(conn *c, char *command) {
    
    int comm = 0;

    /* 
     * for commands set/add/replace, we build an item and read the data
     * directly into it, then continue in nread_complete().
     */ 

    if ((strncmp(command, "add ", 4) == 0 && (comm = NREAD_ADD)) || 
        (strncmp(command, "set ", 4) == 0 && (comm = NREAD_SET)) ||
        (strncmp(command, "replace ", 8) == 0 && (comm = NREAD_REPLACE))) {

        char s_comm[10];
        char key[256];
        int flags;
        time_t expire;
        int len, res;
        item *it;

        res = sscanf(command, "%s %s %u %u %d\n", s_comm, key, &flags, &expire, &len);
        if (res!=5 || strlen(key)==0 ) {
            out_string(c, "CLIENT_ERROR bad command line format");
            return;
        }
        it = item_alloc(key, flags, expire, len+2);
        if (it == 0) {
            out_string(c, "SERVER_ERROR out of memory");
            c->write_and_close = 1;
            return;
        }

        c->item_comm = comm;
        c->item = it;
        c->rcurr = it->data;
        c->rlbytes = it->nbytes;
        c->state = conn_nread;
        return;
    }
    if (strncmp(command, "get ", 4) == 0) {

        char *start = command + 4;
        char key[256];
        int next;
        int i = 0;
        item *it;
        time_t now = time(0);

        while(sscanf(start, " %s%n", key, &next) >= 1) {
            start+=next;
            stats.get_cmds++;
            it = (item *)assoc_find(key);
            if (it && (it->it_flags & ITEM_DELETED)) {
                it = 0;
            }
            if (it && it->exptime && it->exptime < now) {
                item_unlink(it);
                it = 0;
            }

            if (it) {
                stats.get_hits++;
                it->usecount++;
                item_update(it);
                *(c->ilist + i) = it;
                i++;
                if (i > c->isize) {
                    c->isize *= 2;
                    c->ilist = realloc(c->ilist, sizeof(item *)*c->isize);
                }
            } else stats.get_misses++;
        }
        c->icurr = c->ilist;
        c->ileft = i;
        if (c->ileft) {
            c->ipart = 0;
            c->state = conn_mwrite;
            c->ibytes = 0;
            return;
        } else {
            out_string(c, "END");
            return;
        }
    }

    if (strncmp(command, "delete ", 7) == 0) {
        char key [256];
        char *start = command+7;
        item *it;

        sscanf(start, " %s", key);
        it = assoc_find(key);
        if (!it) {
            out_string(c, "NOT_FOUND");
            return;
        } else {
            it->usecount++;
            /* use its expiration time as its deletion time now */
            it->exptime = time(0) + 4;
            it->it_flags |= ITEM_DELETED;
            todelete[delcurr++] = it;
            if (delcurr >= deltotal) {
                deltotal *= 2;
                todelete = realloc(todelete, sizeof(item *)*deltotal);
            }
        }
        out_string(c, "DELETED");
        return;
    }
        
    if (strncmp(command, "stats", 5) == 0) {
        process_stat(c, command);
        return;
    }

    if (strcmp(command, "version") == 0) {
        out_string(c, "VERSION 2.0");
        return;
    }

    out_string(c, "ERROR");
    return;
}
Beispiel #8
0
void world_update_dt(void) {
    t_sprite_iterator iter;
    t_spritePtr sprite;
    t_item_iterator iiter;
    t_itemPtr item;
    
//    if (world.t-debug_time_printed > 10.0) {
//        debug_time_printed+=10.0;
//        printf("t= %.1f   %d sprites nexts: %.1f %.1f %.1f\n",debug_time_printed,world.sprites_n,world.time_next_piligrim,world.time_next_cansado,world.time_next_bici);
//    }
    
    
    
    // en la iteracion de actualizacion acumula collision candidates para despues
    collision_candidates_n=0;
    collision_bici_n=0;
    t_pos collision_x=world.player.vx*game_dt;
    // y calcula el walker mas retrasado y su velocidad
    t_pos min_x=500;
    t_v min_v=c_v_fast;
    world_iterate_sprites(&iter);
    // actualiza posicion y velocidad y estado de la animacion
    // tiene que hacerse al final para el player porque su velocidad se usa en el calculo de los demas
    while ( NULL != (sprite=world_iterate_next(&iter)) ) {
        sprite_update(sprite);

        if (sprite->anim_state_id > 100) {
            printf("warning : anim state invalid\n");
        }
        
        if (abs(sprite->x-collision_x)<200.0) { // si estan cerca pueden colisionar
            collision_candidates[collision_candidates_n]=sprite;
            collision_candidates_n+=1;
        }
        if ((sprite->sprite_class->id==SPRCLS_ID_PEREGRINO || sprite->sprite_class->id==SPRCLS_ID_BORDONERO) && (sprite->anim_state_id<STID_WALKER_STOP || sprite->anim_state_id==STID_WALKER_RECEIVED_HIT)) {// || sprite->anim_state_id==STID_WALKER_RECEIVING_HIT)) {
            if (sprite->x<min_x && sprite->x>0) {
                min_x=sprite->x;
                min_v=sprite->vx;
            }
        }
        if (collision_bici_n<1 && sprite->sprite_class->id==SPRCLS_ID_BICI) {
            collision_bici_n+=1;
            collision_bici=sprite;
        }
        
        // posible destruccion de sprites lejanos cuidado no usar despues sprite
        if (sprite->x-world.cam_x < -100 && sprite->sprite_class->id<SPRCLS_ID_BICI ) {
            world_delete_sprite(iter.i);
        }
        if (sprite->x-world.cam_x > 2000 && sprite->sprite_class->id==SPRCLS_ID_BICI ) {
            world_delete_sprite(iter.i);
            audio_una_bici_menos();
        }
    }
    
    //if (collision_candidates_n>0) printf("collision candidates: %d\n",collision_candidates_n);
    
    item_iterate_init(&iiter);
    while ( NULL != (item=item_iterate_next(&world, &iiter)) ) {
        item_update(item);
        
        if (item->x<-800) {
            item_delete(&world,iiter.i);
        }
    }
    

    if (world.player.x>world.player_checkpoint) {
//        printf("checkpoint !!!\n");
        world.player_checkpoint+=7000.0;
        world.player_deadline=world.t+60.0;
    }
    
    if (!world.waiting_to_start) {
        if (world.player.anim_state_id!=STID_PLAYER_FALLEN) {
            world_lose_life(game_dt*c_life_loss_per_second);
        }
    }
        
    if ( ( world.player_deadline<world.t || world.player_vida<=0.0 ) && ( world.player.anim_state_id<STID_PLAYER_STOP || world.player.anim_state_id==STID_PLAYER_SIDE ) ) {
        if (world.player.anim_state_id!=STID_PLAYER_TIRED) {
            audio_stop_music();
            audio_play_player_clip(AUDIO_SND_GAMEOVER);
            stats_end_game_timedistance();
        }
        world.player.vx=0;
        sprite_set_state(&world.player, STID_PLAYER_TIRED);
    }
    
    if (world.player.anim_state_id<STID_PLAYER_STOP && (world.player_untouchable_time<=world.t) ) {
        if(min_x<90.0) {
            world.player.vx=0.5*min_v;
        } else if (min_x<100.0) {
            world.player.vx=min_v;        
        }
    }
    
    sprite_update(&world.player);
    
    // controles
    t_id player_state=world.player.anim_state_id;

    if (!world.waiting_to_start) {
        if (world.player.vx<c_v_fast && player_state<STID_PLAYER_STOP) world.player.vx+=c_v_a*game_dt;
    } else {
        if (world.t>world_waiting_to_start_time) {
            world.waiting_to_start=False;
            sprite_set_state(&world.player, STID_PLAYER_WALK);
        }
    }

    if ( game_salto_pressed && player_state<STID_PLAYER_STOP ) {
        sprite_set_state(&world.player, STID_PLAYER_PREPARINGJUMP);
        audio_play_player_clip(AUDIO_SND_SALTO);
    }
    if ( game_hit_pressed && player_state<STID_PLAYER_STOP ) {
//        audio_play_player_clip(AUDIO_SND_HIT);
        audio_play_golpe_inicio();
        sprite_set_state(&world.player, STID_PLAYER_PREPARINGHIT);
    }
    if ( game_esquivar_pressed && player_state<STID_PLAYER_STOP ) {
        world.player.vx=0;
        sprite_set_state(&world.player, STID_PLAYER_SIDE);
    }
    if ( game_stop_pressed && player_state<STID_PLAYER_STOP ) {
        world.player.vx=c_v_slow*.7+c_v_fast*.3;
        //world_gain_life(1.0);
        //sprite_set_state(&world.player, STID_PLAYER_STOP);
    }
//    if ( !game_stop_pressed && player_state==STID_PLAYER_STOP ) {
//        world.player.vx=c_v_slow;
//        sprite_set_state(&world.player, STID_PLAYER_WALK);
//    }
    if ( !game_esquivar_pressed && player_state==STID_PLAYER_SIDE ) {
        world.player.vx=c_v_slow;
        sprite_set_state(&world.player, STID_PLAYER_WALK);
    }
    
//    // el estado puede haber cambiado por los controles
//    player_state=world.player.anim_state_id;
//    if ( world.player_vida<.1 && player_state<STID_PLAYER_STOP ) {
//        world.player.vx=c_v_slow*1.5;
//    }
//    
//    if ( world.player_vida<.05 && player_state<STID_PLAYER_STOP ) {
//        world.player.vx=c_v_slow*1.0;
//    }
    
    // si tienes poca vida y aun no estas muerto declarate in panic
    if (world.player_vida<.3 &&  world.player.anim_state_id!=STID_PLAYER_TIRED ) {
        float vida=world.player_vida;
        int ifreq=(vida<0.1)?3:((vida<0.2)?2:1);
        ifreq=1; // anular frecuencia variable del warning
        audio_low_warning_play(ifreq);
        achievments_inpanicnow(true);
    } else {
        achievments_inpanicnow(false);
    }
    
    
    // colisiones
    if ( world.player.y <0.001 && world.player.vy<0.001 ) {
        if ( player_state==STID_PLAYER_JUMP ) {
            world.player.y=0;
            world.player.vy=0;
//            world.player.vx=c_v_fast;
            sprite_set_state(&world.player, STID_PLAYER_WALK);
            audio_play_player_clip(AUDIO_SND_CAIDABIEN);
        } else if ( player_state==STID_PLAYER_FALL ) {
            world.player.y=0;
            world.player.vy=0;
            //world.player.vx=c_v_slow*0.5;
            sprite_set_state(&world.player, STID_PLAYER_FALLEN);
            audio_play_player_clip(AUDIO_SND_CAIDA);
        } else if ( player_state==STID_PLAYER_FALLEN ) {
            world.player.vx-=c_v_fast*0.02;
            if (world.player.vx<0) world.player.vx=0;
        }
    }
    
    int i;
    for (i=0; i<collision_candidates_n; i++) {
        sprite=collision_candidates[i];
        // colisionan sprite y player ???
        world_handle_collision_candidate(&world.player, sprite);
    }
    
    item_iterate_init(&iiter);
    while ( NULL != (item=item_iterate_next(&world, &iiter)) ) {
        if (item->item_class->type==ITEM_CLS_TUMBA) continue;
        if (world_item_collision_check(&world.player, item) /* && player_state==STID_PLAYER_STOP */ ) {
            audio_play_player_clip(AUDIO_SND_COMER);
            if (item->item_class->type==ITEM_CLS_POWERUP_LIFE) {
                world_gain_life(1.0);                
            } else {
                world_gain_life(c_life_gain_per_food);
                achievments_eaten(item);
            }
            item_delete(&world,iiter.i);
            stats_add_comida(item->item_class->type);
        }
    }
    
    // vuelta extra para que las bicis arrasen
    // del collision candidates se ha quedado con una bici y prueba si colisiona con otros
    if (collision_bici_n>0 && collision_bici->x<1500.0) {
        world_iterate_sprites(&iter);
        while ( NULL != (sprite=world_iterate_next(&iter)) ) {
            if (sprite!=collision_bici) {
               
                if (world_nonplayer_collision_check(collision_bici,sprite)) {
                    if ( (sprite->sprite_class->id==SPRCLS_ID_PEREGRINO || sprite->sprite_class->id==SPRCLS_ID_BORDONERO) && sprite->anim_state_id<STID_WALKER_STOP) {
                        audio_play_other_clip(AUDIO_SND_CAIDA);
                        sprite_set_state(sprite, STID_WALKER_RECEIVING_HIT);
                        sprite->vx=0.0;
                    }
                }
            }
        }
    }
    
    //int i;
//	int min_i;
//	t_pos min_x=200;
//	t_v min_v=100;
//    for (i=0;i<MAX_N_WALKER;i++) {
//        if (world.walker_list[i].valid) {
//            world.walker_list[i].x+=(world.walker_list[i].v-world.player_vx)*game_dt;
//			if (!world.walker_list[i].apartado) {
//				if (world.walker_list[i].x<min_x) {
//					min_x=world.walker_list[i].x;
//					min_v=world.walker_list[i].v;
//					min_i=i;
//				}
//			}
//            // regenera rapido para probar
//            if (world.walker_list[i].x-world.cam_x<-25.0) {
//               // printf("regenerate now\n");
//                world.walker_list[i].x=480+world.cam_x+24-100*log(rand()*1.0/RAND_MAX);
//                world.walker_list[i].v=c_v_slow+5.0*(int)(4.0*(rand()*1.0/RAND_MAX));
//				world.walker_list[i].apartado=0;
//            }
//            
//        }
//    }
//
//    if (min_x<50) {
//		world.player_vx=min_v;
//		if (!world.player_slow) {
//			world.player_slow=1;
//			world.player_slow_since=world.t;
//		} else {
//			if (world.t-world.player_slow_since>3.0) {
//				world.walker_list[min_i].apartado=1;
//				world.walker_list[min_i].v=0;
//			}
//		}
//	
//	} else {
//		world.player_vx=c_v_fast;
//		world.player_slow=0;
//	}
//	
//	if (world.player.y<0.0001) {
//		world.player.y=0;
////		if (game_salto_pressed) {
////			world.player_vy=100;
////		}
//        }	
//    
    
    //genera
    
//    if (world.t > world.time_next_piligrim) {
//        int tipo=rand()%2;   // de momento 50 - 50 cambiar probabilidades
//        world_create_walker(1000.0, tipo);
//        world.time_next_piligrim=world.t+random_exp_time(20.0);
//    }    
//    if (world.t > world.time_next_cansado) {
//        world_create_walker(1000.0, 2);
//        world.time_next_cansado=world.t+random_exp_time(20.0);
//    }    
//    if (world.t > world.time_next_bici) {
//        world_create_walker(-400.0, 3);
//        world.time_next_bici=world.t+random_exp_time(20.0);
//    }
    
    
    //probando cosas mas creativas para la camara
    // camara on fuerza que proporcional a la distancia al punto deseado
    // y con rozamiento viscoso para que no oscile

    if (cam_type==0) {    
    
        t_pos cam_now,cam_target;
        cam_now=world.cam_x-world.player.vx*game_dt;
        cam_target=-c_cam_offset;
//
    world.cam_v+=250.0*(cam_target-cam_now)*game_dt-1.5*world.cam_v;

//    if (abs(world.cam_v)>500.0) world.cam_v=copysign(500.0,world.cam_v);
//        world.cam_v=copysign(200,(cam_target-cam_now));
    
        float smooth_alfa=0; // 0 no smooth
        
        t_pos cam_smooth=cam_now+world.cam_v*game_dt;
        world.cam_x=world.cam_x*smooth_alfa+cam_smooth*(1-smooth_alfa);
        
    
 //   if (world.cam_x>cam_target) {world.cam_x=cam_target;world.cam_v=world.player.vx;}
//    if (world.cam_x+200<cam_target) {world.cam_x=cam_target;world.cam_v=world.player.vx;}

//    if (world.cam_x<-300) {world.cam_x=-300; world.cam_v=world.player.vx;}
//    if (world.cam_x>-100) {world.cam_x=-100; world.cam_v=world.player.vx;}
    } else if (cam_type==1) {
        
        // camara simple con distancia maxima y minima
        t_pos max_cam_distance=300;
        t_pos min_cam_distance=200-10000*c_cam_init_free;
        t_pos cam_now=world.cam_x-world.player.vx*game_dt+world.cam_v*game_dt;
        if (cam_now<-max_cam_distance) {world.cam_v=world.player.vx;cam_now=-max_cam_distance;}
        if (cam_now>-min_cam_distance) {world.cam_v=0*world.player.vx;cam_now=-min_cam_distance;}
        world.cam_x=cam_now;
        // cuando la camara alcanza al jugador se acaba el estado inicial de camara
        if (c_cam_init_free && cam_now<-200) c_cam_init_free=0;
    
    } else if (cam_type==2) {
        // de momento la camara sigue al jugador siempre a una distancia
        world.cam_x=-c_cam_offset;
    }
    
    
//    if(game_hit_pressed) {
//        world.cam_x+=10;
//    }
//    if(game_esquivar_pressed) {
//        world.cam_x-=10;
//    }
    
    
    world_update_zones();
    generador_update();
    
    world.t+=game_dt;
    
    achievments_check_update();
}
Beispiel #9
0
static inline void
asc_process_read(struct conn *c, struct token *token, int ntoken)
{
    rstatus_t status;
    char *key;
    size_t nkey;
    unsigned valid_key_iter = 0;
    struct item *it;
    struct token *key_token;
    bool return_cas;

    if (!asc_ntoken_valid(c, ntoken)) {
        log_hexdump(LOG_INFO, c->req, c->req_len, "client error on c %d for "
                    "req of type %d with %d invalid tokens", c->sd,
                    c->req_type, ntoken);
        asc_write_client_error(c);
        return;
    }

    return_cas = (c->req_type == REQ_GETS) ? true : false;
    key_token = &token[TOKEN_KEY];

    do {
        while (key_token->len != 0) {

            key = key_token->val;
            nkey = key_token->len;

            if (nkey > KEY_MAX_LEN) {
                log_debug(LOG_INFO, "client error on c %d for req of type %d "
                          "and %d length key", c->sd, c->req_type, nkey);
                asc_write_client_error(c);
                return;
            }

            if (return_cas) {
                stats_thread_incr(gets);
            } else {
                stats_thread_incr(get);
            }

            it = item_get(key, nkey);
            if (it != NULL) {
                /* item found */
                if (return_cas) {
                    stats_slab_incr(it->id, gets_hit);
                } else {
                    stats_slab_incr(it->id, get_hit);
                }

                if (valid_key_iter >= c->isize) {
                    struct item **new_list;

                    new_list = mc_realloc(c->ilist, sizeof(struct item *) * c->isize * 2);
                    if (new_list != NULL) {
                        c->isize *= 2;
                        c->ilist = new_list;
                    } else {
                        item_remove(it);
                        break;
                    }
                }

                status = asc_respond_get(c, valid_key_iter, it, return_cas);
                if (status != MC_OK) {
                    log_debug(LOG_INFO, "client error on c %d for req of type "
                              "%d with %d tokens", c->sd, c->req_type, ntoken);
                    stats_thread_incr(cmd_error);
                    item_remove(it);
                    break;
                }

                log_debug(LOG_VVERB, ">%d sending key %.*s", c->sd, it->nkey,
                          item_key(it));
                item_update(it);
                *(c->ilist + valid_key_iter) = it;
                valid_key_iter++;
            } else {
                /* item not found */
                if (return_cas) {
                    stats_thread_incr(gets_miss);
                } else {
                    stats_thread_incr(get_miss);
                }
                klog_write(c->peer, c->req_type, key, nkey, 1, 0);
            }

            key_token++;
        }

        /*
         * If the command string hasn't been fully processed, get the next set
         * of token.
         */
        if (key_token->val != NULL) {
            ntoken = asc_tokenize(key_token->val, token, TOKEN_MAX);
            /* ntoken is unused */
            key_token = token;
        }

    } while (key_token->val != NULL);

    c->icurr = c->ilist;
    c->ileft = valid_key_iter;
    if (return_cas) {
        c->scurr = c->slist;
        c->sleft = valid_key_iter;
    }

    log_debug(LOG_VVERB, ">%d END", c->sd);

    /*
     * If the loop was terminated because of out-of-memory, it is not
     * reliable to add END\r\n to the buffer, because it might not end
     * in \r\n. So we send SERVER_ERROR instead.
     */
    if (key_token->val != NULL || conn_add_iov(c, "END\r\n", 5) != MC_OK ||
        (c->udp && conn_build_udp_headers(c) != MC_OK)) {
        log_debug(LOG_DEBUG, "server error on c %d for req of type %d with "
                  "enomem", c->sd, c->req_type);
        asc_write_server_error(c);
    } else {
        conn_set_state(c, CONN_MWRITE);
        c->msg_curr = 0;
    }
}