Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 10442 was 10440, checked in by patrick, 18 years ago

making weapons loadable

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