From: sf17k Date: Mon, 30 Jul 2012 01:25:06 +0000 (-0400) Subject: some findClosest* refactoring X-Git-Url: https://git.jsancho.org/?a=commitdiff_plain;h=26debbd380c6922e5a0b60d99567c6374a4fef9a;p=lugaru.git some findClosest* refactoring --- diff --git a/Source/GameTick.cpp b/Source/GameTick.cpp index 03c0efd..3522f20 100644 --- a/Source/GameTick.cpp +++ b/Source/GameTick.cpp @@ -609,27 +609,38 @@ static void ch_size(const char *args) player[0].scale = atof(args) * .2; } -static int find_closest() -{ - int closest = 0; - float closestdist = std::numeric_limits::max(); - - for (int i = 1; i < numplayers; i++) { - float distance; - distance = findDistancefast(&player[i].coords,&player[0].coords); - if (distance < closestdist) { - closestdist = distance; - closest = i; +static int findClosestPlayer(){ + int closest = -1; + float closestdist = std::numeric_limits::max(); + + for(int i=1; i::max(); + + for(int i=0; i=0) player[closest].scale = atof(args) * .2; } @@ -660,8 +671,8 @@ static void ch_proportion(const char *args) static void ch_proportionnear(const char *args) { - int closest = find_closest(); - if (closest) + int closest = findClosestPlayer(); + if(closest>=0) set_proportion(closest, args); } @@ -682,8 +693,8 @@ static void ch_protection(const char *args) static void ch_protectionnear(const char *args) { - int closest = find_closest(); - if (closest) + int closest = findClosestPlayer(); + if(closest>=0) set_protection(closest, args); } @@ -704,8 +715,8 @@ static void ch_armor(const char *args) static void ch_armornear(const char *args) { - int closest = find_closest(); - if (closest) + int closest = findClosestPlayer(); + if(closest>=0) set_armor(closest, args); } @@ -745,8 +756,8 @@ static void ch_noclothes(const char *args) static void ch_noclothesnear(const char *args) { - int closest = find_closest(); - if (closest) + int closest = findClosestPlayer(); + if(closest>=0) set_noclothes(closest, args); } @@ -774,8 +785,8 @@ static void ch_clothes(const char *args) static void ch_clothesnear(const char *args) { - int closest = find_closest(); - if (closest) + int closest = findClosestPlayer(); + if(closest>=0) set_clothes(closest, args); } @@ -2652,18 +2663,8 @@ void doDebugKeys(){ } if(Input::isKeyPressed(SDLK_x)&&Input::isKeyDown(SDLK_LSHIFT)){ - int closest=-1; - float closestdist=-1; - float distance; - if(numplayers>1) - for(int i=1;i=0){ if(player[closest].num_weapons){ if(weapons[player[closest].weaponids[0]].getType()==sword) weapons[player[closest].weaponids[0]].setType(staff); @@ -2683,37 +2684,20 @@ void doDebugKeys(){ } if(Input::isKeyDown(SDLK_u)){ - int closest=-1; - float closestdist=-1; - float distance; - if(numplayers>1) - for(int i=1;i=0){ + player[closest].yaw+=multiplier*50; + player[closest].targetyaw=player[closest].yaw; + } } if(Input::isKeyPressed(SDLK_o)&&!Input::isKeyDown(SDLK_LSHIFT)){ - int closest=-1; - float closestdist=-1; - float distance; - if(numplayers>1) - for(int i=1;i=0){ player[closest].whichskin++; if(player[closest].whichskin>9) player[closest].whichskin=0; @@ -2736,18 +2720,8 @@ void doDebugKeys(){ } if(Input::isKeyPressed(SDLK_o)&&Input::isKeyDown(SDLK_LSHIFT)){ - int closest=-1; - float closestdist=-1; - float distance; - if(numplayers>1) - for(int i=1;i=0){ if(player[closest].creature==wolftype){ headprop=player[closest].proportionhead.x/1.1; bodyprop=player[closest].proportionbody.x/1.1; @@ -2822,23 +2796,22 @@ void doDebugKeys(){ if(((Input::isKeyPressed(SDLK_i)&&!Input::isKeyDown(SDLK_LSHIFT)))){ - int closest=-1; - float closestdist=-1; - float distance; + int closest = -1; + float closestdist = std::numeric_limits::max(); + + for(int i=1; i1) - for(int i=1;i1) - for(int i=1;i=0 && findDistancefast(&player[closest].coords,&player[0].coords)<144){ + blah=player[closest].coords; emit_sound_at(splattersound, blah); - emit_sound_at(breaksound2, blah); for(int i=0;i1) - for(int i=1;i0&&closest>=0){ + int closest=findClosestPlayer(); + if(closest>=0){ //player[closest]=player[numplayers-1]; //player[closest].skeleton=player[numplayers-1].skeleton; numplayers--; @@ -3050,20 +3000,9 @@ void doDebugKeys(){ } if(Input::isKeyPressed(SDLK_DELETE)&&Input::isKeyDown(SDLK_LCTRL)){ - int closest=-1; - float closestdist=-1; - float distance; - if(max_objects>1) - for(int i=1;i0&&closest>=0){ + int closest=findClosestObject(); + if(closest>=0) objects.position[closest].y-=500; - } } if(Input::isKeyPressed(SDLK_m)&&Input::isKeyDown(SDLK_LSHIFT)){ @@ -3354,17 +3293,9 @@ void doDebugKeys(){ if(editorpitch<-.01)editorpitch=-.01; } if(Input::isKeyPressed(SDLK_DELETE)&&objects.numobjects&&Input::isKeyDown(SDLK_LSHIFT)){ - int closest=-1; - float closestdist=-1; - float distance; - for(int i=0;i0&&closest>=0)objects.DeleteObject(closest); + int closest=findClosestObject(); + if(closest>=0) + objects.DeleteObject(closest); } } }