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 | |
---|
25 | #include "shell_command.h" |
---|
26 | SHELL_COMMAND(setDistanceToPlayer, AttackModule, setDistanceToPlayer); |
---|
27 | SHELL_COMMAND(setDistanceToNPC, AttackModule, setDistanceToNPC); |
---|
28 | SHELL_COMMAND(setMaxAccleartion, AttackModule, setMaxAccleartion); |
---|
29 | SHELL_COMMAND(setTestValue, AttackModule, setTestValue); |
---|
30 | SHELL_COMMAND(setTestValue2, AttackModule, setTestValue2); |
---|
31 | |
---|
32 | float AttackModule::distanceToPlayer=15; |
---|
33 | float AttackModule::distanceToNPC=2; |
---|
34 | float AttackModule::maxAccleration=300.0f; |
---|
35 | float AttackModule::testValue=2; |
---|
36 | float AttackModule::testValue2=40; |
---|
37 | |
---|
38 | void AttackModule::setDistanceToPlayer(float newValue){ distanceToPlayer=newValue; } |
---|
39 | void AttackModule::setDistanceToNPC(float newValue){ distanceToNPC=newValue; } |
---|
40 | void AttackModule::setMaxAccleartion(float newValue){ maxAccleration=newValue; } |
---|
41 | void AttackModule::setTestValue(float newValue){ testValue=newValue; } |
---|
42 | void AttackModule::setTestValue2(float newValue){ testValue2=newValue; } |
---|
43 | |
---|
44 | AttackModule::AttackModule() |
---|
45 | { |
---|
46 | tickCount=0; |
---|
47 | randomFreq=60; |
---|
48 | } |
---|
49 | |
---|
50 | |
---|
51 | void AttackModule::process(float dt) |
---|
52 | { |
---|
53 | if(npc == NULL)return; |
---|
54 | |
---|
55 | Vector tmpVector; |
---|
56 | float tmpFloat; |
---|
57 | Vector npcCollision; |
---|
58 | Vector playerCollision; |
---|
59 | bool autoRotate=true; |
---|
60 | |
---|
61 | weight=1; |
---|
62 | speedMax=1000.0f; |
---|
63 | |
---|
64 | |
---|
65 | //get information about player |
---|
66 | Player* pl = State::getPlayer(); |
---|
67 | if( pl == NULL)return; |
---|
68 | Vector playerPosition = pl->getPlayable()->getAbsCoor(); |
---|
69 | float playerRadius=getRadius( pl->getPlayable() ); |
---|
70 | |
---|
71 | |
---|
72 | //get information about myself |
---|
73 | Vector myPosition = npc->getAbsCoor(); |
---|
74 | float myRadius = getRadius(npc); |
---|
75 | |
---|
76 | |
---|
77 | //anti player collision |
---|
78 | Vector vectorToPlayer = playerPosition - myPosition; |
---|
79 | |
---|
80 | tmpFloat=vectorToPlayer.len()-playerRadius-myRadius-distanceToPlayer; |
---|
81 | if(tmpFloat<0.1)tmpFloat=0.1; |
---|
82 | playerCollision=vectorToPlayer/(tmpFloat*tmpFloat)*(-1); |
---|
83 | |
---|
84 | |
---|
85 | //anti NPC collision |
---|
86 | for(ObjectList<WorldEntity>::const_iterator it = WorldEntity::objectList().begin(); it != WorldEntity::objectList().end(); ++it) |
---|
87 | { |
---|
88 | if(*it==npc)continue; |
---|
89 | |
---|
90 | tmpVector=myPosition-(*it)->getAbsCoor(); |
---|
91 | tmpFloat=tmpVector.len()-myRadius-distanceToNPC-getRadius(*it); |
---|
92 | |
---|
93 | if(tmpFloat<0.1)tmpFloat=0.1; |
---|
94 | tmpVector=tmpVector/(tmpFloat*tmpFloat); |
---|
95 | |
---|
96 | npcCollision=npcCollision+tmpVector; |
---|
97 | } |
---|
98 | |
---|
99 | |
---|
100 | //random movement |
---|
101 | //randomFreq=testValue2; |
---|
102 | if(++tickCount>=randomFreq && movement.len()<60){ |
---|
103 | tickCount=0; |
---|
104 | int x = (rand()%101)-50; //-50-50 |
---|
105 | int z = (rand()%101)-50; //-50-50 |
---|
106 | randomVector=Vector(x,0,z); |
---|
107 | randomFreq=(rand()%81)+70; //70-150 Ticks |
---|
108 | } |
---|
109 | |
---|
110 | |
---|
111 | |
---|
112 | //calculate correction vector |
---|
113 | Vector vectorToDestination=destination-myPosition; |
---|
114 | |
---|
115 | Vector correction= playerCollision*50*3 |
---|
116 | + npcCollision*50*3 |
---|
117 | + destinationMovement*2//-movement |
---|
118 | + (vectorToDestination-movement)*3 |
---|
119 | + (randomVector * testValue); |
---|
120 | |
---|
121 | |
---|
122 | correction.y=0; |
---|
123 | |
---|
124 | |
---|
125 | //limit accleration |
---|
126 | float correctionLen=correction.len(); |
---|
127 | if(correctionLen>maxAccleration*dt)correction=correction/correctionLen*maxAccleration*dt; |
---|
128 | movement+=correction; |
---|
129 | |
---|
130 | |
---|
131 | //limit speed |
---|
132 | float movementLen=movement.len(); |
---|
133 | if(movementLen>speedMax)movement=movement/movementLen*speedMax; |
---|
134 | |
---|
135 | |
---|
136 | //move NPC... |
---|
137 | npc->shiftCoor(movement * dt); |
---|
138 | |
---|
139 | |
---|
140 | //rotate NPC |
---|
141 | view = target->getAbsCoor()-myPosition; |
---|
142 | view = view.cross( Vector(0,1,0) ).getNormalized(); |
---|
143 | |
---|
144 | npc->setAbsDirSoft( Quaternion( view, Vector(0,1,0)),1); |
---|
145 | } |
---|
146 | |
---|
147 | |
---|