Skip to content

Commit

Permalink
[s2479] Creature: Move from an adhoc model to probability model for d…
Browse files Browse the repository at this point in the history
…isplayids

Rename is per official naming
ModelId is smth different
  • Loading branch information
killerwife committed Apr 27, 2024
1 parent 956579b commit e483ab6
Show file tree
Hide file tree
Showing 8 changed files with 63 additions and 51 deletions.
16 changes: 10 additions & 6 deletions sql/base/mangos.sql
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ DROP TABLE IF EXISTS `db_version`;
CREATE TABLE `db_version` (
`version` varchar(120) DEFAULT NULL,
`creature_ai_version` varchar(120) DEFAULT NULL,
`required_s2478_01_mangos_creature_cls_stats` bit(1) DEFAULT NULL
`required_s2479_01_mangos_displayid_probability` bit(1) DEFAULT NULL
) ENGINE=MyISAM DEFAULT CHARSET=utf8 ROW_FORMAT=DYNAMIC COMMENT='Used DB version notes';

--
Expand Down Expand Up @@ -1316,10 +1316,14 @@ CREATE TABLE `creature_template` (
`MinLevel` tinyint(3) unsigned NOT NULL DEFAULT '1',
`MaxLevel` tinyint(3) unsigned NOT NULL DEFAULT '1',
`HeroicEntry` mediumint(8) unsigned NOT NULL DEFAULT '0',
`ModelId1` mediumint(8) unsigned NOT NULL DEFAULT '0',
`ModelId2` mediumint(8) unsigned NOT NULL DEFAULT '0',
`ModelId3` mediumint(8) unsigned NOT NULL DEFAULT '0',
`ModelId4` mediumint(8) unsigned NOT NULL DEFAULT '0',
`DisplayId1` mediumint(8) unsigned NOT NULL DEFAULT '0',
`DisplayId2` mediumint(8) unsigned NOT NULL DEFAULT '0',
`DisplayId3` mediumint(8) unsigned NOT NULL DEFAULT '0',
`DisplayId4` mediumint(8) unsigned NOT NULL DEFAULT '0',
`DisplayIdProbability1` SMALLINT UNSIGNED NOT NULL DEFAULT 0,
`DisplayIdProbability2` SMALLINT UNSIGNED NOT NULL DEFAULT 0,
`DisplayIdProbability3` SMALLINT UNSIGNED NOT NULL DEFAULT 0,
`DisplayIdProbability4` SMALLINT UNSIGNED NOT NULL DEFAULT 0,
`Faction` smallint(5) unsigned NOT NULL DEFAULT '0',
`Scale` float NOT NULL DEFAULT '1',
`Family` tinyint(4) NOT NULL DEFAULT '0',
Expand Down Expand Up @@ -1414,7 +1418,7 @@ CREATE TABLE `creature_template` (
LOCK TABLES `creature_template` WRITE;
/*!40000 ALTER TABLE `creature_template` DISABLE KEYS */;
INSERT INTO `creature_template` VALUES
(1,'Waypoint (Only GM can see it)','Visual',NULL,1,1,0,10045,0,0,0,35,1,8,8,1,1,0,0,4096,0,130,5242886,0,0,0,0,0.91,1.14286,20,0,0,0,0,0,0,-1,1,1,1,1,1,1,1,1,1,1,1,8,8,0,0,7,7,1.76,2.42,0,3,100,2000,2200,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,'','');
(1,'Waypoint (Only GM can see it)','Visual',NULL,1,1,0,10045,0,0,0,100,0,0,0,35,1,8,8,1,1,0,0,4096,0,130,5242886,0,0,0,0,0.91,1.14286,20,0,0,0,0,0,0,-1,1,1,1,1,1,1,1,1,1,1,1,8,8,0,0,7,7,1.76,2.42,0,3,100,2000,2200,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,'','');
/*!40000 ALTER TABLE `creature_template` ENABLE KEYS */;
UNLOCK TABLES;

Expand Down
19 changes: 19 additions & 0 deletions sql/updates/mangos/s2479_01_mangos_displayid_probability.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
ALTER TABLE db_version CHANGE COLUMN required_s2478_01_mangos_creature_cls_stats required_s2479_01_mangos_displayid_probability bit;

ALTER TABLE `creature_template` CHANGE `ModelId1` `DisplayId1` MEDIUMINT UNSIGNED NOT NULL DEFAULT 0;
ALTER TABLE `creature_template` CHANGE `ModelId2` `DisplayId2` MEDIUMINT UNSIGNED NOT NULL DEFAULT 0;
ALTER TABLE `creature_template` CHANGE `ModelId3` `DisplayId3` MEDIUMINT UNSIGNED NOT NULL DEFAULT 0;
ALTER TABLE `creature_template` CHANGE `ModelId4` `DisplayId4` MEDIUMINT UNSIGNED NOT NULL DEFAULT 0;
ALTER TABLE `creature_template` ADD COLUMN `DisplayIdProbability1` SMALLINT UNSIGNED NOT NULL DEFAULT 0 AFTER `DisplayId4`;
ALTER TABLE `creature_template` ADD COLUMN `DisplayIdProbability2` SMALLINT UNSIGNED NOT NULL DEFAULT 0 AFTER `DisplayIdProbability1`;
ALTER TABLE `creature_template` ADD COLUMN `DisplayIdProbability3` SMALLINT UNSIGNED NOT NULL DEFAULT 0 AFTER `DisplayIdProbability2`;
ALTER TABLE `creature_template` ADD COLUMN `DisplayIdProbability4` SMALLINT UNSIGNED NOT NULL DEFAULT 0 AFTER `DisplayIdProbability3`;

-- setting probabilities to exactly replicate previous behaviour
UPDATE creature_template SET DisplayIdProbability1=100 WHERE DisplayId1!=0;
UPDATE creature_template SET DisplayIdProbability1=50,DisplayIdProbability2=50 WHERE DisplayId1!=0 AND DisplayId2!=0 AND EXISTS(SELECT modelid FROM creature_model_info WHERE creature_template.DisplayId2=creature_model_info.modelid AND modelid_alternative > 0);
UPDATE creature_template SET DisplayIdProbability1=0,DisplayIdProbability2=0 WHERE DisplayId1!=0 AND DisplayId2!=0 AND NOT EXISTS(SELECT modelid FROM creature_model_info WHERE creature_template.DisplayId2=creature_model_info.modelid AND modelid_alternative > 0);
UPDATE creature_template SET DisplayIdProbability1=33,DisplayIdProbability2=33,DisplayIdProbability3=33 WHERE DisplayId1!=0 AND DisplayId2!=0 AND DisplayId3!=0 AND EXISTS(SELECT modelid FROM creature_model_info WHERE creature_template.DisplayId2=creature_model_info.modelid AND modelid_alternative > 0);
UPDATE creature_template SET DisplayIdProbability1=0,DisplayIdProbability2=0,DisplayIdProbability3=100 WHERE DisplayId1!=0 AND DisplayId2!=0 AND DisplayId3!=0 AND NOT EXISTS(SELECT modelid FROM creature_model_info WHERE creature_template.DisplayId2=creature_model_info.modelid AND modelid_alternative > 0);
UPDATE creature_template SET DisplayIdProbability1=25,DisplayIdProbability2=25,DisplayIdProbability3=25,DisplayIdProbability4=25 WHERE DisplayId1!=0 AND DisplayId2!=0 AND DisplayId3!=0 AND DisplayId4!=0;

56 changes: 22 additions & 34 deletions src/game/Entities/Creature.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -674,37 +674,25 @@ uint32 Creature::ChooseDisplayId(const CreatureInfo* cinfo, const CreatureData*

// use defaults from the template
uint32 display_id = 0;
// pick based on probability
uint32 chanceTotal = 0;
for (uint32 i = 0; i < MAX_CREATURE_MODEL; ++i)
if (cinfo->DisplayId[i])
chanceTotal += cinfo->DisplayIdProbability[i];

// models may be categorized as (in this order):
// if mod4 && mod3 && mod2 && mod1 use any, by 25%-chance (other gender is selected and replaced after this function)
// if mod3 && mod2 && mod1 use mod3 unless mod2 has modelid_alt_model (then all by 33%-chance)
// if mod2 use mod2 unless mod2 has modelid_alt_model (then both by 50%-chance)
// if mod1 use mod1

// The follow decision tree needs to be updated if MAX_CREATURE_MODEL is changed.
static_assert(MAX_CREATURE_MODEL == 4, "Need to update model selection code for new or removed model fields");

// model selected here may be replaced with other_gender using own function
if (cinfo->ModelId[3] && cinfo->ModelId[2] && cinfo->ModelId[1] && cinfo->ModelId[0])
{
display_id = cinfo->ModelId[urand(0, 3)];
}
else if (cinfo->ModelId[2] && cinfo->ModelId[1] && cinfo->ModelId[0])
{
uint32 modelid_tmp = sObjectMgr.GetCreatureModelAlternativeModel(cinfo->ModelId[1]);
display_id = modelid_tmp ? cinfo->ModelId[urand(0, 2)] : cinfo->ModelId[2];
}
else if (cinfo->ModelId[1])
{
// We use this to eliminate invisible models vs. "dummy" models (infernals, etc).
// Where it's expected to select one of two, model must have a alternative model defined (alternative model is normally the same as defined in ModelId1).
// Same pattern is used in the above model selection, but the result may be ModelId3 and not ModelId2 as here.
uint32 modelid_tmp = sObjectMgr.GetCreatureModelAlternativeModel(cinfo->ModelId[1]);
display_id = modelid_tmp ? cinfo->ModelId[urand(0, 1)] : cinfo->ModelId[1];
}
else if (cinfo->ModelId[0])
int32 roll = irand(0, std::max(int32(chanceTotal) - 1, 0)); // avoid 0
for (uint32 i = 0; i < MAX_CREATURE_MODEL; ++i)
{
display_id = cinfo->ModelId[0];
if (cinfo->DisplayId[i])
{
if (roll < cinfo->DisplayIdProbability[i])
{
display_id = cinfo->DisplayId[i];
break;
}
else
roll -= cinfo->DisplayIdProbability[i];
}
}

// fail safe, we use creature entry 1 and make error
Expand All @@ -713,7 +701,7 @@ uint32 Creature::ChooseDisplayId(const CreatureInfo* cinfo, const CreatureData*
sLog.outErrorDb("Call customer support, ChooseDisplayId can not select native model for creature entry %u, model from creature entry 1 will be used instead.", cinfo->Entry);

if (const CreatureInfo* creatureDefault = ObjectMgr::GetCreatureTemplate(1))
display_id = creatureDefault->ModelId[0];
display_id = creatureDefault->DisplayId[0];
}

return display_id;
Expand Down Expand Up @@ -1221,12 +1209,12 @@ void Creature::SaveToDB(uint32 mapid, uint8 spawnMask)
// The following if-else assumes that there are 4 model fields and needs updating if this is changed.
static_assert(MAX_CREATURE_MODEL == 4, "Need to update custom model check for new/removed model fields.");

if (displayId != cinfo->ModelId[0] && displayId != cinfo->ModelId[1] &&
displayId != cinfo->ModelId[2] && displayId != cinfo->ModelId[3])
if (displayId != cinfo->DisplayId[0] && displayId != cinfo->DisplayId[1] &&
displayId != cinfo->DisplayId[2] && displayId != cinfo->DisplayId[3])
{
for (int i = 0; i < MAX_CREATURE_MODEL && displayId; ++i)
if (cinfo->ModelId[i])
if (CreatureModelInfo const* minfo = sObjectMgr.GetCreatureModelInfo(cinfo->ModelId[i]))
if (cinfo->DisplayId[i])
if (CreatureModelInfo const* minfo = sObjectMgr.GetCreatureModelInfo(cinfo->DisplayId[i]))
if (displayId == minfo->modelid_other_gender)
displayId = 0;
}
Expand Down
3 changes: 2 additions & 1 deletion src/game/Entities/Creature.h
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,8 @@ struct CreatureInfo
uint32 MinLevel;
uint32 MaxLevel;
uint32 HeroicEntry;
uint32 ModelId[MAX_CREATURE_MODEL];
uint32 DisplayId[MAX_CREATURE_MODEL];
uint32 DisplayIdProbability[MAX_CREATURE_MODEL];
uint32 Faction;
float Scale;
uint32 Family; // enum CreatureFamily values (optional)
Expand Down
2 changes: 1 addition & 1 deletion src/game/Entities/QueryHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ void WorldSession::HandleCreatureQueryOpcode(WorldPacket& recv_data)
data << uint32(0); // unknown wdbFeild11
data << uint32(ci->PetSpellDataId); // Id from CreatureSpellData.dbc wdbField12

for (uint32 i : ci->ModelId)
for (uint32 i : ci->DisplayId)
data << i;

data << float(ci->HealthMultiplier); // health multiplier
Expand Down
12 changes: 6 additions & 6 deletions src/game/Globals/ObjectMgr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -547,20 +547,20 @@ void ObjectMgr::LoadCreatureTemplates()

for (int j = 0; j < MAX_CREATURE_MODEL; ++j)
{
if (cInfo->ModelId[j])
if (cInfo->DisplayId[j])
{
CreatureDisplayInfoEntry const* displayEntry = sCreatureDisplayInfoStore.LookupEntry(cInfo->ModelId[j]);
CreatureDisplayInfoEntry const* displayEntry = sCreatureDisplayInfoStore.LookupEntry(cInfo->DisplayId[j]);
if (!displayEntry)
{
sLog.outErrorDb("Creature (Entry: %u) has nonexistent modelid_%d (%u), can crash client", cInfo->Entry, j + 1, cInfo->ModelId[j]);
const_cast<CreatureInfo*>(cInfo)->ModelId[j] = 0;
sLog.outErrorDb("Creature (Entry: %u) has nonexistent modelid_%d (%u), can crash client", cInfo->Entry, j + 1, cInfo->DisplayId[j]);
const_cast<CreatureInfo*>(cInfo)->DisplayId[j] = 0;
}
else if (!displayScaleEntry)
displayScaleEntry = displayEntry;

CreatureModelInfo const* minfo = sCreatureModelStorage.LookupEntry<CreatureModelInfo>(cInfo->ModelId[j]);
CreatureModelInfo const* minfo = sCreatureModelStorage.LookupEntry<CreatureModelInfo>(cInfo->DisplayId[j]);
if (!minfo)
sLog.outErrorDb("Creature (Entry: %u) are using modelid_%d (%u), but creature_model_info are missing for this model.", cInfo->Entry, j + 1, cInfo->ModelId[j]);
sLog.outErrorDb("Creature (Entry: %u) are using modelid_%d (%u), but creature_model_info are missing for this model.", cInfo->Entry, j + 1, cInfo->DisplayId[j]);
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/game/Server/SQLStorages.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@

#include "Server/SQLStorages.h"

const char CreatureInfosrcfmt[] = "isssiiiiiiiifiiiiliiiiiiiiiffiiiiiiiifffffffffffiiiiffffiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiss";
const char CreatureInfodstfmt[] = "isssiiiiiiiifiiiiliiiiiiiiiffiiiiiiiifffffffffffiiiiffffiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiisi";
const char CreatureInfosrcfmt[] = "isssiiiiiiiiiiiifiiiiliiiiiiiiiffiiiiiiiifffffffffffiiiiffffiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiss";
const char CreatureInfodstfmt[] = "isssiiiiiiiiiiiifiiiiliiiiiiiiiffiiiiiiiifffffffffffiiiiffffiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiisi";
const char CreatureDataAddonInfofmt[] = "iibbiis";
const char CreatureConditionalSpawnSrcFmt[] = "iiix";
const char CreatureConditionalSpawnDstFmt[] = "iii";
Expand Down
2 changes: 1 addition & 1 deletion src/shared/revision_sql.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@
#define REVISION_DB_REALMD "required_s2474_01_realmd_joindate_datetime"
#define REVISION_DB_LOGS "required_s2433_01_logs_anticheat"
#define REVISION_DB_CHARACTERS "required_s2473_01_characters_item_instance_text_id_fix"
#define REVISION_DB_MANGOS "required_s2478_01_mangos_creature_cls_stats"
#define REVISION_DB_MANGOS "required_s2479_01_mangos_displayid_probability"
#endif // __REVISION_SQL_H__

4 comments on commit e483ab6

@insunaa
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can this be used to show bunnies in GM mode, and can we now disable incorrect display IDs through the probability fields?

@beirbones
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seem to be having an issue applying this to a sql server

> Trying to apply cmangos_custom.sql ... FAILED!
>>> ERROR 1054 (42S22) at line 138: Unknown column 'modelid2' in 'field list'

@killerwife
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am merging it to tbc-db rn. Also you can disable anything through probability as you wish.

@beirbones
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am merging it to tbc-db rn. Also you can disable anything through probability as you wish.
Just bad timing on my part then :)

Please sign in to comment.