RectangleMobility.cc

00001 //
00002 // Copyright (C) 2005 Andras Varga
00003 //
00004 // This program is free software; you can redistribute it and/or
00005 // modify it under the terms of the GNU General Public License
00006 // as published by the Free Software Foundation; either version 2
00007 // of the License, or (at your option) any later version.
00008 //
00009 // This program is distributed in the hope that it will be useful,
00010 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012 // GNU General Public License for more details.
00013 //
00014 // You should have received a copy of the GNU General Public License
00015 // along with this program; if not, write to the Free Software
00016 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00017 //
00018 
00019 #include "RectangleMobility.h"
00020 
00021 #include <FWMath.h>
00022 
00023 
00024 Define_Module(RectangleMobility);
00025 
00026 
00032 void RectangleMobility::initialize(int stage)
00033 {
00034     BaseMobility::initialize(stage);
00035 
00036     debugEV << "initializing RectangleMobility stage " << stage << endl;
00037 
00038     if (stage == 0)
00039     {
00040         x1 = par("x1");
00041         y1 = par("y1");
00042         x2 = par("x2");
00043         y2 = par("y2");
00044 
00045         move.setSpeed(par("speed"));
00046 
00047         // calculate helper vars
00048         double dx = x2-x1;
00049         double dy = y2-y1;
00050         corner1 = dx;
00051         corner2 = corner1 + dy;
00052         corner3 = corner2 + dx;
00053         corner4 = corner3 + dy;
00054 
00055         // determine start position
00056         double startPos = par("startPos");
00057         startPos = fmod(startPos,4);
00058         if (startPos<1)
00059             d = startPos*dx; // top side
00060         else if (startPos<2)
00061             d = corner1 + (startPos-1)*dy; // right side
00062         else if (startPos<3)
00063             d = corner2 + (startPos-2)*dx; // bottom side
00064         else
00065             d = corner3 + (startPos-3)*dy; // left side
00066         calculateXY();
00067 
00068         move.setStart(targetPos);
00069 
00070         WATCH(d);
00071     }
00072     else
00073     {
00074       if(!world->use2D()) {
00075       opp_warning("This mobility module does not yet support 3 dimensional movement."\
00076             "Movements will probably be incorrect.");
00077     }
00078     }
00079 }
00080 
00081 
00082 void RectangleMobility::makeMove()
00083 {
00084     d += move.getSpeed() * updateInterval.dbl();
00085     while (d<0) d+=corner4;
00086     while (d>=corner4) d-=corner4;
00087 
00088     calculateXY();
00089 
00090     fixIfHostGetsOutside();
00091 }
00092 
00093 void RectangleMobility::fixIfHostGetsOutside()
00094 {
00095     Coord dummy(world->use2D());
00096     double dum;
00097 
00098     handleIfOutside( RAISEERROR, targetPos, dummy, dummy, dum );
00099 }
00100 
00101 void RectangleMobility::calculateXY()
00102 {
00103     // update the position
00104     move.setStart(targetPos, simTime());
00105 
00106     // calcultae new target position
00107     if (d < corner1)
00108     {
00109         // top side
00110         targetPos.setX(x1 + d);
00111         targetPos.setY(y1);
00112     }
00113     else if (d < corner2)
00114     {
00115         // right side
00116         targetPos.setX(x2);
00117         targetPos.setY(y1 + d - corner1);
00118     }
00119     else if (d < corner3)
00120     {
00121         // bottom side
00122         targetPos.setX(x2 - d + corner2);
00123         targetPos.setY(y2);
00124     }
00125     else
00126     {
00127         // left side
00128         targetPos.setX(x1);
00129         targetPos.setY(y2 - d + corner3);
00130     }
00131 
00132     move.setDirectionByTarget(targetPos);
00133 }