vector<PlayfieldNode> TetrisAI::generate(PlayfieldNode node, int ttype) { vector<PlayfieldNode> generatedNode; vector<PlayfieldPosition> generatedPosition; // Finding valid ending positions Tetromino testTetromion; testTetromion.generate(ttype); PlayfieldPosition spawn = getSpawnPosition(ttype); int uniqueRotation = getUniqueRotation(ttype); // Rotation for (int rotation = 0; rotation < uniqueRotation; rotation++) { // x Position for (int xModifier = -5; xModifier <= 5; xModifier++) { // Check Edge if (isInBound(testTetromion, spawn.x + (double) xModifier)) { double xPos = spawn.x + (double) xModifier; double yPos = getProjectedY(node, testTetromion, xPos); generatedPosition.push_back( PlayfieldPosition(xPos, yPos, rotation, ttype)); } } testTetromion.clock(); } // Generating node from those positions for (vector<PlayfieldPosition>::iterator it = generatedPosition.begin(); it != generatedPosition.end(); it++) { generatedNode.push_back(lock(node, *it)); } return generatedNode; }
double TetrisAI::getProjectedY(PlayfieldNode node, Tetromino tetromino, double xPos) { PlayfieldPosition spawn = getSpawnPosition(tetromino.getTType()); double projected = spawn.y; while (!checkCollision(node, tetromino, xPos, projected + 1)) { projected++; } return projected; }
void TetrisAI::search() { // Beam Search mSearchTimer.start(); mReady = false; mNewThread = false; multimap<int, PlayfieldNode> nodeMap; multimap<int, PlayfieldNode> newNodeMap; nodeMap.insert(make_pair(grade(mCurrentNode), mCurrentNode)); // Queue for (vector<int>::iterator it = mTetrominoQueue.begin(); it != mTetrominoQueue.begin() + mSearchDepth; it++) { // Current Map newNodeMap.clear(); for (multimap<int, PlayfieldNode>::iterator nit = nodeMap.begin(); nit != nodeMap.end(); nit++) { // New Node vector<PlayfieldNode> newSet = generate(nit->second, *it); // Adding to new Map for (vector<PlayfieldNode>::iterator vit = newSet.begin(); vit != newSet.end(); vit++) { newNodeMap.insert(make_pair(grade(*vit), *vit)); } } if (newNodeMap.size() > SEARCH_BEAM_WIDTH) { // Beaming multimap<int, PlayfieldNode>::iterator beamingIt = newNodeMap.begin(); advance(beamingIt, newNodeMap.size() - SEARCH_BEAM_WIDTH); newNodeMap.erase(newNodeMap.begin(), beamingIt); } nodeMap = newNodeMap; } // Best node is last node multimap<int, PlayfieldNode>::iterator bestNodeIt = nodeMap.end(); advance(bestNodeIt, -1); PlayfieldNode bestNode = bestNodeIt->second; // Constructing inputs vector<int> commands; PlayfieldPosition instruction = bestNode.mTetrominoPositions.at(0); for (int i = 0; i < instruction.rotation; i++) { commands.push_back(INPUT_CLOCKWISE); } PlayfieldPosition spawn = getSpawnPosition(instruction.ttype); int relativeX = instruction.x - spawn.x; int directionInput = INPUT_RIGHT; if (relativeX < 0) { directionInput = INPUT_LEFT; } for (int i = 0; i < abs(relativeX); i++) { commands.push_back(directionInput); } commands.push_back(INPUT_HARDDROP); execute(commands); int searchTime = mSearchTimer.getTicks(); mReady = true; //printf("Thinking time: %i\n", searchTime); }
void AttackableStaticNpc::respawn(void) { // The cell we will spawn in. this->setParentId(getCellIdForSpawn()); // Default spawn position. glm::vec3 position(getSpawnPosition()); // Respawn delay. If the creature have an unique delay, use that. Else use the one provided by the parent object. this->setRespawnDelay(0); if (this->hasInternalAttribute("creature_respawn_delay")) { uint64 respawnDelay = this->getInternalAttribute<uint64>("creature_respawn_delay"); // mRespawnDelay = respawnDelay; this->setRespawnDelay(respawnDelay); } // Let us get the spawn point. // Use the supplied direction? this->mDirection = getSpawnDirection(); if (this->getParentId() == 0) { // Heightmap only works outside. position.y = this->getHeightAt2DPosition(position.x, position.z, true); } this->mPosition = this->getSpawnPosition(); // Default spawn position. // mSpawned = false; if (this->hasInternalAttribute("creature_xp")) { uint32 xp = this->getInternalAttribute<uint32>("creature_xp"); this->setWeaponXp(xp); } else { assert(false); this->setWeaponXp(0); } if (this->hasAttribute("creature_health")) { int32 health = this->getAttribute<int32>("creature_health"); this->mHam.mHealth.setCurrentHitPoints(health); this->mHam.mHealth.setMaxHitPoints(health); this->mHam.mHealth.setBaseHitPoints(health); } else { assert(false); this->mHam.mHealth.setCurrentHitPoints(500); this->mHam.mHealth.setMaxHitPoints(500); this->mHam.mHealth.setBaseHitPoints(500); } if (this->hasAttribute("creature_strength")) { int32 strength = this->getAttribute<int32>("creature_strength"); this->mHam.mStrength.setCurrentHitPoints(strength); this->mHam.mStrength.setMaxHitPoints(strength); this->mHam.mStrength.setBaseHitPoints(strength); } else { assert(false); this->mHam.mStrength.setCurrentHitPoints(500); this->mHam.mStrength.setMaxHitPoints(500); this->mHam.mStrength.setBaseHitPoints(500); } if (this->hasAttribute("creature_constitution")) { int32 constitution = this->getAttribute<int32>("creature_constitution"); this->mHam.mConstitution.setCurrentHitPoints(constitution); this->mHam.mConstitution.setMaxHitPoints(constitution); this->mHam.mConstitution.setBaseHitPoints(constitution); } else { assert(false); this->mHam.mConstitution.setCurrentHitPoints(500); this->mHam.mConstitution.setMaxHitPoints(500); this->mHam.mConstitution.setBaseHitPoints(500); } /* if (this->hasAttribute("creature_action")) { int32 action = this->getAttribute<int32>("creature_action"); this->mHam.mAction.setCurrentHitPoints(action); this->mHam.mAction.setMaxHitPoints(action); this->mHam.mAction.setBaseHitPoints(action); } else { assert(false); this->mHam.mAction.setCurrentHitPoints(500); this->mHam.mAction.setMaxHitPoints(500); this->mHam.mAction.setBaseHitPoints(500); } if (this->hasAttribute("creature_quickness")) { int32 quickness = this->getAttribute<int32>("creature_quickness"); this->mHam.mQuickness.setCurrentHitPoints(quickness); this->mHam.mQuickness.setMaxHitPoints(quickness); this->mHam.mQuickness.setBaseHitPoints(quickness); } else { assert(false); this->mHam.mQuickness.setCurrentHitPoints(500); this->mHam.mQuickness.setMaxHitPoints(500); this->mHam.mQuickness.setBaseHitPoints(500); } if (this->hasAttribute("creature_stamina")) { int32 stamina = this->getAttribute<int32>("creature_stamina"); this->mHam.mStamina.setCurrentHitPoints(stamina); this->mHam.mStamina.setMaxHitPoints(stamina); this->mHam.mStamina.setBaseHitPoints(stamina); } else { assert(false); this->mHam.mStamina.setCurrentHitPoints(500); this->mHam.mStamina.setMaxHitPoints(500); this->mHam.mStamina.setBaseHitPoints(500); } if (this->hasAttribute("creature_mind")) { int32 mind = this->getAttribute<int32>("creature_mind"); this->mHam.mMind.setCurrentHitPoints(mind); this->mHam.mMind.setMaxHitPoints(mind); this->mHam.mMind.setBaseHitPoints(mind); } else { assert(false); this->mHam.mMind.setCurrentHitPoints(500); this->mHam.mMind.setMaxHitPoints(500); this->mHam.mMind.setBaseHitPoints(500); } if (this->hasAttribute("creature_focus")) { int32 focus = this->getAttribute<int32>("creature_focus"); this->mHam.mFocus.setCurrentHitPoints(focus); this->mHam.mFocus.setMaxHitPoints(focus); this->mHam.mFocus.setBaseHitPoints(focus); } else { assert(false); this->mHam.mFocus.setCurrentHitPoints(500); this->mHam.mFocus.setMaxHitPoints(500); this->mHam.mFocus.setBaseHitPoints(500); } if (this->hasAttribute("creature_willpower")) { int32 willpower = this->getAttribute<int32>("creature_willpower"); this->mHam.mWillpower.setCurrentHitPoints(willpower); this->mHam.mWillpower.setMaxHitPoints(willpower); this->mHam.mWillpower.setBaseHitPoints(willpower); } else { assert(false); this->mHam.mWillpower.setCurrentHitPoints(500); this->mHam.mWillpower.setMaxHitPoints(500); this->mHam.mWillpower.setBaseHitPoints(500); } */ this->mHam.calcAllModifiedHitPoints(); // All init is done, just the spawn in the world is left. this->spawn(); }