Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 10499 was 10499, checked in by patrick, 17 years ago

enable ai and weapon stuff

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