00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "MassMobility.h"
00023
00024 #include <FWMath.h>
00025
00026
00027 Define_Module(MassMobility);
00028
00029
00036 void MassMobility::initialize(int stage)
00037 {
00038 BaseMobility::initialize(stage);
00039
00040 debugEV << "initializing MassMobility stage " << stage << endl;
00041
00042 if (stage == 0)
00043 {
00044 changeInterval = &par("changeInterval");
00045 changeAngleBy = &par("changeAngleBy");
00046
00047
00048 move.setSpeed(par("speed"));
00049 currentAngle = uniform(0, 360);
00050
00051 step.setX(move.getSpeed() * cos(PI * currentAngle / 180) * updateInterval.dbl());
00052 step.setY(move.getSpeed() * sin(PI * currentAngle / 180) * updateInterval.dbl());
00053
00054 }
00055 else if( stage == 1 )
00056 {
00057 if(!world->use2D()) {
00058 opp_warning("This mobility module does not yet support 3 dimensional movement."\
00059 "Movements will probably be incorrect.");
00060 }
00061
00062
00063 move.setDirectionByTarget(move.getStartPos() + step);
00064 move.setStart(move.getStartPos(), simTime());
00065
00066 targetPos = move.getStartPos();
00067
00068 scheduleAt(simTime() + uniform(0, changeInterval->doubleValue()), new cMessage("turn", MK_CHANGE_DIR));
00069 }
00070 }
00071
00072
00076 void MassMobility::handleSelfMsg(cMessage * msg)
00077 {
00078 Coord dummy(world->use2D());
00079
00080 switch (msg->getKind()){
00081 case MOVE_HOST:
00082 BaseMobility::handleSelfMsg( msg );
00083 break;
00084 case MK_CHANGE_DIR:
00085 currentAngle += changeAngleBy->doubleValue();
00086
00087 step.setX(move.getSpeed() * cos(PI * currentAngle / 180) * updateInterval.dbl());
00088 step.setY(move.getSpeed() * sin(PI * currentAngle / 180) * updateInterval.dbl());
00089
00090 move.setDirectionByTarget(move.getStartPos() + step);
00091
00092 scheduleAt(simTime() + changeInterval->doubleValue(), msg);
00093 break;
00094 default:
00095 opp_error("Unknown self message kind in MassMobility class");
00096 break;
00097 }
00098
00099 }
00100
00105 void MassMobility::makeMove()
00106 {
00107 move.setStart(targetPos, simTime());
00108 targetPos += step;
00109
00110
00111 fixIfHostGetsOutside();
00112 }
00113
00114 void MassMobility::fixIfHostGetsOutside()
00115 {
00116 Coord dummy(world->use2D());
00117
00118 handleIfOutside( REFLECT, targetPos, dummy, step, currentAngle );
00119 }