QtCraft API C++ classes QML types Handbook

QcMobManager Class

The creatures on the server (decision E-422): their store, the order of their tick (behaviour in QcMobBehaviour: wander, chase along paths, attack, flee when hurt for passive mobs; despawning; natural spawning in QcNaturalSpawner; village life in QcVillagePopulation), damage, and saving and loading the persistent ones. More...

Header: #include <server/mobs/QcMobManager.h>

Public Types

BlockAction
CreatureHit
enum class Departure { Died, Removed, Despawned, Void, Unloaded }
DepartureListener
enum class DepartureToken { }
PlayerHit
PlayerView
Stored

Public Functions

QcMobManager(const QcMobManager &)
qsizetype awakeCount() const
qsizetype awakeSummonedCount() const
const QcMobBehaviour &behaviour() const
void carryBlock(QcCreature &mob, QcBlockId block) const
void clearSentHurts()
QList<QcMobCrowd::Crammed> crammed(int limit) const
std::optional<QList<QcItemDrop>> damage(const QcServerCombat::Blow &blow)
QList<QcCreature> &editableMobs()
QcCreature *find(quint32 id)
const QcCreature *find(quint32 id) const
void forgetPopulated(QcChunkPos pos)
bool hurt(quint32 mobId, double amount)
int hurtTicks() const
bool isPopulated(QcChunkPos pos) const
void keep(QcCreature &mob, bool kept)
const QList<QcCreature> &mobs() const
bool naturalSpawning() const
quint64 nextUid() const
void notePopulated(QcChunkPos pos)
QcMobManager::DepartureToken onDeparture(QcMobManager::DepartureListener listener)
const QcPathPlanner &paths() const
QList<QcMobManager::Stored> persistentMobs() const
void populateChunk(QcServerWorld &world, QcChunkPos pos, const std::function<quint32 ()> &nextEntityId)
const QSet<QcChunkPos> &populatedChunks() const
QcVillagePopulation &population()
const QcVillagePopulation &population() const
QcVec3 pushOnPlayer(const QcVec3 &feet, quint64 tick)
void release(QcCreature &child)
void relinkFamilies()
bool remove(quint32 mobId)
void removeAll()
void removeDepartureListener(QcMobManager::DepartureToken token)
void restore(const QList<QcMobManager::Stored> &mobs, const QSet<QcChunkPos> &populated, const std::function<quint32 ()> &nextEntityId)
void setCrowdLimits(qint64 throttled, qint64 idle)
void setGriefing(bool on)
void setHurtTicks(int ticks)
void setNaturalSpawning(bool on)
void setNextUid(quint64 next)
void setPathLimits(qint64 nodesPerTick, qint64 nodesPerSearch, qint64 pathLength)
void setWeather(float rain, float thunder)
void setYawRandom(QRandomGenerator *random)
quint32 spawn(QcCreatureKindId type, const QcVec3 &position, quint32 id, bool underground = false, bool persistent = false)
QcNaturalSpawner &spawner()
const QcNaturalSpawner &spawner() const
QList<QcMobManager::BlockAction> takeBlockActions()
QList<QcMobManager::CreatureHit> takeCreatureHits()
QList<QcProjectileManager::Launch> takeLaunches()
bool teleport(quint32 mobId, const QcBlockAccess &world)
QList<QcMobManager::PlayerHit> tick(QcServerWorld &world, const QList<QcMobManager::PlayerView> &players, double worldTime, double dt, const std::function<quint32 ()> &nextEntityId)
void unload(const std::function<bool (const QcCreature &)> &leaves)
QcVillagerBehaviour &villagers()
const QcVillagerBehaviour &villagers() const
std::optional<QList<QcItemDrop>> wound(quint32 mobId, double amount)
std::optional<QList<QcItemDrop>> woundAt(qsizetype index, double amount)
QcMobManager &operator=(const QcMobManager &)

Static Public Members

const double despawnDistance
const double flyMaxHeight
const double flyMinHeight
const double knockbackLift
const double knockbackSpeed
QcVec3 knockbackVelocity(const QcVec3 &away, double strength, double lift = 1.0)
QcMobManager::Stored storedOf(const QcCreature &mob)

Detailed Description

