BaseWorldUtility.cc

00001 /* -*- mode:c++ -*- ********************************************************
00002  * file:        BaseWorldUtility.h
00003  *
00004  * author:      Tom Parker
00005  *
00006  * copyright:   (C) 2006 Parallel and Distributed Systems Group (PDS) at
00007  *              Technische Universiteit Delft, The Netherlands.
00008  *
00009  *              This program is free software; you can redistribute it
00010  *              and/or modify it under the terms of the GNU General Public
00011  *              License as published by the Free Software Foundation; either
00012  *              version 2 of the License, or (at your option) any later
00013  *              version.
00014  *              For further information see file COPYING
00015  *              in the top level directory
00016  ***************************************************************************
00017  * description: basic world utility class
00018  *              provides world-required values
00019  **************************************************************************/
00020 
00021 #include "BaseWorldUtility.h"
00022 #include "FindModule.h"
00023 #include "BaseConnectionManager.h"
00024 
00025 Define_Module(BaseWorldUtility);
00026 
00027 
00028 const double BaseWorldUtility::speedOfLight = 299792458.0; //metres per second
00029 
00030 BaseWorldUtility::BaseWorldUtility():
00031     isInitialized(false)
00032 {}
00033 
00034 void BaseWorldUtility::initialize(int stage) {
00035   Blackboard::initialize(stage);
00036 
00037   if (stage == 0) {
00038         initializeIfNecessary();
00039   }
00040   else if(stage == 1) {
00041     //check if necessary modules are there
00042     //Connection Manager
00043     if(!FindModule<BaseConnectionManager*>::findGlobalModule()) {
00044       opp_warning("Could not find a connection manager module in the network!");
00045     }
00046   }
00047 }
00048 
00049 void BaseWorldUtility::initializeIfNecessary()
00050 {
00051   if(isInitialized)
00052     return;
00053 
00054   use2DFlag = par("use2D");
00055 
00056   if (use2DFlag) {
00057     playgroundSize = Coord(par("playgroundSizeX"),
00058                  par("playgroundSizeY"));
00059   } else {
00060     playgroundSize = Coord(par("playgroundSizeX"),
00061                  par("playgroundSizeY"),
00062                  par("playgroundSizeZ"));
00063   }
00064 
00065   if(playgroundSize.getX() <= 0) {
00066     opp_error("Playground size in X direction is invalid: "\
00067           "(%f). Should be greater zero.", playgroundSize.getX());
00068   }
00069   if(playgroundSize.getY() <= 0) {
00070     opp_error("Playground size in Y direction is invalid: "\
00071           "(%f). Should be greater zero.", playgroundSize.getY());
00072   }
00073   if(!use2DFlag && playgroundSize.getZ() <= 0) {
00074     opp_error("Playground size in Z direction is invalid: "\
00075           "(%f). Should be greater zero (or use 2D mode).", playgroundSize.getZ());
00076   }
00077 
00078   useTorusFlag = par("useTorus");
00079 
00080   airFrameId = 0;
00081 
00082   isInitialized = true;
00083 }
00084 
00085 Coord BaseWorldUtility::getRandomPosition()
00086 {
00087   initializeIfNecessary();
00088 
00089     if (use2DFlag) {
00090         return Coord(uniform(0, playgroundSize.getX()),
00091                      uniform(0, playgroundSize.getY()));
00092     } else {
00093         return Coord(uniform(0, playgroundSize.getX()),
00094                      uniform(0, playgroundSize.getY()),
00095                      uniform(0, playgroundSize.getZ()));
00096     }
00097 }
00098