1 /*=============================================================================
2 Blobby Volley 2
3 Copyright (C) 2006 Jonathan Sieber (jonathan_sieber@yahoo.de)
4 Copyright (C) 2006 Daniel Knobe (daniel-knobe@web.de)
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 of the License, or
9 (at your option) any later version.
10 
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 GNU General Public License for more details.
15 
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19 =============================================================================*/
20 
21 /* header include */
22 #include "PlayerInput.h"
23 
24 /* includes */
25 #include <ostream>
26 #include <cassert>
27 
28 #include "raknet/BitStream.h"
29 
30 #include "DuelMatch.h"
31 #include "GameConstants.h"
32 
33 /* implementation */
34 
35 /* PlayerInput */
setAll(unsigned char all)36 void PlayerInput::setAll( unsigned char all )
37 {
38 	left = all & 4;
39 	right = all & 2;
40 	up =  all & 1;
41 }
42 
operator ==(const PlayerInput & other) const43 bool PlayerInput::operator==(const PlayerInput& other) const
44 {
45 	return left == other.left && right == other.right && up == other.up;
46 }
47 
getAll() const48 unsigned char PlayerInput::getAll() const
49 {
50 	unsigned char c = 0;
51 	c = (left ? 4 : 0) + (right ? 2 : 0) + (up ? 1 : 0);
52 	return c;
53 }
54 
55 /* PlayerInputAbs */
56 
PlayerInputAbs()57 PlayerInputAbs::PlayerInputAbs() : mFlags( F_RELATIVE ), mTarget(-1)
58 {
59 
60 }
61 
PlayerInputAbs(RakNet::BitStream & stream)62 PlayerInputAbs::PlayerInputAbs(RakNet::BitStream& stream)
63 {
64 	stream.Read( mFlags );
65 	stream.Read( mTarget );
66 }
67 
PlayerInputAbs(bool l,bool r,bool j)68 PlayerInputAbs::PlayerInputAbs(bool l, bool r, bool j) : mFlags( F_RELATIVE ), mTarget(-1)
69 {
70 	setLeft(l);
71 	setRight(r);
72 	setJump(j);
73 }
74 
75 
76 // set input
setLeft(bool v)77 void PlayerInputAbs::setLeft( bool v )
78 {
79 	if(v)
80 		mFlags |= F_LEFT;
81 	else
82 		mFlags &= ~F_LEFT;
83 }
84 
setRight(bool v)85 void PlayerInputAbs::setRight( bool v )
86 {
87 	if(v)
88 		mFlags |= F_RIGHT;
89 	else
90 		mFlags &= ~F_RIGHT;
91 }
92 
setJump(bool v)93 void PlayerInputAbs::setJump( bool v)
94 {
95 	if(v)
96 		mFlags |= F_JUMP;
97 	else
98 		mFlags &= ~F_JUMP;
99 }
100 
setTarget(short target,PlayerSide player)101 void PlayerInputAbs::setTarget( short target, PlayerSide player )
102 {
103 	mFlags &= F_JUMP;	// reset everything but the jump flag, i.e. no left/right and no relative
104 	mTarget = target;
105 
106 	if(player == LEFT_PLAYER )
107 	{
108 		setLeft(true);
109 	}
110 	if(player == RIGHT_PLAYER )
111 	{
112 		setRight(true);
113 	}
114 }
115 
swapSides()116 void PlayerInputAbs::swapSides()
117 {
118 	bool left = mFlags & F_LEFT;
119 	bool right = mFlags & F_RIGHT;
120 
121 	setLeft(right);
122 	setRight(left);
123 
124 	mTarget = RIGHT_PLANE - mTarget;
125 }
126 
toPlayerInput(const DuelMatch * match) const127 PlayerInput PlayerInputAbs::toPlayerInput( const DuelMatch* match ) const
128 {
129 	if( mFlags & F_RELATIVE)
130 		return PlayerInput( mFlags & F_LEFT, mFlags & F_RIGHT, mFlags & F_JUMP );
131 	else
132 	{
133 		bool left = false;
134 		bool right = false;
135 
136 		PlayerSide side = mFlags & F_LEFT ? LEFT_PLAYER : RIGHT_PLAYER;
137 
138 		// here we load the current position of the player.
139 		float blobpos = match->getBlobPosition(side).x;
140 
141 		float distance = std::abs(blobpos - mTarget);
142 
143 		if ( std::abs(blobpos + BLOBBY_SPEED - mTarget) < distance )
144 			right = true;
145 		else if (std::abs(blobpos - BLOBBY_SPEED - mTarget) < distance)
146 			left = true;
147 		return PlayerInput( left, right, mFlags & F_JUMP );
148 	}
149 
150 }
151 
writeTo(RakNet::BitStream & stream)152 void PlayerInputAbs::writeTo(RakNet::BitStream& stream)
153 {
154 	stream.Write( mFlags );
155 	stream.Write( mTarget );
156 }
157 
158 
operator <<(std::ostream & out,const PlayerInput & input)159 std::ostream& operator<< (std::ostream& out, const PlayerInput& input)
160 {
161 	out << (input.left ? 't' : 'f') << (input.right ? 't' : 'f') << (input.up ? 't' : 'f');
162 	return out;
163 }
164