Skip to content

Commit

Permalink
more accurate elementCount (part_change_type, pasting stamps, lua), p…
Browse files Browse the repository at this point in the history
…revent having multiple STKM / SPWN even more
  • Loading branch information
jacob1 committed Nov 21, 2014
1 parent 6066ae8 commit c9cc2a1
Show file tree
Hide file tree
Showing 6 changed files with 156 additions and 151 deletions.
63 changes: 41 additions & 22 deletions src/lua/CommandInterface.cpp
Expand Up @@ -34,54 +34,73 @@ void CommandInterface::Log(LogType type, std::string message)
m->Log(message);
}

int CommandInterface::GetPropertyOffset(std::string key_, FormatType & format)
int CommandInterface::GetPropertyOffset(std::string key, FormatType & format)
{
char * key = (char *)key_.c_str();
int offset;
if (strcmp(key, "type")==0){
int offset = -1;
if (!key.compare("type"))
{
offset = offsetof(Particle, type);
format = FormatInt;
} else if (strcmp(key, "life")==0){
format = FormatElement;
}
else if (!key.compare("life"))
{
offset = offsetof(Particle, life);
format = FormatInt;
} else if (strcmp(key, "ctype")==0){
}
else if (!key.compare("ctype"))
{
offset = offsetof(Particle, ctype);
format = FormatInt;
} else if (strcmp(key, "temp")==0){
}
else if (!key.compare("temp"))
{
offset = offsetof(Particle, temp);
format = FormatFloat;
} else if (strcmp(key, "tmp2")==0){
}
else if (!key.compare("tmp2"))
{
offset = offsetof(Particle, tmp2);
format = FormatInt;
} else if (strcmp(key, "tmp")==0){
}
else if (!key.compare("tmp"))
{
offset = offsetof(Particle, tmp);
format = FormatInt;
} else if (strcmp(key, "vy")==0){
}
else if (!key.compare("vy"))
{
offset = offsetof(Particle, vy);
format = FormatFloat;
} else if (strcmp(key, "vx")==0){
}
else if (!key.compare("vx"))
{
offset = offsetof(Particle, vx);
format = FormatFloat;
} else if (strcmp(key, "x")==0){
}
else if (!key.compare("x"))
{
offset = offsetof(Particle, x);
format = FormatFloat;
} else if (strcmp(key, "y")==0){
}
else if (!key.compare("y"))
{
offset = offsetof(Particle, y);
format = FormatFloat;
} else if (strcmp(key, "dcolour")==0){
offset = offsetof(Particle, dcolour);
format = FormatInt;
} else if (strcmp(key, "dcolor")==0){
}
else if (!key.compare("dcolor") || !key.compare("dcolour"))
{
offset = offsetof(Particle, dcolour);
format = FormatInt;
} else if (!strcmp(key, "pavg0")){
}
else if (!key.compare("pavg0"))
{
offset = offsetof(Particle, pavg[0]);
format = FormatFloat;
} else if (!strcmp(key, "pavg1")){
}
else if (!key.compare("pavg1"))
{
offset = offsetof(Particle, pavg[1]);
format = FormatFloat;
} else {
offset = -1;
}
return offset;
}
Expand Down
4 changes: 2 additions & 2 deletions src/lua/CommandInterface.h
Expand Up @@ -15,9 +15,9 @@ class CommandInterface {
GameController * c;
public:
enum LogType { LogError, LogWarning, LogNotice };
enum FormatType { FormatInt, FormatString, FormatChar, FormatFloat };
enum FormatType { FormatInt, FormatString, FormatChar, FormatFloat, FormatElement };
CommandInterface(GameController * c, GameModel * m);
int GetPropertyOffset(std::string key_, FormatType & format);
int GetPropertyOffset(std::string key, FormatType & format);
int GetParticleType(std::string type);
void Log(LogType type, std::string message);
//void AttachGameModel(GameModel * m);
Expand Down
158 changes: 55 additions & 103 deletions src/lua/LegacyLuaAPI.cpp
Expand Up @@ -23,34 +23,32 @@
#ifndef FFI
int luacon_partread(lua_State* l)
{
int format, offset, tempinteger;
int tempinteger, i = cIndex;
float tempfloat;
int i;
const char * key = luaL_optstring(l, 2, "");
offset = luacon_particle_getproperty(key, &format);

i = cIndex;
std::string key = luaL_optstring(l, 2, "");
CommandInterface::FormatType format;
int offset = luacon_ci->GetPropertyOffset(key, format);

if (i < 0 || i >= NPART || offset==-1)
if (i < 0 || i >= NPART)
return luaL_error(l, "Out of range");
if (offset == -1)
{
if (i < 0 || i >= NPART)
return luaL_error(l, "Out of range");
else if (!strcmp(key, "id"))
if (!key.compare("id"))
{
lua_pushnumber(l, i);
return 1;
}
else
return luaL_error(l, "Invalid property");
return luaL_error(l, "Invalid property");
}

switch(format)
{
case 0:
case CommandInterface::FormatInt:
case CommandInterface::FormatElement:
tempinteger = *((int*)(((unsigned char*)&luacon_sim->parts[i])+offset));
lua_pushnumber(l, tempinteger);
break;
case 1:
case CommandInterface::FormatFloat:
tempfloat = *((float*)(((unsigned char*)&luacon_sim->parts[i])+offset));
lua_pushnumber(l, tempfloat);
break;
Expand All @@ -60,29 +58,27 @@ int luacon_partread(lua_State* l)

int luacon_partwrite(lua_State* l)
{
int format, offset;
int i;
const char * key = luaL_optstring(l, 2, "");
offset = luacon_particle_getproperty(key, &format);

i = cIndex;
int tempinteger, i = cIndex;
float tempfloat;
std::string key = luaL_optstring(l, 2, "");
CommandInterface::FormatType format;
int offset = luacon_ci->GetPropertyOffset(key, format);

if (i < 0 || i >= NPART || offset==-1)
{
if (i < 0 || i >= NPART)
return luaL_error(l, "array index out of bounds");
else
return luaL_error(l, "Invalid property");
}
if (i < 0 || i >= NPART)
return luaL_error(l, "Out of range");
if (offset == -1)
return luaL_error(l, "Invalid property");

switch(format)
{
case 0:
case CommandInterface::FormatInt:
*((int*)(((unsigned char*)&luacon_sim->parts[i])+offset)) = luaL_optinteger(l, 3, 0);
break;
case 1:
case CommandInterface::FormatFloat:
*((float*)(((unsigned char*)&luacon_sim->parts[i])+offset)) = luaL_optnumber(l, 3, 0);
break;
case CommandInterface::FormatElement:
luacon_sim->part_change_type(i, luacon_sim->parts[i].x, luacon_sim->parts[i].y, luaL_optinteger(l, 3, 0));
}
return 0;
}
Expand All @@ -107,57 +103,6 @@ int luacon_partswrite(lua_State* l)
}
#endif

int luacon_particle_getproperty(const char * key, int * format)
{
int offset;
if (!strcmp(key, "type")) {
offset = offsetof(Particle, type);
*format = 0;
} else if (!strcmp(key, "life")) {
offset = offsetof(Particle, life);
*format = 0;
} else if (!strcmp(key, "ctype")) {
offset = offsetof(Particle, ctype);
*format = 0;
} else if (!strcmp(key, "temp")) {
offset = offsetof(Particle, temp);
*format = 1;
} else if (!strcmp(key, "tmp")) {
offset = offsetof(Particle, tmp);
*format = 0;
} else if (!strcmp(key, "tmp2")) {
offset = offsetof(Particle, tmp2);
*format = 0;
} else if (!strcmp(key, "vy")) {
offset = offsetof(Particle, vy);
*format = 1;
} else if (!strcmp(key, "vx")) {
offset = offsetof(Particle, vx);
*format = 1;
} else if (!strcmp(key, "x")){
offset = offsetof(Particle, x);
*format = 1;
} else if (!strcmp(key, "y")) {
offset = offsetof(Particle, y);
*format = 1;
} else if (!strcmp(key, "dcolour")) {
offset = offsetof(Particle, dcolour);
*format = 0;
} else if (!strcmp(key, "dcolor")) {
offset = offsetof(Particle, dcolour);
*format = 0;
} else if (!strcmp(key, "pavg0")) {
offset = offsetof(Particle, pavg[0]);
*format = 1;
} else if (!strcmp(key, "pavg1")) {
offset = offsetof(Particle, pavg[1]);
*format = 1;
} else {
offset = -1;
}
return offset;
}

int luacon_transition_getproperty(const char * key, int * format)
{
int offset;
Expand Down Expand Up @@ -1125,7 +1070,7 @@ int luatpt_set_property(lua_State* l)
if(!lua_isnumber(l, acount) && lua_isstring(l, acount))
{
name = luaL_optstring(l, acount, "none");
if ((partsel = luacon_ci->GetParticleType(std::string(name)))==-1)
if ((partsel = luacon_ci->GetParticleType(std::string(name))) == -1)
return luaL_error(l, "Unrecognised element '%s'", name);
}
}
Expand Down Expand Up @@ -1178,7 +1123,9 @@ int luatpt_set_property(lua_State* l)
ny = (int)(parts[i].y + .5f);
if (nx >= x && nx < x+w && ny >= y && ny < y+h && (!partsel || partsel == parts[i].type))
{
if(format == CommandInterface::FormatFloat)
if (format == CommandInterface::FormatElement)
luacon_sim->part_change_type(i, nx, ny, t);
else if(format == CommandInterface::FormatFloat)
*((float*)(((unsigned char*)&luacon_sim->parts[i])+offset)) = f;
else
*((int*)(((unsigned char*)&luacon_sim->parts[i])+offset)) = t;
Expand Down Expand Up @@ -1209,7 +1156,9 @@ int luatpt_set_property(lua_State* l)
if (partsel && partsel != luacon_sim->parts[i].type)
return 0;

if(format == CommandInterface::FormatFloat)
if (format == CommandInterface::FormatElement)
luacon_sim->part_change_type(i, luacon_sim->parts[i].x, luacon_sim->parts[i].y, t);
else if (format == CommandInterface::FormatFloat)
*((float*)(((unsigned char*)&luacon_sim->parts[i])+offset)) = f;
else
*((int*)(((unsigned char*)&luacon_sim->parts[i])+offset)) = t;
Expand Down Expand Up @@ -1329,40 +1278,42 @@ int luatpt_get_elecmap(lua_State* l)

int luatpt_get_property(lua_State* l)
{
int i, r, y;
const char *prop;
prop = luaL_optstring(l, 1, ""); //x coord or particle index, depending on arguments
i = luaL_optint(l, 2, 0);
y = luaL_optint(l, 3, -1);
if (y!=-1 && y < YRES && y >= 0 && i < XRES && i >= 0)
std::string prop = luaL_optstring(l, 1, "");
int i = luaL_optint(l, 2, 0); //x coord or particle index, depending on arguments
int y = luaL_optint(l, 3, -1);
if (y!=-1 && y<YRES && y>=0 && i < XRES && i>=0)
{
r = luacon_sim->pmap[y][i];
if (!r)
r = luacon_sim->photons[y][i];
int r = luacon_sim->pmap[y][i];
if (!r)
{
if (!strcmp(prop,"type"))
r = luacon_sim->photons[y][i];
if (!r)
{
lua_pushinteger(l, 0);
return 1;
if (!prop.compare("type"))
{
lua_pushinteger(l, 0);
return 1;
}
return luaL_error(l, "Particle does not exist");
}
return luaL_error(l, "Particle does not exist");
}
i = r>>8;
}
else if (y!=-1)
else if (y != -1)
return luaL_error(l, "Coordinates out of range (%d,%d)", i, y);
if (i < 0 || i >= NPART)
return luaL_error(l, "Invalid particle ID '%d'", i);

if (luacon_sim->parts[i].type)
{
int format, tempinteger;
int tempinteger;
float tempfloat;
int offset = luacon_particle_getproperty(prop, &format);
CommandInterface::FormatType format;
int offset = luacon_ci->GetPropertyOffset(prop, format);

if (offset == -1)
{
if (!strcmp(prop,"id"))
if (!prop.compare("id"))
{
lua_pushnumber(l, i);
return 1;
Expand All @@ -1372,17 +1323,18 @@ int luatpt_get_property(lua_State* l)
}
switch(format)
{
case 0:
case CommandInterface::FormatInt:
case CommandInterface::FormatElement:
tempinteger = *((int*)(((unsigned char*)&luacon_sim->parts[i])+offset));
lua_pushnumber(l, tempinteger);
break;
case 1:
case CommandInterface::FormatFloat:
tempfloat = *((float*)(((unsigned char*)&luacon_sim->parts[i])+offset));
lua_pushnumber(l, tempfloat);
}
return 1;
}
else if (!strcmp(prop,"type"))
else if (!prop.compare("type"))
{
lua_pushinteger(l, 0);
return 1;
Expand Down
1 change: 0 additions & 1 deletion src/lua/LuaScriptHelper.h
Expand Up @@ -35,7 +35,6 @@ int luacon_elementread(lua_State* l);
int luacon_elementwrite(lua_State* l);
int luacon_transitionread(lua_State* l);
int luacon_transitionwrite(lua_State* l);
int luacon_particle_getproperty(const char * key, int * format);
int luacon_transition_getproperty(const char * key, int * format);
int luacon_element_getproperty(const char * key, int * format, unsigned int * modified_stuff);
//int process_command_lua(pixel *vid_buf, char *console, char *console_error);
Expand Down

0 comments on commit c9cc2a1

Please sign in to comment.