void Photon::CrossDnOrNot(MCMLModel * model) { /* Decide whether the photon will be transmitted or be reflected on the bottom boundary (uz>0) of the current layer. If the photon is transmitted, move the photon to "layer+1". If "layer" is the last layer, record the transmitted weight as transmittance. See comments for CrossUpOrNot. Update the photon parmameters. */ double uz0 = uz; // z directional cosine double r = 0.0; // reflectance double uz1; // cosines of transmission alpha. always positive short layer0 = layer; double ni = model->layerObj.layer[layer0].n; double nt = model->layerObj.layer[layer+1].n; // Get r if (uz0 <= model->layerObj.cosCrit[1][layer]) r = 1.0; // total internal reflection else r = RFresnel(ni, nt, uz0, &uz1); #if PARTIALREFLECTION if ((layer0 == model->layerObj.numLayers) && (r < 1.0)) { uz = uz1; RecordT(model, r); uz = -uz0; } else if (RandomNum() > r) { // transmitted to layer+1 layer += 1; ux *= ni/nt; uy *= ni/nt; uz = uz1; } else // reflected uz = -uz0; #else if (RandomNum() > r) { // transmitted to layer+1 if (layer0 == model->layerObj.numLayers) { uz = uz1; RecordT(model, 0.0); dead = true; } else { layer += 1; ux *= ni/nt; uy *= ni/nt; uz = uz1; } } else // reflected uz = -uz0; #endif }
luna::Table * RandomTable() { auto t = g_gc.NewTable(); int array_count = RandomNum(10); for (int i = 0; i < array_count; ++i) t->SetArrayValue(i + 1, RandomValue(true)); int hash_count = RandomNum(20); for (int i = 0; i < hash_count; ++i) t->SetValue(RandomValue(true), RandomValue(true)); return t; }
bool cSpawnArea::findValidLocation( sLocation& location ) { int a=0; do { location.x = RandomNum( where->second.x1, where->second.x2 ); location.y = RandomNum( where->second.y1, where->second.y2 ); location.z = mapElevation( location ); if( canNpcWalkHere( location ) ) return true; } while (++a < 100); return false; }
void RandomLoop() { int free_global_count_max = 20; while (true) { g_scopeClosure.clear(); g_scopeString.clear(); g_scopeTable.clear(); int scope_count = RandomRange(1, 1000); RunScope(scope_count); TouchGlobalTable(RandomNum(scope_count)); int free_count = RandomRange(1, free_global_count_max++); FreeGlobal(free_count); if (free_global_count_max >= 1000) free_global_count_max = 20; g_gc.CheckGC(); #ifdef _MSC_VER Sleep(RandomRange(1, 20)); #else usleep(RandomRange(1000, 20000)); #endif // _MSC_VER } }
void StoreItemRandomValue(pItem pi,int tmpreg) { // Function Created by Magius(CHE) for trade System int max=0,min=0; if ( ! pi ) return; if (pi->good<0) return; if (tmpreg<0) { pItem pio=pi->getOutMostCont(); if (pio->isInWorld()) tmpreg=calcRegionFromXY( pio->getPosition() ); else { pChar pc=cSerializable::findCharBySerial(pio->getContSerial()); if (!pc) return; tmpreg=calcRegionFromXY( pc->getPosition() ); } } if (tmpreg<0 || tmpreg>255 || pi->good<0 || pi->good>255) return; min=region[tmpreg].goodrnd1[pi->good]; max=region[tmpreg].goodrnd2[pi->good]; if (max!=0 || min!=0) pi->rndvaluerate=(int) RandomNum(min,max); }
virtual void execute() { if ( spell == -1 ) { return; // Shouldn't happen } if ( m_npc->canHandleEvent( EVENT_CASTSPELL ) ) { PyObject *target = PyGetObjectObject( objTarget ); PyObject* args = Py_BuildValue( "(Nii[]N)", m_npc->getPyObject(), spell, 0, target ); bool result = m_npc->callEventHandler( EVENT_CASTSPELL, args ); Q_UNUSED( result ); Py_DECREF( args ); } // Calculate the next spell delay if ( spell == 41 ) { nextSpellTime = Server::instance()->time() + m_npc->actionSpeed(); // Dispel gets special handling } else { float scale = m_npc->skillValue( MAGERY ) * 0.003; unsigned int minDelay = (unsigned int)( 6000 - ( scale * 750 ) ); unsigned int maxDelay = (unsigned int)( 6000 - ( scale * 1250 ) ); nextSpellTime = Server::instance()->time() + RandomNum( minDelay, maxDelay ); } }
QString cTerritory::getGuardSect( void ) const { if ( guardSections_.count() > 0 ) return this->guardSections_[RandomNum( 0, this->guardSections_.size() - 1 )]; else return QString(); }
/* Chose a random spell */ void chooseSpell( int& spell, cUObject*& objTarget, Coord& posTarget, P_CHAR currentVictim ) { Q_UNUSED( posTarget ); // If we are not summoned, try healing if ( m_npc->hitpoints() < m_npc->maxHitpoints() // Only try to heal if we're not at full health && !m_npc->summoned() // Summoned creatures dont heal && ( m_npc->skillValue( MAGERY ) / 100 ) > RandomNum( 1, 100 ) // We dont heal with every step ) { int toheal = m_npc->maxHitpoints() - m_npc->hitpoints(); // How many hitpoints did we loose? if ( toheal >= 50 && m_npc->mana() >= 11 ) { // Is it worth the effort? Do we have enough mana? spell = 29; // Greater Heal objTarget = m_npc; return; } else if ( toheal >= 10 && m_npc->mana() >= 4 ) { // Only bother if we have enough mana spell = 4; // Heal objTarget = m_npc; return; } } spell = getRandomHarmfulSpell(); objTarget = currentVictim; }
void cCommands::RegSpawnNum (int s, int r, int n) // rewrite by LB { int i, spawn=0; unsigned int currenttime=uiCurrentTime; char *temps; r++; if (r<=0 || r>=255) return; temps = new char[100]; spawn = (spawnregion[r].max-spawnregion[r].current); if (n > spawn) { sprintf(temps, "%d too many for region %d, spawning %d to reach MAX:%d instead.",n, r-1, spawn, spawnregion[r].max); sysmessage(s, temps); n=spawn; } sprintf(temps, "Region %d is Spawning: %d NPCs/items, this will cause some lag.", r-1, spawn); sysbroadcast(temps); for(i=1;i<spawn;i++) { doregionspawn(r); }//for spawnregion[r].nexttime=currenttime+(MY_CLOCKS_PER_SEC*60*RandomNum(spawnregion[r].mintime,spawnregion[r].maxtime)); sprintf(temps, "Done. %d total NPCs/items spawned in Spawnregion %d.",spawn,r-1); sysmessage(s, temps); if (temps!=NULL) delete [] temps; // fixing memory leak, LB }//regspawnnum
void cCommands::RegSpawnMax (int s, int r) // rewrite LB { int i, spawn; unsigned int currenttime=uiCurrentTime; char *temps; r++; if (r<=0 || r>=255) return; temps = new char[100]; spawn = (spawnregion[r].max-spawnregion[r].current); sprintf(temps, "Region %d is Spawning %d items/NPCs, this will cause some lag.", r-1, spawn); sysbroadcast(temps); for(i=1;i<spawn;i++) { doregionspawn(r); } spawnregion[r].nexttime=currenttime+(MY_CLOCKS_PER_SEC*60*RandomNum(spawnregion[r].mintime,spawnregion[r].maxtime)); sprintf(temps, "Done. %d total NPCs/items spawned in Spawnregion %d.",spawn,r-1); sysmessage(s, temps); if (temps!=NULL) delete [] temps; }
void cTerritory::init( void ) { cBaseRegion::init(); midilist_ = ""; resores_ = ""; firstcoin_ = Config::instance()->defaultFirstCoin(); secondcoin_ = Config::instance()->defaultSecondCoin(); thirdcoin_ = Config::instance()->defaultThirdCoin(); flags_ = 0; extraflags_ = 0; guardowner_ = QString(); fixedlight_ = -1; lightmodifier_ = 0; snowchance_ = Config::instance()->snowDefaultChance();; rainchance_ = Config::instance()->rainDefaultChance();; guardSections_ = QStringList(); // guardSections_.push_back( "standard_guard" ); isRaining_ = 0; isSnowing_ = 0; weatherday_ = 0; weatherhour_ = 0; // Duration rainduration_ = Config::instance()->rainDefaultDuration(); rainrangeduration_ = Config::instance()->rainDefaultDurationRange(); snowduration_ = Config::instance()->snowDefaultDuration(); snowrangeduration_ = Config::instance()->snowDefaultDurationRange(); dryduration_ = Config::instance()->dryDefaultDuration(); dryrangeduration_ = Config::instance()->dryDefaultDurationRange(); // Intensity minintensity_ = Config::instance()->minDefaultIntensity(); maxintensity_ = Config::instance()->maxDefaultIntensity(); intensity_ = RandomNum( minintensity_, maxintensity_ ); // Storm Parameters stormchecked_ = 0; }
void TouchGlobalTable(int count) { if (g_globalTable.empty()) return ; std::size_t total_scope = 0; total_scope += g_scopeTable.size(); total_scope += g_scopeString.size(); total_scope += g_scopeClosure.size(); if (total_scope == 0) return ; for (int i = 0; i < count; ++i) { auto setter = [&](luna::Value &v, std::size_t index) { if (index < g_scopeTable.size()) { v.type_ = luna::ValueT_Table; v.table_ = g_scopeTable[index]; } else if (index < g_scopeTable.size() + g_scopeString.size()) { index -= g_scopeTable.size(); v.type_ = luna::ValueT_String; v.str_ = g_scopeString[index]; } else { index -= g_scopeTable.size() + g_scopeString.size(); v.type_ = luna::ValueT_Closure; v.closure_ = g_scopeClosure[index]; } }; luna::Value key; luna::Value value; auto key_index = RandomNum(total_scope); auto value_index = RandomNum(total_scope); setter(key, key_index); setter(value, value_index); auto global_index = RandomNum(g_globalTable.size()); auto global = g_globalTable[global_index]; global->SetValue(key, value); CHECK_BARRIER(g_gc, global); } }
void cItem::SetRandPosInCont(P_ITEM pCont) { setPosition("z", 9); CONTINFOMAP::iterator iter( contInfo.find( pCont->getId() ) ); if( iter==contInfo.end() || iter->second==contInfoGump.end()) { setPosition("x", RandomNum(18, 118)); setPosition("y", RandomNum(50, 100)); LogWarning("trying to put something INTO a non container, id=0x%X",pCont->getId() ); } else { setPosition("x", RandomNum(iter->second->second.upperleft.x, iter->second->second.downright.x)); setPosition("y", RandomNum(iter->second->second.upperleft.y, iter->second->second.downright.y)); } }
cSpawnDinamic::cSpawnDinamic( pItem pi ) { this->item=pi->getSerial(); this->item_spawned.clear(); this->npc_spawned.clear(); this->current=0; this->nextspawn=getClockmSecs()+ (60*RandomNum( pi->morey, pi->morez)*SECS); }
luna::Function * RandomFunction() { auto f = g_gc.NewFunction(); auto s = RandomString(); f->SetModuleName(s); f->SetLine(RandomNum(1000)); int instruction_count = RandomRange(10, 1000); for (int i = 0; i < instruction_count; ++i) { unsigned int op_min = luna::OpType_LoadNil; unsigned int op_max = luna::OpType_GetGlobal; luna::OpType op = static_cast<luna::OpType>(RandomRange(op_min, op_max)); luna::Instruction instruction(op, RandomNum(128), RandomNum(128), RandomNum(128)); f->AddInstruction(instruction, i); } int const_num = RandomNum(5); for (int i = 0; i < const_num; ++i) f->AddConstNumber(RandomNum(100000)); int const_str = RandomNum(5); for (int i = 0; i < const_str; ++i) f->AddConstString(RandomString()); CHECK_BARRIER(g_gc, f); return f; }
TChar DbCreator::RandomAlphaChar(TBool aIsUpper = EFalse) { const TInt KUppercaseA(65); const TInt KLowercaseA(97); const TInt KZOffset(25); TInt min = aIsUpper ? KUppercaseA : KLowercaseA; TInt max = min + KZOffset; return TChar(RandomNum(min, max)); }
void cCombat::DoCombatAnimations(P_CHAR pc_attacker, P_CHAR pc_defender, int fightskill, int bowtype, int los) { short id = pc_attacker->id(); int cc,aa; // khpae - turn toward the defender int newdir = chardir (pc_attacker, pc_defender); if (((pc_attacker->dir&0x07) != newdir) && (newdir >= 0)) { Movement->Walking (pc_attacker, newdir, 256); } // khpae - turning end if (id<0x0190) { aa=4+(rand()%3); // bugfix, LB, some creatures dont have animation #4 cc=(creatures[id].who_am_i)&0x2; // anti blink bit set ? if (cc==2) { aa++; if (id==5) // eagles need special treatment { switch (RandomNum(0, 2)) { case 0: aa=0x1; break; case 1: aa=0x14; break; case 2: aa=0x4; break; } } } npcaction(pc_attacker,aa); playmonstersound(pc_attacker, pc_attacker->id(), SND_ATTACK); } else if (pc_attacker->onhorse) { CombatOnHorse(pc_attacker); // determines weapon in hand and runs animation kolours (09/19/98) } else { CombatOnFoot(pc_attacker); // determines weapon in hand and runs animation kolours (09/19/98) } if (fightskill==ARCHERY) { if (los) { if (bowtype==1) { delequan(pc_attacker, 0x0F3F, 1, NULL); movingeffect3(pc_attacker, pc_defender, 0x0F, 0x42, 0x08, 0x00, 0x00,0,0,0,0); } else { delequan(pc_attacker, 0x1BFB, 1, NULL); movingeffect3(pc_attacker, pc_defender, 0x1B, 0xFE, 0x08, 0x00, 0x00,0,0,0,0); } } } }
void DragAndDrop::dropOnBeggar( cUOSocket* socket, P_ITEM pItem, P_CHAR pBeggar ) { int tempint; if( ( pBeggar->hunger() < 6 ) && pItem->type() == 14 ) { pBeggar->talk( tr("*cough* Thank thee!") ); pBeggar->soundEffect( 0x3A + RandomNum( 1, 3 ) ); // *You see Snowwhite eating some poisoned apples* // Color: 0x0026 pBeggar->emote( tr( "*You see %1 eating %2*" ).arg( pBeggar->name() ).arg( pItem->getName() ) ); // We try to feed it more than it needs if( pBeggar->hunger() + pItem->amount() > 6 ) { // client->player()->karma += ( 6 - pBeggar->hunger() ) * 10; tempint = ( 6 - pBeggar->hunger() ) * 10; socket->player()->setKarma( socket->player()->karma() + tempint ); pItem->setAmount( pItem->amount() - ( 6 - pBeggar->hunger() ) ); pBeggar->setHunger( 6 ); // Pack the rest into his backpack bounceItem( socket, pItem ); return; } pBeggar->setHunger( pBeggar->hunger() + pItem->amount() ); // client->player()->karma += pItem->amount() * 10; tempint = pItem->amount() * 10; socket->player()->setKarma( socket->player()->karma() + tempint ); pItem->remove(); return; } // No Food? Then it has to be Gold if( pItem->id() != 0xEED ) { pBeggar->talk( tr("Sorry, but i can only use gold.") ); bounceItem( socket, pItem ); return; } pBeggar->talk( tr( "Thank you %1 for the %2 gold!" ).arg( socket->player()->name() ).arg( pItem->amount() ) ); socket->sysMessage( tr("You have gained some karma!") ); if( pItem->amount() <= 100 ) socket->player()->setKarma( socket->player()->karma() + 10 ); else socket->player()->setKarma( socket->player()->karma() + 50 ); pItem->remove(); }
void ParticleSystem::ResetParticle(int index) { particles[index].x = RandomNum(0.5*spread, -0.5*spread); particles[index].y = RandomNum(0.5*spread, -0.5*spread); particles[index].z = RandomNum(0.5*spread, -0.5*spread); particles[index].red = RandomNum(1, 0); particles[index].green = RandomNum(1, 0); particles[index].blue = RandomNum(1, 0); particles[index].vx = vx + RandomNum(spread/2, -spread/2); particles[index].vy = vy + RandomNum(spread, 0); particles[index].vz = vz + RandomNum(spread/2, -spread/2); particles[index].lifespan = 0; }
void Human_Guard_Fight::execute() { // talk only in about every 10th check switch ( RandomNum( 0, 20 ) ) { case 0: m_npc->talk( tr( "Thou shalt regret thine actions, swine!" ), 0xFFFF, 0, true ); break; case 1: m_npc->talk( tr( "Death to all Evil!" ), 0xFFFF, 0, true ); break; } }
QString WPDefManager::getRandomListEntry( QString ListSection ) { QDomElement* DefSection = this->getSection( WPDT_LIST, ListSection ); if( !DefSection->isNull() ) { return DefSection->childNodes().item( RandomNum( 0, DefSection->childNodes().count()-1 ) ).nodeName(); } else return ""; }
void Photon::Roulette() { /* The photon weight is small, and the photon packet tries to survive a roulette. */ if (w == 0.0) dead = true; else if (RandomNum() < CHANCE) // survived the roulette. w /= CHANCE; else dead = true; }
HBufC* DbCreator::RandomNameL() { const TInt KNameLen( RandomNum(5, 10) ); HBufC* name = HBufC::NewLC(KNameLen); TPtr namePtr = name->Des(); for (TInt i = 0; i < KNameLen; ++i) { namePtr.Append(RandomAlphaChar(i == 0)); } CleanupStack::Pop(name); return name; }
luna::Closure * RandomClosure() { if (g_globalFunction.empty()) g_globalFunction.push_back(RandomFunction()); auto index = RandomNum(g_globalFunction.size()); auto proto = g_globalFunction[index]; auto c = g_gc.NewClosure(); c->SetPrototype(proto); return c; }
virtual float preCondition() { // Can we cast a spell? if ( nextSpellTime > Server::instance()->time() ) { return 0.0f; } Monster_Aggressive *ai = static_cast<Monster_Aggressive*>( m_ai ); // If we are already casting a spell, cancel this if ( m_npc->hasScript( "magic" ) ) { return 0.0f; } // Reinitialize to "zero" spell = -1; objTarget = 0; posTarget = Coord::null; // We dont have a spell ready, are ready to cast. P_NPC dispelTarget = findDispelOpponent(); if ( m_npc->poison() != -1 ) { // Always try to cure if poisoned spell = 11; // Cure objTarget = m_npc; } else if ( dispelTarget && m_npc->skillValue( MAGERY ) * 0.01 * 75 > RandomNum( 1, 100 ) ) { // We have something to dispel that is attacking us. Easily dispatch threat. spell = 41; // Dispel objTarget = dispelTarget; } else { P_CHAR currentVictim = ai->currentVictim(); if ( currentVictim && m_npc->inRange( currentVictim, 12 ) ) { chooseSpell( spell, objTarget, posTarget, currentVictim ); // Choose a spell } } if ( spell == -1 ) { return 0.0f; // We couldn't find a spell to cast } return 2.0f; // Give us priority in the fuzzy logic }
QString cDefinitions::getRandomListEntry( const QString& ListSection ) { QStringList* list = NULL; QMap<QString, QStringList>::iterator it = listcache_.find( ListSection ); if ( it != listcache_.end() ) list = &( it.data() ); if ( !list || list->isEmpty() ) return QString(); else return ( *list )[RandomNum( 0, list->size() - 1 )]; }
HBufC* DbCreator::RandomPhoneNumL() { const TInt KAsciiZero(48); const TInt KTenDigits(10); HBufC* num = HBufC::NewLC(KTenDigits); TPtr numPtr = num->Des(); for (TInt i = 0; i < KTenDigits; ++i) { numPtr.Append( TChar(RandomNum(KAsciiZero, KAsciiZero + 9) ) ); } CleanupStack::Pop(num); return num; }
luna::Value RandomValue(bool exclude_table) { luna::ValueT type = luna::ValueT_Nil; int percent = RandomRange(1, 100); if (percent <= 20) type = luna::ValueT_Nil; else if (percent <= 30) type = luna::ValueT_Bool; else if (percent <= 60) type = luna::ValueT_Number; else if (percent <= 70) type = luna::ValueT_String; else if (percent <= 80) type = luna::ValueT_Closure; else if (percent <= 90) type = luna::ValueT_CFunction; else type = exclude_table ? luna::ValueT_Number : luna::ValueT_Table; luna::Value value; value.type_ = type; switch (type) { case luna::ValueT_Nil: break; case luna::ValueT_Bool: value.bvalue_ = RandomRange(0, 1) ? true : false; break; case luna::ValueT_Number: value.num_ = RandomNum(100000); break; case luna::ValueT_Obj: value.obj_ = RandomString(); break; case luna::ValueT_String: value.str_ = RandomString(); break; case luna::ValueT_Closure: value.closure_ = RandomClosure(); break; case luna::ValueT_Table: value.table_ = RandomTable(); break; case luna::ValueT_CFunction: break; default: break; } return value; }
void Action_Flee::execute() { P_CHAR pFleeFrom = World::instance()->findChar( pFleeFromSer ); if ( !pFleeFrom ) { pFleeFromSer = INVALID_SERIAL; return; } if ( !m_npc->hasPath() ) { Coord newPos = m_npc->pos(); Coord fleePos = pFleeFrom->pos(); // find a valid spot in a circle of flee_radius fields to move to float rnddist = ( float ) RandomNum( 1, Config::instance()->pathfindFleeRadius() ); if ( newPos != fleePos ) { int v1 = newPos.x - fleePos.x; int v2 = newPos.y - fleePos.y; float v_norm = sqrt( ( double ) ( v1* v1 + v2* v2 ) ); newPos.x = newPos.x + ( Q_INT16 ) floor( rnddist * v1 / v_norm ); newPos.y = newPos.y + ( Q_INT16 ) floor( rnddist * v2 / v_norm ); } else { float rndphi = ( float ) RandomNum( 0, 100 ) / 100.0f * 2 * 3.14; newPos.x = newPos.x + ( Q_INT16 ) floor( sin( rndphi ) * rnddist ); newPos.y = newPos.y + ( Q_INT16 ) floor( cos( rndphi ) * rnddist ); } // we use pathfinding for fleeing movePath( newPos ); } else movePath( m_npc->pathDestination() ); }
double SpinTheta(double g) { /* Choose (sample) a new theta angle for photon propagation according to the anisotropy. If anisotropy g is 0, then cos(theta) = 2*rand-1. otherwise sample according to the Henyey-Greenstein function. Returns the cosine of the polar deflection angle theta. */ double temp, cost; if (g == 0.0) cost = 2*RandomNum() - 1; else { temp = (1 - g*g)/(1 - g + 2*g*RandomNum()); cost = (1 + g*g - temp*temp)/(2*g); if (cost < -1) cost = -1.0; else if (cost > 1) cost = 1.0; } return cost; }