Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 10444 was 10444, checked in by snellen, 17 years ago


File size: 7.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 "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      this->setParent(this->entityTrack->getTrackNode());
77
78
79    // create the weapons and their manager
80
81
82  this->getWeaponManager().changeWeaponConfig(1);
83//   Weapon* wpRight = new TestGun(0);
84//   wpRight->setName("testGun Right");
85//   Weapon* wpLeft = new TestGun(1);
86//   wpLeft->setName("testGun Left");
87//
88//   wpRight->toList( this->getOMListNumber());
89//   wpLeft->toList( this->getOMListNumber());
90//
91//
92//   this->addWeapon(wpLeft, 1, 0);
93//   this->addWeapon(wpRight,1 ,1);
94//
95//   wpLeft->increaseEnergy( 100);
96//   wpRight->increaseEnergy( 100);
97
98  this->setHealthMax(100);
99  this->setHealth(80);
100
101//   this->getWeaponManager().setSlotCount(7);
102//
103//   this->getWeaponManager().setSlotPosition(0, Vector(-2.6, .1, -3.0));
104//   this->getWeaponManager().setSlotCapability(0, WTYPE_ALLDIRS | WTYPE_DIRECTIONAL);
105//
106//   this->getWeaponManager().setSlotPosition(1, Vector(-2.6, .1, 3.0));
107//   this->getWeaponManager().setSlotCapability(1, WTYPE_ALLDIRS | WTYPE_DIRECTIONAL);
108//
109//   this->getWeaponManager().setSlotPosition(2, Vector(-1.5, .5, -.5));
110//   this->getWeaponManager().setSlotDirection(2, Quaternion(-M_PI_4*.5, Vector(1,0,0)));
111//
112//   this->getWeaponManager().setSlotPosition(3, Vector(-1.5, .5, .5));
113//   this->getWeaponManager().setSlotDirection(3, Quaternion(M_PI_4*.5, Vector(1,0,0)));
114//
115//   this->getWeaponManager().setSlotPosition(4, Vector(-1.5, -.5, .5));
116//   this->getWeaponManager().setSlotDirection(4, Quaternion(-M_PI_4*.5+M_PI, Vector(1,0,0)));
117//
118//   this->getWeaponManager().setSlotPosition(5, Vector(-1.5, -.5, -.5));
119//   this->getWeaponManager().setSlotDirection(5, Quaternion(+M_PI_4*.5-M_PI, Vector(1,0,0)));
120//
121//   this->getWeaponManager().setSlotPosition(6, Vector(-1, 0.0, 0));
122//   this->getWeaponManager().setSlotCapability(6, WTYPE_ALLDIRS | WTYPE_DIRECTIONAL);
123//
124//   this->getWeaponManager().getFixedTarget()->setParent(this);
125//   this->getWeaponManager().getFixedTarget()->setRelCoor(100000,0,0);
126
127
128
129
130}
131
132
133NPC::~NPC ()
134{
135 if(! this->entityTrack)
136  AIEngine::getInstance()->removeAI(teamNumber,swarmNumber,(WorldEntity*)this);
137}
138
139
140
141/**
142 * loads the xml tags
143 * @param root: root xml tag for this element
144 */
145void NPC::loadParams(const TiXmlElement* root)
146{
147   WorldEntity::loadParams(root);
148
149  LoadParam(root, "enableAI", this, NPC, enableAI)
150      .describe("enables the AI algorithms");
151
152  LoadParam(root, "team", this, NPC, setTeamNumber)
153  .describe("this sets the team number")
154  .defaultValues(0);
155
156  LoadParam(root, "swarm", this, NPC, setSwarmNumber)
157  .describe("this sets the swarm number")
158  .defaultValues(0);
159
160  LoadParam(root, "maxSpeed", this, NPC, setMaxSpeed)
161  .describe("this sets the NPC max Speed")
162  .defaultValues(0);
163
164  LoadParam(root, "attackDistance", this, NPC, setAttackDistance)
165  .describe("this sets the NPC distance to target")
166  .defaultValues(0);
167
168
169    // Track
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}
Note: See TracBrowser for help on using the repository browser.