Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/trunk/src/ai/attack_module.cc @ 10753

Last change on this file since 10753 was 10527, checked in by bknecht, 18 years ago

some minor changes at the AI

File size: 4.4 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        //PRINTF(0)("attack process\n");
57
58        if(npc == NULL)return;
59
60        Vector tmpVector;
61        float tmpFloat;
62        Vector npcCollision;
63        Vector playerCollision;
64
65        weight=1;
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        //get information about myself
74        Vector myPosition = npc->getAbsCoor();
75        float myRadius = getRadius(npc);
76        //float vMax=1000.0f/myRadius;
77        float vMax=maxSpeed;
78        float aMax=1000/myRadius;
79
80        //anti player collision
81        Vector vectorToPlayer = playerPosition - myPosition;
82
83        tmpFloat=vectorToPlayer.len()-playerRadius-myRadius-distanceToPlayer;
84        if(tmpFloat<0.1)tmpFloat=0.1;
85        playerCollision=vectorToPlayer/(tmpFloat*tmpFloat)*(-1);
86
87
88        //anti NPC collision
89        for(ObjectList<WorldEntity>::const_iterator it = WorldEntity::objectList().begin(); it != WorldEntity::objectList().end(); ++it)
90        {
91                if((*it)->isA(Weapon::staticClassID()) )continue;
92                if((*it)->isA(Projectile::staticClassID()) )continue;
93                if(*it==npc)continue;
94
95                tmpVector=myPosition-(*it)->getAbsCoor();
96                tmpFloat=tmpVector.len()-myRadius-distanceToNPC-getRadius(*it);
97
98                if(tmpFloat<0.1)tmpFloat=0.1;
99                tmpVector=tmpVector/(tmpFloat*tmpFloat);
100
101                npcCollision=npcCollision+tmpVector;
102        }
103
104
105        //random movement
106        //randomFreq=testValue2;
107        if(++tickCount>=randomFreq){
108                tickCount=0;
109                int x = (rand()%101)-50;                        //-50-50
110                int z = (rand()%101)-50;                        //-50-50
111                randomVector=Vector(x,0,z);
112                randomFreq=(rand()%81)+70;                      //70-150 Ticks
113        }
114
115
116
117        //calculate correction vector
118        Vector vectorToDestination=destination-myPosition;
119
120        Vector correction=              playerCollision*40*3 *6/myRadius
121                                                                +       npcCollision*50*3 *6/myRadius
122                                                                +       destinationMovement*2//-movement
123                                                                +       (vectorToDestination-movement)*3
124                                                                +       (randomVector * testValue);
125
126
127        correction.y=0;
128
129
130        //limit accleration
131        float correctionLen=correction.len();
132        if(correctionLen>maxAccleration*dt)correction=correction/correctionLen*maxAccleration*dt;
133        movement+=correction;
134
135
136        //limit speed
137        float movementLen=movement.len();
138        if(movementLen>vMax)movement=movement/movementLen*vMax;
139
140
141        //move NPC...
142        npc->shiftCoor(movement * dt);
143
144
145        //rotate NPC
146        view = target->getAbsCoor()-myPosition;
147        Vector randomView=view.cross(Vector(0,1,0)).getNormalized();
148        randomView=randomView*((rand()%4)-2);
149
150        view = target->getAbsCoor()+randomView-myPosition;
151        view = view.cross( Vector(0,1,0) ).getNormalized();
152
153        npc->setAbsDirSoft( Quaternion( view, Vector(0,1,0)),8/myRadius);
154
155
156        if(npc->isA(NPC::staticClassID()) ){
157                fireTimeout-=dt;
158                if(fireTimeout<=0){
159                        fireTimeout=(rand()%21)/10+1;
160                        //std::cout << "Fiiiiirrreee!\n";
161                        NPC* npc2 = static_cast<NPC*>(npc);
162                        npc2->fire();
163                }
164        }
165}
166
167
Note: See TracBrowser for help on using the repository browser.