aboutsummaryrefslogtreecommitdiff
path: root/src/shared/lua_api/phys/bcharactercontroller.cpp
blob: 4dfe79142098321a1a0fb8387bd01b4c21ee01fb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
#include <stdio.h>
#include <stdlib.h>
#include <list>
extern "C" {
  #include <lua.h>
  #include <lauxlib.h>
  #include <lualib.h>
}
#include <btBulletDynamicsCommon.h>
#include <BulletDynamics/Character/btKinematicCharacterController.h>
#include <BulletCollision/CollisionDispatch/btGhostObject.h>
#include "bcharactercontroller.hpp"
#include <shared/lua_api/common.hpp>

extern btDiscreteDynamicsWorld* World;
extern std::list<btCollisionObject*> Objects;
extern std::list<btKinematicCharacterController*> Chars;

//{character} :: btKinematicCharacterController*
btKinematicCharacterController *popCharacter(lua_State *L){
	lua_getfield(L,-1,"type");//{char},"type"
	if(lua_isnil(L,-1)){
		lua_pushstring(L,"Tried to call a character method on something that had not 'type'");
		lua_error(L);
	}
	const char *s = lua_tostring(L,-1);//{char},"type"
	if(strcmp(s,"character")!= 0){
		printf("Tried to pop character when it was not a character!\n");
		lua_pushstring(L,"Tried to call a character method on a ");
		lua_pushstring(L,s);
		lua_concat(L,2);
		lua_error(L);
	}
	lua_getfield(L,-2,"character");//{char},"type",ud_character
	if(lua_isnil(L,-1)){
		printf("Failed to get a \"character\" field\n");
		lua_pushstring(L,"Character object was not set up correctly\n");
		lua_error(L);
	}
	btKinematicCharacterController *c = (btKinematicCharacterController*)lua_touserdata(L,-1);
	lua_pop(L,3);
	return c;
	
}
/*
static LBPhysNode* checkisbphysbox(lua_State* L, int index){
  void* ud = luaL_checkudata(L,index,"phys.physbox");
  luaL_argcheck(L,ud != NULL, index, "'phys.physbox' expected");
  return (LBPhysNode*) ud;
}
*/

/*
static LISceneNode* checkismesh(lua_State* L){
  return checkismesh(L,1);
}
*/
// ud_character :: ({v3 size}, {v3 origin})
void makenewbcharactercontroller(lua_State* L){
	printf("Creating new character controller\n");
	//lua_pushstring(L,"Character controller is totally fucking broken for now\n");
	//lua_error(L);
	double px,py,pz; //position
	double sx,sy,sz; //size
	//double mass;
	
	//mass = lua_tonumber(L,-1);//{v3_size},{v3_origin},mass
	//lua_pop(L,1);//{v3_size},{v3_origin}
	//printf("Got mass: %f\n",mass);
	
	popvector3d(L,&px,&py,&pz);//{v3_size}
	printf("Got position: (%f,%f,%f)\n",px,py,pz);
	popvector3d(L,&sx,&sy,&sz);//

	btVector3 vshape = btVector3(sx * 0.5f, sy * 0.5f, sz * 0.5f);
	printf("Got size: (%f,%f,%f)\n",sx,sy,sz);
	btVector3 pos = btVector3(px,py,pz);
	btTransform transform = btTransform(btQuaternion(0,0,0,1),pos);

	// Create the shape
	btConvexShape* cshape = new btBoxShape(vshape);



	// Add mass
	//btVector3 localinertia = btVector3(0,0,0);
	//shape->calculateLocalInertia(mass, localinertia);

	// Create the rigid body object
	//btRigidBody::btRigidBodyConstructionInfo cinfo = btRigidBody::btRigidBodyConstructionInfo(
		//mass,
		//motionstate,
		//shape,
		//localinertia
	//);
	btPairCachingGhostObject *ghost = new btPairCachingGhostObject();
	ghost->setWorldTransform(transform);
	ghost->setCollisionShape(cshape);
	World->addCollisionObject(ghost,btBroadphaseProxy::CharacterFilter, btBroadphaseProxy::StaticFilter|btBroadphaseProxy::DefaultFilter);
	//ghost->setCollisionFlags(btCollisionObject::CF_CHARACTER_OBJECT);
	printf("Character controller created\n");
	btKinematicCharacterController *cc = new btKinematicCharacterController(ghost, cshape, 8, btVector3(0,1,0));
	//cc->setMaxSlope(3.14 / 4.0);
	//cinfo.m_friction = 0;
    
	// Add it to the world
	printf("About to add action\n");
	World->addAction(cc);
	printf("Finished adding action\n");
	//printf("Added rigid body to world: %p\n",World);
	printf("Added to Chars\n");
	//Chars.push_back(cc);
	//Objects.push_back(ghost);

	lua_pushlightuserdata(L,cc);//ud_cc
}

// char:getvelocity()
int bcharactergetvelocity(lua_State *L){
	btKinematicCharacterController *r = popCharacter(L);
	btVector3 v = r->getLinearVelocity();
	pushvector3d(L,v.x(),v.y(),v.z());
	return 1;
}

// char:setvelocity(v3 vel)
int bcharactersetvelocity(lua_State *L){
	double x,y,z;
	popvector3d(L,&x,&y,&z);
	btKinematicCharacterController *r = popCharacter(L);
	r->setLinearVelocity(btVector3(x,y,z));
	return 0;
}

