1 | |
---|
2 | |
---|
3 | /* |
---|
4 | orxonox - the future of 3D-vertical-scrollers |
---|
5 | |
---|
6 | Copyright (C) 2004 orx |
---|
7 | |
---|
8 | This program is free software; you can redistribute it and/or modify |
---|
9 | it under the terms of the GNU General Public License as published by |
---|
10 | the Free Software Foundation; either version 2, or (at your option) |
---|
11 | any later version. |
---|
12 | |
---|
13 | ### File Specific: |
---|
14 | main-programmer: Patrick Boenzli |
---|
15 | co-programmer: |
---|
16 | */ |
---|
17 | #define DEBUG_SPECIAL_MODULE DEBUG_MODULE_WORLD_ENTITY |
---|
18 | |
---|
19 | |
---|
20 | #include "movement_module.h" |
---|
21 | #include "ai_module.h" |
---|
22 | #include "ai_team.h" |
---|
23 | #include "ai_swarm.h" |
---|
24 | #include "ai_engine.h" |
---|
25 | |
---|
26 | #include "player.h" |
---|
27 | #include "playable.h" |
---|
28 | |
---|
29 | #include "weapons/test_gun.h" |
---|
30 | #include "weapons/turret.h" |
---|
31 | #include "weapons/cannon.h" |
---|
32 | |
---|
33 | #include "loading/factory.h" |
---|
34 | #include "debug.h" |
---|
35 | #include "loading/load_param.h" |
---|
36 | #include "util/loading/load_param_xml.h" |
---|
37 | #include "track/track.h" |
---|
38 | |
---|
39 | #include "particles/dot_emitter.h" |
---|
40 | #include "particles/emitter_node.h" |
---|
41 | #include "particles/sprite_particles.h" |
---|
42 | |
---|
43 | #include "weapons/test_gun.h" |
---|
44 | #include "weapons/light_blaster.h" |
---|
45 | #include "weapons/medium_blaster.h" |
---|
46 | #include "weapons/heavy_blaster.h" |
---|
47 | #include "weapons/swarm_launcher.h" |
---|
48 | #include "weapons/spike_launcher.h" |
---|
49 | #include "weapons/spike_thrower.h" |
---|
50 | #include "weapons/acid_launcher.h" |
---|
51 | #include "weapons/boomerang_gun.h" |
---|
52 | #include "weapons/turret.h" |
---|
53 | #include "weapons/cannon.h" |
---|
54 | |
---|
55 | #include "mount_point.h" |
---|
56 | #include "weapons/weapon_slot.h" |
---|
57 | |
---|
58 | #include "npc.h" |
---|
59 | |
---|
60 | ObjectListDefinition(NPC); |
---|
61 | CREATE_FACTORY(NPC); |
---|
62 | |
---|
63 | |
---|
64 | #include "script_class.h" |
---|
65 | CREATE_SCRIPTABLE_CLASS(NPC, |
---|
66 | addMethod("getAbsCoorX", Executor0ret<PNode, lua_State*, float>(&PNode::getAbsCoorX)) |
---|
67 | ->addMethod("getAbsCoorY", Executor0ret<PNode, lua_State*, float>(&PNode::getAbsCoorY)) |
---|
68 | ->addMethod("getAbsCoorZ", Executor0ret<PNode, lua_State*, float>(&PNode::getAbsCoorZ)) |
---|
69 | ->addMethod("setAbsCoor", Executor3<PNode, lua_State*,float,float,float>(&PNode::setAbsCoor)) |
---|
70 | ->addMethod("setAbsDir", Executor4<PNode, lua_State*,float,float,float,float>(&PNode::setAbsDir)) |
---|
71 | ->addMethod("fire", Executor0<NPC, lua_State*>(&NPC::fire)) |
---|
72 | ->addMethod("pause", Executor1<WorldEntity, lua_State*, bool>(&WorldEntity::pauseTrack)) |
---|
73 | ->addMethod("setAI", Executor1<NPC, lua_State*, bool>(&NPC::setAI)) |
---|
74 | ->addMethod("setVisibility", Executor1<WorldEntity, lua_State*, bool>(&WorldEntity::setVisibility)) |
---|
75 | ->addMethod("destroy", Executor0<NPC, lua_State*/*, WorldEntity**/>(&NPC::destroy)) |
---|
76 | ); |
---|
77 | |
---|
78 | NPC::NPC(const TiXmlElement* root) |
---|
79 | : weaponMan(this) |
---|
80 | { |
---|
81 | this->registerObject(this, NPC::_objectList); |
---|
82 | |
---|
83 | this->toList(OM_GROUP_01); |
---|
84 | this->bAIEnabled = false; |
---|
85 | |
---|
86 | |
---|
87 | |
---|
88 | // create the weapons and their manager |
---|
89 | Weapon* wpRight1 = new LightBlaster (); |
---|
90 | wpRight1->setName( "LightBlaster"); |
---|
91 | Weapon* wpLeft1 = new LightBlaster (); |
---|
92 | wpLeft1->setName( "LightBlaster"); |
---|
93 | |
---|
94 | Weapon* wpRight2 = new MediumBlaster (); |
---|
95 | wpRight2->setName( "MediumBlaster"); |
---|
96 | Weapon* wpLeft2 = new MediumBlaster (); |
---|
97 | wpLeft2->setName( "MediumBlaster"); |
---|
98 | |
---|
99 | Weapon* wpRight3 = new HeavyBlaster (1); |
---|
100 | wpRight3->setName( "HeavyBlaster"); |
---|
101 | Weapon* wpLeft3 = new HeavyBlaster (0); |
---|
102 | wpLeft3->setName( "HeavyBlaster"); |
---|
103 | |
---|
104 | Weapon* cannon = new SwarmLauncher(); |
---|
105 | cannon->setName( "SwarmLauncher"); |
---|
106 | |
---|
107 | Weapon* spike = new SpikeThrower(); |
---|
108 | spike->setName( "SpikeThrower" ); |
---|
109 | |
---|
110 | |
---|
111 | Weapon* acid0 = new AcidLauncher(); |
---|
112 | acid0->setName( "AcidSplasher" ); |
---|
113 | |
---|
114 | Weapon* acid1 = new AcidLauncher(); |
---|
115 | acid1->setName( "AcidSplasher" ); |
---|
116 | |
---|
117 | |
---|
118 | this->weaponMan.addWeapon( wpLeft1, 0, 0); |
---|
119 | this->weaponMan.addWeapon( wpRight1, 0, 1); |
---|
120 | |
---|
121 | this->weaponMan.addWeapon( wpLeft2, 1, 2); |
---|
122 | this->weaponMan.addWeapon( wpRight2, 1, 3); |
---|
123 | |
---|
124 | this->weaponMan.addWeapon( wpLeft3, 2, 4); |
---|
125 | this->weaponMan.addWeapon( wpRight3, 2, 5); |
---|
126 | /* |
---|
127 | this->weaponMan.addWeapon( wpLeft1, 3, 0); |
---|
128 | this->weaponMan.addWeapon( wpRight1, 3, 1); |
---|
129 | |
---|
130 | this->weaponMan.addWeapon( wpLeft2, 3, 2); |
---|
131 | this->weaponMan.addWeapon( wpRight2, 3, 3); |
---|
132 | |
---|
133 | this->weaponMan.addWeapon( wpLeft3, 3, 4); |
---|
134 | this->weaponMan.addWeapon( wpRight3, 3, 5); |
---|
135 | */ |
---|
136 | |
---|
137 | this->weaponMan.addWeapon( acid0, 3, 0); |
---|
138 | this->weaponMan.addWeapon( acid1, 3, 1); |
---|
139 | |
---|
140 | |
---|
141 | if( root != NULL) |
---|
142 | this->loadParams(root); |
---|
143 | |
---|
144 | if( this->bAIEnabled && ! this->entityTrack) |
---|
145 | { |
---|
146 | std::cout << "Team Number: " << teamNumber << "\n"; |
---|
147 | std::cout << "Swarm Number:" << swarmNumber << "\n"; |
---|
148 | |
---|
149 | AIEngine::getInstance()->addAI(teamNumber,swarmNumber,(WorldEntity*)this,maxSpeed,attackDistance); |
---|
150 | } |
---|
151 | |
---|
152 | this->bFire = false; |
---|
153 | if( this->entityTrack) |
---|
154 | { |
---|
155 | this->setParent(this->entityTrack->getTrackNode()); |
---|
156 | this->setRelCoor(0,0,0); |
---|
157 | } |
---|
158 | |
---|
159 | |
---|
160 | |
---|
161 | |
---|
162 | // this->secWeaponMan.addWeapon( acid0, 2, 2); |
---|
163 | // this->secWeaponMan.addWeapon( acid1, 2, 3); |
---|
164 | |
---|
165 | |
---|
166 | // this->weaponMan.changeWeaponConfig(3); |
---|
167 | |
---|
168 | // this->getWeaponManager().changeWeaponConfig(1); |
---|
169 | |
---|
170 | this->setHealthMax(100); |
---|
171 | this->setHealth(80); |
---|
172 | |
---|
173 | this->getWeaponManager().setSlotCount(7); |
---|
174 | |
---|
175 | |
---|
176 | // now get slots from the mount points |
---|
177 | // const std::vector<MountPoint*>::const_iterator it = this->beginMountPoints(); |
---|
178 | // std::vector<MountPoint*> list = this->getMountPoints(); |
---|
179 | std::map<int, MountPoint*>::iterator it = this->mountPointMap.begin(); |
---|
180 | // PRINTF(0)("\n\n\n\nsize: %i\n", this->mountPointMap.size()); |
---|
181 | for( ;it != this->mountPointMap.end(); it++) |
---|
182 | { |
---|
183 | PRINTF(0)("5555555555 this npc got mountss\n\n\n"); |
---|
184 | WeaponSlot* ws = dynamic_cast<WeaponSlot*>((*it).second->getMount()); |
---|
185 | printf("more\n"); |
---|
186 | if( ws != NULL && ws->isA(WeaponSlot::staticClassID())) |
---|
187 | { |
---|
188 | // |
---|
189 | PRINTF(0)("5555555555 this npc got slots\n\n\n"); |
---|
190 | |
---|
191 | this->getWeaponManager().setSlotPosition(ws->getWeaponSlot(), ws->getRelCoor()); |
---|
192 | this->getWeaponManager().setSlotDirection(ws->getWeaponSlot(), ws->getRelDir()); |
---|
193 | } |
---|
194 | } |
---|
195 | |
---|
196 | |
---|
197 | |
---|
198 | this->getWeaponManager().setSlotPosition(0, Vector(-2.6, .1, -3.0)); |
---|
199 | this->getWeaponManager().setSlotCapability(0, WTYPE_ALLDIRS | WTYPE_DIRECTIONAL); |
---|
200 | |
---|
201 | this->getWeaponManager().setSlotPosition(1, Vector(-2.6, .1, 3.0)); |
---|
202 | this->getWeaponManager().setSlotCapability(1, WTYPE_ALLDIRS | WTYPE_DIRECTIONAL); |
---|
203 | |
---|
204 | this->getWeaponManager().setSlotPosition(2, Vector(-1.5, .5, -.5)); |
---|
205 | this->getWeaponManager().setSlotDirection(2, Quaternion(-M_PI_4*.5, Vector(1,0,0))); |
---|
206 | |
---|
207 | this->getWeaponManager().setSlotPosition(3, Vector(-1.5, .5, .5)); |
---|
208 | this->getWeaponManager().setSlotDirection(3, Quaternion(M_PI_4*.5, Vector(1,0,0))); |
---|
209 | |
---|
210 | this->getWeaponManager().setSlotPosition(4, Vector(-1.5, -.5, .5)); |
---|
211 | this->getWeaponManager().setSlotDirection(4, Quaternion(-M_PI_4*.5+M_PI, Vector(1,0,0))); |
---|
212 | |
---|
213 | this->getWeaponManager().setSlotPosition(5, Vector(-1.5, -.5, -.5)); |
---|
214 | this->getWeaponManager().setSlotDirection(5, Quaternion(+M_PI_4*.5-M_PI, Vector(1,0,0))); |
---|
215 | |
---|
216 | this->getWeaponManager().setSlotPosition(6, Vector(-1, 0.0, 0)); |
---|
217 | this->getWeaponManager().setSlotCapability(6, WTYPE_ALLDIRS | WTYPE_DIRECTIONAL); |
---|
218 | |
---|
219 | this->getWeaponManager().getFixedTarget()->setParent(this); |
---|
220 | this->getWeaponManager().getFixedTarget()->setRelCoor(100000,0,0); |
---|
221 | |
---|
222 | |
---|
223 | |
---|
224 | |
---|
225 | } |
---|
226 | |
---|
227 | |
---|
228 | NPC::~NPC () |
---|
229 | { |
---|
230 | if(! this->entityTrack) |
---|
231 | AIEngine::getInstance()->removeAI(teamNumber,swarmNumber,(WorldEntity*)this); |
---|
232 | } |
---|
233 | |
---|
234 | |
---|
235 | |
---|
236 | /** |
---|
237 | * loads the xml tags |
---|
238 | * @param root: root xml tag for this element |
---|
239 | */ |
---|
240 | void NPC::loadParams(const TiXmlElement* root) |
---|
241 | { |
---|
242 | WorldEntity::loadParams(root); |
---|
243 | |
---|
244 | LoadParam(root, "enableAI", this, NPC, enableAI) |
---|
245 | .describe("enables the AI algorithms"); |
---|
246 | |
---|
247 | LoadParam(root, "team", this, NPC, setTeamNumber) |
---|
248 | .describe("this sets the team number") |
---|
249 | .defaultValues(0); |
---|
250 | |
---|
251 | LoadParam(root, "swarm", this, NPC, setSwarmNumber) |
---|
252 | .describe("this sets the swarm number") |
---|
253 | .defaultValues(0); |
---|
254 | |
---|
255 | LoadParam(root, "maxSpeed", this, NPC, setMaxSpeed) |
---|
256 | .describe("this sets the NPC max Speed") |
---|
257 | .defaultValues(0); |
---|
258 | |
---|
259 | LoadParam(root, "attackDistance", this, NPC, setAttackDistance) |
---|
260 | .describe("this sets the NPC distance to target") |
---|
261 | .defaultValues(0); |
---|
262 | |
---|
263 | LoadParam(root, "weapon-config", this, NPC, setWeaponConfig); |
---|
264 | |
---|
265 | // LoadParamXML(root, "Weapons", this, NPC, addWeapons) |
---|
266 | // .describe("creates and adds weapons"); |
---|
267 | } |
---|
268 | |
---|
269 | void NPC::setAI(bool activate) |
---|
270 | { |
---|
271 | if(activate) AIEngine::getInstance()->addAI(teamNumber,swarmNumber,(WorldEntity*)this,maxSpeed,attackDistance); |
---|
272 | else AIEngine::getInstance()->removeAI(teamNumber,swarmNumber,(WorldEntity*)this); |
---|
273 | } |
---|
274 | |
---|
275 | void NPC::destroy( /*WorldEntity* killer*/ ) |
---|
276 | { |
---|
277 | |
---|
278 | PRINTF(5)("NPC destroy\n"); |
---|
279 | |
---|
280 | EmitterNode* node = NULL; |
---|
281 | DotEmitter* emitter = NULL; |
---|
282 | SpriteParticles* explosionParticles = NULL; |
---|
283 | |
---|
284 | explosionParticles = new SpriteParticles(200); |
---|
285 | explosionParticles->setName("SpaceShipExplosionParticles"); |
---|
286 | explosionParticles->setLifeSpan(.2, .3); |
---|
287 | explosionParticles->setRadius(0.0, 10.0); |
---|
288 | explosionParticles->setRadius(.5, 6.0); |
---|
289 | explosionParticles->setRadius(1.0, 3.0); |
---|
290 | explosionParticles->setColor(0.0, 1,1,1,.9); |
---|
291 | explosionParticles->setColor(0.1, 1,1,0,.9); |
---|
292 | explosionParticles->setColor(0.5, .8,.4,0,.5); |
---|
293 | explosionParticles->setColor(1.0, .2,.2,.2,.5); |
---|
294 | |
---|
295 | |
---|
296 | emitter = new DotEmitter( 2000, 70, 360); |
---|
297 | //emitter->setSpread( 0, M_2_PI); |
---|
298 | emitter->setEmissionRate( 200.0); |
---|
299 | //emitter->setEmissionVelocity( 200.0); |
---|
300 | //emitter->setSystem( explosionParticles); |
---|
301 | //emitter->setAbsCoor( this->getAbsCoor()); |
---|
302 | |
---|
303 | node = new EmitterNode( .1f); |
---|
304 | node->setupParticle( emitter, explosionParticles); |
---|
305 | node->setAbsDir( this->getAbsDir()); |
---|
306 | node->setVelocity( this->getVelocity() * .9f); |
---|
307 | node->setAbsCoor( this->getAbsCoor()); |
---|
308 | if( !node->start()) |
---|
309 | PRINTF(0)("Explosion node not correctly started!"); |
---|
310 | /* |
---|
311 | PNode* node = new PNode(); |
---|
312 | node->setAbsCoor(this->getAbsCoor()); |
---|
313 | Explosion* explosion = new Explosion(); |
---|
314 | explosion->explode( node, Vector(5,5,5)); |
---|
315 | */ |
---|
316 | /* |
---|
317 | if( this->hasPlayer()) |
---|
318 | { |
---|
319 | this->setAbsCoor(Vector(-10000,10000,10000)); |
---|
320 | this->hide(); |
---|
321 | } |
---|
322 | else |
---|
323 | {*/ |
---|
324 | //this->setAbsCoor( this->getAbsCoor() + Vector(100,0,0) + Vector(1,0,0) * VECTOR_RAND(150).dot(Vector(1,0,0))); |
---|
325 | //} |
---|
326 | |
---|
327 | } |
---|
328 | |
---|
329 | void NPC::setWeaponConfig(int i) |
---|
330 | { |
---|
331 | this->weaponMan.changeWeaponConfig(i); |
---|
332 | } |
---|
333 | |
---|
334 | void NPC::addWeapons(const TiXmlElement* root) |
---|
335 | { |
---|
336 | if( root == NULL) |
---|
337 | return; |
---|
338 | |
---|
339 | LOAD_PARAM_START_CYCLE(root, element); |
---|
340 | { |
---|
341 | PRINTF(0)("got weapon: %s\n", element->Value()); |
---|
342 | BaseObject* obj = Factory::fabricate(element); |
---|
343 | if( obj != NULL && obj->isA( Weapon::staticClassID())) |
---|
344 | { |
---|
345 | Weapon* w = dynamic_cast<Weapon*>(obj); |
---|
346 | PRINTF(0)("created a weapon\n"); |
---|
347 | int preferedSlot = w->getPreferedSlot(); |
---|
348 | int preferedSide = w->getPreferedSide(); |
---|
349 | |
---|
350 | this->addWeapon( w, preferedSide, preferedSlot); |
---|
351 | } |
---|
352 | } |
---|
353 | LOAD_PARAM_END_CYCLE(element); |
---|
354 | } |
---|
355 | |
---|
356 | |
---|
357 | /** |
---|
358 | * @brief adds a Weapon to the NPC. |
---|
359 | * @param weapon the Weapon to add. |
---|
360 | * @param configID the Configuration ID to add this weapon to. |
---|
361 | * @param slotID the slotID to add the Weapon to. |
---|
362 | */ |
---|
363 | bool NPC::addWeapon(Weapon* weapon, int configID, int slotID) |
---|
364 | { |
---|
365 | weapon->setOwner(this->getOwner()); |
---|
366 | |
---|
367 | |
---|
368 | if(this->weaponMan.addWeapon(weapon, configID, slotID)) |
---|
369 | { |
---|
370 | return true; |
---|
371 | } |
---|
372 | else |
---|
373 | { |
---|
374 | if (weapon != NULL) |
---|
375 | PRINTF(1)("Unable to add Weapon (%s::%s) to %s::%s\n", |
---|
376 | weapon->getClassCName(), weapon->getCName(), this->getClassCName(), this->getCName()); |
---|
377 | else |
---|
378 | PRINTF(1)("No weapon defined\n"); |
---|
379 | return false; |
---|
380 | |
---|
381 | } |
---|
382 | } |
---|
383 | |
---|
384 | /** |
---|
385 | * @brief removes a Weapon. |
---|
386 | * @param weapon the Weapon to remove. |
---|
387 | */ |
---|
388 | void NPC::removeWeapon(Weapon* weapon) |
---|
389 | { |
---|
390 | this->weaponMan.removeWeapon(weapon); |
---|
391 | |
---|
392 | } |
---|
393 | |
---|
394 | /** |
---|
395 | * @brief jumps to the next WeaponConfiguration |
---|
396 | */ |
---|
397 | void NPC::nextWeaponConfig() |
---|
398 | { |
---|
399 | this->weaponMan.nextWeaponConfig(); |
---|
400 | } |
---|
401 | |
---|
402 | /** |
---|
403 | * @brief moves to the last WeaponConfiguration |
---|
404 | */ |
---|
405 | void NPC::previousWeaponConfig() |
---|
406 | { |
---|
407 | this->weaponMan.previousWeaponConfig(); |
---|
408 | } |
---|
409 | |
---|
410 | |
---|
411 | |
---|
412 | |
---|
413 | /** |
---|
414 | * ticking |
---|
415 | * @param dt time since last tick |
---|
416 | */ |
---|
417 | void NPC::tick(float dt) |
---|
418 | { |
---|
419 | this->weaponMan.tick(dt); |
---|
420 | if (this->bFire) |
---|
421 | { |
---|
422 | std::cout << "fire..\n"; |
---|
423 | weaponMan.fire(); |
---|
424 | } |
---|
425 | this->bFire = false; |
---|
426 | |
---|
427 | if(this->entityTrack) |
---|
428 | this->entityTrack->tick(dt); |
---|
429 | |
---|
430 | } |
---|
431 | |
---|
432 | void NPC::draw() const |
---|
433 | { |
---|
434 | if( this->entityTrack != NULL && this->isDrawTrack()) |
---|
435 | this->entityTrack->drawGraph(); |
---|
436 | |
---|
437 | WorldEntity::draw(); |
---|
438 | } |
---|
439 | |
---|
440 | // HACK just to make sure they explode as nice as possible :) |
---|
441 | void NPC::hit( float damage, WorldEntity* killer) |
---|
442 | { |
---|
443 | this->setDamage(killer->getDamage()); |
---|
444 | } |
---|