aboutsummaryrefslogtreecommitdiff
path: root/src/shared/lua_api
diff options
context:
space:
mode:
Diffstat (limited to 'src/shared/lua_api')
-rw-r--r--src/shared/lua_api/common.c78
-rw-r--r--src/shared/lua_api/load_core.h13
-rw-r--r--src/shared/lua_api/load_net.cpp88
-rw-r--r--src/shared/lua_api/load_phys.cpp6
-rw-r--r--src/shared/lua_api/load_phys.hpp14
-rw-r--r--src/shared/lua_api/phys/bphysbox.cpp151
-rw-r--r--src/shared/lua_api/phys/bphysbox.hpp13
-rw-r--r--src/shared/lua_api/stream.cpp71
-rw-r--r--src/shared/lua_api/stream.hpp28
9 files changed, 445 insertions, 17 deletions
diff --git a/src/shared/lua_api/common.c b/src/shared/lua_api/common.c
index 1e0c971..140ece1 100644
--- a/src/shared/lua_api/common.c
+++ b/src/shared/lua_api/common.c
@@ -36,3 +36,81 @@ void loadLLibs(lua_State* L){
lua_call(L,1,0);
*/
}
+
+
+int pushvector3i(lua_State* L,long a,long b,long c){
+ lua_newtable(L);
+
+ lua_pushinteger(L,1);
+ lua_pushinteger(L,a);
+ lua_settable(L,-3);
+
+ lua_pushinteger(L,2);
+ lua_pushinteger(L,b);
+ lua_settable(L,-3);
+
+ lua_pushinteger(L,3);
+ lua_pushinteger(L,c);
+ lua_settable(L,-3);
+
+ return 1;
+}
+int pushvector3d(lua_State* L,double a,double b,double c){
+ lua_newtable(L);//{}
+
+ lua_pushinteger(L,1);//{},1
+ lua_pushnumber(L,a);//{},1,a
+ lua_settable(L,-3);//{}
+
+ lua_pushinteger(L,2);//{},2
+ lua_pushnumber(L,b);//{},2,b
+ lua_settable(L,-3);//{}
+
+ lua_pushinteger(L,3);//{},3
+ lua_pushnumber(L,c);//{},3,c
+ lua_settable(L,-3);//{}
+
+ return 1;
+}
+
+int popvector3i(lua_State* L,long* a,long* b,long* c){//{v3}
+ lua_pushinteger(L,1);//{v3},1
+ lua_gettable(L,-2);//{v3},v3[1]
+ *a = lua_tointeger(L,-1);//{v3},v3[1]
+ lua_pop(L,1);//{v3}
+
+ lua_pushinteger(L,2);//{v3},2
+ lua_gettable(L,-2);//{v3},v3[2]
+ *b = lua_tointeger(L,-1);//{v3},v3[2]
+ lua_pop(L,1);//{v3}
+
+ lua_pushinteger(L,3);//{v3},3
+ lua_gettable(L,-2);//{v3},v3[3]
+ *c = lua_tointeger(L,-1);//{v3},v3[3]
+ lua_pop(L,1);//{v3}
+
+ lua_pop(L,1);//
+ return 0;
+}
+
+
+
+int popvector3d(lua_State* L,double* a,double* b,double* c){
+ lua_pushinteger(L,1);
+ lua_gettable(L,-2);
+ *a = lua_tonumber(L,-1);
+ lua_pop(L,1);
+
+ lua_pushinteger(L,2);
+ lua_gettable(L,-2);
+ *b = lua_tonumber(L,-1);
+ lua_pop(L,1);
+
+ lua_pushinteger(L,3);
+ lua_gettable(L,-2);
+ *c = lua_tonumber(L,-1);
+ lua_pop(L,1);
+
+ lua_pop(L,1);
+ return 0;
+}
diff --git a/src/shared/lua_api/load_core.h b/src/shared/lua_api/load_core.h
new file mode 100644
index 0000000..3e4b2df
--- /dev/null
+++ b/src/shared/lua_api/load_core.h
@@ -0,0 +1,13 @@
+
+struct Entity {
+
+};
+
+struct EntityPoint : Entity{
+ double pos[3] = {0,0,0};
+};
+
+struct EntityMass : PointEntity{
+ double ang[3] = {0,0,0};
+ int mass = 0;
+};
diff --git a/src/shared/lua_api/load_net.cpp b/src/shared/lua_api/load_net.cpp
index cd155fb..5280d42 100644
--- a/src/shared/lua_api/load_net.cpp
+++ b/src/shared/lua_api/load_net.cpp
@@ -20,18 +20,45 @@ extern "C" {
#include "load_net.hpp"
-//#include <zmq.h>
#include "../util/hashmap.h"
+#include "stream.hpp"
std::map<int,std::map<std::string,int>> netfuncs;
+// stream:readInt()
+int lstream_readInt(lua_State* L){//self
+ lua_getfield(L,-1,"data");//self,lightuserdata
+ if(!lua_islightuserdata(L,-1)){
+ lua_pushstring(L,"data field was not light userdata");
+ lua_error(L);
+ }
+ struct stream* s = (struct stream*)lua_touserdata(L,-1);//self,lightuserdata
+ printf("Stream in readint was %p\n",s);
+ stream_print(s);
+ int i = stream_readInt(s);
+ printf("In readint, int read from stream was %d\n",i);
+ lua_pop(L,2);//
+ lua_pushinteger(L,i);//int
+ return 1;
+}
+// stream:writeInt(number)
+int lstream_writeInt(lua_State* L){//self,number
+ lua_getfield(L,-2,"data");//self,number,lightuserdata
+ struct stream* s = (struct stream*)lua_touserdata(L,-1);//self,number,lightuserdata
+ printf("Stream in writeInt was %p\n",s);
+ stream_print(s);
+ int num = lua_tointeger(L,-2);//self,number,lightuserdata
+ printf("Integer was %d\n",num);
+ stream_writeInt(s,num);
+ lua_pop(L,3);//
+ return 0;
+}
+
void gameloop_net(lua_State* L){
//printf("Doing net of gameloop\n");
for(std::map<int,std::map<std::string,int>>::iterator it = netfuncs.begin(); it != netfuncs.end(); ++it){
- //printf("Looking for new data for socket %d\n",it->first);
char* buf = NULL;
int bytes = nn_recv(it->first, &buf, NN_MSG,NN_DONTWAIT);
- //printf("After recv\n");
if(bytes < 0 && nn_errno() != EAGAIN){
lua_pushstring(L,"Failed to receive");
lua_error(L);
@@ -39,22 +66,27 @@ void gameloop_net(lua_State* L){
//do nothing
}else{
//find how long until the first null character
- int msglen = strlen(buf);
- char msg[msglen];
- msg[msglen] = '\0';
- sprintf(msg,"%s",buf);
+ struct stream* stream = stream_create();
+ stream->length = bytes;
+ stream->data = buf;
+ stream_print(stream);
+ char* msg = stream_readString(stream);
std::map<std::string,int> themap = it->second;
std::string s = std::string(msg);
std::map<std::string,int>::iterator in = themap.find(s);
if(in != themap.end()){
lua_rawgeti(L,LUA_REGISTRYINDEX,(*in).second);
lua_newtable(L);
+ lua_pushlightuserdata(L,stream);
+ lua_setfield(L,-2,"data");
+ luaL_getmetatable(L,"net.stream");
+ lua_setmetatable(L,-2);
lua_call(L,1,0);
}
nn_freemsg(buf);
+ free(stream);//We manually set stream->data so free_stream would crash here
}
}
- //hashmap_iterate(netfuncs,check_socket,0);
}
int bindsocket(lua_State*L){
@@ -66,7 +98,7 @@ int bindsocket(lua_State*L){
int id = nn_bind(fd,s);
if(id < 0){
const char* errstr = nn_strerror(nn_errno());
- char* failmsg = "Failed to bind socket: ";
+ char* failmsg = (char*)"Failed to bind socket: ";
int faillen = strlen(failmsg + 2);
char buf[faillen + strlen(errstr)];
sprintf(buf,"%s%s\n",failmsg,errstr);
@@ -99,16 +131,28 @@ int connectsocket(lua_State* L){
return 0;
}
+//socket:send("name",function(stream))
int send(lua_State* L){
- const char* data = lua_tostring(L,-1);
- lua_pushstring(L,"fd");
- lua_gettable(L,-2);
- int fd = lua_tonumber(L,-1);
+ const char* data = lua_tostring(L,-2);//socket,"name",function(stream)
+ lua_pushstring(L,"fd");//socket,"name",function(stream),"fd"
+ lua_gettable(L,-4);//socket,"name",function(stream),int_socketdescriptor
+ int fd = lua_tonumber(L,-1);//socket,"name",function(stream),int_socketdescriptor
+ lua_pop(L,1);//socket,"name",function(stream)
int dlen = strlen(data);
- int err = nn_send(fd,(void*)data,dlen,0);
+ struct stream* s = stream_create();
+ stream_writeString(s,data,dlen);
+ lua_newtable(L);//socket,"name",function(stream),streamtable
+ luaL_getmetatable(L,"net.stream");//socket,"name",function(stream),streamtable,net.stream
+ lua_setmetatable(L,-2);//socket,"name",function(stream),streamtable
+ lua_pushlightuserdata(L,s);//socket,"name",function(stream),streamtable,lightudata
+ lua_setfield(L,-2,"data");//socket,"name",function(stream),streamtable
+ lua_call(L,1,0);//socket,"name"
+ lua_pop(L,2);//
+ int err = nn_send(fd,s->data,s->length,0);
+ stream_free(s);
if(err < 0){
const char* errstr = nn_strerror(nn_errno());
- char* failmsg = "Failed to bind socket: ";
+ char* failmsg = (char*)"Failed to bind socket: ";
int faillen = strlen(failmsg + 2);
char buf[faillen + strlen(errstr)];
sprintf(buf,"%s%s\n",failmsg,errstr);
@@ -119,7 +163,7 @@ int send(lua_State* L){
}
//LUA:
-//socket:receive(s_name,function(socket))
+//socket:receive(s_name,function(stream))
int netreceive(lua_State* L){
const char* name = lua_tostring(L,-2);
int slen = strlen(name);
@@ -215,7 +259,17 @@ void loadNetLibs(lua_State* L){
//Set the table to gobal "net"
lua_setglobal(L,"net");
-
+
+ //Create the metatable for streams
+ luaL_newmetatable(L,"net.stream"); //net.stream
+ lua_newtable(L);
+ lua_pushcfunction(L,lstream_readInt);//net.stream,{},readInt()
+ lua_setfield(L,-2,"readInt");//net.stream,{}
+ lua_pushcfunction(L,lstream_writeInt);//net.stream,{},writeInt()
+ lua_setfield(L,-2,"writeInt");//net.stream,{}
+ lua_setfield(L,-2,"__index");
+
+
//Create the metatable for sockets
luaL_newmetatable(L,"net.socket");
lua_pushvalue(L,-1);
diff --git a/src/shared/lua_api/load_phys.cpp b/src/shared/lua_api/load_phys.cpp
new file mode 100644
index 0000000..c91396c
--- /dev/null
+++ b/src/shared/lua_api/load_phys.cpp
@@ -0,0 +1,6 @@
+#include "load_phys.hpp"
+
+
+void loadPhysLibs(lua_State* L){
+
+}
diff --git a/src/shared/lua_api/load_phys.hpp b/src/shared/lua_api/load_phys.hpp
new file mode 100644
index 0000000..5e05642
--- /dev/null
+++ b/src/shared/lua_api/load_phys.hpp
@@ -0,0 +1,14 @@
+extern "C" {
+ #include <lua.h>
+ #include <lauxlib.h>
+ #include <lualib.h>
+}
+#include "load_core.hpp"
+
+struct EntityPhysics : EntityMass{
+ btTransform* transform;
+ btRigidBody* rigidbody;
+ btCollisionShape* shape;
+}EntityPhysics;
+
+void loadPhysLibs(lua_State* L);
diff --git a/src/shared/lua_api/phys/bphysbox.cpp b/src/shared/lua_api/phys/bphysbox.cpp
new file mode 100644
index 0000000..be9db07
--- /dev/null
+++ b/src/shared/lua_api/phys/bphysbox.cpp
@@ -0,0 +1,151 @@
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <list>
+extern "C" {
+ #include <lua.h>
+ #include <lauxlib.h>
+ #include <lualib.h>
+}
+#include <btBulletDynamicsCommon.h>
+#include "bphysbox.hpp"
+#include "../common.h"
+
+extern btDiscreteDynamicsWorld* World;
+extern std::list<btRigidBody*> Objects;
+/*
+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);
+}
+*/
+
+// phys.newphysbox(vector3 size, vector3 origin, double mass)
+int newbphysbox(lua_State* L){
+ printf("Createing bphysbox!\n");
+ int nargs = lua_gettop(L);
+ if(nargs != 3){
+ lua_pushfstring(L,"Expected 3 arguments, got %d",nargs);
+ 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: %d\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);
+
+ // Set the initial position of the object
+ btTransform transform;
+ transform.setIdentity();
+ transform.setOrigin(pos);
+
+ // Give it a default MotionState
+ btDefaultMotionState* motionstate = new btDefaultMotionState(transform);
+
+ // Create the shape
+ btCollisionShape* shape = new btBoxShape(vshape);
+
+ // Add mass
+ btVector3 localinertia;
+ shape->calculateLocalInertia(mass, localinertia);
+
+ // Create the rigid body object
+ btRigidBody* rigidbody = new btRigidBody(mass, motionstate, shape, localinertia);
+
+ // Add it to the world
+ World->addRigidBody(rigidbody);
+ Objects.push_back(rigidbody);
+
+ //Create it's lua representation
+ lua_newtable(L);//{}
+ lua_pushlightuserdata(L,rigidbody);//{},ud_rigidbody
+ lua_setfield(L,-2,"rigidbody");//{}
+
+ //Set it's metatable
+ luaL_getmetatable(L, "phys.physbox");//{},{phys.physbox}
+ lua_setmetatable(L, -2);//{}
+
+ return 1;
+}
+
+//{phys.physbox}:delete()
+static int delbphysbox(lua_State* L){//self
+ printf("Attempting to delete physbox\n");
+ lua_getfield(L,-1,"rigidbody");//self,ud_rigidbody
+ btRigidBody* r = (btRigidBody*)lua_touserdata(L,-1);//self,ud_rigidbody
+ delete r->getCollisionShape();
+ delete r->getMotionState();
+ delete r;
+
+ return 0;
+}
+
+// physbox:setpos({v3 pos})
+static int bphyssetpos(lua_State *L){//self,{v3 pos}
+ double nx,ny,nz;
+ popvector3d(L,&nx,&ny,&nz);//self
+
+ lua_getfield(L,-1,"rigidbody");//self,ud_rigidbody
+ btRigidBody* i = (btRigidBody*)lua_touserdata(L,-1);//self
+ btMotionState* ms = i->getMotionState();
+ btTransform bt;
+ ms->getWorldTransform(bt);
+
+ btVector3 to = btVector3(nx,ny,nz);
+ bt.setOrigin(to);
+ ms->setWorldTransform(bt);
+ i->activate();
+ lua_pop(L,1);//
+ return 0;
+}
+
+// {v3 pos} :: physbox:getpos()
+static int bphysgetpos(lua_State *L){//self
+ lua_getfield(L,-1,"rigidbody");//self,ud_rigidbody
+ btRigidBody* i = (btRigidBody*)lua_touserdata(L,-1);//self,ud_rigidbody
+ btTransform bt = i->getWorldTransform();
+ btVector3 bv = bt.getOrigin();
+ lua_pop(L,2);//
+ pushvector3d(L,bv.x(),bv.y(),bv.z());//{}
+
+ return 1;
+}
+
+static const luaL_reg bphysbox_m[] = {
+ {"getpos", bphysgetpos},
+ {"setpos", bphyssetpos},
+ {"delete", delbphysbox},
+ {0, 0},
+};
+
+void bphysbox_register(lua_State* L){//
+
+ luaL_newmetatable(L, "phys.physbox");//{phys.physbox}
+ lua_newtable(L);//{phys.physbox},{}
+ luaL_register(L,NULL,bphysbox_m);//{phys.physbox},{}
+ lua_setfield(L,-2,"__index");//{phys.physbox}
+
+ lua_pop(L,1);//
+
+ lua_getglobal(L,"phys");//{}
+ lua_pushcfunction(L,newbphysbox);
+ lua_setfield(L,-2,"newphysbox");
+
+}
diff --git a/src/shared/lua_api/phys/bphysbox.hpp b/src/shared/lua_api/phys/bphysbox.hpp
new file mode 100644
index 0000000..cdc4476
--- /dev/null
+++ b/src/shared/lua_api/phys/bphysbox.hpp
@@ -0,0 +1,13 @@
+
+
+#include <stdio.h>
+#include <stdlib.h>
+extern "C" {
+ #include <lua.h>
+ #include <lauxlib.h>
+ #include <lualib.h>
+}
+#include <irrlicht.h>
+
+void bphysbox_register(lua_State* L);
+int newbphysbox(lua_State* L);
diff --git a/src/shared/lua_api/stream.cpp b/src/shared/lua_api/stream.cpp
new file mode 100644
index 0000000..3ec2394
--- /dev/null
+++ b/src/shared/lua_api/stream.cpp
@@ -0,0 +1,71 @@
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+#include "stream.hpp"
+
+char stx = 0x002;
+char etx = 0x003;
+
+struct stream* stream_create(){
+ struct stream* s = (struct stream*)malloc(sizeof(struct stream));
+ s->length = 0;
+ s->data = (byte*)malloc(sizeof(byte)*0);
+ s->read = 0;
+ return s;
+}
+void stream_writeInt(struct stream* s, int number){
+ long o = s->length;
+ s->length += sizeof(int);
+ s->data = (byte*)realloc(s->data,s->length);
+ int* ptr = (int*)(s->data + o);
+ *ptr = number;
+}
+int stream_readInt(struct stream* s){
+ int* ptr = (int*)(s->data + s->read);
+ s->read += sizeof(int);
+ return *ptr;
+}
+void stream_writeData(struct stream* s, const char* d, int len){
+ long o = s->length;
+ s->length += sizeof(char)*len;
+ s->data = (byte*)realloc(s->data,s->length);
+ char* ptr = (char*)(s->data + o);
+ memcpy(ptr,d,len);
+}
+void stream_readData(struct stream* s, int len, char* out){
+ char* ptr = (char*)(s->data + s->read);
+ memcpy(out,ptr,len);
+ s->read += len;
+}
+void stream_writeString(struct stream* s, const char* str, size_t len){
+ stream_writeData(s,str,len);
+ stream_writeData(s,"\0",1);//Null character
+}
+char* stream_readString(struct stream* s){
+ char* ptr = (char*)(s->data + s->read);
+ s->read += strlen(ptr) + 1;//+1 for null character
+ return ptr;
+}
+void stream_writeDouble(struct stream* s, double number){
+ long o = s->length;
+ s->length += sizeof(double);
+ s->data = (byte*)realloc(s->data,s->length);
+ double* ptr = (double*)(s->data + o);
+ *ptr = number;
+}
+
+void stream_print(struct stream* s){
+ printf("Length:%ld\nRead:%ld\nData starts at %p\nData:",s->length, s->read, s->data);
+ long i = 0;
+ byte* ptr = s->data;
+ while(i < s->length){
+ printf("%4X",*(ptr + i));
+ i++;
+ }
+ printf("\n");
+}
+
+void stream_free(struct stream* s){
+ free(s->data);
+ free(s);
+}
diff --git a/src/shared/lua_api/stream.hpp b/src/shared/lua_api/stream.hpp
new file mode 100644
index 0000000..71eaf0a
--- /dev/null
+++ b/src/shared/lua_api/stream.hpp
@@ -0,0 +1,28 @@
+#include <stdlib.h>
+
+//Char is not definitvely a byte, read the fucking standard
+#define byte char
+
+typedef struct stream {
+ long length;
+ byte* data;
+ long read;
+} stream;
+
+struct stream* stream_create();
+
+void stream_writeInt(struct stream* s, int number);
+int stream_readInt(struct stream* s);
+
+void stream_writeString(struct stream* s, const char* string, size_t len);
+char* stream_readString(struct stream* s);
+
+void stream_writeData(struct stream* s, const char* data, int len);
+void stream_readData(struct stream* s, int len, char* out);
+
+void stream_writeDouble(struct stream* s, double number);
+double stream_readDouble(struct stream* s);
+
+void stream_print(struct stream* s);
+
+void stream_free(struct stream* s);