summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authormadmaxoft <github@xoft.cz>2013-10-20 13:25:56 +0200
committermadmaxoft <github@xoft.cz>2013-10-20 13:25:56 +0200
commit848d061de167be9fd802cc8a6b14a934702af81a (patch)
tree5436be82842e1883588fb07dcc634ba2381ea368
parentRewritten MobTypesManager not to leak memory. (diff)
downloadcuberite-848d061de167be9fd802cc8a6b14a934702af81a.tar
cuberite-848d061de167be9fd802cc8a6b14a934702af81a.tar.gz
cuberite-848d061de167be9fd802cc8a6b14a934702af81a.tar.bz2
cuberite-848d061de167be9fd802cc8a6b14a934702af81a.tar.lz
cuberite-848d061de167be9fd802cc8a6b14a934702af81a.tar.xz
cuberite-848d061de167be9fd802cc8a6b14a934702af81a.tar.zst
cuberite-848d061de167be9fd802cc8a6b14a934702af81a.zip
-rw-r--r--VC2008/MCServer.vcproj8
-rw-r--r--source/Bindings.cpp96
-rw-r--r--source/Bindings.h2
-rw-r--r--source/MobSpawner.cpp6
-rw-r--r--source/MobTypesManager.cpp229
-rw-r--r--source/MobTypesManager.h41
-rw-r--r--source/Mobs/Monster.cpp227
-rw-r--r--source/Mobs/Monster.h26
-rw-r--r--source/World.cpp14
9 files changed, 351 insertions, 298 deletions
diff --git a/VC2008/MCServer.vcproj b/VC2008/MCServer.vcproj
index 82227097f..8c432280f 100644
--- a/VC2008/MCServer.vcproj
+++ b/VC2008/MCServer.vcproj
@@ -739,14 +739,6 @@
>
</File>
<File
- RelativePath="..\source\MobTypesManager.cpp"
- >
- </File>
- <File
- RelativePath="..\source\MobTypesManager.h"
- >
- </File>
- <File
RelativePath="..\source\MonsterConfig.cpp"
>
</File>
diff --git a/source/Bindings.cpp b/source/Bindings.cpp
index 3fa93fce7..e0ab2b2b3 100644
--- a/source/Bindings.cpp
+++ b/source/Bindings.cpp
@@ -1,6 +1,6 @@
/*
** Lua binding: AllToLua
-** Generated automatically by tolua++-1.0.92 on 10/20/13 10:19:10.
+** Generated automatically by tolua++-1.0.92 on 10/20/13 13:24:03.
*/
#ifndef __cplusplus
@@ -29261,6 +29261,97 @@ static int tolua_AllToLua_cMonster_GetMobFamily00(lua_State* tolua_S)
}
#endif //#ifndef TOLUA_DISABLE
+/* method: MobTypeToString of class cMonster */
+#ifndef TOLUA_DISABLE_tolua_AllToLua_cMonster_MobTypeToString00
+static int tolua_AllToLua_cMonster_MobTypeToString00(lua_State* tolua_S)
+{
+#ifndef TOLUA_RELEASE
+ tolua_Error tolua_err;
+ if (
+ !tolua_isusertable(tolua_S,1,"cMonster",0,&tolua_err) ||
+ !tolua_isnumber(tolua_S,2,0,&tolua_err) ||
+ !tolua_isnoobj(tolua_S,3,&tolua_err)
+ )
+ goto tolua_lerror;
+ else
+#endif
+ {
+ cMonster::eType a_MobType = ((cMonster::eType) (int) tolua_tonumber(tolua_S,2,0));
+ {
+ AString tolua_ret = (AString) cMonster::MobTypeToString(a_MobType);
+ tolua_pushcppstring(tolua_S,(const char*)tolua_ret);
+ }
+ }
+ return 1;
+#ifndef TOLUA_RELEASE
+ tolua_lerror:
+ tolua_error(tolua_S,"#ferror in function 'MobTypeToString'.",&tolua_err);
+ return 0;
+#endif
+}
+#endif //#ifndef TOLUA_DISABLE
+
+/* method: StringToMobType of class cMonster */
+#ifndef TOLUA_DISABLE_tolua_AllToLua_cMonster_StringToMobType00
+static int tolua_AllToLua_cMonster_StringToMobType00(lua_State* tolua_S)
+{
+#ifndef TOLUA_RELEASE
+ tolua_Error tolua_err;
+ if (
+ !tolua_isusertable(tolua_S,1,"cMonster",0,&tolua_err) ||
+ !tolua_iscppstring(tolua_S,2,0,&tolua_err) ||
+ !tolua_isnoobj(tolua_S,3,&tolua_err)
+ )
+ goto tolua_lerror;
+ else
+#endif
+ {
+ const AString a_MobTypeName = ((const AString) tolua_tocppstring(tolua_S,2,0));
+ {
+ cMonster::eType tolua_ret = (cMonster::eType) cMonster::StringToMobType(a_MobTypeName);
+ tolua_pushnumber(tolua_S,(lua_Number)tolua_ret);
+ tolua_pushcppstring(tolua_S,(const char*)a_MobTypeName);
+ }
+ }
+ return 2;
+#ifndef TOLUA_RELEASE
+ tolua_lerror:
+ tolua_error(tolua_S,"#ferror in function 'StringToMobType'.",&tolua_err);
+ return 0;
+#endif
+}
+#endif //#ifndef TOLUA_DISABLE
+
+/* method: FamilyFromType of class cMonster */
+#ifndef TOLUA_DISABLE_tolua_AllToLua_cMonster_FamilyFromType00
+static int tolua_AllToLua_cMonster_FamilyFromType00(lua_State* tolua_S)
+{
+#ifndef TOLUA_RELEASE
+ tolua_Error tolua_err;
+ if (
+ !tolua_isusertable(tolua_S,1,"cMonster",0,&tolua_err) ||
+ !tolua_isnumber(tolua_S,2,0,&tolua_err) ||
+ !tolua_isnoobj(tolua_S,3,&tolua_err)
+ )
+ goto tolua_lerror;
+ else
+#endif
+ {
+ cMonster::eType a_MobType = ((cMonster::eType) (int) tolua_tonumber(tolua_S,2,0));
+ {
+ cMonster::eFamily tolua_ret = (cMonster::eFamily) cMonster::FamilyFromType(a_MobType);
+ tolua_pushnumber(tolua_S,(lua_Number)tolua_ret);
+ }
+ }
+ return 1;
+#ifndef TOLUA_RELEASE
+ tolua_lerror:
+ tolua_error(tolua_S,"#ferror in function 'FamilyFromType'.",&tolua_err);
+ return 0;
+#endif
+}
+#endif //#ifndef TOLUA_DISABLE
+
/* Open function */
TOLUA_API int tolua_AllToLua_open (lua_State* tolua_S)
{
@@ -31353,6 +31444,9 @@ TOLUA_API int tolua_AllToLua_open (lua_State* tolua_S)
tolua_constant(tolua_S,"mfMaxplusone",cMonster::mfMaxplusone);
tolua_function(tolua_S,"GetMobType",tolua_AllToLua_cMonster_GetMobType00);
tolua_function(tolua_S,"GetMobFamily",tolua_AllToLua_cMonster_GetMobFamily00);
+ tolua_function(tolua_S,"MobTypeToString",tolua_AllToLua_cMonster_MobTypeToString00);
+ tolua_function(tolua_S,"StringToMobType",tolua_AllToLua_cMonster_StringToMobType00);
+ tolua_function(tolua_S,"FamilyFromType",tolua_AllToLua_cMonster_FamilyFromType00);
tolua_endmodule(tolua_S);
tolua_cclass(tolua_S,"cLineBlockTracer","cLineBlockTracer","",NULL);
tolua_beginmodule(tolua_S,"cLineBlockTracer");
diff --git a/source/Bindings.h b/source/Bindings.h
index 0983c7174..cf3da377e 100644
--- a/source/Bindings.h
+++ b/source/Bindings.h
@@ -1,6 +1,6 @@
/*
** Lua binding: AllToLua
-** Generated automatically by tolua++-1.0.92 on 10/20/13 10:19:10.
+** Generated automatically by tolua++-1.0.92 on 10/20/13 13:24:04.
*/
/* Exported function */
diff --git a/source/MobSpawner.cpp b/source/MobSpawner.cpp
index bdeb423c2..75232b30f 100644
--- a/source/MobSpawner.cpp
+++ b/source/MobSpawner.cpp
@@ -2,8 +2,6 @@
#include "Globals.h" // NOTE: MSVC stupidness requires this to be the same across all modules
#include "MobSpawner.h"
-#include "MobTypesManager.h"
-#include "Mobs/Monster.h"
#include "Mobs/IncludeAllMonsters.h"
cMobSpawner::tMobTypes& cMobSpawner::m_MobTypes()
@@ -57,7 +55,7 @@ cMobSpawner::cMobSpawner(cMonster::eFamily a_MonsterFamily,const std::set<cMonst
{
for (std::set<cMonster::eType>::const_iterator itr = a_AllowedTypes.begin(); itr != a_AllowedTypes.end(); itr++)
{
- if (cMobTypesManager::FamilyFromType(*itr) == a_MonsterFamily)
+ if (cMonster::FamilyFromType(*itr) == a_MonsterFamily)
{
m_AllowedTypes.insert(*itr);
}
@@ -256,7 +254,7 @@ cMonster* cMobSpawner::TryToSpawnHere(BLOCKTYPE a_BlockType, NIBBLETYPE a_BlockM
if (CanSpawnHere(m_MobType, a_BlockType, a_BlockMeta, a_BlockType_below, a_BlockMeta_below, a_BlockType_above, a_BlockMeta_above, a_Biome, a_Level))
{
- cMonster* newMob = cMobTypesManager::NewMonsterFromType(m_MobType);
+ cMonster * newMob = cMonster::NewMonsterFromType(m_MobType);
if (newMob)
{
m_Spawned.insert(newMob);
diff --git a/source/MobTypesManager.cpp b/source/MobTypesManager.cpp
deleted file mode 100644
index a2f81cf5b..000000000
--- a/source/MobTypesManager.cpp
+++ /dev/null
@@ -1,229 +0,0 @@
-
-#include "Globals.h" // NOTE: MSVC stupidness requires this to be the same across all modules
-
-#include "MobTypesManager.h"
-#include "MersenneTwister.h"
-#include "Mobs/Monster.h"
-#include "Mobs/IncludeAllMonsters.h"
-#include "FastRandom.h"
-
-
-
-
-
-/** Map for cMonster::eType <-> string
-Needs to be alpha-sorted by the strings, because binary search is used in StringToMobType()
-The strings need to be lowercase (for more efficient comparisons in StringToMobType())
-*/
-static const struct
-{
- cMonster::eType m_Type;
- const char * m_lcName;
-} g_MobTypeNames[] =
-{
- {cMonster::mtBat, "bat"},
- {cMonster::mtBlaze, "blaze"},
- {cMonster::mtCaveSpider, "cavespider"},
- {cMonster::mtChicken, "chicken"},
- {cMonster::mtCow, "cow"},
- {cMonster::mtCreeper, "creeper"},
- {cMonster::mtEnderman, "enderman"},
- {cMonster::mtGhast, "ghast"},
- {cMonster::mtHorse, "horse"},
- {cMonster::mtMagmaCube, "magmacube"},
- {cMonster::mtMooshroom, "mooshroom"},
- {cMonster::mtOcelot, "ocelot"},
- {cMonster::mtPig, "pig"},
- {cMonster::mtSheep, "sheep"},
- {cMonster::mtSilverfish, "silverfish"},
- {cMonster::mtSkeleton, "skeleton"},
- {cMonster::mtSlime, "slime"},
- {cMonster::mtSpider, "spider"},
- {cMonster::mtSquid, "squid"},
- {cMonster::mtVillager, "villager"},
- {cMonster::mtWitch, "witch"},
- {cMonster::mtWolf, "wolf"},
- {cMonster::mtZombie, "zombie"},
- {cMonster::mtZombiePigman, "zombiepigman"},
-} ;
-
-
-
-
-
-cMonster * cMobTypesManager::NewMonsterFromType(cMonster::eType a_MobType, int a_Size)
-{
- cFastRandom Random;
-
- cMonster * toReturn = NULL;
-
- // unspecified size get rand[1,3] for Monsters that need size
- switch (a_MobType)
- {
- case cMonster::mtMagmaCube:
- case cMonster::mtSlime:
- {
- if (a_Size == -1)
- {
- a_Size = Random.NextInt(2, a_MobType) + 1;
- }
- if ((a_Size <= 0) || (a_Size >= 4))
- {
- ASSERT(!"Random for size was supposed to pick in [1..3] and picked outside");
- a_Size = 1;
- }
- break;
- }
- default: break;
- } // switch (a_MobType)
-
- // Create the mob entity
- switch (a_MobType)
- {
- case cMonster::mtMagmaCube: toReturn = new cMagmaCube(a_Size); break;
- case cMonster::mtSlime: toReturn = new cSlime(a_Size); break;
- case cMonster::mtBat: toReturn = new cBat(); break;
- case cMonster::mtBlaze: toReturn = new cBlaze(); break;
- case cMonster::mtCaveSpider: toReturn = new cCavespider(); break;
- case cMonster::mtChicken: toReturn = new cChicken(); break;
- case cMonster::mtCow: toReturn = new cCow(); break;
- case cMonster::mtCreeper: toReturn = new cCreeper(); break;
- case cMonster::mtEnderman: toReturn = new cEnderman(); break;
- case cMonster::mtGhast: toReturn = new cGhast(); break;
- // TODO:
- // case cMonster::mtHorse: toReturn = new cHorse(); break;
- case cMonster::mtMooshroom: toReturn = new cMooshroom(); break;
- case cMonster::mtOcelot: toReturn = new cOcelot(); break;
- case cMonster::mtPig: toReturn = new cPig(); break;
- // TODO: Implement sheep color
- case cMonster::mtSheep: toReturn = new cSheep(0); break;
- case cMonster::mtSilverfish: toReturn = new cSilverfish(); break;
- // TODO: Implement wither skeleton geration
- case cMonster::mtSkeleton: toReturn = new cSkeleton(false); break;
- case cMonster::mtSpider: toReturn = new cSpider(); break;
- case cMonster::mtSquid: toReturn = new cSquid(); break;
- case cMonster::mtVillager: toReturn = new cVillager(cVillager::vtFarmer); break;
- case cMonster::mtWitch: toReturn = new cWitch(); break;
- case cMonster::mtWolf: toReturn = new cWolf(); break;
- case cMonster::mtZombie: toReturn = new cZombie(false); break;
- case cMonster::mtZombiePigman: toReturn = new cZombiePigman(); break;
- default:
- {
- ASSERT(!"Unhandled Mob type");
- }
- }
- return toReturn;
-}
-
-
-
-
-
-AString cMobTypesManager::MobTypeToString(cMonster::eType a_MobType)
-{
- // Mob types aren't sorted, so we need to search linearly:
- for (int i = 0; i < ARRAYCOUNT(g_MobTypeNames); i++)
- {
- if (g_MobTypeNames[i].m_Type == a_MobType)
- {
- return g_MobTypeNames[i].m_lcName;
- }
- }
-
- // Not found:
- return "";
-}
-
-
-
-
-
-cMonster::eType cMobTypesManager::StringToMobType(const AString & a_Name)
-{
- AString lcName(a_Name);
- StrToLower(lcName);
-
- // Binary-search for the lowercase name:
- int lo = 0, hi = ARRAYCOUNT(g_MobTypeNames);
- while (hi - lo > 1)
- {
- int mid = (lo + hi) / 2;
- int res = strcmp(g_MobTypeNames[mid].m_lcName, lcName.c_str());
- if (res == 0)
- {
- return g_MobTypeNames[mid].m_Type;
- }
- if (res < 0)
- {
- hi = mid;
- }
- else
- {
- lo = mid;
- }
- }
- // Range has collapsed to at most two elements, compare each:
- if (strcmp(g_MobTypeNames[lo].m_lcName, lcName.c_str()) == 0)
- {
- return g_MobTypeNames[lo].m_Type;
- }
- if ((lo != hi) && (strcmp(g_MobTypeNames[hi].m_lcName, lcName.c_str()) == 0))
- {
- return g_MobTypeNames[hi].m_Type;
- }
-
- // Not found:
- return cMonster::mtInvalidType;
-}
-
-
-
-
-
-cMonster::eFamily cMobTypesManager::FamilyFromType(cMonster::eType a_Type)
-{
- static const struct
- {
- cMonster::eType m_Type;
- cMonster::eFamily m_Family;
- } TypeMap[] =
- {
- {cMonster::mtBat, cMonster::mfAmbient},
- {cMonster::mtBlaze, cMonster::mfHostile},
- {cMonster::mtCaveSpider, cMonster::mfHostile},
- {cMonster::mtChicken, cMonster::mfPassive},
- {cMonster::mtCow, cMonster::mfPassive},
- {cMonster::mtCreeper, cMonster::mfHostile},
- {cMonster::mtEnderman, cMonster::mfHostile},
- {cMonster::mtGhast, cMonster::mfHostile},
- {cMonster::mtHorse, cMonster::mfPassive},
- {cMonster::mtMagmaCube, cMonster::mfHostile},
- {cMonster::mtMooshroom, cMonster::mfHostile},
- {cMonster::mtOcelot, cMonster::mfHostile},
- {cMonster::mtPig, cMonster::mfPassive},
- {cMonster::mtSheep, cMonster::mfPassive},
- {cMonster::mtSilverfish, cMonster::mfHostile},
- {cMonster::mtSkeleton, cMonster::mfHostile},
- {cMonster::mtSlime, cMonster::mfHostile},
- {cMonster::mtSpider, cMonster::mfHostile},
- {cMonster::mtSquid, cMonster::mfWater},
- {cMonster::mtVillager, cMonster::mfPassive},
- {cMonster::mtWitch, cMonster::mfHostile},
- {cMonster::mtWolf, cMonster::mfHostile},
- {cMonster::mtZombie, cMonster::mfHostile},
- {cMonster::mtZombiePigman, cMonster::mfHostile},
- } ;
-
- for (int i = 0; i < ARRAYCOUNT(TypeMap); i++)
- {
- if (TypeMap[i].m_Type == a_Type)
- {
- return TypeMap[i].m_Family;
- }
- }
- return cMonster::mfMaxplusone;
-}
-
-
-
-
diff --git a/source/MobTypesManager.h b/source/MobTypesManager.h
deleted file mode 100644
index ce50ab4b0..000000000
--- a/source/MobTypesManager.h
+++ /dev/null
@@ -1,41 +0,0 @@
-
-#pragma once
-
-#include "Mobs/Monster.h" // this is a side effect of declaring cMonster::eType inside cMonster MG TODO : make a namespace
-
-
-
-
-// fwd:
-class cFastRandom;
-
-
-
-
-
-/**
-This class aggregates static functions about mob types:
- - create a mob from its type (as enum) (in that way it is a compiler-proxy for mobs)
- - transform MobTypes from enums to string and vice versa
- - return mob family from given type
-*/
-class cMobTypesManager
-{
-public:
- static AString MobTypeToString(cMonster::eType a_MobType);
- static cMonster::eType StringToMobType(const AString& a_MobTypeName);
- static cMonster::eFamily FamilyFromType(cMonster::eType a_MobType);
-
- /** create a new object of the specified mob.
- a_MobType is the type of the mob to be created
- a_Size is the size (for mobs with size)
- if a_Size is let to -1 for entities that need size, size will be random
- asserts and returns null if mob type is not specified
- asserts if invalid size for mobs that need size
- */
- static cMonster * NewMonsterFromType(cMonster::eType a_MobType, int a_Size = -1);
-} ;
-
-
-
-
diff --git a/source/Mobs/Monster.cpp b/source/Mobs/Monster.cpp
index 591c41e22..73abc069d 100644
--- a/source/Mobs/Monster.cpp
+++ b/source/Mobs/Monster.cpp
@@ -1,7 +1,7 @@
#include "Globals.h" // NOTE: MSVC stupidness requires this to be the same across all modules
-#include "Monster.h"
+#include "IncludeAllMonsters.h"
#include "../Root.h"
#include "../Server.h"
#include "../ClientHandle.h"
@@ -9,7 +9,6 @@
#include "../Entities/Player.h"
#include "../Defines.h"
#include "../MonsterConfig.h"
-#include "../MobTypesManager.h"
#include "../MersenneTwister.h"
#include "../Vector3f.h"
@@ -17,13 +16,54 @@
#include "../Vector3d.h"
#include "../Tracer.h"
#include "../Chunk.h"
+#include "../FastRandom.h"
-// #include "../../iniFile/iniFile.h"
-
+/** Map for eType <-> string
+Needs to be alpha-sorted by the strings, because binary search is used in StringToMobType()
+The strings need to be lowercase (for more efficient comparisons in StringToMobType())
+*/
+static const struct
+{
+ cMonster::eType m_Type;
+ const char * m_lcName;
+} g_MobTypeNames[] =
+{
+ {cMonster::mtBat, "bat"},
+ {cMonster::mtBlaze, "blaze"},
+ {cMonster::mtCaveSpider, "cavespider"},
+ {cMonster::mtChicken, "chicken"},
+ {cMonster::mtCow, "cow"},
+ {cMonster::mtCreeper, "creeper"},
+ {cMonster::mtEnderman, "enderman"},
+ {cMonster::mtGhast, "ghast"},
+ {cMonster::mtHorse, "horse"},
+ {cMonster::mtMagmaCube, "magmacube"},
+ {cMonster::mtMooshroom, "mooshroom"},
+ {cMonster::mtOcelot, "ocelot"},
+ {cMonster::mtPig, "pig"},
+ {cMonster::mtSheep, "sheep"},
+ {cMonster::mtSilverfish, "silverfish"},
+ {cMonster::mtSkeleton, "skeleton"},
+ {cMonster::mtSlime, "slime"},
+ {cMonster::mtSpider, "spider"},
+ {cMonster::mtSquid, "squid"},
+ {cMonster::mtVillager, "villager"},
+ {cMonster::mtWitch, "witch"},
+ {cMonster::mtWolf, "wolf"},
+ {cMonster::mtZombie, "zombie"},
+ {cMonster::mtZombiePigman, "zombiepigman"},
+} ;
+
+
+
+
+
+///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+// cMonster:
cMonster::cMonster(const AString & a_ConfigName, eType a_MobType, const AString & a_SoundHurt, const AString & a_SoundDeath, double a_Width, double a_Height)
: super(etMonster, a_Width, a_Height)
@@ -467,6 +507,183 @@ void cMonster::SetSightDistance(float sd)
+AString cMonster::MobTypeToString(cMonster::eType a_MobType)
+{
+ // Mob types aren't sorted, so we need to search linearly:
+ for (int i = 0; i < ARRAYCOUNT(g_MobTypeNames); i++)
+ {
+ if (g_MobTypeNames[i].m_Type == a_MobType)
+ {
+ return g_MobTypeNames[i].m_lcName;
+ }
+ }
+
+ // Not found:
+ return "";
+}
+
+
+
+
+
+cMonster::eType cMonster::StringToMobType(const AString & a_Name)
+{
+ AString lcName(a_Name);
+ StrToLower(lcName);
+
+ // Binary-search for the lowercase name:
+ int lo = 0, hi = ARRAYCOUNT(g_MobTypeNames);
+ while (hi - lo > 1)
+ {
+ int mid = (lo + hi) / 2;
+ int res = strcmp(g_MobTypeNames[mid].m_lcName, lcName.c_str());
+ if (res == 0)
+ {
+ return g_MobTypeNames[mid].m_Type;
+ }
+ if (res < 0)
+ {
+ hi = mid;
+ }
+ else
+ {
+ lo = mid;
+ }
+ }
+ // Range has collapsed to at most two elements, compare each:
+ if (strcmp(g_MobTypeNames[lo].m_lcName, lcName.c_str()) == 0)
+ {
+ return g_MobTypeNames[lo].m_Type;
+ }
+ if ((lo != hi) && (strcmp(g_MobTypeNames[hi].m_lcName, lcName.c_str()) == 0))
+ {
+ return g_MobTypeNames[hi].m_Type;
+ }
+
+ // Not found:
+ return mtInvalidType;
+}
+
+
+
+
+
+cMonster::eFamily cMonster::FamilyFromType(eType a_Type)
+{
+ static const struct
+ {
+ eType m_Type;
+ eFamily m_Family;
+ } TypeMap[] =
+ {
+ {mtBat, mfAmbient},
+ {mtBlaze, mfHostile},
+ {mtCaveSpider, mfHostile},
+ {mtChicken, mfPassive},
+ {mtCow, mfPassive},
+ {mtCreeper, mfHostile},
+ {mtEnderman, mfHostile},
+ {mtGhast, mfHostile},
+ {mtHorse, mfPassive},
+ {mtMagmaCube, mfHostile},
+ {mtMooshroom, mfHostile},
+ {mtOcelot, mfHostile},
+ {mtPig, mfPassive},
+ {mtSheep, mfPassive},
+ {mtSilverfish, mfHostile},
+ {mtSkeleton, mfHostile},
+ {mtSlime, mfHostile},
+ {mtSpider, mfHostile},
+ {mtSquid, mfWater},
+ {mtVillager, mfPassive},
+ {mtWitch, mfHostile},
+ {mtWolf, mfHostile},
+ {mtZombie, mfHostile},
+ {mtZombiePigman, mfHostile},
+ } ;
+
+ for (int i = 0; i < ARRAYCOUNT(TypeMap); i++)
+ {
+ if (TypeMap[i].m_Type == a_Type)
+ {
+ return TypeMap[i].m_Family;
+ }
+ }
+ return mfMaxplusone;
+}
+
+
+
+
+
+cMonster * cMonster::NewMonsterFromType(cMonster::eType a_MobType, int a_Size)
+{
+ cFastRandom Random;
+
+ cMonster * toReturn = NULL;
+
+ // unspecified size get rand[1,3] for Monsters that need size
+ switch (a_MobType)
+ {
+ case mtMagmaCube:
+ case mtSlime:
+ {
+ if (a_Size == -1)
+ {
+ a_Size = Random.NextInt(2, a_MobType) + 1;
+ }
+ if ((a_Size <= 0) || (a_Size >= 4))
+ {
+ ASSERT(!"Random for size was supposed to pick in [1..3] and picked outside");
+ a_Size = 1;
+ }
+ break;
+ }
+ default: break;
+ } // switch (a_MobType)
+
+ // Create the mob entity
+ switch (a_MobType)
+ {
+ case mtMagmaCube: toReturn = new cMagmaCube(a_Size); break;
+ case mtSlime: toReturn = new cSlime(a_Size); break;
+ case mtBat: toReturn = new cBat(); break;
+ case mtBlaze: toReturn = new cBlaze(); break;
+ case mtCaveSpider: toReturn = new cCavespider(); break;
+ case mtChicken: toReturn = new cChicken(); break;
+ case mtCow: toReturn = new cCow(); break;
+ case mtCreeper: toReturn = new cCreeper(); break;
+ case mtEnderman: toReturn = new cEnderman(); break;
+ case mtGhast: toReturn = new cGhast(); break;
+ // TODO:
+ // case cMonster::mtHorse: toReturn = new cHorse(); break;
+ case mtMooshroom: toReturn = new cMooshroom(); break;
+ case mtOcelot: toReturn = new cOcelot(); break;
+ case mtPig: toReturn = new cPig(); break;
+ // TODO: Implement sheep color
+ case mtSheep: toReturn = new cSheep(0); break;
+ case mtSilverfish: toReturn = new cSilverfish(); break;
+ // TODO: Implement wither skeleton geration
+ case mtSkeleton: toReturn = new cSkeleton(false); break;
+ case mtSpider: toReturn = new cSpider(); break;
+ case mtSquid: toReturn = new cSquid(); break;
+ case mtVillager: toReturn = new cVillager(cVillager::vtFarmer); break;
+ case mtWitch: toReturn = new cWitch(); break;
+ case mtWolf: toReturn = new cWolf(); break;
+ case mtZombie: toReturn = new cZombie(false); break;
+ case mtZombiePigman: toReturn = new cZombiePigman(); break;
+ default:
+ {
+ ASSERT(!"Unhandled Mob type");
+ }
+ }
+ return toReturn;
+}
+
+
+
+
+
void cMonster::AddRandomDropItem(cItems & a_Drops, unsigned int a_Min, unsigned int a_Max, short a_Item, short a_ItemHealth)
{
MTRand r1;
@@ -514,7 +731,7 @@ void cMonster::HandleDaylightBurning(cChunk & a_Chunk)
cMonster::eFamily cMonster::GetMobFamily(void) const
{
- return cMobTypesManager::FamilyFromType(m_MobType);
+ return FamilyFromType(m_MobType);
}
diff --git a/source/Mobs/Monster.h b/source/Mobs/Monster.h
index c416d026c..3b7f40c00 100644
--- a/source/Mobs/Monster.h
+++ b/source/Mobs/Monster.h
@@ -71,6 +71,9 @@ public:
// tolua_end
+ enum MState{ATTACKING, IDLE, CHASING, ESCAPING} m_EMState;
+ enum MPersonality{PASSIVE,AGGRESSIVE,COWARDLY} m_EMPersonality;
+
float m_SightDistance;
/** Creates the mob object.
@@ -132,9 +135,28 @@ public:
virtual bool IsTame (void) const { return false; }
virtual bool IsSitting (void) const { return false; }
- enum MState{ATTACKING, IDLE, CHASING, ESCAPING} m_EMState;
- enum MPersonality{PASSIVE,AGGRESSIVE,COWARDLY} m_EMPersonality;
+ // tolua_begin
+
+ /// Translates MobType enum to a string
+ static AString MobTypeToString(eType a_MobType);
+ /// Translates MobType string to the enum
+ static eType StringToMobType(const AString & a_MobTypeName);
+
+ /// Returns the mob family based on the type
+ static eFamily FamilyFromType(eType a_MobType);
+
+ // tolua_end
+
+ /** Creates a new object of the specified mob.
+ a_MobType is the type of the mob to be created
+ a_Size is the size (for mobs with size)
+ if a_Size is let to -1 for entities that need size, size will be random
+ asserts and returns null if mob type is not specified
+ asserts if invalid size for mobs that need size
+ */
+ static cMonster * NewMonsterFromType(eType a_MobType, int a_Size = -1);
+
protected:
cEntity * m_Target;
diff --git a/source/World.cpp b/source/World.cpp
index bcf32df85..2943209b7 100644
--- a/source/World.cpp
+++ b/source/World.cpp
@@ -31,7 +31,6 @@
#include "Mobs/IncludeAllMonsters.h"
#include "MobCensus.h"
#include "MobSpawner.h"
-#include "MobTypesManager.h"
#include "MersenneTwister.h"
#include "Generating/Trees.h"
@@ -508,11 +507,11 @@ void cWorld::Start(void)
AStringVector SplitList = StringSplitAndTrim(AllMonsters, ",");
for (AStringVector::const_iterator itr = SplitList.begin(), end = SplitList.end(); itr != end; ++itr)
{
- cMonster::eType ToAdd = cMobTypesManager::StringToMobType(*itr);
+ cMonster::eType ToAdd = cMonster::StringToMobType(*itr);
if (ToAdd != cMonster::mtInvalidType)
{
m_AllowedMobs.insert(ToAdd);
- LOGD("Allowed mob: %s", cMobTypesManager::MobTypeToString(ToAdd).c_str()); // a bit reverse working, but very few ressources wasted
+ LOGD("Allowed mob: %s", itr->c_str());
}
else
{
@@ -2524,20 +2523,21 @@ int cWorld::SpawnMob(double a_PosX, double a_PosY, double a_PosZ, cMonster::eTyp
{
cMonster * Monster = NULL;
- int SlSize = GetTickRandomNumber(2) + 1; // 1 .. 3 - Slime
int ShColor = GetTickRandomNumber(15); // 0 .. 15 - Sheep
bool SkType = GetDimension() == dimNether ; // Skeleton
- Monster = cMobTypesManager::NewMonsterFromType(a_MonsterType);
- if (Monster)
+ Monster = cMonster::NewMonsterFromType(a_MonsterType);
+ if (Monster != NULL)
+ {
Monster->SetPosition(a_PosX, a_PosY, a_PosZ);
+ }
return SpawnMobFinalize(Monster);
}
-int cWorld::SpawnMobFinalize(cMonster* a_Monster)
+int cWorld::SpawnMobFinalize(cMonster * a_Monster)
{
if (!a_Monster)
return -1;