User Tools

Site Tools


mr:pet_mechanic

Pet Mechanic - Code References

Pet Mechanic allows the hero to tame and command mobs as permanent companions that fight alongside them.

Entity Type

Game Mechanic (AI/Pet System)

Java Classes

Pet Management: PetInventoryManager.java

  • Package: `com.nyrds.pixeldungeon.mechanics`
  • Methods:
    • `hasPets(Hero hero)` - Check if hero has pets
    • `openPetInventoryFromToolbar(Hero hero)` - Open pet inventory UI
    • `giveItemToPet(Hero hero, Mob pet, Item item)` - Transfer item to pet's backpack
    • `takeItemFromPet(Hero hero, Mob pet, Item item)` - Take item from pet's backpack

Pet Base Class: Mob.java (pet functionality)

  • Mobs become pets via static `Mob.makePet(Mob pet, Char owner)` or instance `mob.makePet(Char owner)`
  • Pet state tracked via `setOwnerId(ownerId)` on the mob; pet fraction follows the owner's fraction

Mob Pet Methods: Mob.java

  • `makePet(Char owner)` / `Mob.makePet(Mob, Char)` - Convert mob to pet
  • `isPet()` - Check if mob is a pet
  • `canBePet()` - Check if mob species can be tamed

Hero Pet List: Char.java

  • `getPets_l()` - Get list of hero's pets (Lua interface, defined on `Char`)

AI States: MobAi.java

  • `ControlledAi` - Direct player control state
  • `Hunting` - Pet follows and attacks enemies
  • `Wandering` - Idle behavior near hero
  • `Fleeing` - Low health retreat

UI: Toolbar.java

  • Pet inventory button (index 10, tinted)
  • Opens `WndPetInventory` window

Windows: WndPetItem.java

  • Pet inventory management UI
  • Related windows: `WndPetBag`, `WndPetSelect`, `WndPetQuantity`, `WndPetInventoryOptions`

Actions: CommonActions.java

  • `AC_GIVE_TO_PET` - Give item to pet
  • `AC_TAKE_FROM_PET` - Take item from pet

JSON Configuration

Mob Pet Property: In `mobsDesc/<Mob>.json`

{
  "canBePet": true,
  "name": "Rat_Name",
  ...
}

Mobs with `“canBePet”: true` (e.g. Bee, DeepSnail, MazeShadow, ShamanElder):

  • Boss mobs: `false` (e.g. BeeHive)
  • Hostile NPCs: `false`

String Resources

English (values/strings_all.xml):

<string name="PetInventory_Title">Pet Inventory</string>
<string name="PetInventory_ViewManage">View and manage your pet's equipment</string>
<string name="PetInventory_GiveToPet">Give to Pet</string>
<string name="PetInventory_TakeFromPet">Take from Pet</string>
<string name="PetInventory_GaveItem">You gave %s to your pet</string>
<string name="PetInventory_TookItem">You took %s from your pet</string>
<string name="PetInventory_GiveN">Give %d</string>
<string name="PetInventory_TakeN">Take %d</string>
<string name="PetInventory_PetBackpackFull">Pet's backpack is full</string>
<string name="PetInventory_HeroBackpackFull">Your backpack is full</string>
<string name="PetInventory_TooFar">Pet is too far away</string>
<string name="PetInventory_CantGiveEquipped">Can't give equipped items</string>
<string name="Mob_Cannot_Be_Pet">This creature cannot be tamed</string>
<string name="PetInventory_ACTakeFromPet">Take from Pet</string>
<string name="PetInventory_ACGiveToPet">Give to Pet</string>

Russian (values-ru/strings_all.xml):

<string name="PetInventory_Title">Инвентарь питомца</string>
<string name="PetInventory_ViewManage">Просмотр и управление экипировкой питомца</string>
<string name="PetInventory_GiveToPet">Отдать питомцу</string>
<string name="PetInventory_TakeFromPet">Забрать у питомца</string>
<string name="PetInventory_GaveItem">Вы отдали %s питомцу</string>
<string name="PetInventory_TookItem">Вы забрали %s у питомца</string>
<string name="PetInventory_GiveN">Отдать %d</string>
<string name="PetInventory_TakeN">Забрать %d</string>
<string name="PetInventory_PetBackpackFull">Рюкзак питомца полон</string>
<string name="PetInventory_HeroBackpackFull">Ваш рюкзак полон</string>
<string name="PetInventory_TooFar">Питомец слишком далеко</string>
<string name="PetInventory_CantGiveEquipped">Нельзя отдать экипированные предметы</string>
<string name="Mob_Cannot_Be_Pet">Это существо нельзя приручить</string>
<string name="PetInventory_ACTakeFromPet">Забрать у питомца</string>
<string name="PetInventory_ACGiveToPet">Отдать питомцу</string>

Lua Scripts

  • Pet AI: `scripts/lib/commonClasses.lua` - `RPD.Mob:makePet(target, caster)`
  • Possess Spell: `scripts/spells/Possess.lua` - Temporary pet control
  • Raise Dead: `scripts/spells/RaiseDead.lua` - Undead pet creation
  • Exhumation: `scripts/spells/Exhumation.lua` - Wraith pet creation
  • Summon Beast: `scripts/spells/SummonBeast.lua` - Temporary summons

Game Mechanics

  • Taming Methods:
    • Possess Spell (temporary)
    • Summon Beast Spell (temporary)
    • Raise Dead (undead pets)
    • Exhumation (Wraith pets)
    • Certain items/artifacts (e.g. Carcass, HeartOfDarkness, ChaosStaff)
  • Pet Capabilities:
    • Follow hero automatically
    • Attack enemies in range
    • Have own inventory (accessible via toolbar)
    • Gain experience and level up
    • Can be healed with potions/spells
  • Pet Rules:
    • Only mobs with `canBePet: true` in JSON can be tamed
    • Pets persist between levels (`followOnLevelChanged` returns true for hero-owned pets)
  • Pet Inventory:
    • Accessible via toolbar button (pet icon)
    • Separate from hero inventory
    • Items are transferred to/from the pet's backpack

Code Fragments

Taming a mob (actual implementation in Mob.java):

public static Mob makePet(@NotNull Mob pet, int ownerId) {
    var owner = CharsList.getById(ownerId);
    if(owner.fraction()==Fraction.HEROES) {
        pet.expForKill = 0;
    }
    if (pet.canBePet()) {
        pet.setFraction(owner.fraction());
        pet.setOwnerId(ownerId);
    }
    return pet;
}

Lua pet creation:

-- Possess spell
RPD.Mob:makePet(target, caster)
 
-- Raise Dead
local mob = RPD.MobFactory:mobByName(latestDeadMob.class)
RPD.Mob:makePet(mob, chr)
level:spawnMob(mob)

mr/pet_mechanic.txt · Last modified: by 127.0.0.1