From c9cc2a1a6b1c74950671bf1df538cb712b134b39 Mon Sep 17 00:00:00 2001 From: jacob1 Date: Thu, 20 Nov 2014 21:51:45 -0500 Subject: [PATCH] more accurate elementCount (part_change_type, pasting stamps, lua), prevent having multiple STKM / SPWN even more --- src/lua/CommandInterface.cpp | 63 ++++++++----- src/lua/CommandInterface.h | 4 +- src/lua/LegacyLuaAPI.cpp | 158 ++++++++++++--------------------- src/lua/LuaScriptHelper.h | 1 - src/lua/TPTScriptInterface.cpp | 45 +++++++--- src/simulation/Simulation.cpp | 36 +++++--- 6 files changed, 156 insertions(+), 151 deletions(-) diff --git a/src/lua/CommandInterface.cpp b/src/lua/CommandInterface.cpp index 8f8e5cef24..55b1bb718e 100644 --- a/src/lua/CommandInterface.cpp +++ b/src/lua/CommandInterface.cpp @@ -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; } diff --git a/src/lua/CommandInterface.h b/src/lua/CommandInterface.h index f4caf4f6ee..bb715135e9 100644 --- a/src/lua/CommandInterface.h +++ b/src/lua/CommandInterface.h @@ -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); diff --git a/src/lua/LegacyLuaAPI.cpp b/src/lua/LegacyLuaAPI.cpp index cd70c91a99..179387fa9b 100644 --- a/src/lua/LegacyLuaAPI.cpp +++ b/src/lua/LegacyLuaAPI.cpp @@ -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; @@ -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; } @@ -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; @@ -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); } } @@ -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; @@ -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; @@ -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=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; @@ -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; diff --git a/src/lua/LuaScriptHelper.h b/src/lua/LuaScriptHelper.h index b990823007..645cc54f40 100644 --- a/src/lua/LuaScriptHelper.h +++ b/src/lua/LuaScriptHelper.h @@ -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); diff --git a/src/lua/TPTScriptInterface.cpp b/src/lua/TPTScriptInterface.cpp index 797650bf23..e8b18d9d6d 100644 --- a/src/lua/TPTScriptInterface.cpp +++ b/src/lua/TPTScriptInterface.cpp @@ -262,8 +262,7 @@ AnyType TPTScriptInterface::tptS_set(std::deque * words) FormatType propertyFormat; int propertyOffset = GetPropertyOffset(property.Value(), propertyFormat); - - if(propertyOffset==-1) + if (propertyOffset == -1) throw GeneralException("Invalid property"); //Selector @@ -306,7 +305,7 @@ AnyType TPTScriptInterface::tptS_set(std::deque * words) if (property.Value() == "type" && (newValue < 0 || newValue >= PT_NUM || !sim->elements[newValue].Enabled)) throw GeneralException("Invalid element"); - if(selector.GetType() == TypePoint || selector.GetType() == TypeNumber) + if (selector.GetType() == TypePoint || selector.GetType() == TypeNumber) { int partIndex = -1; if(selector.GetType() == TypePoint) @@ -329,10 +328,13 @@ AnyType TPTScriptInterface::tptS_set(std::deque * words) case FormatFloat: *((float*)(partsBlock+(partIndex*sizeof(Particle))+propertyOffset)) = newValuef; break; + case FormatElement: + sim->part_change_type(partIndex, sim->parts[partIndex].x, sim->parts[partIndex].y, newValue); + break; } returnValue = 1; } - else if(selector.GetType() == TypeString && ((StringType)selector).Value() == "all") + else if (selector.GetType() == TypeString && ((StringType)selector).Value() == "all") { switch(propertyFormat) { @@ -356,27 +358,37 @@ AnyType TPTScriptInterface::tptS_set(std::deque * words) } } break; + case FormatElement: + { + for (int j = 0; j < NPART; j++) + if (sim->parts[j].type) + { + returnValue++; + sim->part_change_type(j, sim->parts[j].x, sim->parts[j].y, newValue); + } + } + break; } } else if(selector.GetType() == TypeString || selector.GetType() == TypeNumber) { int type; - if(selector.GetType() == TypeNumber) + if (selector.GetType() == TypeNumber) type = ((NumberType)selector).Value(); - else if(selector.GetType() == TypeString) + else if (selector.GetType() == TypeString) type = GetParticleType(((StringType)selector).Value()); - if(type<0 || type>=PT_NUM) + if (type<0 || type>=PT_NUM) throw GeneralException("Invalid particle type"); - if(type==0) + if (type==0) throw GeneralException("Cannot set properties of particles that do not exist"); std::cout << propertyOffset << std::endl; switch(propertyFormat) { case FormatInt: { - for(int j = 0; j < NPART; j++) - if(sim->parts[j].type == type) + for (int j = 0; j < NPART; j++) + if (sim->parts[j].type == type) { returnValue++; *((int*)(partsBlock+(j*sizeof(Particle))+propertyOffset)) = newValue; @@ -385,14 +397,23 @@ AnyType TPTScriptInterface::tptS_set(std::deque * words) break; case FormatFloat: { - for(int j = 0; j < NPART; j++) - if(sim->parts[j].type == type) + for (int j = 0; j < NPART; j++) + if (sim->parts[j].type == type) { returnValue++; *((float*)(partsBlock+(j*sizeof(Particle))+propertyOffset)) = newValuef; } } break; + case FormatElement: + { + for (int j = 0; j < NPART; j++) + if (sim->parts[j].type == type) + { + returnValue++; + sim->part_change_type(j, sim->parts[j].x, sim->parts[j].y, newValue); + } + } } } else diff --git a/src/simulation/Simulation.cpp b/src/simulation/Simulation.cpp index 2bfe797bbc..3d4b922974 100644 --- a/src/simulation/Simulation.cpp +++ b/src/simulation/Simulation.cpp @@ -102,18 +102,18 @@ int Simulation::Load(int fullX, int fullY, GameSave * save) tempPart.tmp = partMap[tempPart.tmp]; } - if(r = pmap[y][x]) + //Replace existing + if (r = pmap[y][x]) { - //Replace existing + elementCount[parts[r>>8].type]--; parts[r>>8] = tempPart; i = r>>8; pmap[y][x] = 0; - elementCount[parts[r>>8].type]--; elementCount[tempPart.type]++; } + //Allocate new particle else { - //Allocate new particle if (pfree == -1) break; i = pfree; @@ -326,9 +326,7 @@ Snapshot * Simulation::CreateSnapshot() void Simulation::Restore(const Snapshot & snap) { parts_lastActiveIndex = NPART-1; - - for(int i = 0; i 0 && parts[i].type < PT_NUM && elementCount[parts[i].type]) + elementCount[parts[i].type]--; + elementCount[t]++; + + if (t == PT_SPAWN && player.spawnID < 0) + player.spawnID = i; + else if (t == PT_SPAWN2 && player2.spawnID < 0) + player2.spawnID = i; + else if (t == PT_STKM) + Element_STKM::STKM_init_legs(this, &player, i); + else if (t == PT_STKM2) + Element_STKM::STKM_init_legs(this, &player2, i); + else if (t == PT_FIGH) + { + if (parts[i].tmp2 >= 0 && parts[i].tmp2 < 100) + Element_STKM::STKM_init_legs(this, &fighters[parts[i].tmp2], i); + } + parts[i].type = t; if (elements[t].Properties & TYPE_ENERGY) { @@ -3622,10 +3638,8 @@ void Simulation::update_particles_i(int start, int inc) } elementRecount |= !(currentTick%180); - if(elementRecount) - { + if (elementRecount) std::fill(elementCount, elementCount+PT_NUM, 0); - } for (i=0; i<=parts_lastActiveIndex; i++) if (parts[i].type) @@ -3637,7 +3651,7 @@ void Simulation::update_particles_i(int start, int inc) continue; } - if(elementRecount) + if (elementRecount) elementCount[t]++; elem_properties = elements[t].Properties;