A comprehensive mob (hostile NPC) system has been added to the game server with the following features:
- Mob Spawning: Zone-based spawning with configurable parameters
- Loot System: Drop tables with chance-based item generation
- Experience Rewards: Level-based experience on mob death
- Respawn System: Automatic respawning with configurable timers
- Mob Variants: Leveled versions of mobs with scaled stats
- Python Integration: Event handlers for mob death, loot drops, and experience
include/game/GameEntity.hpp- Base entity classinclude/game/MobSystem.hpp- Mob system interface
src/game/GameEntity.cpp- Entity base implementationsrc/game/MobSystem.cpp- Mob system implementationsrc/game/NPCSystem.cpp- NPC system implementation (enhanced)
scripts/mobs.py- Python event handlers for mob system
- Updated
config/config.jsonwith mob spawn zones
Mobs spawn in predefined zones with the following properties:
- Center Position: 3D coordinates
- Radius: Spawn area size
- Mob Type: Which mob to spawn (Goblin, Orc, Dragon, Slime)
- Level Range: Min/max level for spawned mobs
- Max Mobs: Maximum mobs in zone at once
- Respawn Time: Time before respawn after death
Example configuration:
{
"name": "goblin_forest",
"center": [100.0, 10.0, 100.0],
"radius": 50.0,
"mobType": 0,
"minLevel": 1,
"maxLevel": 5,
"maxMobs": 15,
"respawnTime": 30.0
}Each mob type has a default loot table with:
- Item ID: Item identifier
- Quantity Range: Min/max quantity
- Drop Chance: Probability (0.0 to 1.0)
- Level Range: Valid mob levels for this loot
Default loot tables are defined for:
- Goblin: Gold coins, goblin ears, rusty sword
- Orc: Gold coins, orc tusks, iron sword, leather armor
- Dragon: Gold coins, dragon scales, dragon heart, legendary sword
- Slime: Gold coins, slime core, health potion
Mobs award experience based on:
- Base Experience: 10 × level
- Mob Type: Different multipliers per type
- Level Scaling: Higher level mobs = more experience
Experience is automatically awarded to the killer when a mob dies.
Mobs can have different levels with scaled stats:
- Health Multiplier: 1.0 + (level - 1) × 0.2
- Damage Multiplier: 1.0 + (level - 1) × 0.15
- Experience: 10 × level
Variants are automatically generated for levels 1-50.
When a mob dies:
- Death is recorded with position and time
- Loot is generated and dropped
- Experience is awarded to killer
- Respawn is scheduled based on zone settings
- Mob is despawned
Respawns are processed periodically, maintaining zone population.
The MobSystem is integrated into GameLogic:
- Initialized during server startup
- Updated each game tick
- Handles mob death in combat interactions
- Processes spawn zones and respawns
When a player attacks an NPC:
- Damage is applied
- If mob dies,
MobSystem::OnMobDeath()is called - Loot is generated and dropped
- Experience is awarded
- Respawn is scheduled
The following events are fired for Python scripts:
mob_death- When a mob diesmob_loot_drop- When loot is droppedplayer_experience_gain- When player gains experiencemob_spawn- When a mob spawns
// Spawn a mob at a specific position
uint64_t mobId = mobSystem.SpawnMob(NPCType::GOBLIN, position, level);
// Spawn a mob in a zone
uint64_t mobId = mobSystem.SpawnMobInZone("goblin_forest");MobSpawnZone zone;
zone.name = "goblin_forest";
zone.center = glm::vec3(100.0f, 10.0f, 100.0f);
zone.radius = 50.0f;
zone.mobType = NPCType::GOBLIN;
zone.minLevel = 1;
zone.maxLevel = 5;
zone.maxMobs = 15;
zone.respawnTime = 30.0f;
mobSystem.RegisterSpawnZone(zone);std::vector<LootItem> lootTable = {
{"gold_coin", 1, 5, 0.8f, 1, 100},
{"rare_item", 1, 1, 0.1f, 10, 50}
};
mobSystem.SetDefaultLootTable(NPCType::GOBLIN, lootTable);Mob system configuration is in config/config.json:
{
"mobs": {
"enabled": true,
"spawnZones": [
{
"name": "goblin_forest",
"center": [100.0, 10.0, 100.0],
"radius": 50.0,
"mobType": 0,
"minLevel": 1,
"maxLevel": 5,
"maxMobs": 15,
"respawnTime": 30.0
}
],
"experienceMultiplier": 1.0,
"lootDropChance": 1.0
}
}Example Python handler:
def on_mob_death(event_data):
mob_id = event_data['data']['mobId']
killer_id = event_data['data']['killerId']
mob_type = event_data['data']['mobType']
# Custom logic here
if mob_type == 2: # Dragon
server.log_info(f"Player {killer_id} defeated a dragon!")
return TrueCurrently supported hostile mobs:
- GOBLIN (0) - Weak, fast, common
- ORC (1) - Medium strength, slower
- DRAGON (2) - Very strong, rare, high rewards
- SLIME (3) - Weak, very common, low rewards
Potential additions:
- Mob AI behaviors (patrol, chase, flee)
- Mob groups/squads
- Elite mob variants
- Boss mobs with special mechanics
- Dynamic spawn zones based on player activity
- Mob quests and objectives
- Mob reputation system
SpawnMob(type, position, level)- Spawn a mobSpawnMobInZone(zoneName)- Spawn in zoneOnMobDeath(mobId, killerId)- Handle mob deathGenerateLoot(type, level)- Generate lootGetExperienceReward(type, level)- Get experience valueRegisterSpawnZone(zone)- Register spawn zoneUpdateSpawnZones(deltaTime)- Update spawn logicProcessRespawns(deltaTime)- Process respawn queue
TakeDamage(damage, attackerId)- Apply damageIsAlive()/IsDead()- Health statusGetStats()- Get mob statisticsSetTarget(targetId)- Set AI targetUpdate(deltaTime)- Update AI and movement