// phys.newphysbox(vector3 size, vector3 origin, double mass)
int newbcharactercontroller(lua_State* L){
	//printf("Createing bphysbox!\n");
	//Create it's lua representation
	makenewbcharactercontroller(L);//ud_cc
	btKinematicCharacterController* r = (btKinematicCharacterController*)lua_touserdata(L,-1);//ud_cc
	lua_pop(L,1);//
	lua_newtable(L);//{}
	lua_pushlightuserdata(L,r);//{},ud_cc
	lua_setfield(L,-2,"character");//{character=ud_cc}

	lua_pushstring(L,"character");
	lua_setfield(L,-2,"type");//{character=ud_cc,type="character"}

	//Set it's metatable
	luaL_getmetatable(L, "phys.charactercontroller");//{},{phys.charactercontroller}
	lua_setmetatable(L, -2);//{cc}

  	return 1;
}

// char:setgravity(v3 gravity)
int bcharactersetgravity(lua_State *L){
	double x,y,z;
	popvector3d(L,&x,&y,&z);
	btKinematicCharacterController *c = popCharacter(L);
	c->setGravity(btVector3(x,y,z));
	return 0;
}

// char:getpos() :: v3
int bcharactergetpos(lua_State *L){
	btKinematicCharacterController *c = popCharacter(L);
	btVector3 pos = c->getGhostObject()->getWorldTransform().getOrigin();
	pushvector3d(L,pos.x(),pos.y(),pos.z());
	return 1;
}

// char:setpos(v3 pos)
int bcharactersetpos(lua_State *L){
	double x,y,z;
	popvector3d(L,&x,&y,&z);
	btKinematicCharacterController *c = popCharacter(L);
	btTransform t = c->getGhostObject()->getWorldTransform();
	t.setOrigin(btVector3(x,y,z));
	c->getGhostObject()->setWorldTransform(t);
	return 0;
}

// char:onground()
int bcharacteronground(lua_State *L){
	btKinematicCharacterController *c = popCharacter(L);
	lua_pushboolean(L,c->onGround() == true ? 1 : 0);
	return 1;
}

// char:jump(v3 jump)
int bcharacterjump(lua_State *L){
	printf("Jump called\n");
	double x,y,z;
	popvector3d(L,&x,&y,&z);
	btKinematicCharacterController *c = popCharacter(L);
	printf("About to jump\n");
	c->jump(btVector3(x,y,z));
	printf("Done jumping\n");
	return 0;
}

//{phys.physbox}:delete()
static int delbcharactercontroller(lua_State* L){//self
	//printf("Attempting to delete physbox\n");
	lua_getfield(L,-1,"character");//self,ud_character
	btKinematicCharacterController* r = (btKinematicCharacterController*)lua_touserdata(L,-1);//self,ud_character
	lua_pop(L,2);
	delete r->getGhostObject();
	delete r;
	return 0;
}

//{char},{v3_dir} ::
int bcharsetwalkdirection(lua_State *L){
	double x,y,z;
	popvector3d(L,&x,&y,&z);//{char}
	lua_getfield(L,-1,"character");//{char},ud_cc
	btKinematicCharacterController* cc = (btKinematicCharacterController*)lua_touserdata(L,-1);
	lua_pop(L,2);
	cc->setWalkDirection(btVector3(x,y,z));
	return 0;
}

// char:setfallspeed(n)
int bcharactersetfallspeed(lua_State *L){
	double speed = luaL_optnumber(L,-1,1);
	printf("Got number: %f\n",speed);
	lua_pop(L,1);
	btKinematicCharacterController *c = popCharacter(L);
	printf("About to set speed\n");
	c->setFallSpeed(speed);
	printf("Done setting speed\n");
	return 0;
}

// char:getmaxslope()
int bcharactergetmaxslope(lua_State *L){
	btKinematicCharacterController *c = popCharacter(L);
	btScalar s = c->getMaxSlope();
	lua_pushnumber(L,s);
	return 1;
}

// char:setmaxslope(slope)
int bcharactersetmaxslope(lua_State *L){
	btScalar s = lua_tonumber(L,-1);
	lua_pop(L,1);
	btKinematicCharacterController *c = popCharacter(L);
	c->setMaxSlope(s);
	return 0;
}

// char:setvelocityforinterval(vec3 {velocity},number interval)
int bcharactersetvelocityforinterval(lua_State *L){
	double interval = lua_tonumber(L,-1);
	lua_pop(L,1);
	double x,y,z;
	popvector3d(L,&x,&y,&z);
	btKinematicCharacterController *c = popCharacter(L);
	c->setVelocityForTimeInterval(btVector3(x,y,z),interval);
	return 0;
}

extern const luaL_reg bcharactercontroller_m[] = {
	{"setwalkdir",          bcharsetwalkdirection},
	{"remove",              delbcharactercontroller},
	{"getvelocity",         bcharactergetvelocity},
	{"setvelocity",         bcharactersetvelocity},
	{"setgravity",          bcharactersetgravity},
	{"getpos",              bcharactergetpos},
	{"setpos",              bcharactersetpos},
	{"onground",            bcharacteronground},
	{"jump",                bcharacterjump},
	{"setfallspeed",        bcharactersetfallspeed},
	{"getmaxslope",         bcharactergetmaxslope},
	{"setmaxslope",         bcharactersetmaxslope},
	{"setvelocityforinterval",bcharactersetvelocityforinterval},
	{0, 0},
};

void bcharactercontroller_register(lua_State* L){//
	//printf("Registered bphysbox\n");

	luaL_newmetatable(L, "phys.charactercontroller");//{phys.characontroller}
	lua_newtable(L);//{phys.charcontroller},{}
	luaL_register(L,NULL,bcharactercontroller_m);//{phys.charcontroller},{}
	lua_setfield(L,-2,"__index");//{phys.charcontroller}

	lua_pop(L,1);//

	lua_getglobal(L,"phys");//{}
	lua_pushcfunction(L,newbcharactercontroller);//{},newbcharactercontroller()
	lua_setfield(L,-2,"newbcharactercontroller");//{}

	lua_pop(L,1);
}