forked from Illarion-eV/Illarion-Server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Monster.cpp
349 lines (307 loc) · 11.6 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
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
// illarionserver - server for the game Illarion
// Copyright 2011 Illarion e.V.
//
// This file is part of illarionserver.
//
// illarionserver 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.
//
// illarionserver 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 illarionserver. If not, see <http://www.gnu.org/licenses/>.
#include "Monster.hpp"
#include "Random.hpp"
#include "tuningConstants.hpp"
#include "data/CommonObjectTable.hpp"
#include <iostream>
#include "data/MonsterTable.hpp"
#include <boost/shared_ptr.hpp>
#include "script/LuaMonsterScript.hpp"
#include "World.hpp"
std::auto_ptr<IdCounter> Monster::monsteridc;
// the table with monster descriptions
extern MonsterTable *MonsterDescriptions;
Monster::Monster(const TYPE_OF_CHARACTER_ID &type, const position &newpos, SpawnPoint *spawnpoint) throw(unknownIDException)
: Character(),lastTargetPosition(position(0,0,0)),lastTargetSeen(false), spawn(spawnpoint), monstertype(type) {
if (monsteridc.get() == 0) {
monsteridc.reset(new IdCounter(configOptions["monsteridc"], MONSTER_BASE)); // reset monster id to 0xBB000000
}
character = monster;
actionPoints = NP_MAX_AP;
SetAlive(true);
setType(type);
pos=newpos;
}
void Monster::performStep(position targetpos) {
Character::direction dir;
if (getNextStepDir(targetpos, MAX_PATH_FIND, dir)) {
std::cout << "performStep here! " << dir << std::endl;
move(dir);
std::cout << "performStep here! pass 1" << std::endl;
} else {
dir = static_cast<Character::direction>(unsignedShortRandom(0, 7));
move(dir);
}
// unsigned char direction = 0;
// short int xoffs;
// short int yoffs;
// short int zoffs;
// xoffs = targetpos.x - pos.x;
// yoffs = targetpos.y - pos.y;
// zoffs = targetpos.z - pos.z;
// if ( ( abs(xoffs) <= MAX_PATH_FIND ) && ( abs(yoffs) <= MAX_PATH_FIND ) )
// {
// // player near... start breadth-first search to find a way towards...
// // should be replaced by A* search later...
// bool karte[MAX_PATH_FIND * 2 + 1][MAX_PATH_FIND * 2 + 1];
// char distances[MAX_PATH_FIND * 2 + 1][MAX_PATH_FIND * 2 + 1];
// SuchFeldListe Wertungen;
// Field* temp4;
// for ( int x = 0; x < MAX_PATH_FIND * 2 + 1; ++x )
// {
// for ( int y = 0; y < MAX_PATH_FIND * 2 + 1; ++y )
// {
// if (_world->GetPToCFieldAt(temp4,pos.x-MAX_PATH_FIND+x,pos.y-MAX_PATH_FIND+y,pos.z))
// {
// karte[x][y]=!(temp4->moveToPossible());
// }
// else
// {
// karte[x][y]=true;
// }
// }
// }
// karte[MAX_PATH_FIND][MAX_PATH_FIND] = false;
// karte[MAX_PATH_FIND+xoffs][MAX_PATH_FIND+yoffs] = false; // we want to move to our target...
// feld aktFeld, neuFeld;
// for ( int x = 0; x < MAX_PATH_FIND * 2 + 1; ++x )
// for ( int y = 0; y < MAX_PATH_FIND * 2 + 1; ++y )
// distances[x][y] = 127;
// aktFeld.x=MAX_PATH_FIND;
// aktFeld.y=MAX_PATH_FIND;
// aktFeld.distance=0;
// Wertungen.push_back(aktFeld);
// while (!Wertungen.empty())
// {
// aktFeld = Wertungen[0];
// Wertungen.erase(Wertungen.begin());
// if ( distances[aktFeld.x][aktFeld.y] > aktFeld.distance )
// distances[aktFeld.x][aktFeld.y] = aktFeld.distance;
// if ( ! karte[aktFeld.x][aktFeld.y] )//Karte frei
// {
// neuFeld.distance = aktFeld.distance + 1;
// neuFeld.x = aktFeld.x;
// if ( aktFeld.y > 0 )
// {
// neuFeld.y = aktFeld.y-1;
// if ( ( distances[neuFeld.x][neuFeld.y] > neuFeld.distance ) && ( ! karte[neuFeld.x][neuFeld.y] ) )
// {
// distances[neuFeld.x][neuFeld.y] = neuFeld.distance;
// Wertungen.push_back(neuFeld);
// }
// }
// if ( aktFeld.y < MAX_PATH_FIND * 2 )
// {
// neuFeld.y = aktFeld.y + 1;
// if ( ( distances[neuFeld.x][neuFeld.y] > neuFeld.distance ) && ( ! karte[neuFeld.x][neuFeld.y] ) )
// {
// distances[neuFeld.x][neuFeld.y] = neuFeld.distance;
// Wertungen.push_back(neuFeld);
// }
// }
// neuFeld.y = aktFeld.y;
// if ( aktFeld.x > 0 )
// {
// neuFeld.x = aktFeld.x - 1;
// if ( ( distances[neuFeld.x][neuFeld.y] > neuFeld.distance ) && ( ! karte[neuFeld.x][neuFeld.y] ) )
// {
// distances[neuFeld.x][neuFeld.y] = neuFeld.distance;
// Wertungen.push_back(neuFeld);
// }
// }
// if ( aktFeld.x < MAX_PATH_FIND * 2 )
// {
// neuFeld.x = aktFeld.x + 1;
// if ( ( distances[neuFeld.x][neuFeld.y] > neuFeld.distance ) && ( ! karte[neuFeld.x][neuFeld.y] ) )
// {
// distances[neuFeld.x][neuFeld.y] = neuFeld.distance;
// Wertungen.push_back(neuFeld);
// }
// }
// }
// }
// feld path;
// // starting from our target...
// path.x = MAX_PATH_FIND + xoffs;
// path.y = MAX_PATH_FIND + yoffs;
// path.distance = distances[path.x][path.y];
// while (path.distance > 1 && path.distance < 127)
// {
// // find out which field to go to...
// if (path.x > 0 && distances[path.x-1][path.y] < path.distance)
// {
// path.x--;
// path.distance = distances[path.x][path.y];
// continue;
// }
// else if (path.x <2*MAX_PATH_FIND && distances[path.x+1][path.y] < path.distance)
// {
// path.x++;
// path.distance = distances[path.x][path.y];
// continue;
// }
// else if (path.y >0 && distances[path.x][path.y-1] < path.distance)
// {
// path.y--;
// path.distance = distances[path.x][path.y];
// continue;
// }
// else if (path.y <2*MAX_PATH_FIND && distances[path.x][path.y+1] < path.distance)
// {
// path.y++;
// path.distance = distances[path.x][path.y];
// continue;
// }
// }
// if (path.x > MAX_PATH_FIND) // east
// direction = 2;
// if (path.x < MAX_PATH_FIND) // west
// direction = 6;
// if (path.y > MAX_PATH_FIND) // south
// direction = 4;
// if (path.y < MAX_PATH_FIND) // north
// direction = 0;
// // if we can't reach our target... just make a random move... some movement is better then none at all :)
// if (path.distance == 127)
// direction = 2 * unsignedShortRandom( 0, 3);
// }
// else //Nur in die richtung laufen
// {
// if (abs(xoffs) > abs(yoffs) )
// {
// direction = (xoffs>0)?6:2;
// }
// else
// {
// direction = (yoffs>0)?4:0;
// }
// }
//move( (Character::direction)direction );
}
void Monster::setType(const TYPE_OF_CHARACTER_ID &type) throw(unknownIDException) {
deleteAllSkills();
MonsterStruct monsterdef;
if (! MonsterDescriptions->find(type, monsterdef)) {
throw unknownIDException();
}
// set attributes
battrib.luck = battrib.trueluck = rnd(monsterdef.attributes.luck);
battrib.strength = battrib.truestrength = rnd(monsterdef.attributes.strength);
battrib.dexterity = battrib.truedexterity = rnd(monsterdef.attributes.dexterity);
battrib.constitution = battrib.trueconstitution = rnd(monsterdef.attributes.constitution);
battrib.agility = battrib.trueagility = rnd(monsterdef.attributes.agility);
battrib.intelligence = battrib.trueintelligence = rnd(monsterdef.attributes.intelligence);
battrib.perception = battrib.trueperception = rnd(monsterdef.attributes.perception);
battrib.willpower = battrib.truewillpower = rnd(monsterdef.attributes.willpower);
battrib.essence = battrib.trueessence = rnd(monsterdef.attributes.essence);
battrib.hitpoints = battrib.truehitpoints = monsterdef.hitpoints; //Setzen der Hitpoints
battrib.body_height = battrib.truebody_height = rand()%(monsterdef.maxsize-monsterdef.minsize+1) + monsterdef.minsize;
// set skills
for (MonsterStruct::skilltype::iterator it = monsterdef.skills.begin(); it != monsterdef.skills.end(); ++it) {
increaseSkill(5, it->first, rnd(it->second));
}
// add items
int itemprobability;
bool found;
for (MonsterStruct::itemtype::iterator it = monsterdef.items.begin(); it != monsterdef.items.end(); ++it) {
// check if we should equip the item...
found = false;
itemprobability = rnd(1,1000);
for (std::list<itemdef_t>::iterator itemit = it->second.begin(); !found && itemit != it->second.end(); ++itemit) {
//std::cout<<"Itemprop for item: "<< itemit->itemid<<" prob: "<<itemprobability<<" Itemit prob="<<itemit->probability<<"\n";
if (itemit->probability <= itemprobability) {
//std::cout<<"Added Item:"<<itemit->itemid<<"\n";
characterItems[ it->first ].id = itemit->itemid; // Eingeweide
characterItems[ it->first ].number = rnd(itemit->amount);
characterItems[ it->first ].wear = itemit->AgeingSpeed;
found = true;
}
itemprobability += itemit->probability;
}
}
SetMovement(monsterdef.movement);
race = monsterdef.race;
_canAttack = monsterdef.canattack;
id=monsteridc->nextFreeId();
char mname[ 80 ];
sprintf(mname, "%0#6X %s", id, monsterdef.name.c_str());
name=mname;
appearance = appearance_alive();
}
void Monster::setSpawn(SpawnPoint *sp) {
spawn = sp;
}
Monster::~Monster() {
#ifdef Character_DEBUG
std::cout << "Monster Destruktor Start" << std::endl;
#endif
if (spawn) {
spawn->dead(monstertype);
}
#ifdef Character_DEBUG
std::cout << "Monster Destruktor Ende" << std::endl;
#endif
}
void Monster::remove() {
battrib.truehitpoints = 0;
battrib.hitpoints = battrib.truehitpoints;
Character::SetAlive(false);
}
void Monster::SetAlive(bool t) {
bool wasAlive = Character::IsAlive();
Character::SetAlive(t);
if (!t && wasAlive) {
MonsterStruct monStruct;
if (MonsterDescriptions->find(getType(), monStruct)) {
if (monStruct.script) {
monStruct.script->onDeath(this);
} else {
std::cerr<<"Script for Monster: "<<getType()<<" not active!"<<std::endl;
}
} else {
std::cerr<<"Can't finde Description for Monster: " << getType() << " on Death not called!"<<std::endl;
}
}
}
bool Monster::attack(Character *target, int &sound, bool &updateInv) {
MonsterStruct monStruct;
if (MonsterDescriptions->find(getType(), monStruct)) {
if (monStruct.script) {
monStruct.script->onAttack(this,target);
} else {
std::cerr<<"Script for Monster: "<<getType()<<"not active!"<<std::endl;
}
} else {
std::cerr<<"Can't find Description for Monster: " << getType() << " onAttack not called!" << std::endl;
}
return Character::attack(target,sound,updateInv);
}
void Monster::receiveText(talk_type tt, std::string message, Character *cc) {
MonsterStruct monStruct;
if (MonsterDescriptions->find(getType(), monStruct)) {
if (monStruct.script!=NULL && monStruct.script->existsEntrypoint("receiveText")) {
//Nur Script aufrufen wenn man sich nicht selber hört.
if (this != cc) {
monStruct.script->receiveText(this,tt,message,cc);
}
}
} else {
std::cerr<<"Can't find description for monster: " << getType() << " receiveText not called!"<<std::endl;
}
}