summaryrefslogtreecommitdiffstats
path: root/source/MobTypesManager.cpp
blob: a2f81cf5b7f29faec602f2ccd21466a114b29e9c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229

#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;
}