Peaceful animals (not hostile, ambient or flying) are persistent (decision E-086): they never despawn, rest while their chunk is not loaded and are saved with the world. Other creatures are not saved; they appear again around the players. Every creature that leaves the store raises one notification with the reason (Departure) for its listeners.

Member Type Documentation

[alias] QcMobManager::BlockAction

A block a creature takes or puts down.

[alias] QcMobManager::CreatureHit

A blow of one creature at another.

enum class QcMobManager::Departure

Why a creature left the store: it died, was removed without a death (caught, commands), despawned far from the players or in daylight, fell into the void, or its chunk unloaded (it rests in the chunk's entity record and comes back with it).

ConstantValueDescription
QcMobManager::Departure::Died0It died.
QcMobManager::Departure::Removed1It was removed without a death (caught, commands).
QcMobManager::Departure::Despawned2It despawned far from the players or in daylight.
QcMobManager::Departure::Void3It fell into the void.
QcMobManager::Departure::Unloaded4Its chunk unloaded.

[alias] QcMobManager::DepartureListener

Called with the creature just before it leaves the store. Every creature that leaves in the same step is still in the store while the listeners run, so they may read it (find, mobs()); they must not add or remove creatures.

enum class QcMobManager::DepartureToken

Names a subscription to departures (onDeparture), for removeDepartureListener.

[alias] QcMobManager::PlayerHit

A blow of a creature at a player.

[alias] QcMobManager::PlayerView

What the mob logic knows about a player.

[alias] QcMobManager::Stored

The data exchanged with the server and the brains (QcMobExchange): a saved mob.

Member Function Documentation

[delete] QcMobManager::QcMobManager(const QcMobManager &)

Copy-constructs an instance of QcMobManager. This function is deleted.

qsizetype QcMobManager::awakeCount() const

Returns the number of mobs that are not dormant.

qsizetype QcMobManager::awakeSummonedCount() const

Returns the number of mobs that are not dormant and came by /summon.

const QcMobBehaviour &QcMobManager::behaviour() const

Returns what the creatures did in the last tick: crowds, tiers, paths (tests, measurements).

void QcMobManager::carryBlock(QcCreature &mob, QcBlockId block) const

Lets mob carry the block state block (qcAirBlockId: none) with its item in the hand.

void QcMobManager::clearSentHurts()

Forgets the hurts and strikes the clients were told of (QcCreature::hurtUnsent, QcCreature::struckUnsent) after a world sync (decision E-971): a player's blow lands between two ticks, so it must live until the next sync, through the creatures' tick in between.

QList<QcMobCrowd::Crammed> QcMobManager::crammed(int limit) const

Returns the creatures that stood in a cell with more than limit creatures at the start of the last tick (game rule maxEntityCramming; 0: none).

std::optional<QList<QcItemDrop>> QcMobManager::damage(const QcServerCombat::Blow &blow)

Applies blow to its creature: damage from blow.from, pushing it away with knockback times a blow's push (0: not at all) and lift times its lift, less what the kind resists (a golem stands firm, decision E-471); returns the mob's drops when it died. A hit without damage (a snowball) pushes and frightens but gives no invulnerability (decision E-427). During the creature's invulnerability only a stronger hit takes, with the difference and without the push (QcHurtWindow, decision E-429). blow.attacker is the player who dealt it (0: none, as for lightning or a golem's blow): a neutral creature turns on that player (QcCreature::struckBy, decision E-460).

QList<QcCreature> &QcMobManager::editableMobs()

Returns every mob for changes in place: for the breeding timers (QcMobBreeding) and the parts that set up a mob they just spawned; never add or remove mobs through it.

QcCreature *QcMobManager::find(quint32 id)

Returns the mob with the entity id id for changes, or null.

const QcCreature *QcMobManager::find(quint32 id) const

Returns the mob with the entity id id, or null.

void QcMobManager::forgetPopulated(QcChunkPos pos)

The chunk at pos left the world; whether it got its animals goes with it (decision E-374).

bool QcMobManager::hurt(quint32 mobId, double amount)

Deals amount damage without a source (lava) to the mob mobId: no knockback, no fleeing. Returns true when the mob died and was removed.

int QcMobManager::hurtTicks() const

Returns the game ticks a creature stays invulnerable after a hurt (setHurtTicks()).

See also setHurtTicks().

bool QcMobManager::isPopulated(QcChunkPos pos) const

