-
Notifications
You must be signed in to change notification settings - Fork 4
/
Monster.cpp
182 lines (149 loc) · 4.1 KB
/
Monster.cpp
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
#include "Monster.h"
#include "Game.h"
#include "DisplayText.h"
#include "Dungeon.h"
CMonster::CMonster()
:m_fHP(0.0f),
m_fCurHP(0.0f),
m_fCurAC(0.0f),
m_fLastHPTime(0.0f),
m_md(NULL),
m_pBrain(NULL),
m_fColorChangeInterval(COLOR_CHANGE_TIMEOUT+1)
{
m_pBrain = new CAIBrain;
}
CMonster::~CMonster()
{
if(m_pBrain)
{
g_pGame->GetAIMgr()->DestroyBrain(m_pBrain);
m_pBrain = NULL;
}
}
void CMonster::Init( CMonsterDef *pmd )
{
m_md = pmd;
if( pmd->m_fBaseHP != 0.0f )
{
m_fHP = pmd->m_fBaseHP;
}
else
{
m_fHP = Util::Roll(pmd->m_szHD);
}
m_fCurHP = m_fHP;
m_fCurAC = pmd->m_fBaseAC;
}
void CMonster::InitBrain( CMonsterDef *pmd )
{
m_pBrain->m_fSpeed = pmd->m_fSpeed;
m_pBrain->m_dwMoveType = pmd->m_dwMoveType;
m_pBrain->SetState(BRAINSTATE_SEEK);
m_pBrain->SetParent(this);
}
JResult CMonster::CreateMonster( CMonsterDef *pmd )
{
int desired = Util::Roll(pmd->m_szAppear);
for( int count=0; count < desired; count++ )
{
CMonster *pMon;
pMon = new CMonster;
// Initialize the Monster from the MonsterDef
pMon->Init(pmd);
// Initialize the Brain
// TODO: move to AIBrain::Init()
pMon->InitBrain(pmd);
// Put the monster in the world
pMon->SpawnMonster();
// Now that the monster is set up, add it to the global lists (monsters, brains)
pMon->m_pllLink = g_pGame->GetDungeon()->m_llMonsters->Add(pMon);
pMon->m_pBrain->m_pllLink = g_pGame->GetAIMgr()->m_llAIBrains->Add(pMon->m_pBrain);
}
return JSUCCESS;
}
JResult CMonster::SpawnMonster()
{
bool bMonsterSpawned = false;
printf("Trying to spawn monster type: %s...", m_md->m_szName);
JVector vTryPos;
while( !bMonsterSpawned )
{
vTryPos.Init( (float)(Util::GetRandom(0, DUNG_WIDTH-1)), (float)(Util::GetRandom(0, DUNG_HEIGHT-1)) );
//printf("Trying to spawn monster type: %d at <%.2f %.2f>...\n", m_md->m_dwType, vTryPos.x, vTryPos.y );
//g_pGame->GetMsgs()->Printf( "Trying to spawn monster type: %d at <%.2f %.2f>...\n", m_md->m_dwType, vTryPos.x, vTryPos.y );
if( g_pGame->GetDungeon()->IsWalkableFor(vTryPos) == DUNG_COLL_NO_COLLISION )
{
SetPos(vTryPos);
g_pGame->GetDungeon()->GetTile(GetPos())->m_pCurMonster = this;
bMonsterSpawned = true;
printf( "Success!\n" );
//g_pGame->GetMsgs()->Printf( "Success!\n" );
}
}
return JSUCCESS;
}
float CMonster::Attack()
{
float fRoll = Util::Roll( "1d100" );
printf( "%s rolled: %.2f\n", GetName(), fRoll );
return fRoll;
}
float CMonster::Damage( float fDamageMult )
{
char *szDamage = m_md->m_llAttacks->GetHead()->m_lpData->m_szDamage;
float fDamageModifier = 0.0f;
float fDamage = ( Util::Roll( szDamage ) + fDamageModifier ) * fDamageMult;
printf( "%s did %.2f damage (damagemult: %.2f). ", GetName(), fDamage, fDamageMult );
return fDamage;
}
bool CMonster::Hit( float &fRoll )
{
return ( fRoll >= m_fCurAC );
}
int CMonster::TakeDamage( float fDamage )
{
int retval = STATUS_INVALID;
if( (int)fDamage < (int)m_fCurHP )
{
m_fCurHP -= fDamage;
retval = STATUS_ALIVE;
}
else
{
m_fCurHP = 0;
retval = STATUS_DEAD;
}
printf( "Remaining HP: %.2f \n", m_fCurHP );
return retval;
}
// draw routines
void CMonster::SetColor()
{
if( m_fColorChangeInterval < COLOR_CHANGE_TIMEOUT ) return;
if( (m_md->m_dwFlags & MON_COLOR_MULTI) == MON_COLOR_MULTI )
{
int which_color = Util::GetRandom(0,m_md->m_Colors->length()-1);
(m_md->m_Color).SetColor(*(m_md->m_Colors->GetLink(which_color)->m_lpData));
}
m_fColorChangeInterval = 0.0f;
}
unsigned char MonIDs[MON_IDX_MAX+1] = "abcddefghhikllmnoprsuwxyzABCDFFFGGHIJKLOPRSTUVWWXY&.,$t";
void CMonster::Draw()
{
Uint8 monster_tile = MonIDs[m_md->m_dwIndex] - ' ' - 1;
JVector DUNG_ASPECT;
SetColor();
//PreDraw();
g_pGame->GetDungeon()->m_TileSet->SetTileColor( m_md->m_Color );
g_pGame->GetDungeon()->m_TileSet->DrawTile( monster_tile, GetPos(), vSize, false );
//PostDraw();
}
void CMonster::PreDraw()
{
g_pGame->GetDungeon()->PreDraw();
}
void CMonster::PostDraw()
{
g_pGame->GetDungeon()->PostDraw();
}