Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/trunk/src/world_entities/npcs/npc.cc @ 10528

Last change on this file since 10528 was 10512, checked in by bknecht, 18 years ago

some changes so the vertical scroller will work better

File size: 9.6 KB
Line 
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
40#include "weapons/test_gun.h"
41#include "weapons/light_blaster.h"
42#include "weapons/medium_blaster.h"
43#include "weapons/heavy_blaster.h"
44#include "weapons/swarm_launcher.h"
45#include "weapons/spike_launcher.h"
46#include "weapons/spike_thrower.h"
47#include "weapons/acid_launcher.h"
48#include "weapons/boomerang_gun.h"
49#include "weapons/turret.h"
50#include "weapons/cannon.h"
51
52#include "npc.h"
53
54ObjectListDefinition(NPC);
55CREATE_FACTORY(NPC);
56
57
58#include "script_class.h"
59CREATE_SCRIPTABLE_CLASS(NPC,
60                        addMethod("getAbsCoorX", Executor0ret<PNode, lua_State*, float>(&PNode::getAbsCoorX))
61                        ->addMethod("getAbsCoorY", Executor0ret<PNode, lua_State*, float>(&PNode::getAbsCoorY))
62                        ->addMethod("getAbsCoorZ", Executor0ret<PNode, lua_State*, float>(&PNode::getAbsCoorZ))
63                        ->addMethod("setAbsCoor", Executor3<PNode, lua_State*,float,float,float>(&PNode::setAbsCoor))
64                        ->addMethod("setAbsDir", Executor4<PNode, lua_State*,float,float,float,float>(&PNode::setAbsDir))
65                        ->addMethod("fire", Executor0<NPC, lua_State*>(&NPC::fire))
66                        ->addMethod("pause", Executor1<WorldEntity, lua_State*, bool>(&WorldEntity::pauseTrack))
67                        ->addMethod("setAI", Executor1<NPC, lua_State*, bool>(&NPC::setAI))
68                        ->addMethod("setVisibility", Executor1<WorldEntity, lua_State*, bool>(&WorldEntity::setVisibility))
69                       );
70
71NPC::NPC(const TiXmlElement* root)
72  : weaponMan(this)
73{
74  this->registerObject(this, NPC::_objectList);
75
76  this->toList(OM_GROUP_01);
77  this->bAIEnabled = false;
78
79
80
81   // create the weapons and their manager
82  Weapon* wpRight1 = new LightBlaster ();
83  wpRight1->setName( "LightBlaster");
84  Weapon* wpLeft1 = new LightBlaster ();
85  wpLeft1->setName( "LightBlaster");
86
87  Weapon* wpRight2 = new MediumBlaster ();
88  wpRight2->setName( "MediumBlaster");
89  Weapon* wpLeft2 = new MediumBlaster ();
90  wpLeft2->setName( "MediumBlaster");
91
92  Weapon* wpRight3 = new HeavyBlaster (1);
93  wpRight3->setName( "HeavyBlaster");
94  Weapon* wpLeft3 = new HeavyBlaster (0);
95  wpLeft3->setName( "HeavyBlaster");
96
97  Weapon* cannon = new SwarmLauncher();
98  cannon->setName( "SwarmLauncher");
99
100  Weapon* spike = new SpikeThrower();
101  spike->setName( "SpikeThrower" );
102
103
104  Weapon* acid0 = new AcidLauncher();
105  acid0->setName( "AcidSplasher" );
106
107  Weapon* acid1 = new AcidLauncher();
108  acid1->setName( "AcidSplasher" );
109
110
111  this->weaponMan.addWeapon( wpLeft1, 0, 0);
112  this->weaponMan.addWeapon( wpRight1, 0, 1);
113
114  this->weaponMan.addWeapon( wpLeft2, 1, 2);
115  this->weaponMan.addWeapon( wpRight2, 1, 3);
116
117  this->weaponMan.addWeapon( wpLeft3, 2, 4);
118  this->weaponMan.addWeapon( wpRight3, 2, 5);
119/*
120  this->weaponMan.addWeapon( wpLeft1, 3, 0);
121  this->weaponMan.addWeapon( wpRight1, 3, 1);
122
123  this->weaponMan.addWeapon( wpLeft2, 3, 2);
124  this->weaponMan.addWeapon( wpRight2, 3, 3);
125
126  this->weaponMan.addWeapon( wpLeft3, 3, 4);
127  this->weaponMan.addWeapon( wpRight3, 3, 5);
128*/
129
130  this->weaponMan.addWeapon( acid0, 3, 0);
131  this->weaponMan.addWeapon( acid1, 3, 1);
132
133
134  if( root != NULL)
135    this->loadParams(root);
136
137  if( this->bAIEnabled && ! this->entityTrack)
138  {
139    std::cout << "Team Number: " << teamNumber << "\n";
140    std::cout << "Swarm Number:" << swarmNumber << "\n";
141
142    AIEngine::getInstance()->addAI(teamNumber,swarmNumber,(WorldEntity*)this,maxSpeed,attackDistance);
143  }
144
145  this->bFire = false;
146  if( this->entityTrack)
147  {
148      this->setParent(this->entityTrack->getTrackNode());
149      this->setRelCoor(0,0,0);
150  }
151
152
153
154
155//   this->secWeaponMan.addWeapon( acid0, 2, 2);
156//   this->secWeaponMan.addWeapon( acid1, 2, 3);
157
158
159//   this->weaponMan.changeWeaponConfig(3);
160
161//   this->getWeaponManager().changeWeaponConfig(1);
162
163  this->setHealthMax(100);
164  this->setHealth(80);
165
166  this->getWeaponManager().setSlotCount(7);
167
168  this->getWeaponManager().setSlotPosition(0, Vector(-2.6, .1, -3.0));
169  this->getWeaponManager().setSlotCapability(0, WTYPE_ALLDIRS | WTYPE_DIRECTIONAL);
170
171  this->getWeaponManager().setSlotPosition(1, Vector(-2.6, .1, 3.0));
172  this->getWeaponManager().setSlotCapability(1, WTYPE_ALLDIRS | WTYPE_DIRECTIONAL);
173
174  this->getWeaponManager().setSlotPosition(2, Vector(-1.5, .5, -.5));
175  this->getWeaponManager().setSlotDirection(2, Quaternion(-M_PI_4*.5, Vector(1,0,0)));
176
177  this->getWeaponManager().setSlotPosition(3, Vector(-1.5, .5, .5));
178  this->getWeaponManager().setSlotDirection(3, Quaternion(M_PI_4*.5, Vector(1,0,0)));
179
180  this->getWeaponManager().setSlotPosition(4, Vector(-1.5, -.5, .5));
181  this->getWeaponManager().setSlotDirection(4, Quaternion(-M_PI_4*.5+M_PI, Vector(1,0,0)));
182
183  this->getWeaponManager().setSlotPosition(5, Vector(-1.5, -.5, -.5));
184  this->getWeaponManager().setSlotDirection(5, Quaternion(+M_PI_4*.5-M_PI, Vector(1,0,0)));
185
186  this->getWeaponManager().setSlotPosition(6, Vector(-1, 0.0, 0));
187  this->getWeaponManager().setSlotCapability(6, WTYPE_ALLDIRS | WTYPE_DIRECTIONAL);
188
189  this->getWeaponManager().getFixedTarget()->setParent(this);
190  this->getWeaponManager().getFixedTarget()->setRelCoor(100000,0,0);
191
192
193
194
195}
196
197
198NPC::~NPC ()
199{
200 if(! this->entityTrack)
201  AIEngine::getInstance()->removeAI(teamNumber,swarmNumber,(WorldEntity*)this);
202}
203
204
205
206/**
207 * loads the xml tags
208 * @param root: root xml tag for this element
209 */
210void NPC::loadParams(const TiXmlElement* root)
211{
212   WorldEntity::loadParams(root);
213
214  LoadParam(root, "enableAI", this, NPC, enableAI)
215      .describe("enables the AI algorithms");
216
217  LoadParam(root, "team", this, NPC, setTeamNumber)
218  .describe("this sets the team number")
219  .defaultValues(0);
220
221  LoadParam(root, "swarm", this, NPC, setSwarmNumber)
222  .describe("this sets the swarm number")
223  .defaultValues(0);
224
225  LoadParam(root, "maxSpeed", this, NPC, setMaxSpeed)
226  .describe("this sets the NPC max Speed")
227  .defaultValues(0);
228
229  LoadParam(root, "attackDistance", this, NPC, setAttackDistance)
230  .describe("this sets the NPC distance to target")
231  .defaultValues(0);
232
233  LoadParam(root, "weapon-config", this, NPC, setWeaponConfig);
234
235//   LoadParamXML(root, "Weapons", this, NPC, addWeapons)
236//   .describe("creates and adds weapons");
237}
238
239void NPC::setAI(bool activate)
240{
241     if(activate) AIEngine::getInstance()->addAI(teamNumber,swarmNumber,(WorldEntity*)this,maxSpeed,attackDistance);
242     else AIEngine::getInstance()->removeAI(teamNumber,swarmNumber,(WorldEntity*)this);
243}
244
245
246void NPC::setWeaponConfig(int i)
247{
248  this->weaponMan.changeWeaponConfig(i);
249}
250
251void NPC::addWeapons(const TiXmlElement* root)
252{
253  if( root == NULL)
254    return;
255
256  LOAD_PARAM_START_CYCLE(root, element);
257  {
258    PRINTF(0)("got weapon: %s\n", element->Value());
259    BaseObject* obj = Factory::fabricate(element);
260    if( obj != NULL && obj->isA( Weapon::staticClassID()))
261    {
262      Weapon* w = dynamic_cast<Weapon*>(obj);
263      PRINTF(0)("created a weapon\n");
264      int preferedSlot = w->getPreferedSlot();
265      int preferedSide = w->getPreferedSide();
266
267      this->addWeapon( w, preferedSide, preferedSlot);
268    }
269  }
270  LOAD_PARAM_END_CYCLE(element);
271}
272
273
274/**
275 * @brief adds a Weapon to the NPC.
276 * @param weapon the Weapon to add.
277 * @param configID the Configuration ID to add this weapon to.
278 * @param slotID the slotID to add the Weapon to.
279 */
280bool NPC::addWeapon(Weapon* weapon, int configID, int slotID)
281{
282  weapon->setOwner(this->getOwner());
283
284
285  if(this->weaponMan.addWeapon(weapon, configID, slotID))
286  {
287    return true;
288  }
289  else
290  {
291    if (weapon != NULL)
292      PRINTF(1)("Unable to add Weapon (%s::%s) to %s::%s\n",
293                weapon->getClassCName(), weapon->getCName(), this->getClassCName(), this->getCName());
294    else
295      PRINTF(1)("No weapon defined\n");
296    return false;
297
298  }
299}
300
301/**
302 * @brief removes a Weapon.
303 * @param weapon the Weapon to remove.
304 */
305void NPC::removeWeapon(Weapon* weapon)
306{
307  this->weaponMan.removeWeapon(weapon);
308
309}
310
311/**
312 * @brief jumps to the next WeaponConfiguration
313 */
314void NPC::nextWeaponConfig()
315{
316  this->weaponMan.nextWeaponConfig();
317}
318
319/**
320 * @brief moves to the last WeaponConfiguration
321 */
322void NPC::previousWeaponConfig()
323{
324  this->weaponMan.previousWeaponConfig();
325}
326
327
328
329
330/**
331 * ticking
332 * @param dt  time since last tick
333 */
334void NPC::tick(float dt)
335{
336  this->weaponMan.tick(dt);
337  if (this->bFire)
338  {
339    std::cout << "fire..\n";
340    weaponMan.fire();
341  }
342  this->bFire = false;
343
344 if(this->entityTrack)
345    this->entityTrack->tick(dt);
346
347}
348
349void NPC::draw() const
350{
351 if( this->entityTrack != NULL && this->isDrawTrack())
352  this->entityTrack->drawGraph();
353
354 WorldEntity::draw();
355}
Note: See TracBrowser for help on using the repository browser.