bool AreaSpawnEvent::executeEvent() { for (MonsterSpawn* spawn : m_spawnList) { uint32_t amount = uniform_random(spawn->minAmount, spawn->maxAmount); for (uint32_t i = 0; i < amount; ++i) { Monster* monster = Monster::createMonster(spawn->name); if (!monster) { std::cout << "[Error - AreaSpawnEvent::executeEvent] Can't create monster " << spawn->name << std::endl; return false; } bool success = false; for (int32_t tries = 0; tries < MAXIMUM_TRIES_PER_MONSTER; tries++) { Position pos(uniform_random(m_fromPos.x, m_toPos.x), uniform_random(m_fromPos.y, m_toPos.y), uniform_random(m_fromPos.z, m_toPos.z)); Tile* tile = g_game.getTile(pos); if (tile && !tile->isMoveableBlocking() && !tile->hasFlag(TILESTATE_PROTECTIONZONE) && tile->getTopCreature() == nullptr && g_game.placeCreature(monster, pos, false, true)) { success = true; break; } } if (!success) { delete monster; } } } return true; }
static cairo_time_t do_wide_fills (cairo_t *cr, int width, int height, int loops) { int count; /* lots and lots of overlapping stroke-like fills */ state = 0xc0ffee; for (count = 0; count < 1000; count++) { cairo_save (cr); cairo_translate (cr, uniform_random (0, width), uniform_random (0, height)); cairo_rotate (cr, uniform_random (-M_PI,M_PI)); cairo_rectangle (cr, 0, 0, uniform_random (0, width), 5); cairo_restore (cr); } cairo_perf_timer_start (); while (loops--) cairo_fill_preserve (cr); cairo_perf_timer_stop (); cairo_new_path (cr); return cairo_perf_timer_elapsed (); }
BlockType_t Creature::blockHit(Creature* attacker, CombatType_t combatType, int32_t& damage, bool checkDefense /* = false */, bool checkArmor /* = false */, bool /* field = false */) { BlockType_t blockType = BLOCK_NONE; if (isImmune(combatType)) { damage = 0; blockType = BLOCK_IMMUNITY; } else if (checkDefense || checkArmor) { bool hasDefense = false; if (blockCount > 0) { --blockCount; hasDefense = true; } if (checkDefense && hasDefense) { int32_t defense = getDefense(); damage -= uniform_random(defense / 2, defense); if (damage <= 0) { damage = 0; blockType = BLOCK_DEFENSE; checkArmor = false; } } if (checkArmor) { int32_t armorValue = getArmor(); if (armorValue > 1) { double armorFormula = armorValue * 0.475; int32_t armorReduction = static_cast<int32_t>(std::ceil(armorFormula)); damage -= uniform_random( armorReduction, armorReduction + static_cast<int32_t>(std::floor(armorFormula)) ); } else if (armorValue == 1) { --damage; } if (damage <= 0) { damage = 0; blockType = BLOCK_ARMOR; } } if (hasDefense && blockType != BLOCK_NONE) { onBlockHit(); } } if (attacker) { attacker->onAttackedCreature(this); attacker->onAttackedCreatureBlockHit(blockType); } onAttacked(); return blockType; }
int main() { std::vector<float> a(10); uniform_random(a,0,10); for (auto el : a) { std::cout << el << " "; } std::cout << '\n'; std::vector<int> b(10); uniform_random(b,0,10); for (auto el : b) { std::cout << el << " "; } }
BlockType_t Creature::blockHit(Creature* attacker, CombatType_t combatType, int32_t& damage, bool checkDefense /* = false */, bool checkArmor /* = false */, bool /* field = false */) { BlockType_t blockType = BLOCK_NONE; if (isImmune(combatType)) { damage = 0; blockType = BLOCK_IMMUNITY; } else if (checkDefense || checkArmor) { bool hasDefense = false; if (blockCount > 0) { --blockCount; hasDefense = true; } if (checkDefense && hasDefense && canUseDefense) { int32_t defense = getDefense(); damage -= uniform_random(defense / 2, defense); if (damage <= 0) { damage = 0; blockType = BLOCK_DEFENSE; checkArmor = false; } } if (checkArmor) { int32_t armor = getArmor(); if (armor > 3) { damage -= uniform_random(armor / 2, armor - (armor % 2 + 1)); } else if (armor > 0) { --damage; } if (damage <= 0) { damage = 0; blockType = BLOCK_ARMOR; } } if (hasDefense && blockType != BLOCK_NONE) { onBlockHit(); } } if (attacker) { attacker->onAttackedCreature(this); attacker->onAttackedCreatureBlockHit(blockType); } onAttacked(); return blockType; }
/** * Randomly sets the values of the quaternion to random values. */ void quaternion_t::random( ) { double s = uniform_random(); double roll1 = sqrt(1-s); double roll2 = sqrt(s); double theta1 = 2*PRX_PI*uniform_random(); double theta2 = 2*PRX_PI*uniform_random(); q[0] = sin(theta1) * roll1; q[1] = cos(theta1) * roll1; q[2] = sin(theta2) * roll2; q[3] = cos(theta2) * roll2; }
static cairo_time_t do_curve_stroke (cairo_t *cr, int width, int height, int loops) { state = 0xc0ffee; cairo_set_line_width (cr, 2.); cairo_perf_timer_start (); cairo_perf_set_thread_aware (cr, FALSE); while (loops--) { if (loops == 0) cairo_perf_set_thread_aware (cr, TRUE); double x1 = uniform_random (0, width); double x2 = uniform_random (0, width); double x3 = uniform_random (0, width); double y1 = uniform_random (0, height); double y2 = uniform_random (0, height); double y3 = uniform_random (0, height); cairo_move_to (cr, uniform_random (0, width), uniform_random (0, height)); cairo_curve_to (cr, x1, y1, x2, y2, x3, y3); cairo_stroke(cr); } cairo_perf_timer_stop (); return cairo_perf_timer_elapsed (); }
bool apc_sampler_t::sample_inside(state_t* result_point, const util::vector_t& min_bounds, const util::vector_t& max_bounds) { config_t grasp_config; for( unsigned i = 0; i < max_tries; ++i ) { grasp_config.set_position_at(0, uniform_random(min_bounds[0], max_bounds[0])); grasp_config.set_position_at(1, uniform_random(min_bounds[1], max_bounds[1])); grasp_config.set_position_at(2, uniform_random(min_bounds[2], max_bounds[2])); double angle = uniform_random(-theta_error, theta_error); double qx = sin(angle); double qy = cos(angle); double qz = 0; angle = uniform_random(1.57 - theta_error, 1.57 + theta_error) / 2.0; qx = qx * sin(angle); qy = qy * sin(angle); qz = qz * sin(angle); double qw = cos(angle); grasp_config.set_orientation(qx, qy, qz, qw); if( IK_base != NULL ) { if(IK_base->has_data()) { std::vector< sim::state_t* > states; IK_base->get_near_neighbors( states , grasp_config, 1); manip_space->copy_point( manip_point, states[0] ); } else { // PRX_WARN_S("Linked IK database has no data."); manip_space->uniform_sample(manip_point); } } else manip_space->uniform_sample(manip_point); if( _manipulator->IK_solver(grasp_config, result_point, gripper_mode, manip_point) ) { result_point->memory[15] = 0; return true; } } return false; }
void ConditionSpeed::addCondition(Creature* creature, const Condition* addCondition) { if (conditionType != addCondition->getType()) { return; } if (ticks == -1 && addCondition->getTicks() > 0) { return; } setTicks(addCondition->getTicks()); const ConditionSpeed& conditionSpeed = static_cast<const ConditionSpeed&>(*addCondition); int32_t oldSpeedDelta = speedDelta; speedDelta = conditionSpeed.speedDelta; mina = conditionSpeed.mina; maxa = conditionSpeed.maxa; minb = conditionSpeed.minb; maxb = conditionSpeed.maxb; if (speedDelta == 0) { int32_t min; int32_t max; getFormulaValues(creature->getBaseSpeed(), min, max); speedDelta = uniform_random(min, max); } int32_t newSpeedChange = (speedDelta - oldSpeedDelta); if (newSpeedChange != 0) { g_game.changeSpeed(creature, newSpeedChange); } }
int32_t WeaponDistance::getElementDamage(const Player* player, const Creature* target, const Item* item) const { if (elementType == COMBAT_NONE) { return 0; } int32_t attackValue = elementDamage; if (item->getWeaponType() == WEAPON_AMMO) { Item* bow = const_cast<Player*>(player)->getWeapon(true); if (bow) { attackValue += bow->getAttack(); } } int32_t attackSkill = player->getSkill(SKILL_DIST, SKILL_LEVEL); float attackFactor = player->getAttackFactor(); int32_t maxValue = Weapons::getMaxWeaponDamage(player->getLevel(), attackSkill, attackValue, attackFactor); if (uniform_random(1, 100) <= g_config.getNumber(ConfigManager::CRITICAL_HIT_CHANCE)) { maxValue *= 2; } int32_t minValue = 0; if (target) { if (target->getPlayer()) { minValue = static_cast<int32_t>(std::ceil(player->getLevel() * 0.1)); } else { minValue = static_cast<int32_t>(std::ceil(player->getLevel() * 0.2)); } } return -normal_random(minValue, static_cast<int32_t>(maxValue * player->getVocation()->distDamageMultiplier)); }
bool Weapon::useFist(Player* player, Creature* target) { const Position& playerPos = player->getPosition(); const Position& targetPos = target->getPosition(); if (Position::areInRange<1, 1>(playerPos, targetPos)) { float attackFactor = player->getAttackFactor(); int32_t attackSkill = player->getSkill(SKILL_FIST, SKILL_LEVEL); int32_t attackValue = 7; int32_t maxDamage = Weapons::getMaxWeaponDamage(player->getLevel(), attackSkill, attackValue, attackFactor); if (uniform_random(1, 100) <= g_config.getNumber(ConfigManager::CRITICAL_HIT_CHANCE)) { maxDamage *= 2; } CombatParams params; params.combatType = COMBAT_PHYSICALDAMAGE; params.blockedByArmor = true; params.blockedByShield = true; CombatDamage damage; damage.primary.type = params.combatType; damage.primary.value = -normal_random(0, maxDamage); Combat::doCombatHealth(player, target, damage, params); if (!player->hasFlag(PlayerFlag_NotGainSkill) && player->getAddAttackSkill()) { player->addSkillAdvance(SKILL_FIST, 1); } return true; } return false; }
bool Npc::getRandomStep(Direction& dir) { std::vector<Direction> dirList; const Position& creaturePos = getPosition(); if (canWalkTo(creaturePos, NORTH)) { dirList.push_back(NORTH); } if (canWalkTo(creaturePos, SOUTH)) { dirList.push_back(SOUTH); } if (canWalkTo(creaturePos, EAST)) { dirList.push_back(EAST); } if (canWalkTo(creaturePos, WEST)) { dirList.push_back(WEST); } if (dirList.empty()) { return false; } std::random_shuffle(dirList.begin(), dirList.end()); dir = dirList[uniform_random(0, dirList.size() - 1)]; return true; }
bool ConditionDamage::init() { if (periodDamage != 0) { return true; } if (damageList.empty()) { setTicks(0); int32_t amount = uniform_random(minDamage, maxDamage); if (amount != 0) { if (startDamage > maxDamage) { startDamage = maxDamage; } else if (startDamage == 0) { startDamage = std::max<int32_t>(1, std::ceil(amount / 20.0)); } std::list<int32_t> list; ConditionDamage::generateDamageList(amount, startDamage, list); for (int32_t value : list) { addDamage(1, tickInterval, -value); } } } return !damageList.empty(); }
void stabilize_manager::stabilize_continuous (u_int32_t t) { stabilize_continuous_tmo = NULL; if (continuous_stabilizing ()) { // stabilizing too fast t = 2 * t; warnx << gettime () << " " << myID <<" stabilize_continuous: slow down " << t << "\n"; } else { // Try to keep t around cts_timer_base if (t >= cts_timer_base) t -= stabilize_decrease_timer; if (t < cts_timer_base/2) t = (int)(stabilize_slowdown_factor * t); for (unsigned int i = 0; i < clients.size (); i++) clients[i]->do_continuous (); } if (t > cts_timer_max) t = cts_timer_max; u_int32_t t1 = uniform_random (0.5 * t, 1.5 * t); u_int32_t sec = t1 / 1000; u_int32_t nsec = (t1 % 1000) * 1000000; continuous_timer = t; stabilize_continuous_tmo = delaycb (sec, nsec, wrap (this, &stabilize_manager::stabilize_continuous, t)); }
bool Npc::getRandomStep(Direction& dir) const { std::vector<Direction> dirList; const Position& creaturePos = getPosition(); if (canWalkTo(creaturePos, DIRECTION_NORTH)) { dirList.push_back(DIRECTION_NORTH); } if (canWalkTo(creaturePos, DIRECTION_SOUTH)) { dirList.push_back(DIRECTION_SOUTH); } if (canWalkTo(creaturePos, DIRECTION_EAST)) { dirList.push_back(DIRECTION_EAST); } if (canWalkTo(creaturePos, DIRECTION_WEST)) { dirList.push_back(DIRECTION_WEST); } if (dirList.empty()) { return false; } dir = dirList[uniform_random(0, dirList.size() - 1)]; return true; }
void Creature::onWalk(Direction& dir) { if (hasCondition(CONDITION_DRUNK)) { uint32_t r = uniform_random(0, 20); if (r <= 4) { switch (r) { case 0: dir = NORTH; break; case 1: dir = WEST; break; case 3: dir = SOUTH; break; case 4: dir = EAST; break; default: break; } g_game.internalCreatureSay(this, SPEAK_MONSTER_SAY, "Hicks!", false); } } }
reply_ptr make_bulk_reply(size_t size) { auto r = std::make_unique<reply>(); r->t = reply::bulk_type; r->bulk = std::vector<char>(size); std::generate(begin(r->bulk), end(r->bulk), []{ return static_cast<char>(uniform_random(0, 0xFF)); }); return r; }
static cairo_time_t draw_random_curve (cairo_t *cr, cairo_fill_rule_t fill_rule, int width, int height, int loops) { double x[3*NUM_SEGMENTS]; double y[3*NUM_SEGMENTS]; int i; cairo_save (cr); cairo_set_source_rgb (cr, 0, 0, 0); cairo_paint (cr); for (i = 0; i < 3*NUM_SEGMENTS; i++) { x[i] = uniform_random (0, width); y[i] = uniform_random (0, height); } state = 0x12345678; cairo_translate (cr, 1, 1); cairo_set_fill_rule (cr, fill_rule); cairo_set_source_rgb (cr, 1, 0, 0); cairo_new_path (cr); cairo_move_to (cr, 0, 0); for (i = 0; i < NUM_SEGMENTS; i++) { cairo_curve_to (cr, x[3*i+0], y[3*i+0], x[3*i+1], y[3*i+1], x[3*i+2], y[3*i+2]); } cairo_close_path (cr); cairo_perf_timer_start (); cairo_perf_set_thread_aware (cr, FALSE); while (loops--) { if (loops == 0) cairo_perf_set_thread_aware (cr, TRUE); cairo_fill_preserve (cr); } cairo_perf_timer_stop (); cairo_restore (cr); return cairo_perf_timer_elapsed (); }
void pmaint::start () { if (active_cb) return; int jitter = uniform_random (0, PRTTMLONG); active_cb = delaycb (PRTTMLONG + jitter, wrap (this, &pmaint::pmaint_next)); }
/** * Seeds the values of this vector with a random point on the unit circle. * */ void vector_t::random() { for( unsigned int i=0; i<_vec.size(); i++ ) _vec[i] = uniform_random(); double magn = norm(); for( unsigned int i=0; i<_vec.size(); i++ ) _vec[i] /= magn; }
void ConditionOutfit::changeOutfit(Creature* creature, int32_t index /*= -1*/) { if (!outfits.empty()) { if (index == -1) { index = uniform_random(0, outfits.size() - 1); } Outfit_t outfit = outfits[index]; g_game.internalCreatureChangeOutfit(creature, outfit); } }
void Creature::onWalk(Direction& dir) { if (hasCondition(CONDITION_DRUNK)) { uint32_t r = uniform_random(0, 20); if (r <= DIRECTION_DIAGONAL_MASK) { if (r < DIRECTION_DIAGONAL_MASK) { dir = static_cast<Direction>(r); } g_game.internalCreatureSay(this, TALKTYPE_MONSTER_SAY, "Hicks!", false); } } }
char* reset_doors(char* byob) { // Set the "good" door, use lots of entropy while doing it unsigned good = (unsigned)uniform_random(3); // setup the doors for the contestant to choose from byob[good] = 1; byob[(good+1) % 3] = 0; byob[(good+2) % 3] = 0; return byob; }
double gaussian_random(double mean, double stddev) { init_rand(); #ifdef HAVE_LIBGSL return mean + gsl_ran_gaussian(rng, stddev); #else // Box-Muller algorithm to generate Gaussian from uniform // see Knuth vol II algorithm P, sec. 3.4.1 double v1, v2, s; do { v1 = uniform_random(-1, 1); v2 = uniform_random(-1, 1); s = v1*v1 + v2*v2; } while (s >= 1.0); if (s == 0) { return mean; } else { return mean + v1 * sqrt(-2 * log(s) / s) * stddev; } #endif }
void WeaponDistance::onUsedAmmo(Player* player, Item* item, Tile* destTile) const { if (ammoAction == AMMOACTION_MOVEBACK && breakChance > 0 && uniform_random(1, 100) <= breakChance) { uint16_t newCount = item->getItemCount(); if (newCount > 0) { newCount--; } g_game.transformItem(item, item->getID(), newCount); } else { Weapon::onUsedAmmo(player, item, destTile); } }
int32_t WeaponMelee::getElementDamage(const Player* player, const Item* item) const { int32_t attackSkill = player->getWeaponSkill(item); int32_t attackValue = std::max<int32_t>(0, elementDamage); float attackFactor = player->getAttackFactor(); int32_t maxValue = Weapons::getMaxWeaponDamage(player->getLevel(), attackSkill, attackValue, attackFactor); if (uniform_random(1, 100) <= g_config.getNumber(ConfigManager::CRITICAL_HIT_CHANCE)) { maxValue *= 2; } maxValue = int32_t(maxValue * player->getVocation()->meleeDamageMultiplier); return -normal_random(0, maxValue); }
void mutation_function(population& p) { for(auto i = p.begin(); i != p.end(); ++i) { float prob = 1 - i->adapt; // probability of mutation float r = uniform_random(); // random float between <0,1) if(r < prob) { mutation::random_transposition(i->perm); i->eval = evaluation(i->perm); // after mutation is done we have to evaluate this specimen again } } }
static cairo_time_t do_wide_fills_va (cairo_t *cr, int width, int height, int loops) { int count; state = 0xc0ffee; for (count = 0; count < 1000; count++) { double x = floor (uniform_random (0, width)); double y = floor (uniform_random (0, height)); cairo_rectangle (cr, x, y, 5, ceil (uniform_random (0, height) - y)); } cairo_perf_timer_start (); while (loops--) cairo_fill_preserve (cr); cairo_perf_timer_stop (); cairo_new_path (cr); return cairo_perf_timer_elapsed (); }
bool ConditionSpeed::startCondition(Creature* creature) { if (!Condition::startCondition(creature)) { return false; } if (speedDelta == 0) { int32_t min, max; getFormulaValues(creature->getBaseSpeed(), min, max); speedDelta = uniform_random(min, max); } g_game.changeSpeed(creature, speedDelta); return true; }
void Weapon::onUsedWeapon(Player* player, Item* item, Tile* destTile) const { if (!player->hasFlag(PlayerFlag_NotGainSkill)) { skills_t skillType; uint32_t skillPoint; if (getSkillType(player, item, skillType, skillPoint)) { player->addSkillAdvance(skillType, skillPoint); } } uint32_t manaCost = getManaCost(player); if (manaCost != 0) { player->addManaSpent(manaCost); player->changeMana(-static_cast<int32_t>(manaCost)); } if (!player->hasFlag(PlayerFlag_HasInfiniteSoul) && soul > 0) { player->changeSoul(-static_cast<int32_t>(soul)); } if (breakChance != 0 && uniform_random(1, 100) <= breakChance) { decrementItemCount(item); return; } switch (action) { case WEAPONACTION_REMOVECOUNT: decrementItemCount(item); break; case WEAPONACTION_REMOVECHARGE: { uint16_t charges = item->getCharges(); if (charges > 1) { g_game.transformItem(item, item->getID(), charges - 1); } else { g_game.internalRemoveItem(item); } break; } case WEAPONACTION_MOVE: g_game.internalMoveItem(item->getParent(), destTile, INDEX_WHEREEVER, item, 1, nullptr, FLAG_NOLIMIT); break; default: break; } }