Exemplo n.º 1
0
void Ship :: update (WorldInterface& r_world)
{
    if (isUnitAiSet())
    {
        if (!desired_velocity.isZero())
        {
            double theta = max_rotation_rate * TimeSystem::getFrameDuration();
            if(theta > 0.0)
            {
                rotateTowards(desired_velocity.getNormalized(), theta);
            }
            
            double delta = max_acceleration * TimeSystem::getFrameDuration();
            if(delta > 0.0)
            {
                double currentSpeed = getSpeed();
                double desiredSpeed = desired_velocity.getNorm();
                
                if (currentSpeed < desiredSpeed)
                {
                    currentSpeed += delta;
                    if (currentSpeed > desiredSpeed) currentSpeed = desiredSpeed;
                    setSpeed(currentSpeed);
                }
                else if (currentSpeed > desiredSpeed)
                {
                    currentSpeed -= delta;
                    if (currentSpeed < desiredSpeed) currentSpeed = desiredSpeed;
                    setSpeed(currentSpeed);
                }
            }
        }
        
        if (wantsToFire && isReloaded())
        {
            r_world.addBullet(getPosition(), getForward(), getId());
            wantsToFire = false;
            markReloading();
        }
    }
    
	if(isAlive() && isDying())
	{
		r_world.addExplosion(getPositionPrevious(), getRadius() * EXPLOSION_SIZE_FACTOR, 0);
		m_is_dead = true;

		// no further updates
		assert(invariant());
		return;
	}

	if(isAlive())
	{
		updateBasic();  // moves the Ship

		m_reload_timer += TimeSystem::getFrameDuration();
	}

	assert(invariant());
}
Exemplo n.º 2
0
void NPC::update (uint32_t deltaTime)
{
	IEntity::update(deltaTime);

	if (isDying())
		return;

	if (_dazedTime > 0 && _time - _dazedTime > _dazedTimeout) {
		if (_lastDirectionRight)
			setAnimationType(Animations::ANIMATION_WAKEUP_RIGHT);
		else
			setAnimationType(Animations::ANIMATION_WAKEUP_LEFT);

		const int length = SpriteDefinition::get().getAnimationLength(_type, getAnimationType());
		if (length > 0) {
			TimeManager& t = _map.getTimeManager();
			_idleTimer = t.setTimeout(length, this, &NPC::setIdle);
		}
		_dazedTime = 0;
	}

	if (isMoving()) {
		const float xPos = getPos().x;
		static const float gap = 0.1f;
		if (Between(xPos, _targetPos.x - gap, _targetPos.x + gap)) {
			// target reached
			setIdle();
		}
	} else if (isDazed() || isIdle()) {
		setLinearVelocity(b2Vec2_zero);
	}
}
Exemplo n.º 3
0
bool NPC::shouldCollide (const IEntity* entity) const
{
	if (isDying()) {
		return false;
	}

	if (isDazed()) {
		return entity->isSolid() || entity->isWater();
	}

	if (entity->isWater()) {
		return true;
	}

	if (isFalling()) {
		return entity->isWater();
	}

	if (entity->isPlayer()) {
		const Player* player = static_cast<const Player*>(entity);
		return !player->isCrashed();
	}

	return entity->isSolid();
}
Exemplo n.º 4
0
void Layout::sweep()
{
    assert(isDying());
    assert(parent_ != this);
    if (parent_) {
        assert(parent_->hasChild(this));
        if (!parent_->isDying())
            parent_->removeChild(this);
    }
}
Exemplo n.º 5
0
Arquivo: search.c Projeto: Glank/chess
void* searchMain(void* args){
    SearchThread* self = (SearchThread*)args;
    ChessHNode* start = ChessHNode_new(NULL, self->engine);
    move_t tempLine[MAX_LINE_LENGTH];
    int tempLineLength, eval, depth, i;
    for(depth = 0; depth<MAX_DEPTH && !isDying(self); depth++){
        eval = depthSearch(self, start, depth, tempLine, &tempLineLength);
        if(!isDying(self)){
            ChessMutex_lock(self->bestLineMutex);
            self->bestLineLength = tempLineLength;
            self->bestLineValue = eval;
            for(i=0; i<tempLineLength; i++)
                self->bestLine[i] = tempLine[i];
            if(self->printEachNewLine)
                SearchThread_printBestLine(self);
            ChessMutex_unlock(self->bestLineMutex);
        }
    }
    ChessHNode_delete(start);
    return NULL;
}
Exemplo n.º 6
0
void PlayerServer::dying(PlayerServer *killer)
{
	TRIEV(t_PreDying);
	if (!isDying())
		return;
	int pos = game->currentPosition;
	do 
	{
		PlayerServer* player = static_cast<PlayerServer*>(game->players[pos]);
		if (player->alive) {
			bool canSave = true;
			player->TRIEV(t_CanSave, &canSave, this);
			if (canSave) {
				while (isDying()) {
					unique_ptr<Card> card;
					if (player == this)
						card = move(player->needUseCard(CardProperty_Tao | CardProperty_Jiu));
					else
						card = move(player->needUseCard(CardProperty_Tao));
					if (card) {
						int addhp = 1;
						TRIEV(t_AddHpPoint, &addhp, &card, &player);
						setHp(hp + addhp);
						GAME->addToDeadwood(card);
						if (!isDying()) {
							return;
						}
					}
					else
						break;
				}
			}
		}
		pos++;
		if (pos > (int)game->players.size()) {
			pos = 0;
		}
	} while (pos!=game->currentPosition);
	died(killer);
}
Exemplo n.º 7
0
void Bullet :: update (WorldInterface& r_world)
{
	// explode if out of lifespan (or markDead(false) was called)
	if(isAlive() && isDying())
	{
		r_world.addExplosion(getPosition(), EXPLOSION_SIZE, 0);
		m_is_dead = true;

		// no further updates
		return;
	}

	if (isAlive())
		updateBasic();  // moves the Bullet
}
Exemplo n.º 8
0
void NPC::onContact (b2Contact* contact, IEntity* entity)
{
	IEntity::onContact(contact, entity);

	if (!isDying())
		return;

	if (!entity->isBorder())
		return;

	Border *b = static_cast<Border*>(entity);
	const float y = getPos().y;
	if (b->isBottom() || y > b->getPos().y) {
		_remove = true;
	}
}
Exemplo n.º 9
0
	void Turret::onImpact(DamageType::Type damage_type, float damage, const float3 &force, EntityRef source) {
		float resistance = 0.7f;
		damage *= 1.0f - resistance;

		if(isDying()) {
			m_hit_points -= int(damage);
			return;
		}
			
		m_hit_points -= int(damage);
		if(m_hit_points <= 0.0f) {
			DeathId::Type death_id = deathType(damage_type, damage, force);
			setOrder(new DieOrder(death_id), true);
			onKill(ref(), source);
		}
		else {
			//TODO: sound
		}
	
		ThinkingEntity::onImpact(damage_type, damage, force, source);
	}
