Ejemplo n.º 1
0
bool Recorder::move(Piece* piece, int x, int y)
{
    Piece* opposingPiece = model_->cell(x, y);
    bool takeMove = false;
    bool takenHadMoved = true;
    QString takenPiece;
    ChessBoardModel::PieceColor takenColor = ChessBoardModel::BLACK;
    if(opposingPiece)
    {
        takeMove = true;
        takenPiece = opposingPiece->objectName();
        takenColor = opposingPiece->color();
        takenHadMoved = opposingPiece->hasMoved();
    }

    int oldX = piece->position().x();
    int oldY = piece->position().y();
    bool hadMoved = piece->hasMoved();
    if(piece->moveTo(QPoint(x, y)))
    {
        Q_ASSERT(!takeMove || takenColor != piece->color());
        MoveAction* moveAction = new MoveAction(piece, oldX, oldY, hadMoved);
        if(takeMove)
            moveAction->setTakenPiece(takenPiece, takenColor, takenHadMoved);
        queueAction(moveAction);
        return true;
    }

    return false;
}
Ejemplo n.º 2
0
Piece* Recorder::create(const QString& name, int x, int y, const QString& colorString)
{
    if(!model_)
    {
        return NULL;
    }
    ChessBoardModel::PieceColor col = colorFromString(colorString);
    Piece* result = model_->create(name, x, y, col);
    if(result)
    {
        queueAction(new CreateAction(name, x, y, col));
    }
    return result;
}
Ejemplo n.º 3
0
void Player::updateLevelFromExperience(const Level& level, bool updatePoints)
{
	auto oldLevel = currentLevel;
	currentLevel = level.getLevelFromExperience(experience);
	expNextLevel = level.getExperienceFromLevel(currentLevel);
	if (updatePoints == true &&
		currentLevel > oldLevel &&
		oldLevel != 0)
	{
		lifeDamage = 0;
		manaDamage = 0;
		Number32 levelUp;
		if (getNumberByHash(ItemProp::LevelUp, {}, levelUp) == true)
		{
			points += levelUp.getUInt32() * (currentLevel - oldLevel);
			queueAction(*class_, str2int16("levelChange"));
		}
	}
}
Ejemplo n.º 4
0
void MadsPlayer::step() {
	if (_visible && _stepEnabled && !_moving && (_direction == _newDirection) && (_madsVm->_currentTimer >= GET_GLOBAL32(2))) {
		if (_actionIndex == 0) {
			int randVal = _vm->_random->getRandomNumber(29999);

			if (GET_GLOBAL(0) == SEX_MALE) {
				switch (_direction) {
				case 1:
				case 3:
				case 7:
				case 9:
					if (randVal < 200) {
						queueAction(-1, 0);
						queueAction(1, 0);
					}
					break;

				case 2:
					if (randVal < 500) {
						for (int i = 0; i < 10; ++i)
							queueAction((randVal < 250) ? 1 : 2, 0);
					} else if (randVal < 750) {
						for (int i = 0; i < 5; ++i)
							queueAction(1, 0);
						queueAction(0, 0);
						for (int i = 0; i < 5; ++i)
							queueAction(2, 0);
					}
					break;

				case 4:
				case 6:
					if (randVal < 500) {
						for (int i = 0; i < 10; ++i)
							queueAction(1, 0);
					}
					break;

				case 5:
				case 8:
					if (randVal < 200) {
						queueAction(-1, 0);
						queueAction(1, 0);
					}
					break;
				}
			}
		}

		SET_GLOBAL32(2, GET_GLOBAL32(2) + 6);
	}

	if (GET_GLOBAL(138) == 1) {
		uint32 diff = _madsVm->_currentTimer - GET_GLOBAL32(142);
		if (diff > 60) {
			SET_GLOBAL32(144, GET_GLOBAL32(144) + 1);
		} else {
			SET_GLOBAL32(144, GET_GLOBAL32(144) + diff);
		}

		SET_GLOBAL32(142, _madsVm->_currentTimer);
	}
}
Ejemplo n.º 5
0
// The main motor control thread
void runMotorControl() {
	if(port.open() < 0) {
		::perror("Failed to start Motor Control");
		return;
	}
	std::this_thread::sleep_for(std::chrono::milliseconds(1000));
	printf("Motor Control thread running\n");
	while(!stop) {
		// Update motor status
		// Write the status command
		char who[3] = {(char)SCMD_ST,(char)1,(char)0};
		port.write(who,3);
		//printf("Motor status requested\n");
		char tmp[2];
		std::this_thread::sleep_for(std::chrono::milliseconds(300));
		// Read response header
		port.read(tmp,2);
		char buff[(uint8_t)tmp[1]];
		// Read response body
		port.read(buff,(uint8_t)tmp[1]);
		// Calc change in time
		::gettimeofday(&this_update,NULL);
		double dt = ((double)(this_update.tv_usec-last_update.tv_usec))/1000000;
		// Update speeds and currents
		int cps;
		short cu;
		for(int i = 0; i < tmp[1]/3; i++) {
			::memcpy(&cps,buff+i*7+1,4);
			::memcpy(&cu,buff+i*7+5,2);
			double speed = convertVelocity(buff[i*7],cps);
			double current = 0.01*(double)cu;
			updateMotorStatus(buff[i*3],speed,current,dt);
		}
		// Update actions
		Action a;
		for(int j = 0; j < (int)actionRunning.size(); j++) {
			a = actionRunning.at(j);
			// Calc average velocity
			double vel_avg;
			for(int i = 0; i < a.num_motors; i++) {
				if(a.direction[i]) {
					vel_avg += stats.find(a.motor[i])->second.speed_inst;
				} else {
					vel_avg -= stats.find(a.motor[i])->second.speed_inst;
				}
			}
			vel_avg /= a.num_motors;
			a.speed_rt = vel_avg;
			// If not measuring distance, skip
			if(!a.use_dist) {
				continue;
			}
			// Calc distance left
			a.distance_remaining -= vel_avg*dt;
			// If finished, set to done and erase
			if(a.distance_remaining < 0) {
				// Mark as done
				a.status = STAT_ACTION_DONE;
				// Create stop action
				Action stop;
				::memcpy(&stop,&a,sizeof(Action));
				stop.speed = 0;
				stop.use_dist = false;
				stop.ovr = true;
				queueAction(stop);
				// Erase
				actionRunning.erase(actionRunning.begin()+j);
			}
		}
		// Set last time
		::memcpy(&last_update,&this_update,sizeof(::timeval));
		// Run the queue
		// Wait for queue to be unlocked
		while(queue_lock) {}
		queue_lock = true;
		// Run queue of actions
		while(!actionQueue.empty()) {
			printf("Dequeuing action\n");
			Action tmp = actionQueue.front();
			// If it does not override and conflicts with running actions, skip
			if(!tmp.ovr && conflictRunning(tmp)) {
				tmp.status = STAT_ACTION_NORUN;
				actionQueue.pop();
				continue;
			}
			// Stage for running and execute
			tmp.status = STAT_ACTION_TORUN;

			actionQueue.pop();
			printf("2\n");

			removeConflictingRunning(tmp);
			printf("1\n");
			execute(tmp);
			if(tmp.status != STAT_ACTION_FAIL) {
				actionRunning.push_back(tmp);
			}
		}
		queue_lock = false;
	}
	running = false;
	printf("Motor Control has stopped\n");
}
Ejemplo n.º 6
0
void FiniteTimeActionComponent::queueMoveTo(float duration_s, float x, float y, std::function<void()> && callback /*= nullptr*/)
{
	queueAction(cocos2d::MoveTo::create(duration_s, { x, y }), std::move(callback));
}
Ejemplo n.º 7
0
void FiniteTimeActionComponent::queueDelay(float delay_s, std::function<void()> && callback /*= nullptr*/)
{
	assert(delay_s > 0.0f && "SequentialInvoker::addDelay() with time <= 0.");

	queueAction(cocos2d::DelayTime::create(delay_s), std::move(callback));
}
Ejemplo n.º 8
0
void FiniteTimeActionComponent::queueCallback(std::function<void()> && callback)
{
	queueAction(cocos2d::CallFunc::create(callback));
}
Ejemplo n.º 9
0
void FiniteTimeActionComponent::queueBlink(float duration_s, int blinkCount, Callback && callback /*= nullptr*/)
{
	queueAction(cocos2d::Blink::create(duration_s, blinkCount), std::move(callback));
}