summaryrefslogtreecommitdiffstats
path: root/src/Entities/Pawn.cpp
blob: 41a5b33ff142a498fbfacfcc2deedf61e5c83442 (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

#include "Globals.h"  // NOTE: MSVC stupidness requires this to be the same across all modules

#include "Pawn.h"
#include "../World.h"
#include "../Bindings/PluginManager.h"





cPawn::cPawn(eEntityType a_EntityType, double a_Width, double a_Height):
	super(a_EntityType, 0, 0, 0, a_Width, a_Height),
	m_EntityEffects(tEffectMap())
{
}





void cPawn::Tick(float a_Dt, cChunk & a_Chunk)
{
	// Iterate through this entity's applied effects
	for (tEffectMap::iterator iter = m_EntityEffects.begin(); iter != m_EntityEffects.end();)
	{
		// Copies values to prevent pesky wrong accesses and erasures
		cEntityEffect::eType EffectType = iter->first;
		cEntityEffect & EffectValues = iter->second;
		
		// Apply entity effect
		HandleEntityEffect(EffectType, EffectValues);
		
		// Reduce the effect's duration
		EffectValues.m_Ticks++;
		
		// Iterates (must be called before any possible erasure)
		++iter;
		
		// Remove effect if duration has elapsed
		if (EffectValues.GetDuration() - EffectValues.m_Ticks <= 0)
		{
			RemoveEntityEffect(EffectType);
		}
		
		// TODO: Check for discrepancies between client and server effect values
	}
	
	super::Tick(a_Dt, a_Chunk);
}





void cPawn::KilledBy(cEntity * a_Killer)
{
	ClearEntityEffects();
}





void cPawn::AddEntityEffect(cEntityEffect::eType a_EffectType, int a_EffectDurationTicks, short a_EffectIntensity, cPawn * a_User, double a_DistanceModifier)
{
	AddEntityEffect(a_EffectType, cEntityEffect(a_EffectDurationTicks, a_EffectIntensity, a_User, a_DistanceModifier));
}





void cPawn::AddEntityEffect(cEntityEffect::eType a_EffectType, cEntityEffect a_Effect)
{
	// Check if the plugins allow the addition:
	if (cPluginManager::Get()->CallHookEntityAddEffect(*this, a_EffectType, a_Effect.GetDuration(), a_Effect.GetIntensity(), a_Effect.GetUser(), a_Effect.GetDistanceModifier()))
	{
		// A plugin disallows the addition, bail out.
		return;
	}
	
	// No need to add empty effects:
	if (a_EffectType == cEntityEffect::effNoEffect)
	{
		return;
	}
	
	a_Effect.SetDuration(a_Effect.GetDuration() * a_Effect.GetDistanceModifier());
	m_EntityEffects[a_EffectType] = a_Effect;
	m_World->BroadcastEntityEffect(*this, a_EffectType, a_Effect.GetIntensity(), a_Effect.GetDuration());
}





void cPawn::RemoveEntityEffect(cEntityEffect::eType a_EffectType)
{
	m_EntityEffects.erase(a_EffectType);
	m_World->BroadcastRemoveEntityEffect(*this, a_EffectType);
}





void cPawn::ClearEntityEffects()
{
	// Iterate through this entity's applied effects
	for (tEffectMap::iterator iter = m_EntityEffects.begin(); iter != m_EntityEffects.end();)
	{
		// Copy values to prevent pesky wrong erasures
		cEntityEffect::eType EffectType = iter->first;
		
		// Iterates (must be called before any possible erasure)
		++iter;
		
		// Remove effect
		RemoveEntityEffect(EffectType);
	}
}





void cPawn::HandleEntityEffect(cEntityEffect::eType a_EffectType, cEntityEffect a_Effect)
{
	switch (a_EffectType)
	{
		// Default effect behaviors
		case cEntityEffect::effInstantHealth:
		{
			// Base heal = 6, doubles for every increase in intensity
			Heal((int)(6 * std::pow(2.0, a_Effect.GetIntensity()) * a_Effect.GetDistanceModifier()));
			return;
		}
		case cEntityEffect::effInstantDamage:
		{
			// Base damage = 6, doubles for every increase in intensity
			int damage = (int)(6 * std::pow(2.0, a_Effect.GetIntensity()) * a_Effect.GetDistanceModifier());
			TakeDamage(dtPotionOfHarming, a_Effect.GetUser(), damage, 0);
			return;
		}
		case cEntityEffect::effStrength:
		{
			// TODO: Implement me!
			return;
		}
		case cEntityEffect::effWeakness:
		{
			// Damage reduction = 0.5 damage, multiplied by potion level (Weakness II = 1 damage)
			// double dmg_reduc = 0.5 * (a_Effect.GetIntensity() + 1);
			
			// TODO: Implement me!
			// TODO: Weakened villager zombies can be turned back to villagers with the god apple
			return;
		}
		case cEntityEffect::effRegeneration:
		{
			// Regen frequency = 50 ticks, divided by potion level (Regen II = 25 ticks)
			int frequency = std::floor(50.0 / (double)(a_Effect.GetIntensity() + 1));
			
			if (a_Effect.m_Ticks % frequency == 0)
			{
				Heal(1);
			}
			
			return;
		}
		case cEntityEffect::effPoison:
		{
			// Poison frequency = 25 ticks, divided by potion level (Poison II = 12 ticks)
			int frequency = std::floor(25.0 / (double)(a_Effect.GetIntensity() + 1));
			
			if (a_Effect.m_Ticks % frequency == 0)
			{
				// Cannot take poison damage when health is at 1
				if (GetHealth() > 1)
				{
					TakeDamage(dtPoisoning, a_Effect.GetUser(), 1, 0);
				}
			}
			
			return;
		}
		case cEntityEffect::effWither:
		{
			// Poison frequency = 40 ticks, divided by effect level (Wither II = 20 ticks)
			int frequency = std::floor(25.0 / (double)(a_Effect.GetIntensity() + 1));
			
			if (a_Effect.m_Ticks % frequency == 0)
			{
				TakeDamage(dtWither, a_Effect.GetUser(), 1, 0);
			}
			//TODO: "<Player> withered away>
			return;
		}
		case cEntityEffect::effFireResistance:
		{
			// TODO: Implement me!
			return;
		}
		case cEntityEffect::effSpeed:
		{
			// TODO: Implement me!
			return;
		}
		case cEntityEffect::effSlowness:
		{
			// TODO: Implement me!
			return;
		}
	}
}