Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

weapon now added to the main model

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)
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      Weapon* w = dynamic_cast<Weapon*>(obj);
188      PRINTF(0)("created a weapon\n");
189      int preferedSlot = w->getPreferedSlot();
190      int preferedSide = w->getPreferedSide();
191
192      this->addWeapon( w, preferedSide, preferedSlot);
193    }
194  }
195  LOAD_PARAM_END_CYCLE(element);
196}
197
198
199/**
200 * @brief adds a Weapon to the NPC.
201 * @param weapon the Weapon to add.
202 * @param configID the Configuration ID to add this weapon to.
203 * @param slotID the slotID to add the Weapon to.
204 */
205bool NPC::addWeapon(Weapon* weapon, int configID, int slotID)
206{
207  weapon->setOwner(this->getOwner());
208
209
210  if(this->weaponMan.addWeapon(weapon, configID, slotID))
211  {
212    return true;
213  }
214  else
215  {
216    if (weapon != NULL)
217      PRINTF(1)("Unable to add Weapon (%s::%s) to %s::%s\n",
218                weapon->getClassCName(), weapon->getCName(), this->getClassCName(), this->getCName());
219    else
220      PRINTF(1)("No weapon defined\n");
221    return false;
222
223  }
224}
225
226/**
227 * @brief removes a Weapon.
228 * @param weapon the Weapon to remove.
229 */
230void NPC::removeWeapon(Weapon* weapon)
231{
232  this->weaponMan.removeWeapon(weapon);
233
234}
235
236/**
237 * @brief jumps to the next WeaponConfiguration
238 */
239void NPC::nextWeaponConfig()
240{
241  this->weaponMan.nextWeaponConfig();
242}
243
244/**
245 * @brief moves to the last WeaponConfiguration
246 */
247void NPC::previousWeaponConfig()
248{
249  this->weaponMan.previousWeaponConfig();
250}
251
252
253
254
255/**
256 * ticking
257 * @param dt  time since last tick
258 */
259void NPC::tick(float dt)
260{
261        //std::cout << "fire..\n";
262  this->weaponMan.tick(dt);
263  if (this->bFire)
264    weaponMan.fire();
265  this->bFire = false;
266
267 if(this->entityTrack)
268    this->entityTrack->tick(dt);
269
270}
Note: See TracBrowser for help on using the repository browser.