onilink_

breeding

Jul 16th, 2020
1,460
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.97 KB | None | 0 0
  1. bool PetHouseAttribute::generateEgg()
  2. {
  3.     Expects(_pets.size() >= 2);
  4.    
  5.     if(!_pets[0].pet || !_pets[1].pet) {
  6.         return false;
  7.     }
  8.     const MonsterAttributesCore& m1 = _pets[0].pet->attribs;
  9.     const MonsterAttributesCore& m2 = _pets[1].pet->attribs;
  10.    
  11.     // 1. Check compatibility
  12.     if(!monstersAreInLove()) {
  13.         return false;
  14.     }
  15.    
  16.     MonsterAttributesCore child(m1.ind);
  17.    
  18.     // 2. Generate rarity
  19.     {
  20.         const bool can_be_shiny = m1.rarity.shiny() || m2.rarity.shiny();
  21.         const unsigned rarity_max =
  22.                 std::max(m1.rarity.rarity(), m2.rarity.rarity());
  23.        
  24.         unsigned rarity = Rand()(0u, rarity_max);
  25.         /// @todo maybe add a bonus if the two parents are shiny
  26.         bool shiny = can_be_shiny && Rand()(1.f) < 0.25f;
  27.         if(shiny && rarity >= 3) {
  28.             child.rarity = MonsterRarity(MonsterRarity::Color(rarity-2));
  29.         }
  30.         else {
  31.             child.rarity = MonsterRarity(uint8_t(rarity));
  32.         }
  33.     }
  34.    
  35.    
  36.     // 3. Generate effect table
  37.     using Effect = MonsterEffectTable::Effect;
  38.     std::vector<Effect> effects_pool;
  39.    
  40.     auto add = [&effects_pool](const Effect& effect){
  41.         if(effect.type == SuffixType::None) {
  42.             return;
  43.         }
  44.         // check if effect not already exists
  45.         for(Effect& eff: effects_pool) {
  46.             if(eff.type == effect.type) {
  47.                 if(eff.level < effect.level) {
  48.                     eff.level = effect.level;
  49.                 }
  50.                 return;
  51.             }
  52.         }
  53.         // add effect to the pool
  54.         effects_pool.push_back(effect);
  55.     };
  56.    
  57.     for(const Effect& effect: m1.effects.table) {
  58.         add(effect);
  59.     }
  60.     for(const Effect& effect: m2.effects.table) {
  61.         add(effect);
  62.     }
  63.    
  64.     // select random effects from the pool
  65.     std::shuffle(begin(effects_pool), end(effects_pool), Rand().engine());
  66.     effects_pool.resize(child.effects.table.size());
  67.     std::copy(begin(effects_pool), end(effects_pool), begin(child.effects.table));
  68.    
  69.     // 4. Generate IVs
  70.     for(unsigned i=0; i<m1.ivs.size(); ++i) {
  71.         const unsigned x1 = m1.ivs[i] & 0x7F;
  72.         const unsigned x2 = m2.ivs[i] & 0x7F;
  73.         const unsigned min = std::min<unsigned>(x1, x2) / 2;
  74.         const unsigned max = std::max<unsigned>(x1, x2);
  75.         child.ivs[i] = uint8_t(Rand()(min, max+1)) | 0x80;
  76.     }
  77.    
  78.     // generate gem
  79.     const auto& info = MonsterInfo::get(m1.ind);
  80.     const ItemIndex egg = info.attribs().egg;
  81.     if(egg.unknown_entry()) {
  82.         return false;
  83.     }
  84.    
  85.     ItemSlot out(egg, 1);
  86.     auto attr = std::make_shared<ItemMonsterAttribute>();
  87.     attr->attribs = child;
  88.     attr->egg = true;
  89.     attr->hatching_duration = 30+uint16_t(info.experienceFactor())*10;
  90.     attr->hatching_duration_max = attr->hatching_duration;
  91.     out.setMonster(attr);
  92.    
  93.     setEggSlot(std::move(out));
  94.    
  95.     return true;
  96. }
Advertisement
Add Comment
Please, Sign In to add comment