Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/playability/src/world_entities/npcs/npc.cc @ 10485

Last change on this file since 10485 was 10449, checked in by patrick, 18 years ago

drawing track is now loadable, merge from branche playability

File size: 7.3 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 "npc.h"
41
42ObjectListDefinition(NPC);
43CREATE_FACTORY(NPC);
44
45
46#include "script_class.h"
47CREATE_SCRIPTABLE_CLASS(NPC,
48                        addMethod("getAbsCoorX", Executor0ret<PNode, lua_State*, float>(&PNode::getAbsCoorX))
49                        ->addMethod("getAbsCoorY", Executor0ret<PNode, lua_State*, float>(&PNode::getAbsCoorY))
50                        ->addMethod("getAbsCoorZ", Executor0ret<PNode, lua_State*, float>(&PNode::getAbsCoorZ))
51                        ->addMethod("setAbsCoor", Executor3<PNode, lua_State*,float,float,float>(&PNode::setAbsCoor))
52                        ->addMethod("setAbsDir", Executor4<PNode, lua_State*,float,float,float,float>(&PNode::setAbsDir))
53                       );
54
55NPC::NPC(const TiXmlElement* root)
56  : weaponMan(this)
57{
58  this->registerObject(this, NPC::_objectList);
59
60  this->toList(OM_GROUP_01);
61  this->bAIEnabled = false;
62
63  if( root != NULL)
64    this->loadParams(root);
65
66  if( this->bAIEnabled && ! this->entityTrack)
67  {
68    std::cout << "Team Number: " << teamNumber << "\n";
69    std::cout << "Swarm Number:" << swarmNumber << "\n";
70
71    AIEngine::getInstance()->addAI(teamNumber,swarmNumber,(WorldEntity*)this,maxSpeed,attackDistance);
72  }
73
74  this->bFire = false;
75  if( this->entityTrack)
76  {
77      this->setParent(this->entityTrack->getTrackNode());
78      this->setRelCoor(0,0,0);
79  }
80
81    // create the weapons and their manager
82
83
84  this->getWeaponManager().changeWeaponConfig(1);
85//   Weapon* wpRight = new TestGun(0);
86//   wpRight->setName("testGun Right");
87//   Weapon* wpLeft = new TestGun(1);
88//   wpLeft->setName("testGun Left");
89//
90//   wpRight->toList( this->getOMListNumber());
91//   wpLeft->toList( this->getOMListNumber());
92//
93//
94//   this->addWeapon(wpLeft, 1, 0);
95//   this->addWeapon(wpRight,1 ,1);
96//
97//   wpLeft->increaseEnergy( 100);
98//   wpRight->increaseEnergy( 100);
99
100  this->setHealthMax(100);
101  this->setHealth(80);
102
103//   this->getWeaponManager().setSlotCount(7);
104//
105//   this->getWeaponManager().setSlotPosition(0, Vector(-2.6, .1, -3.0));
106//   this->getWeaponManager().setSlotCapability(0, WTYPE_ALLDIRS | WTYPE_DIRECTIONAL);
107//
108//   this->getWeaponManager().setSlotPosition(1, Vector(-2.6, .1, 3.0));
109//   this->getWeaponManager().setSlotCapability(1, WTYPE_ALLDIRS | WTYPE_DIRECTIONAL);
110//
111//   this->getWeaponManager().setSlotPosition(2, Vector(-1.5, .5, -.5));
112//   this->getWeaponManager().setSlotDirection(2, Quaternion(-M_PI_4*.5, Vector(1,0,0)));
113//
114//   this->getWeaponManager().setSlotPosition(3, Vector(-1.5, .5, .5));
115//   this->getWeaponManager().setSlotDirection(3, Quaternion(M_PI_4*.5, Vector(1,0,0)));
116//
117//   this->getWeaponManager().setSlotPosition(4, Vector(-1.5, -.5, .5));
118//   this->getWeaponManager().setSlotDirection(4, Quaternion(-M_PI_4*.5+M_PI, Vector(1,0,0)));
119//
120//   this->getWeaponManager().setSlotPosition(5, Vector(-1.5, -.5, -.5));
121//   this->getWeaponManager().setSlotDirection(5, Quaternion(+M_PI_4*.5-M_PI, Vector(1,0,0)));
122//
123//   this->getWeaponManager().setSlotPosition(6, Vector(-1, 0.0, 0));
124//   this->getWeaponManager().setSlotCapability(6, WTYPE_ALLDIRS | WTYPE_DIRECTIONAL);
125//
126//   this->getWeaponManager().getFixedTarget()->setParent(this);
127//   this->getWeaponManager().getFixedTarget()->setRelCoor(100000,0,0);
128
129
130
131
132}
133
134
135NPC::~NPC ()
136{
137 if(! this->entityTrack)
138  AIEngine::getInstance()->removeAI(teamNumber,swarmNumber,(WorldEntity*)this);
139}
140
141
142
143/**
144 * loads the xml tags
145 * @param root: root xml tag for this element
146 */
147void NPC::loadParams(const TiXmlElement* root)
148{
149   WorldEntity::loadParams(root);
150
151  LoadParam(root, "enableAI", this, NPC, enableAI)
152      .describe("enables the AI algorithms");
153
154  LoadParam(root, "team", this, NPC, setTeamNumber)
155  .describe("this sets the team number")
156  .defaultValues(0);
157
158  LoadParam(root, "swarm", this, NPC, setSwarmNumber)
159  .describe("this sets the swarm number")
160  .defaultValues(0);
161
162  LoadParam(root, "maxSpeed", this, NPC, setMaxSpeed)
163  .describe("this sets the NPC max Speed")
164  .defaultValues(0);
165
166  LoadParam(root, "attackDistance", this, NPC, setAttackDistance)
167  .describe("this sets the NPC distance to target")
168  .defaultValues(0);
169
170  LoadParamXML(root, "Weapons", this, NPC, addWeapons)
171  .describe("creates and adds weapons");
172}
173
174
175void NPC::addWeapons(const TiXmlElement* root)
176{
177  if( root == NULL)
178    return;
179
180  LOAD_PARAM_START_CYCLE(root, element);
181  {
182    PRINTF(0)("got weapon: %s\n", element->Value());
183    BaseObject* obj = Factory::fabricate(element);
184    if( obj != NULL && obj->isA( Weapon::staticClassID()))
185    {
186      Weapon* w = dynamic_cast<Weapon*>(obj);
187      PRINTF(0)("created a weapon\n");
188      int preferedSlot = w->getPreferedSlot();
189      int preferedSide = w->getPreferedSide();
190
191      this->addWeapon( w, preferedSide, preferedSlot);
192    }
193  }
194  LOAD_PARAM_END_CYCLE(element);
195}
196
197
198/**
199 * @brief adds a Weapon to the NPC.
200 * @param weapon the Weapon to add.
201 * @param configID the Configuration ID to add this weapon to.
202 * @param slotID the slotID to add the Weapon to.
203 */
204bool NPC::addWeapon(Weapon* weapon, int configID, int slotID)
205{
206  weapon->setOwner(this->getOwner());
207
208
209  if(this->weaponMan.addWeapon(weapon, configID, slotID))
210  {
211    return true;
212  }
213  else
214  {
215    if (weapon != NULL)
216      PRINTF(1)("Unable to add Weapon (%s::%s) to %s::%s\n",
217                weapon->getClassCName(), weapon->getCName(), this->getClassCName(), this->getCName());
218    else
219      PRINTF(1)("No weapon defined\n");
220    return false;
221
222  }
223}
224
225/**
226 * @brief removes a Weapon.
227 * @param weapon the Weapon to remove.
228 */
229void NPC::removeWeapon(Weapon* weapon)
230{
231  this->weaponMan.removeWeapon(weapon);
232
233}
234
235/**
236 * @brief jumps to the next WeaponConfiguration
237 */
238void NPC::nextWeaponConfig()
239{
240  this->weaponMan.nextWeaponConfig();
241}
242
243/**
244 * @brief moves to the last WeaponConfiguration
245 */
246void NPC::previousWeaponConfig()
247{
248  this->weaponMan.previousWeaponConfig();
249}
250
251
252
253
254/**
255 * ticking
256 * @param dt  time since last tick
257 */
258void NPC::tick(float dt)
259{
260        //std::cout << "fire..\n";
261  this->weaponMan.tick(dt);
262  if (this->bFire)
263    weaponMan.fire();
264  this->bFire = false;
265
266 if(this->entityTrack)
267    this->entityTrack->tick(dt);
268
269}
270
271void NPC::draw() const
272{
273 if( this->entityTrack != NULL && this->isDrawTrack())
274  this->entityTrack->drawGraph();
275
276 WorldEntity::draw();
277}
Note: See TracBrowser for help on using the repository browser.