From f3904724d7e6901a881911b1fd49565ec3eb1fdb Mon Sep 17 00:00:00 2001 From: ac-stef Date: Mon, 18 May 2015 14:06:42 +0200 Subject: [PATCH] limit the range of certain entity attributes For example, direction angle values can be reduced to +- 360 degree. (0..360 would be sufficient, but some people like their angles negative) This is not a reduction of functionality or a security measure - it's for convenience only: values now wrap around at the borders of their useful ranges. --- source/src/entities.cpp | 19 +++++++++++++++++++ source/src/protos.h | 1 + source/src/server.h | 4 ++-- source/src/world.cpp | 3 +++ source/src/worldio.cpp | 3 ++- 5 files changed, 27 insertions(+), 3 deletions(-) diff --git a/source/src/entities.cpp b/source/src/entities.cpp index 424c26fc..7a9c212a 100644 --- a/source/src/entities.cpp +++ b/source/src/entities.cpp @@ -662,3 +662,22 @@ void syncentchanges(bool force) changedents.setsize(0); lastentsync = lastmillis; } + +void clampentityattributes(persistent_entity &e) +{ + if(e.type < MAXENTTYPES) + { + int c; + #define CLAMPATTR(x) \ + c = entwraparound[e.type][x - 1]; \ + if(c > 0) e.attr##x = (e.attr##x % c + c) % c; /* fold value into range 0..c */ \ + else if(c < 0) e.attr##x = e.attr##x % (-c) /* fold value into range -c..c */ + CLAMPATTR(1); + CLAMPATTR(2); + CLAMPATTR(3); + CLAMPATTR(4); + CLAMPATTR(5); + CLAMPATTR(6); + CLAMPATTR(7); + } +} diff --git a/source/src/protos.h b/source/src/protos.h index cadcb7c0..0121450b 100644 --- a/source/src/protos.h +++ b/source/src/protos.h @@ -788,6 +788,7 @@ extern void setspawn(int i, bool on); extern void checkitems(playerent *d); extern vector changedents; extern void syncentchanges(bool force = false); +extern void clampentityattributes(persistent_entity &e); // rndmap extern void perlinarea(block &b, int scale, int seed, int psize); diff --git a/source/src/server.h b/source/src/server.h index e2683466..5dd8dc40 100644 --- a/source/src/server.h +++ b/source/src/server.h @@ -464,10 +464,10 @@ short entwraparound[MAXENTTYPES][7] = { 0, 0, 0, 0, 0, 0, 0 }, // helmet { 0, 0, 0, 0, 0, 0, 0 }, // armour { 0, 0, 0, 0, 0, 0, 0 }, // akimbo - { 3600, 0, 0, 0, 3600, 0, 0 }, // mapmodel + { -3600, 0, 0, 0, -3600, 0, 0 }, // mapmodel { 0, 0, 0, 0, 0, 0, 0 }, // trigger { 0, 0, 0, 0, 0, 0, 0 }, // ladder - { 3600, 0, 0, 0, 0, 0, 0 }, // ctf-flag + { -3600, 0, 0, 0, 0, 0, 0 }, // ctf-flag { 256, 0, 0, 0, 0, 0, 0 }, // sound { 0, 0, 0, 0, 0, 0, 0 }, // clip { 0, 0, 0, 0, 0, 0, 0 } // plclip diff --git a/source/src/world.cpp b/source/src/world.cpp index 3cd432b4..6bc09a8b 100644 --- a/source/src/world.cpp +++ b/source/src/world.cpp @@ -185,6 +185,7 @@ void entproperty(int prop, int amount) case 12: e.y += amount; break; case 13: e.z += amount; break; } + clampentityattributes(e); switch(e.type) { case LIGHT: calclight(); break; @@ -301,6 +302,7 @@ entity *newentity(int index, int x, int y, int z, char *what, int v1, int v2, in if(type != PLAYERSTART) e.attr1 = e.attr1 + 70 - (e.attr1 + 70) % 150; break; } + clampentityattributes(e); syncentchanges(true); addmsg(SV_EDITENT, "ri9i3", index<0 ? ents.length() : index, type, e.x, e.y, e.z, e.attr1, e.attr2, e.attr3, e.attr4, e.attr5, e.attr6, e.attr7); e.spawned = true; @@ -404,6 +406,7 @@ void editentity(char **args, int numargs) // index x y z a1 a2 a3 a4 ... case 7: e.attr4 = v; break; } } + clampentityattributes(e); switch(e.type) { case LIGHT: calclight(); break; diff --git a/source/src/worldio.cpp b/source/src/worldio.cpp index 12f4943f..775c9d96 100644 --- a/source/src/worldio.cpp +++ b/source/src/worldio.cpp @@ -568,6 +568,7 @@ void save_world(char *mname, bool skipoptimise, bool addcomfort) { if(!ne--) break; persistent_entity tmp = ents[i]; + clampentityattributes(tmp); lilswap((short *)&tmp, 4); lilswap(&tmp.attr5, 1); f->write(&tmp, sizeof(persistent_entity)); @@ -716,7 +717,7 @@ bool load_world(char *mname) // still supports all map formats that have if(e.type == LIGHT && e.attr1 < 0) e.attr1 = 0; // negative lights had no meaning before version 10 int ov, ss; #define SCALEATTR(x) \ - if((ss = entwraparound[e.type][x - 1] / entscale[e.type][x - 1])) e.attr##x = (int(e.attr##x) % ss + ss) % ss; \ + if((ss = abs(entwraparound[e.type][x - 1] / entscale[e.type][x - 1]))) e.attr##x = (int(e.attr##x) % ss + ss) % ss; \ e.attr##x = ov = e.attr##x * entscale[e.type][x - 1]; \ if(ov != e.attr##x) conoutf("overflow during conversion of attr%d of entity #%d (%s) - pls check before saving the map", x, i, entnames[e.type]); SCALEATTR(1);