JNR
aiMobState_Ramble.cpp
1 /*
2 
3 Jump'n'Run Engine
4 http://www.atanaslaskov.com/jnr/
5 
6 BSD LICENSE
7 Copyright (c) 2007-2013, Atanas Laskov
8 All rights reserved.
9 
10 Redistribution and use in source and binary forms, with or without
11 modification, are permitted provided that the following conditions are met:
12 1. Redistributions of source code must retain the above copyright notice,
13 this list of conditions and the following disclaimer.
14 2. Redistributions in binary form must reproduce the above copyright notice,
15 this list of conditions and the following disclaimer in the documentation
16 and/or other materials provided with the distribution.
17 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
18 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20 DISCLAIMED. IN NO EVENT SHALL ATANAS LASKOV BE LIABLE FOR ANY
21 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 
28 */
29 
30 #include "stdafx.h"
31 #include "ai.h"
32 
33 void aiMobState_Ramble::load(laFileParser *fp)
34 {
35  aiMobState::load(fp);
36 
37  //Load global params
38  //
39  fp->readBool(&_bJumpy);
40  fp->readBool(&_bTrapSenitive);
41 }
42 
43 void aiMobState_Ramble::perceive_active( std::string strID_Old )
44 {
45  laMonster* pMonster = (laMonster*) self();
46  laPlayer* pPlayer = (laPlayer*) player();
47 
48  pMonster->getPivot()-> bSimulateGravitation = M_TRUE;
49  pMonster->move(+1);
50  pMonster->_fxTrail.enable();
51 }
52 
53 void aiMobState_Ramble::perceive_inactive( std::string strID_New )
54 {
55 }
56 
57 aiMobState_Ramble::~aiMobState_Ramble(void)
58 {
59 }
60 
61 // The player is within monster's territory
62 //
63 void aiMobState_Ramble::perceive_InTerritory(unsigned state)
64 {
65  laMonster* pMonster = (laMonster*) self();
66  laPlayer* pPlayer = (laPlayer*) player();
67 
68  //_select_creature();
69 }
70 
71 // The player is in attack range
72 //
73 void aiMobState_Ramble::perceive_InRange(unsigned state)
74 {
75  laMonster* pMonster = (laMonster*) self();
76  laPlayer* pPlayer = (laPlayer*) player();
77 
78  switch(state)
79  {
80  case PERCEPT_SET:
81  _select_creature( M_TRUE );
82  break;
83 
84  case PERCEPT_RESET:
85  _select_creature( M_FALSE );
86  break;
87 
88  case PERCEPT_HOLD:
89  pPlayer->ctlBattleMode();
90  break;
91  }
92 }
93 
94 // Default
95 //
96 void aiMobState_Ramble::perceive(unsigned state, M_BOOL global, unsigned id, aiPerceptData data)
97 {
98  laMonster* pMonster = (laMonster*) self();
99  //laPlayer* pPlayer = (laPlayer*) player();
100 
101  // Update direction
102  // NOTE: Gravity is on for this AI type, so there is direct control only along X;
103  // The Y direction can be controlled not directly, but by performing jumps/somersaults
104  //
105  int direction = pMonster->getMoveDirection();
106  pMonster->move( this->_update_direction( direction ) );
107 
108  aiMobState::perceive(state, global, id, data);
109 }
110 
111 // Determine mov dir
112 //
113 int aiMobState_Ramble::_update_direction( int dir)
114 {
115  laMonster* pMonster = (laMonster*) self();
116 
117  if( _chasm_infront() )
118  {
119  dir = -1* dir; // => reverse dir
120  }
121  else if( _obstacle_infront() )
122  {
123  if( _obstacle_above() || (!_bJumpy) ) // .. and obstace above or don't care
124  dir = -1* dir; // => reverse dir
125  else // .. no obstacle above
126  pMonster->jump(); // => jump
127  }
128  else
129  {
130  // no obstacle => patrol within territory range
131  //
132  if( !_within_left_range() )
133  dir = +1;
134 
135  if( !_within_right_range() )
136  dir = -1;
137  }
138  return dir;
139 }
140 
141 M_BOOL aiMobState_Ramble::_obstacle_infront()
142 {
143  laMonster* pMonster = (laMonster*) self();
144  laPivot *pMobPivot = pMonster->getPivot();
145 
146  int direction = pMonster->getMoveDirection();
147  laPoint3 vTentacle(direction*M_UNIT*2, 0, 0);
148  laPivot vPivot = (*pMobPivot);
149 
150  vPivot[1] -= M_UNIT * 0.3; // nudge up a bit to obtain a safe margin
151  vTentacle = vPivot.projectVector(vTentacle);
152 
153  if( vTentacle.lenght() <= (vPivot.size.x()*0.5 + M_UNIT*0.15) ) return M_TRUE;
154  else return M_FALSE;
155 }
156 
157 M_BOOL aiMobState_Ramble::_obstacle_above()
158 {
159  laMonster* pMonster = (laMonster*) self();
160  laPivot *pMobPivot = pMonster->getPivot();
161 
162  int direction = pMonster->getMoveDirection();
163  laPoint3 vTentacle(direction*M_UNIT, 0, 0);
164  laPivot vPivot = (*pMobPivot);
165 
166  vPivot[1] -= M_UNIT * 1.5;
167  vTentacle = vPivot.projectVector(vTentacle);
168 
169  if( vTentacle.lenght() <= M_UNIT*0.5 ) return M_TRUE;
170  else return M_FALSE;
171 }
172 
173 M_BOOL aiMobState_Ramble::_chasm_infront()
174 {
175  laMonster* pMonster = (laMonster*) self();
176  laPivot *pMobPivot = pMonster->getPivot();
177 
178  int direction = pMonster->getMoveDirection();
179  laPoint3 vTentacle(0, +3*M_UNIT, 0); //should point downwards
180  laPivot vPivot = (*pMobPivot);
181  M_BOOL bTrap = M_FALSE;
182 
183  vPivot[0] += direction * vPivot.size.x()*0.2;
184  vTentacle = vPivot.projectVector(vTentacle, &bTrap);
185 
186  if( (bTrap && _bTrapSenitive) || (vTentacle.lenght() >= M_UNIT*1.2) ) return M_TRUE;
187  else return M_FALSE;
188 }
Playable Character.
Definition: laPlayer.h:46
#define M_UNIT
Unit of 1 meter.
Adds capabilities and percepts specific to monster creatures.
Definition: laMonster.h:45
Dynamic Object Pivot.
Definition: laPivot.h:44
File Parser.
Definition: laFileParser.h:41