Returns whether the chunk at pos got its creatures (populateChunk()).

void QcMobManager::keep(QcCreature &mob, bool kept)

Keeps mob with the world (persistent, with a persistent id) from now on, or no longer (kept); never a guardian (QcCreature::guards, decision E-591); a named creature always stays kept (decision E-866). A parent of a family takes its children along (decision E-839).

[static] QcVec3 QcMobManager::knockbackVelocity(const QcVec3 &away, double strength, double lift = 1.0)

Returns the velocity of a push along away (horizontal, normalized) with strength times a blow's push, lifting lift times a blow's lift (a golem's blow throws upwards, decision E-471).

const QList<QcCreature> &QcMobManager::mobs() const

Returns every mob, dormant ones too.

bool QcMobManager::naturalSpawning() const

Returns whether creatures spawn by themselves (setNaturalSpawning()).

See also setNaturalSpawning().

quint64 QcMobManager::nextUid() const

Returns the persistent id the next saved creature gets (QcCreature::uid); world.json keeps it. Setting never lowers it below an id already given.

See also setNextUid().

void QcMobManager::notePopulated(QcChunkPos pos)

Marks the chunk at pos as one that got its creatures (a chunk that came back).

QcMobManager::DepartureToken QcMobManager::onDeparture(QcMobManager::DepartureListener listener)

Subscribes listener to every departure from the store until the token it returns is removed or the manager goes.

const QcPathPlanner &QcMobManager::paths() const

Returns the path searches of the creatures (budget use, tests and debug).

QList<QcMobManager::Stored> QcMobManager::persistentMobs() const

Returns the persistent mobs to save.

void QcMobManager::populateChunk(QcServerWorld &world, QcChunkPos pos, const std::function<quint32 ()> &nextEntityId)

Gives the chunk at pos of world seen for the first time, with ids from nextEntityId, the creatures of the village pieces in it (an animal pen's pair, decision E-404), a villager for each free bed in it (decision E-411) and its group of animals (QcNaturalSpawner::chunkAnimalChance, decided by the seed), and remembers it; later calls for the same chunk do nothing. The chunk must be loaded.

const QSet<QcChunkPos> &QcMobManager::populatedChunks() const

Returns every chunk that got its creatures.

QcVillagePopulation &QcMobManager::population()

Returns the villagers of the villages: limits, claims, children and golems (decisions E-411, E-415).

const QcVillagePopulation &QcMobManager::population() const

Returns the villagers of the villages, read-only.

QcVec3 QcMobManager::pushOnPlayer(const QcVec3 &feet, quint64 tick)

Returns the push of the creatures on a player with their feet at feet in server tick tick (decision E-882), a horizontal velocity for their next step (QcPlayerInput::push).

void QcMobManager::release(QcCreature &child)

Releases child from its family: no longer kept, no parent; it goes on as an ordinary monster.

void QcMobManager::relinkFamilies()

Links the children of kept families that wait for their parent (QcCreature::parentUid) to the parent when it is in the world (decision E-839), no more than its family's maxChildren (the others are released); restore does so after every load.

bool QcMobManager::remove(quint32 mobId)

Removes the mob mobId without a death (caught); returns false when there is none.

void QcMobManager::removeAll()

Clears the store without departures (tests that want an empty world).

void QcMobManager::removeDepartureListener(QcMobManager::DepartureToken token)

Ends the subscription token; takes effect from the next departure on (also inside a listener).

void QcMobManager::restore(const QList<QcMobManager::Stored> &mobs, const QSet<QcChunkPos> &populated, const std::function<quint32 ()> &nextEntityId)

Adds the saved mobs as dormant with ids from nextEntityId and takes populated as the chunks that got their creatures.

void QcMobManager::setCrowdLimits(qint64 throttled, qint64 idle)

Sets the thresholds of the AI tiers in crowds to throttled and idle (QcServerLimits crowdThrottled, crowdIdle; decision E-401).

void QcMobManager::setGriefing(bool on)

Sets the game rule mobGriefing to on (decision E-460): without it creatures take and place no blocks.

void QcMobManager::setHurtTicks(int ticks)

Sets the game rule mobInvulnerabilityTicks to ticks (decision E-429): the game ticks a creature stays invulnerable after a hurt (0: every hit counts). A lower value shortens the open windows at once.

See also hurtTicks().

void QcMobManager::setNaturalSpawning(bool on)

Sets the game rule mobSpawning to on: without it no creature spawns by itself, neither around players nor with new chunks (decision E-221); /summon and world setups still add them.

See also naturalSpawning().

void QcMobManager::setNextUid(quint64 next)

Sets nextUid() to next, unless that is lower.

See also nextUid().

void QcMobManager::setPathLimits(qint64 nodesPerTick, qint64 nodesPerSearch, qint64 pathLength)

Sets the budgets of the path searches: nodesPerTick, nodesPerSearch, pathLength (QcServerLimits pathNodesPerTick, pathNodesPerSearch, pathLength).

void QcMobManager::setWeather(float rain, float thunder)

Sets the strengths 0..1 of rain and thunder (QcWeatherState), which darken the sky for the monsters' light test (QcSpawnLight::skyDarkening).

void QcMobManager::setYawRandom(QRandomGenerator *random)

Sets random as the sequence the yaws of new creatures come from (the monster spawning's own, decision E-836); null returns to the creatures' sequence. It must outlive its use.

quint32 QcMobManager::spawn(QcCreatureKindId type, const QcVec3 &position, quint32 id, bool underground = false, bool persistent = false)

Spawns a mob of the kind type at position with the id id directly (commands, tests), in a cave when underground, kept with the world when persistent; returns its id.

QcNaturalSpawner &QcMobManager::spawner()

Returns the natural spawning around players and with new chunks: caps, spawn chunks (decision E-086).

const QcNaturalSpawner &QcMobManager::spawner() const

Returns the natural spawning, read-only.

[static] QcMobManager::Stored QcMobManager::storedOf(const QcCreature &mob)

Returns mob as saved with the world.

QList<QcMobManager::BlockAction> QcMobManager::takeBlockActions()

Returns the blocks the creatures took or put down since the last call (decision E-460).

QList<QcMobManager::CreatureHit> QcMobManager::takeCreatureHits()

Returns the blows of creatures at creatures since the last call (QcMobBrain::Context::creatureHits).

QList<QcProjectileManager::Launch> QcMobManager::takeLaunches()

Returns the shots of the creatures since the last call (QcMobBrain::Context::launches, decision E-425); the server adds them as projectiles after the tick.

bool QcMobManager::teleport(quint32 mobId, const QcBlockAccess &world)

Teleports the creature mobId to a safe place near it in world (QcMobTeleport) when its kind teleports and it may now; returns true when it did.

QList<QcMobManager::PlayerHit> QcMobManager::tick(QcServerWorld &world, const QList<QcMobManager::PlayerView> &players, double worldTime, double dt, const std::function<quint32 ()> &nextEntityId)

Advances all mobs in world among players by one tick of dt seconds at worldTime, new creatures with ids from nextEntityId, and returns the hits they dealt to players.

void QcMobManager::unload(const std::function<bool (const QcCreature &)> &leaves)

Takes the mobs leaves picks out of the store because their chunk unloads (Departure::Unloaded).

QcVillagerBehaviour &QcMobManager::villagers()

Returns the villagers' day, doors, roads and trades (decision E-411).

const QcVillagerBehaviour &QcMobManager::villagers() const

Returns the villagers' behaviour, read-only.

std::optional<QList<QcItemDrop>> QcMobManager::wound(quint32 mobId, double amount)

Deals amount damage like hurt() to the mob mobId; returns the mob's drops when it died (cramming, decision E-401).

std::optional<QList<QcItemDrop>> QcMobManager::woundAt(qsizetype index, double amount)

Deals amount damage like wound() to the mob at index in mobs() and returns its drops when it died; a death removes it from the list.

[delete] QcMobManager &QcMobManager::operator=(const QcMobManager &)

Copy-assigns other to this QcMobManager instance. This function is deleted.

Member Variable Documentation

const double QcMobManager::despawnDistance

Creatures that are not kept vanish at once beyond this distance from every player (decision E-836).

const double QcMobManager::knockbackLift

The upward speed of a blow's push at strength 1; a weaker push lifts less, a stronger one only pushes further (decision E-427).

const double QcMobManager::knockbackSpeed

The horizontal speed of a blow's push at strength 1 (m/s).