Skip to content

Commit

Permalink
remove duplicate function, fix Simulation::GetParticleType, fixes #402
Browse files Browse the repository at this point in the history
  • Loading branch information
jacob1 committed Apr 22, 2017
1 parent 01d17cb commit 866289c
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 36 deletions.
21 changes: 0 additions & 21 deletions src/lua/CommandInterface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -105,27 +105,6 @@ int CommandInterface::GetPropertyOffset(std::string key, FormatType & format)
return offset;
}

int CommandInterface::GetParticleType(std::string type)
{
int i = -1;
char * txt = (char*)type.c_str();

//Scope
Element * elements = m->GetSimulation()->elements;

// alternative names for some elements
if (strcasecmp(txt,"C4")==0) return PT_PLEX;
else if (strcasecmp(txt,"C5")==0) return PT_C5;
else if (strcasecmp(txt,"NONE")==0) return PT_NONE;
for (i=1; i<PT_NUM; i++) {
if (strcasecmp(txt, elements[i].Name)==0 && elements[i].Enabled)
{
return i;
}
}
return -1;
}

std::string CommandInterface::GetLastError()
{
return lastError;
Expand Down
1 change: 0 additions & 1 deletion src/lua/CommandInterface.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ class CommandInterface {
enum FormatType { FormatInt, FormatString, FormatChar, FormatFloat, FormatElement };
CommandInterface(GameController * c, GameModel * m);
int GetPropertyOffset(std::string key, FormatType & format);
int GetParticleType(std::string type);
void Log(LogType type, std::string message);
//void AttachGameModel(GameModel * m);
virtual bool OnActiveToolChanged(int toolSelection, Tool * tool) {return true;}
Expand Down
10 changes: 5 additions & 5 deletions src/lua/LegacyLuaAPI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -413,7 +413,7 @@ int luacon_elementwrite(lua_State* l)
//Convert to upper case
for (size_t j = 0; j < strlen(tempstring); j++)
tempstring[j] = toupper(tempstring[j]);
if(luacon_ci->GetParticleType(tempstring) != -1)
if(luacon_sim->GetParticleType(tempstring) != -1)
{
free(tempstring);
return luaL_error(l, "Name in use");
Expand Down Expand Up @@ -664,7 +664,7 @@ int luatpt_getelement(lua_State *l)
{
luaL_checktype(l, 1, LUA_TSTRING);
const char* name = luaL_optstring(l, 1, "");
if ((t = luacon_ci->GetParticleType(name))==-1)
if ((t = luacon_sim->GetParticleType(name))==-1)
return luaL_error(l, "Unrecognised element '%s'", name);
lua_pushinteger(l, t);
}
Expand Down Expand Up @@ -852,7 +852,7 @@ int luatpt_create(lua_State* l)
return luaL_error(l, "Unrecognised element number '%d'", t);
} else {
const char* name = luaL_optstring(l, 3, "dust");
if ((t = luacon_ci->GetParticleType(std::string(name))) == -1)
if ((t = luacon_sim->GetParticleType(std::string(name))) == -1)
return luaL_error(l,"Unrecognised element '%s'", name);
}
retid = luacon_sim->create_part(-1, x, y, t);
Expand Down Expand Up @@ -1065,7 +1065,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_sim->GetParticleType(std::string(name))) == -1)
return luaL_error(l, "Unrecognised element '%s'", name);
}
}
Expand All @@ -1082,7 +1082,7 @@ int luatpt_set_property(lua_State* l)
else
{
name = luaL_checklstring(l, 2, NULL);
if ((t = luacon_ci->GetParticleType(std::string(name)))==-1)
if ((t = luacon_sim->GetParticleType(std::string(name)))==-1)
return luaL_error(l, "Unrecognised element '%s'", name);
}
if (!lua_isnumber(l, 3) || acount >= 6)
Expand Down
6 changes: 3 additions & 3 deletions src/lua/TPTScriptInterface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ AnyType TPTScriptInterface::tptS_set(std::deque<std::string> * words)
}
else
{
newValue = GetParticleType(((StringType)value).Value());
newValue = m->GetSimulation()->GetParticleType(((StringType)value).Value());
if (newValue < 0 || newValue >= PT_NUM)
{
// TODO: add element CAKE to invalidate this
Expand Down Expand Up @@ -386,7 +386,7 @@ AnyType TPTScriptInterface::tptS_set(std::deque<std::string> * words)
if (selector.GetType() == TypeNumber)
type = ((NumberType)selector).Value();
else if (selector.GetType() == TypeString)
type = GetParticleType(((StringType)selector).Value());
type = m->GetSimulation()->GetParticleType(((StringType)selector).Value());

if (type<0 || type>=PT_NUM)
throw GeneralException("Invalid particle type");
Expand Down Expand Up @@ -446,7 +446,7 @@ AnyType TPTScriptInterface::tptS_create(std::deque<std::string> * words)
if(createType.GetType() == TypeNumber)
type = ((NumberType)createType).Value();
else if(createType.GetType() == TypeString)
type = GetParticleType(((StringType)createType).Value());
type = m->GetSimulation()->GetParticleType(((StringType)createType).Value());
else
throw GeneralException("Invalid type");

Expand Down
15 changes: 9 additions & 6 deletions src/simulation/Simulation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4726,15 +4726,18 @@ void Simulation::UpdateParticles(int start, int end)

int Simulation::GetParticleType(std::string type)
{
int i = -1;
char * txt = (char*)type.c_str();

// alternative names for some elements
if (strcasecmp(txt,"C4")==0) i = PT_PLEX;
else if (strcasecmp(txt,"C5")==0) i = PT_C5;
else if (strcasecmp(txt,"NONE")==0) i = PT_NONE;
for (i=1; i<PT_NUM; i++) {
if (strcasecmp(txt, elements[i].Name)==0 && strlen(elements[i].Name) && elements[i].Enabled)
if (!strcasecmp(txt, "C4"))
return PT_PLEX;
else if (!strcasecmp(txt, "C5"))
return PT_C5;
else if (!strcasecmp(txt, "NONE"))
return PT_NONE;
for (int i = 1; i < PT_NUM; i++)
{
if (!strcasecmp(txt, elements[i].Name) && strlen(elements[i].Name) && elements[i].Enabled)
{
return i;
}
Expand Down

0 comments on commit 866289c

Please sign in to comment.