Exemplo n.º 10
0
void QmlCppEngine::slaveEngineStateChanged
    (DebuggerEngine *slaveEngine, const DebuggerState newState)
{
    DebuggerEngine *otherEngine = (slaveEngine == m_cppEngine)
         ? m_qmlEngine : m_cppEngine;

    QTC_CHECK(otherEngine != slaveEngine);

    if (debug) {
        EDEBUG("GOT SLAVE STATE: " << slaveEngine << newState);
        EDEBUG("  OTHER ENGINE: " << otherEngine << otherEngine->state());
        EDEBUG("  COMBINED ENGINE: " << this << state() << isDying());
    }

    // Idea is to follow the state of the cpp engine, except where we are stepping in QML.
    // That is, when the QmlEngine moves between InferiorStopOk, and InferiorRunOk, InferiorStopOk ...
    //
    // Accordingly, the 'active engine' is the cpp engine until the qml engine enters the
    // InferiorStopOk state. The cpp engine becomes the active one again as soon as it itself enters
    // the InferiorStopOk state.

    if (slaveEngine == m_cppEngine) {
        switch (newState) {
        case DebuggerNotReady: {
            // Can this ever happen?
            break;
        }
        case EngineSetupRequested: {
            // set by queueSetupEngine()
            QTC_ASSERT(state() == EngineSetupRequested, qDebug() << state());
            break;
        }
        case EngineSetupFailed: {
            qmlEngine()->quitDebugger();
            notifyEngineSetupFailed();
            break;
        }
        case EngineSetupOk: {
            notifyEngineSetupOk();
            break;
        }
        case InferiorSetupRequested: {
            // set by queueSetupInferior()
            QTC_ASSERT(state() == InferiorSetupRequested, qDebug() << state());
            break;
        }
        case InferiorSetupFailed: {
            qmlEngine()->quitDebugger();
            notifyInferiorSetupFailed();
            break;
        }
        case InferiorSetupOk: {
            notifyInferiorSetupOk();
            break;
        }
        case EngineRunRequested: {
            // set by queueRunEngine()
            break;
        }
        case EngineRunFailed: {
            qmlEngine()->quitDebugger();
            notifyEngineRunFailed();
            break;
        }
        case InferiorUnrunnable: {
            qmlEngine()->quitDebugger();
            notifyInferiorUnrunnable();
            break;
        }
        case InferiorRunRequested: {
            // might be set already by notifyInferiorRunRequested()
            QTC_ASSERT(state() == InferiorRunRequested
                       || state() == InferiorStopOk, qDebug() << state());
            if (state() != InferiorRunRequested)
                notifyInferiorRunRequested();
            break;
        }
        case InferiorRunOk: {
            QTC_ASSERT(state() == EngineRunRequested
                       || state() == InferiorRunRequested, qDebug() << state());
            if (state() == EngineRunRequested)
                notifyEngineRunAndInferiorRunOk();
            else if (state() == InferiorRunRequested)
                notifyInferiorRunOk();

            if (qmlEngine()->state() == InferiorStopOk) {
                // track qml engine again
                setState(InferiorStopRequested);
                notifyInferiorStopOk();
                setActiveEngine(m_qmlEngine);
            }
            break;
        }
        case InferiorRunFailed: {
            qmlEngine()->quitDebugger();
            notifyInferiorRunFailed();
            break;
        }
        case InferiorStopRequested: {
            if (m_activeEngine == cppEngine()) {
                // might be set by doInterruptInferior()
                QTC_ASSERT(state() == InferiorStopRequested
                           || state() == InferiorRunOk, qDebug() << state());
                if (state() == InferiorRunOk)
                    setState(InferiorStopRequested);
            } else {
                // we're debugging qml, but got an interrupt, or abort
                QTC_ASSERT(state() == InferiorRunOk
                          || state() == InferiorStopOk
                           || state() == InferiorRunRequested, qDebug() << state());

                if (state() == InferiorRunOk) {
                    setState(InferiorStopRequested);
                } else if (state() == InferiorStopOk) {
                    notifyInferiorRunRequested();
                    notifyInferiorRunOk();
                    setState(InferiorStopRequested);
                } else if (state() == InferiorRunRequested) {
                    notifyInferiorRunOk();
                    setState(InferiorStopRequested);
                }
                // now track cpp engine
                setActiveEngine(m_cppEngine);
            }
            break;
        }
        case InferiorStopOk: {
            if (isDying()) {
                EDEBUG("... CPP ENGINE STOPPED DURING SHUTDOWN ");
                QTC_ASSERT(state() == InferiorStopRequested
                           || state() == InferiorRunOk
                           || state() == InferiorStopOk, qDebug() << state());

                // Just to make sure, we're shutting down anyway ...
                setActiveEngine(m_cppEngine);

                if (state() == InferiorStopRequested)
                    setState(InferiorStopOk);
                // otherwise we're probably inside notifyInferiorStopOk already
            } else {
                if (m_activeEngine != cppEngine()) {
                    showStatusMessage(tr("C++ debugger activated"));
                    setActiveEngine(m_cppEngine);
                }

                QTC_ASSERT(state() == InferiorStopRequested
                           || state() == InferiorRunRequested
                           || state() == EngineRunRequested
                           || state() == InferiorRunOk
                           || state() == InferiorStopOk, qDebug() << state());
                switch (state()) {
                case InferiorStopRequested:
                    EDEBUG("... CPP ENGINE STOPPED EXPECTEDLY");
                    notifyInferiorStopOk();
                    break;
                case EngineRunRequested:
                    EDEBUG("... CPP ENGINE STOPPED ON STARTUP");
                    notifyEngineRunAndInferiorStopOk();
                    break;
                case InferiorRunOk:
                    EDEBUG("... CPP ENGINE STOPPED SPONTANEOUSLY");
                    notifyInferiorSpontaneousStop();
                    break;
                case InferiorRunRequested:
                    // can happen if qml engine was active
                    notifyInferiorRunFailed();
                default:
                    break;
                }
            }
            break;
        }
        case InferiorStopFailed: {
            QTC_ASSERT(state() == InferiorStopRequested, qDebug() << state());
            notifyInferiorStopFailed();
            break;
        }
        case InferiorExitOk: {
            // InferiorExitOk will be called through notifyInferiorExited
            // when InferiorShutDownOk is reached
            qmlEngine()->quitDebugger();
            break;
        }
        case InferiorShutdownRequested: {
            // might be set by queueShutdownInferior() already
            QTC_ASSERT(state() == InferiorShutdownRequested
                       || state() == InferiorStopOk, qDebug() << state());
            if (state() == InferiorStopOk)
                setState(InferiorShutdownRequested);
            qmlEngine()->quitDebugger();
            break;
        }
        case InferiorShutdownFailed: {
            QTC_ASSERT(state() == InferiorShutdownRequested, qDebug() << state());
            notifyInferiorShutdownFailed();
            break;
        }
        case InferiorShutdownOk: {
            if (state() == InferiorShutdownRequested) {
                notifyInferiorShutdownOk();
            } else {
                // we got InferiorExitOk before, but ignored it ...
                notifyInferiorExited();
            }
            break;
        }
        case EngineShutdownRequested: {
            // set by queueShutdownEngine()
            QTC_ASSERT(state() == EngineShutdownRequested, qDebug() << state());
            break;
        }
        case EngineShutdownFailed: {
            QTC_ASSERT(state() == EngineShutdownRequested, qDebug() << state());
            notifyEngineShutdownFailed();
            break;
        }
        case EngineShutdownOk: {
            QTC_ASSERT(state() == EngineShutdownRequested, qDebug() << state());
            notifyEngineShutdownOk();
            break;
        }
        case DebuggerFinished: {
            // set by queueFinishDebugger()
            QTC_ASSERT(state() == DebuggerFinished, qDebug() << state());
            break;
        }
        }
    } else {
        // QML engine state change
        if (newState == InferiorStopOk) {
            if (isDying()) {
                EDEBUG("... QML ENGINE STOPPED DURING SHUTDOWN ");

                // Just to make sure, we're shutting down anyway ...
                setActiveEngine(m_cppEngine);

                if (state() == InferiorStopRequested)
                    notifyInferiorStopOk();
                // otherwise we're probably inside notifyInferiorStopOk already
            } else {
                if (m_activeEngine != qmlEngine()) {
                    showStatusMessage(tr("QML debugger activated"));
                    setActiveEngine(m_qmlEngine);
                }

                QTC_ASSERT(state() == InferiorRunOk
                           || state() == InferiorStopRequested
                           || state() == InferiorShutdownRequested, qDebug() << state());

                if (state() == InferiorRunOk)
                    notifyInferiorSpontaneousStop();
                else if (state() == InferiorStopRequested)
                    notifyInferiorStopOk();
            }

        } else if (newState == InferiorRunOk) {
            if (m_activeEngine == qmlEngine()) {
                QTC_ASSERT(state() == InferiorRunRequested, qDebug() << state());
                notifyInferiorRunOk();
            }
        }
    }
}
Exemplo n.º 11
0
Arquivo: search.c Projeto: Glank/chess
int alphabeta(
    SearchThread* self, ChessHNode* node, 
    int depth, int quiecense, int deepQuiecense,
    int alpha, int beta,
    move_t* lineout, int* lineoutLength){
    TNode* tnode = TTable_lookup(self->table, node);
    if(tnode!=NULL && tnode->depth>=(depth+quiecense+deepQuiecense)){
        (*lineoutLength) = 0;
        if(tnode->evaluation==INT_MIN)
            return INT_MIN+100*node->halfMoveNumber+depth;
        if(tnode->evaluation==INT_MAX)
            return INT_MAX-100*(node->halfMoveNumber+depth);
        return tnode->evaluation;
    }
    if(isDying(self)){
        (*lineoutLength) = 0;
        return 0;
    }
    if(node->type==ABSOLUTE){
        (*lineoutLength) = 0;
        return node->evaluation;
    }
    //quiecense extensions
    if(depth==0){
        int delta = (node->evaluation)-(node->parent->evaluation);
        delta = delta<0?-delta:delta;
        if(node->inCheck)
            delta = INT_MAX;
        else if(node->parent != NULL && node->parent->inCheck)
            delta = INT_MAX;
        if(delta>=100 && quiecense){
            depth++; //not quiet
            quiecense--;
        }
        else if(delta>=200 && deepQuiecense){
            depth++; //not even kindof quiet
            deepQuiecense--;
        }
        else{
            *lineoutLength = 0;
            return node->evaluation;
        }
    }
    //expand
    ChessHNode_expand(node, self->engine);
    //if terminal
    if(node->childrenCount==0){
        ChessHNode_deleteChildren(node);
        *lineoutLength = 0;
        return node->evaluation;
    }
    //else, sort
    TTable_sortChildren(self->table, node);

    int i, best=0;
    ChessHNode* child;
    TNode tnew;
    tnew.depth = depth-1;
    tnew.halfMoveNumber = node->halfMoveNumber+1;
    tnew.isCut = 0;
    move_t lines[node->childrenCount][depth+quiecense+deepQuiecense];
    int lineLengths[node->childrenCount];
    //if maximizing player
    if(node->toPlay==WHITE){
        for(i=0; !tnew.isCut && i<node->childrenCount; i++){
            child = node->children[i];
            tnew.hash = child->hash;
            ChessBoard_makeMove(self->board, child->move);
            tnew.evaluation = alphabeta(self, child, depth-1, quiecense, deepQuiecense, 
                alpha, beta, lines[i]+1, lineLengths+i);
            ChessBoard_unmakeMove(self->board);
            if(tnew.evaluation>alpha){
                alpha = tnew.evaluation;
                best = i;
            }
            if(beta < alpha)
                tnew.isCut=1;
            if(!isDying(self))
                TTable_trySave(self->table, &tnew);
        }
        lineout[0] = node->children[best]->move;
        *lineoutLength = lineLengths[best]+1;
        for(i=1; i < *lineoutLength; i++)
            lineout[i] = lines[best][i];
        ChessHNode_deleteChildren(node);
        return alpha;
    }
    //minimizing player
    else{
        for(i=0; !tnew.isCut && i<node->childrenCount; i++){
            child = node->children[i];
            tnew.hash = child->hash;
            ChessBoard_makeMove(self->board, child->move);
            tnew.evaluation = alphabeta(self, child, depth-1, quiecense, deepQuiecense, 
                alpha, beta, lines[i]+1, lineLengths+i);
            ChessBoard_unmakeMove(self->board);
            if(tnew.evaluation<beta){
                beta = tnew.evaluation;
                best = i;
            }
            if(beta < alpha)
                tnew.isCut=1;
            if(!isDying(self))
                TTable_trySave(self->table, &tnew);
        }
        lineout[0] = node->children[best]->move;
        *lineoutLength = lineLengths[best]+1;
        for(i=1; i < *lineoutLength; i++)
            lineout[i] = lines[best][i];
        ChessHNode_deleteChildren(node);
        return beta;
    }
}
Exemplo n.º 12
0
//TODO clean this shitty code
void Player::move(Map* map){
    if(fireTimer.isStarted() && fireTimer.getTicks() > PLAYER_FIRE_INTERVAL){
        fireTimer.stop();
    }

    //save current player position before trying to change it
    int initPosX = pos.x;
    int initPosY = pos.y;
    int initPlayerSpeed = playerSpeed;

    const Uint8* currentKeyStates = SDL_GetKeyboardState( NULL ); //get pressed key

    //TODO clean following code

    /************ HANDLE X ***************/
    checkX:
    if(!isDying() && currentKeyStates[ SDL_SCANCODE_RIGHT ]) { //right key pressed
        pos.x += playerSpeed;
    }

    if(!isDying() && currentKeyStates[ SDL_SCANCODE_LEFT ]){ //left key pressed
        pos.x -= playerSpeed;
    }
    mCollider.x = pos.x;

    //if end level or collision
    if( ( pos.x < 0 ) || ( pos.x + PLAYER_WIDTH > map->getLevelWidth() ) || map->checkCollision(mCollider, false) ){

        SDL_Rect upCollider = mCollider;
        upCollider.y -= PLAYER_CLIMB_STEP; //retry in case of 1 pixel step

        if( !jumpTimer.isStarted() && pos.x >= 0 && pos.x + PLAYER_WIDTH < map->getLevelWidth() && !map->checkCollision(upCollider, false) ){
            pos.y = upCollider.y; //move player up (because of step)
        }else{
            pos.x = initPosX;    //revert position to initPos
            mCollider.x = pos.x;

            if(playerSpeed > 0 && !jumpTimer.isStarted()){
                playerSpeed-=2; //retry with smaller displacement
                goto checkX;
            }
        }
    }

    /************ HANDLE Y ***************/
    playerSpeed = initPlayerSpeed; //save initial displacement

    gravity(map); //(try to) move playing according to gravity

    checkY:

    pos.y += mVelY; //add vertical velocity to player position
    mCollider.y = pos.y;

    //if end level or collision
    if( ( pos.y < 0 ) || ( pos.y + PLAYER_HEIGHT > SCREEN_HEIGHT - MARGIN_BOTTOM ) || map->checkCollision(mCollider, false) ){
        pos.y = initPosY; //revert to initial position
        mCollider.y = pos.y;

        if(jumpTimer.isStarted()){ //if in jump

            if(mVelY < 0){ //if going up
                mVelY = -1 * mVelY; //invert vertical velocity
            }else{ //going down
                jumpTimer.stop(); //end of jump (hit ground)
                playerSpeed=PLAYER_INIT_SPEED;
                goto checkY;
            }
        }else if(mVelY>0){ //if falling
            //mVelY=1;
            if(mVelY >= 1){
                mVelY--;
                goto checkY;
            }else{
                mVelY-=0.2;
                goto checkY;
            }
        }
    }
}