/** * Moves a NonPlayer according to a random choice. */ void NonPlayer::update() { TCODRandom * gen = TCODRandom::getInstance(); int choice = gen->getInt(0,7); switch (choice) { case 0: moveSouth(1); break; case 1: moveNorth(1); break; case 2: moveEast(1); break; case 3: moveWest(1); break; case 4: moveSouth(1); moveEast(1); break; case 5: moveSouth(1); moveWest(1); break; case 6: moveNorth(1); moveEast(1); break; case 7: moveNorth(1); moveWest(1); break; } }
void TeleporterTrapAi::update(Actor *owner) { for (Actor **iterator = engine.actors.begin(); iterator != engine.actors.end(); iterator++) { Actor *actor = *iterator; if (actor->destructible && actor->x == owner->x && actor->y == owner->y) { // Trap was found owner->hidden = false; // Find a place to teleport them too while (true) { TCODRandom *rng = TCODRandom::getInstance(); int x = rng->getInt(0, engine.map->width); int y = rng->getInt(0, engine.map->height); if (engine.map->canWalk(x, y)) { engine.gui->message(TCODColor::lightGrey, "%s stepped on %s.", actor->name, owner->name); actor->x = x; actor->y = y; break; } } } } }
void tMap::addItem(int x, int y) { TCODRandom *rng = TCODRandom::getInstance(); int roll = rng->getInt(0, 100); if(roll < 70) { tActor* pHealthPotion = new tActor(x,y, '!', "health potion", TCODColor::violet); pHealthPotion->m_Blocks = false; pHealthPotion->m_pPickable = new tHealer(4); engine.m_Actors.push(pHealthPotion); } else if(roll < 80) { tActor* pLightningBolt = new tActor(x,y, '#', "scroll of lightning", TCODColor::lightYellow); pLightningBolt->m_Blocks = false; pLightningBolt->m_pPickable = new tLightningBolt(5,20); engine.m_Actors.push(pLightningBolt); } else if(roll < 90) { tActor* pFireball = new tActor(x,y, '#', "scroll of fireball", TCODColor::lightYellow); pFireball->m_Blocks = false; pFireball->m_pPickable = new tFireball(3,12); engine.m_Actors.push(pFireball); } else { tActor* pConfuse = new tActor(x,y, '#', "scroll of confusion", TCODColor::lightYellow); pConfuse->m_Blocks = false; pConfuse->m_pPickable = new tConfuse(10,8); engine.m_Actors.push(pConfuse); } }
void EntityMap::placeInRandomRoom(Entity *ent) { TCODRandom *rng = new TCODRandom(); int room = rng->getInt( 1, MAX_ROOMS-1); //derp. ent->move(contextMap->rooms[room]->cX, contextMap->rooms[room]->cY); delete rng; }
void HealAura::apply(Actor *target) { TCODRandom *rng = TCODRandom::getInstance(); int realAmount = rng->getInt(bonus*0.8,bonus*1.25); if ( (life == CONTINUOUS && duration == totalDuration) || life == ITERABLE ) { if (bonus > 0) { if (target->destructible) target->destructible->heal(realAmount); } else { if (target->destructible) target->destructible->takeDamage(target,NULL,-1*realAmount); } } }
bool Dungeon::createRooms(int numberOfRooms, int minSize, int maxSize, TileMap *outputMap){ if (numberOfRooms > MAX_ROOMS) return false; else if (minSize < ROOM_MIN_SIZE || maxSize > ROOM_MAX_SIZE) return false; TCODRandom *rng = new TCODRandom(); dungeonMap = outputMap; i = 0; bool test; while(i < numberOfRooms){ w = rng->getInt(minSize, maxSize); h = rng->getInt(minSize, maxSize); // checks for map boundaries x = rng->getInt( 0, MAP_WIDTH - w - 3); y = rng->getInt( 0, MAP_HEIGHT - h - 3); tempRoom = new Room(x, y, w, h); test = false; // if it's our first room, we import it to our test map and move along // now we go through the list of rooms that exist // in our temporary map if (i > 0) for(x=0; x < i; x++){ if(tempRoom->doesIntersect(dungeonMap->rooms[x])) test = true; } if(!test){ dungeonMap->importRoom(tempRoom); i++; } } delete rng; return true; }
void AI::moveRandom(Actor *me) { int dx = 0, dy = 0; int try_count = 4; TCODRandom *rng = TCODRandom::getInstance(); Map *map = engine.getMap(); do { dx = rng->getInt(-1, 1); dy = rng->getInt(-1, 1); try_count--; } while (!map->canWalk(me->getX() + dx, me->getY() + dy) && try_count > 0); if (try_count > 0) me->moveTo(me->getX() + dx, me->getY() + dy); }
void ConfusedMonsterAi::update(Actor *owner) { if(owner->destructible && owner->destructible->isDead()) { return; } bool actorIsPlayer = (owner == engine.player); if(actorIsPlayer) { engine.gameStatus = Engine::NEW_TURN; Sleep(2000); } TCODRandom *rng = TCODRandom::getInstance(); int dx = rng->getInt(-1, 1); int dy = rng->getInt(-1, 1); if(dx != 0 || dy != 0) { int destX = owner->x + dx; int destY = owner->y + dy; if(engine.map->canWalk(destX, destY)) { owner->x = destX; owner->y = destY; } else { Actor *actor = engine.getActor(destX, destY); if(actor) { owner->attacker->attack(owner, actor); } } } nbTurns--; if(nbTurns == 0) { owner->col = oldColor; owner->ai = oldAi; delete this; } }
BadMother::BadMother(std::string name, int age, int x, int y, char repr) : Person(name, age, x, y, repr) { this->xp_value = 100; this->cls_name = "BadMother"; // this->name = name; // this->age = age; // this->x = x; // this->y = y; //set its representation, aka color and char // Representation * new_repr = new Representation; // this->representation = new_repr; // this->representation->repr = repr; TCODRandom* rnd = TCODRandom::getInstance(); float variant = rnd->getFloat(5.0f, 10.0f); //std::cout << (variant/10.0f) << std::endl; this->representation->setFGColor(TCODColor::blue * (variant/10.0f), true, true, true); this->representation->setBGColor(TCODColor::white , true, true, true); this->img_path = get_data_path()+"img/badmother20x20.png"; // Inventory* inventory = new Inventory; // this->inventory = inventory; // this->equipment = new Equipment; // this->equipment->master = this; // is_fighter = true; // combat = new Combat("temp name", 50, this, 't' ); // combat->assign_to_master(this); // has_live_combat = true; // my_tile = NULL; // this->pack_size = 10; this->attrs->health->max_val = 600; this->attrs->health->current_val = 600; this->attrs->damage->max_val = 50; this->attrs->damage->current_val = 50; };
void Map::addMonster(int x, int y) { TCODRandom *rng = TCODRandom::getInstance(); if ( rng->getInt(0,100) < 80 ) { // create an orc Actor *orc = new Actor(x,y,'o',"orc", TCODColor::desaturatedGreen); orc->destructible = new MonsterDestructible(10,10,0,"dead orc"); orc->attacker = new Attacker(3); orc->ai = new MonsterAi(); engine.actors.push(orc); } else { // create a troll Actor *troll = new Actor(x,y,'T',"troll", TCODColor::darkerGreen); troll->destructible = new MonsterDestructible(16,16,1,"troll carcass"); troll->attacker = new Attacker(4); troll->ai = new MonsterAi(); engine.actors.push(troll); } }
void tMap::createRoom(bool first, int x1, int y1, int x2, int y2, bool withActors) { dig (x1,y1,x2,y2); if(withActors == true) { if ( first ) { // put the player in the first room engine.m_pPlayer->m_XPosition = (x1 + x2) / 2; engine.m_pPlayer->m_YPosition = (y1 + y2) / 2; } else { TCODRandom *rng = TCODRandom::getInstance(); int nbMonsters = rng->getInt(0, MAX_ROOM_MONSTERS); while (nbMonsters > 0) { int x = rng->getInt(x1, x2); int y = rng->getInt(y1, y2); if ( canWalk(x,y) ) { addMonster(x,y); } nbMonsters--; } int nbItems = rng->getInt(0, MAX_ROOM_ITEMS); while(nbItems > 0) { int x = rng->getInt(x1,x2); int y = rng->getInt(y1,y2); if(canWalk(x,y)) { addItem(x,y); } nbItems--; } } } }
void Dungeon::connectRooms(TileMap *outputMap){ TileMap *dungeonMap = outputMap; i = dungeonMap->numRooms; TCODRandom *rng = new TCODRandom(); //x = rng->getInt(5, 30); //y = rng->getInt(5, 30); //w = 5 int prev_x, prev_y, new_x, new_y; for ( x = 0; x < i; x++){ if (x != 0){ prev_x = dungeonMap->rooms[x-1]->cX; prev_y = dungeonMap->rooms[x-1]->cY; new_x = dungeonMap->rooms[x]->cX; new_y = dungeonMap->rooms[x]->cY; if ((rng->getInt(0, 1)) == 1){ dungeonMap->createHall(prev_x, new_x, prev_y, 0); dungeonMap->createHall(prev_y, new_y, new_x, 1); } else { dungeonMap->createHall(prev_y, new_y, prev_x, 1); dungeonMap->createHall(prev_x, new_x, new_y, 0); } } } delete rng; }
void ConfusedMonsterAi::update(Actor *owner) { TCODRandom *rng = TCODRandom::getInstance(); int dx = rng->getInt(-1,1); int dy = rng->getInt(-1,1); if (dx != 0 || dy != 0) { int desx = owner->x + dx; int desy = owner->y + dy; if (engine.map->canWalk(desx,desy)) { owner->x = desx; owner->y = desy; } else { Actor *actor = engine.getActor(desx,desy); if (actor) { owner->attacker->attack(owner,actor); } } } nbTurns--; if (nbTurns == 0) { owner->ai = oldAi; delete this; } }
void DamagingAura::apply(Actor *target){ TCODRandom *rng = TCODRandom::getInstance(); int realDamage = rng->getInt(bonus*0.8,bonus*1.25); TCODList<Actor *> inRadius; if (!smart){ engine.getAllActorsInRadius(inRadius,target->x, target->y, radius); } else { engine.getAllActorsInRadius(inRadius,target->x, target->y, radius, SpellCastingConstants::ENEMY, target->hostile); } if (!inRadius.isEmpty()){ for (Actor **iter = inRadius.begin(); iter != inRadius.end(); iter++) { Actor *act1 = *iter; if (act1->destructible) act1->destructible->takeDamage(act1, target, realDamage); } } inRadius.clearAndDelete(); }
void Map::addItem(int x, int y) { TCODRandom *rng = TCODRandom::getInstance(); int dice = rng->getInt(0, 100); if (dice < 70) { // create a health potion Actor *healthPotion = new Actor(x, y, '!', "health potion", TCODColor::violet); healthPotion->blocks = false; healthPotion->pickable = new Healer(4); engine.actors.push(healthPotion); } else if (dice < 70 + 10) { // create a scroll of lightning bolt Actor *scrollOfLightningBolt = new Actor(x, y, '#', "scroll of lightning bolt", TCODColor::lightYellow); scrollOfLightningBolt->blocks = false; scrollOfLightningBolt->pickable = new LightningBolt(5, 20); engine.actors.push(scrollOfLightningBolt); } else if (dice < 70 + 10 + 10) { // create a scroll of fireball Actor *scrollOfFireball = new Actor(x, y, '#', "scroll of fireball", TCODColor::lightYellow); scrollOfFireball->blocks = false; scrollOfFireball->pickable = new Fireball(3, 12); engine.actors.push(scrollOfFireball); } else { // create a scroll of confusion Actor *scrollOfConfusion = new Actor(x, y, '#', "scroll of confusion", TCODColor::lightYellow); scrollOfConfusion->blocks = false; scrollOfConfusion->pickable = new Confuser(10, 8); engine.actors.push(scrollOfConfusion); } }
void ConfusedMonsterAi::Update(Actor *owner) { TCODRandom *rng = TCODRandom::getInstance(); int32_t dx = rng->getInt(-1, 1); int32_t dy = rng->getInt(-1, 1); if ((dx != 0) || (dy != 0)) { int32_t destx = owner->x_ + dx; int32_t desty = owner->y_ + dy; if (engine.map_->CanWalk(destx, desty)) { owner->x_ = destx; owner->y_ = desty; } else { Actor *actor = engine.GetActor(destx, desty); if (actor) { owner->attacker_->Attack(owner, actor); } } } nbTurns_--; if (nbTurns_ == 0) { owner->ai_ = oldAi_; delete this; } }
bool Object::onAttack(Creature* guy, int attack, int damage, Weapon* weapon) { TCODRandom rngGauss; rngGauss.setDistribution(TCOD_DISTRIBUTION_GAUSSIAN_RANGE); int hit; std::stringstream msg; if (type == OBJ_DOOR_CLOSED || type == OBJ_DOOR_LOCKED) { // Roll attack against the door; doors have 50 defense hit = util::clamp(attack + 950, 500, 1500); hit = rngGauss.getInt(700, 1300, hit); if (hit >= 1250 || (hit > 1000 && (1250 - hit) * damage >= 2000)) { *this = Object(OBJ_DOOR_BROKEN); if (guy->isControlled()) world.addMessage("You hit the door really hard and destroy it."); } else { int broken = 3 - (hit / 740) - (hit / 790) - (hit / 850); bool didBreak = weapon->breakWeapon(broken); if (guy->isControlled()) { Name wpnName = weapon->getName(); msg << "You bash against the door with " << util::format(FORMAT_YOUR, wpnName.name, wpnName.flags) << "."; world.addMessage(msg.str()); if (didBreak) world.addMessage("You hear a cracking sound."); } } return true; } else { return false; } }
//main body of game int main() { int width = 60; int height = width; int cursorX = 0; int cursorY = 0; std::vector< std::vector<bool> > Universe(height+2, std::vector<bool>(width+2)); std::vector< std::vector<bool> > Next(height+2, std::vector<bool>(width+2)); TCODConsole::initRoot(width+12, height, "Life", false); TCODRandom rnd = TCODRandom(); int rndVal = 0; bool hidden = false; int time = 0; //render initial state render(cursorX,cursorY,Universe,hidden,time); //keeps game going while ( !TCODConsole::isWindowClosed() ) { TCOD_key_t key; //check if key has been pressed TCODSystem::checkForEvent(TCOD_EVENT_KEY_PRESS, &key,NULL); switch(key.vk) { //up case TCODK_UP : cursorY = (cursorY - 1); break; //down case TCODK_DOWN : cursorY = (cursorY + 1); break; //left case TCODK_LEFT : cursorX = (cursorX - 1); break; //right case TCODK_RIGHT : cursorX = (cursorX + 1); break; //not a special character case TCODK_CHAR : //flip cell if(key.c == 'f'){ if(Universe[cursorX+1][cursorY+1]) Universe[cursorX+1][cursorY+1] = false; else Universe[cursorX+1][cursorY+1] = true; time = 0; } //step time else if(key.c == 's'){ update(Universe, Next); time++; } //clear Universe else if(key.c == 'c'){ for (int I = 1; I <= width; I++){ for (int J =1; J <=height; J++){ Universe[I][J] = false; } } time = 0; } //randomly fill Universe else if(key.c == 'r'){ for (int I = 1; I <= width; I++){ for (int J =1; J <=height; J++){ rndVal = rnd.getInt(1,100); if (rndVal>50) Universe[I][J] = false; else Universe[I][J] = true; } } time = 0; } //hide cursor else if(key.c == 'h'){ if(hidden) hidden = false; else hidden = true; } break; //default default:break; } //handle cursor wrapping (% was giving issues when going to -1) if (cursorY >= height) cursorY = cursorY - height; else if (cursorY < 0) cursorY = cursorY + height; if (cursorX >= width) cursorX = cursorX - width; else if (cursorX < 0) cursorX = cursorX + width; //render current state render(cursorX,cursorY,Universe,hidden,time); //flush the screen buffer TCODConsole::flush(); } return 0; }