forked from OpenDungeons/OpenDungeons
/
RoomPortal.cpp
195 lines (160 loc) · 6.14 KB
/
RoomPortal.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
183
184
185
186
187
188
189
190
191
192
193
194
195
/*
* Copyright (C) 2011-2014 OpenDungeons Team
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "RoomPortal.h"
#include "ODServer.h"
#include "Seat.h"
#include "Player.h"
#include "Creature.h"
#include "Tile.h"
#include "RoomObject.h"
#include "GameMap.h"
#include "Weapon.h"
#include "CreatureAction.h"
#include "Random.h"
#include <cmath>
RoomPortal::RoomPortal() :
mSpawnCreatureCountdown(0),
mXCenter(0),
mYCenter(0),
mPortalObject(NULL)
{
mType = portal;
}
void RoomPortal::absorbRoom(Room* room)
{
Room::absorbRoom(room);
// Get back the portal mesh reference
if (!mRoomObjects.empty())
{
mPortalObject = mRoomObjects.begin()->second;
}
else
{
// Make sure the reference gets updated when createMesh() is called
mPortalObject = NULL;
}
}
void RoomPortal::createMesh()
{
Room::createMesh();
// Don't recreate the portal if it's already done.
if (mPortalObject != NULL)
return;
mPortalObject = loadRoomObject("PortalObject");
createRoomObjectMeshes();
mPortalObject->setAnimationState("Idle");
}
void RoomPortal::addCoveredTile(Tile* t, double nHP)
{
Room::addCoveredTile(t, nHP);
recomputeCenterPosition();
}
void RoomPortal::removeCoveredTile(Tile* t)
{
Room::removeCoveredTile(t);
// Don't recompute the position.
// Removing a portal tile usually means some creatures are attacking it.
// The portal shouldn't move in that case.
//recomputeCenterPosition();
}
bool RoomPortal::doUpkeep()
{
// Call the super class Room::doUpkeep() function to do any generic upkeep common to all rooms.
Room::doUpkeep();
if (mSpawnCreatureCountdown > 0)
{
--mSpawnCreatureCountdown;
return true;
}
// Randomly choose to spawn a creature.
const double maxCreatures = 15;
//TODO: Improve this probability calculation.
// Count how many creatures are controlled by this color, count both the ones on
// the gameMap and the ones in all the players of that colors' hands'.
double numCreatures = getGameMap()->getCreaturesByColor(getColor()).size();
Seat *controllingSeat = getGameMap()->getSeatByColor(getColor());
for(unsigned int i = 0, numPlayers = getGameMap()->numPlayers(); i < numPlayers; ++i)
{
Player *tempPlayer = getGameMap()->getPlayer(i);
if (tempPlayer->getSeat() == controllingSeat)
numCreatures += tempPlayer->numCreaturesInHand();
}
double targetProbability = powl((maxCreatures - numCreatures) / maxCreatures, 1.5);
if (Random::Double(0.0, 1.0) <= targetProbability)
spawnCreature();
return true;
}
void RoomPortal::spawnCreature()
{
std::cout << "Portal: " << getName() << " spawn creature..." << std::endl;
if (mPortalObject != NULL)
mPortalObject->setAnimationState("Spawn", false);
// If the room has been destroyed, or has not yet been assigned any tiles, then we
// cannot determine where to place the new creature and we should just give up.
if (mCoveredTiles.empty())
return;
Seat* seat = getGameMap()->getSeatByColor(getColor());
if (seat == NULL)
return;
const std::vector<std::string> spawnPool = seat->getSpawnPool();
if (spawnPool.empty())
return;
double randomValue = Random::Int(0, spawnPool.size() - 1);
std::string creatureClassName = spawnPool.at(randomValue);
//TODO: Later check conditions before spawning a creature
CreatureDefinition* classToSpawn = getGameMap()->getClassDescription(creatureClassName);
if (classToSpawn == NULL)
{
std::cout << "Warning: Invalid class name in spawn pool: " << creatureClassName
<< ", for team: " << getColor() << std::endl;
return;
}
std::cout << "Spawning a creature of class " << classToSpawn->getClassName() << std::endl;
// Create a new creature and copy over the class-based creature parameters.
Creature *newCreature = new Creature(getGameMap());
newCreature->setCreatureDefinition(classToSpawn);
// Set the creature specific parameters.
//NOTE: This needs to be modified manually when the level file creature format changes.
newCreature->setName(newCreature->getUniqueCreatureName());
newCreature->setPosition(Ogre::Vector3((Ogre::Real)mXCenter, (Ogre::Real)mYCenter, (Ogre::Real)0.0));
newCreature->setColor(getColor());
//NOTE: This needs to be modified manually when the level file weapon format changes.
newCreature->setWeaponL(new Weapon("none", 0, 1.0, 0, "L", newCreature));
newCreature->setWeaponR(new Weapon("none", 0, 1.0, 0, "R", newCreature));
// Add the creature to the gameMap and create meshes so it is visible.
getGameMap()->addCreature(newCreature);
newCreature->createMesh();
newCreature->getWeaponL()->createMesh();
newCreature->getWeaponR()->createMesh();
mSpawnCreatureCountdown = Random::Uint(15, 30);
//TODO: Inform the clients that this creature has been created by placing a newCreature message in the serverNotificationQueue.
}
void RoomPortal::recomputeCenterPosition()
{
mXCenter = mYCenter = 0.0;
if (mCoveredTiles.empty())
return;
// Loop over the covered tiles and compute the average location (i.e. the center) of the portal.
for (unsigned int i = 0; i < mCoveredTiles.size(); ++i)
{
Tile *tempTile = mCoveredTiles[i];
mXCenter += tempTile->x;
mYCenter += tempTile->y;
}
mXCenter /= static_cast<double>(mCoveredTiles.size());
mYCenter /= static_cast<double>(mCoveredTiles.size());
}