Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/ai/src/ai/attack_module.cc @ 10283

Last change on this file since 10283 was 10283, checked in by tfahrni, 17 years ago
File size: 4.3 KB
Line 
1/*
2   orxonox - the future of 3D-vertical-scrollers
3
4   Copyright (C) 2004 orx
5
6   This program is free software; you can redistribute it and/or modify
7   it under the terms of the GNU General Public License as published by
8   the Free Software Foundation; either version 2, or (at your option)
9   any later version.
10
11   ### File Specific:
12   main-programmer: Thomas Fahrni
13   co-programmer:
14*/
15#define DEBUG_SPECIAL_MODULE DEBUG_MODULE_AI
16
17#include "attack_module.h"
18#include "ai_engine.h"
19#include "state.h"
20#include "debug.h"
21#include "player.h"
22#include "playable.h"
23#include "npcs/npc_test.h"
24#include "weapons/weapon.h"
25#include "projectiles/projectile.h"
26
27#include "shell_command.h"
28SHELL_COMMAND(setDistanceToPlayer, AttackModule, setDistanceToPlayer);
29SHELL_COMMAND(setDistanceToNPC, AttackModule, setDistanceToNPC);
30SHELL_COMMAND(setMaxAccleartion, AttackModule, setMaxAccleartion);
31SHELL_COMMAND(setTestValue, AttackModule, setTestValue);
32SHELL_COMMAND(setTestValue2, AttackModule, setTestValue2);
33
34float AttackModule::distanceToPlayer=15;
35float AttackModule::distanceToNPC=2;
36float AttackModule::maxAccleration=300.0f;
37float AttackModule::testValue=2;
38float AttackModule::testValue2=40;
39
40void AttackModule::setDistanceToPlayer(float newValue){ distanceToPlayer=newValue; }
41void AttackModule::setDistanceToNPC(float newValue){ distanceToNPC=newValue; }
42void AttackModule::setMaxAccleartion(float newValue){ maxAccleration=newValue; }
43void AttackModule::setTestValue(float newValue){ testValue=newValue; }
44void AttackModule::setTestValue2(float newValue){ testValue2=newValue; }
45
46AttackModule::AttackModule()
47{
48        tickCount=0;
49        randomFreq=40;
50        fireTimeout=1;
51}
52
53
54void AttackModule::process(float dt)
55{
56        if(npc == NULL)return;
57
58        Vector tmpVector;
59        float tmpFloat;
60        Vector npcCollision;
61        Vector playerCollision;
62
63        weight=1;
64        speedMax=1000.0f;
65
66
67        //get information about player
68        Player* pl = State::getPlayer();
69        if( pl == NULL)return;
70        Vector playerPosition = pl->getPlayable()->getAbsCoor();
71        float playerRadius=getRadius( pl->getPlayable() );
72
73
74        //get information about myself
75        Vector myPosition = npc->getAbsCoor();
76        float myRadius = getRadius(npc);
77
78
79        //anti player collision
80        Vector vectorToPlayer = playerPosition - myPosition;
81
82        tmpFloat=vectorToPlayer.len()-playerRadius-myRadius-distanceToPlayer;
83        if(tmpFloat<0.1)tmpFloat=0.1;
84        playerCollision=vectorToPlayer/(tmpFloat*tmpFloat)*(-1);
85
86
87        //anti NPC collision
88        for(ObjectList<WorldEntity>::const_iterator it = WorldEntity::objectList().begin(); it != WorldEntity::objectList().end(); ++it)
89        {
90                if((*it)->isA(Weapon::staticClassID()) )continue;
91                if((*it)->isA(Projectile::staticClassID()) )continue;
92                if(*it==npc)continue;
93
94                tmpVector=myPosition-(*it)->getAbsCoor();
95                tmpFloat=tmpVector.len()-myRadius-distanceToNPC-getRadius(*it);
96
97                if(tmpFloat<0.1)tmpFloat=0.1;
98                tmpVector=tmpVector/(tmpFloat*tmpFloat);
99
100                npcCollision=npcCollision+tmpVector;
101        }
102
103
104        //random movement
105        //randomFreq=testValue2;
106        if(++tickCount>=randomFreq){
107                tickCount=0;
108                int x = (rand()%101)-50;                        //-50-50
109                int z = (rand()%101)-50;                        //-50-50
110                randomVector=Vector(x,0,z);
111                randomFreq=(rand()%81)+70;                      //70-150 Ticks
112        }
113
114
115
116        //calculate correction vector
117        Vector vectorToDestination=destination-myPosition;
118
119        Vector correction=              playerCollision*50*3
120                                                                +       npcCollision*50*3 *6/myRadius
121                                                                +       destinationMovement*2//-movement
122                                                                +       (vectorToDestination-movement)*3
123                                                                +       (randomVector * testValue);
124
125
126        correction.y=0;
127
128
129        //limit accleration
130        float correctionLen=correction.len();
131        if(correctionLen>maxAccleration*dt)correction=correction/correctionLen*maxAccleration*dt;
132        movement+=correction;
133
134
135        //limit speed
136        float movementLen=movement.len();
137        if(movementLen>speedMax)movement=movement/movementLen*speedMax;
138
139
140        //move NPC...
141        npc->shiftCoor(movement * dt);
142
143
144        //rotate NPC
145        view = target->getAbsCoor()-myPosition;
146        Vector randomView=view.cross(Vector(0,1,0)).getNormalized();
147        randomView=randomView*((rand()%4)-2);
148
149        view = target->getAbsCoor()+randomView-myPosition;
150        view = view.cross( Vector(0,1,0) ).getNormalized();
151
152        npc->setAbsDirSoft( Quaternion( view, Vector(0,1,0)),1);
153
154
155        if(npc->isA(NPC::staticClassID()) ){
156                fireTimeout-=dt;
157                if(fireTimeout<=0){
158                        fireTimeout=(rand()%21)/10+1;
159                        //std::cout << "Fiiiiirrreee!\n";
160                        NPC* npc2 = static_cast<NPC*>(npc);
161                        npc2->fire();
162                }
163        }
164}
165
166
Note: See TracBrowser for help on using the repository browser.