
Originally Posted by
flameswor10
Code:
int RandomNum(int lowest, int highest)
{
int random_integer;
int range=(highest-lowest)+1;
random_integer = lowest+int(range*rand()/(RAND_MAX + 1.0));
return random_integer;
}
How to use
Code:
Lucky = RandomNum(1,100000);
Credits:
Me for function
I found similar post by Raygoe in 2007, honestly you didnt create the function.
[Highlight=C++]#include "input.h"
#include "OgreStringConverter.h"
#include "simulation.h"
#include "Ogre.h"
#include <string>
#include <sstream>
#include <iostream>
#include <ctime>
#include <cstdlib>
#include "OgreWindowEventUtilities.h"
int randomNum (int lowest, int highest) {
int random_integer;
int range = (highest-lowest)+1;
random_integer = lowest+int(range*rand()/(RAND_MAX + 1.0));
return random_integer;
}
InputHandler::InputHandler(Simulation *sim, unsigned long hWnd, Ogre::Root* ogre, Ogre::RenderWindow* window, Ogre::SceneManager* sceneMgr, Ogre::Camera* camera, Ogre::SceneNode* buildnode, Ogre::SceneNode* cameraNode) {
root_ogre = ogre;
rWindow = window;
sMan = sceneMgr;
cam = camera;
bNode = buildnode;
robotI = 0;
OIS::ParamList pl;
pl.insert(OIS::ParamList::value_type("WINDOW", Ogre::StringConverter::toString(hWnd)));
//SHOW MOUSE
pl.insert(std::make_pair(std::string("w32_mouse"), std::string("DISCL_FOREGROUND")));
pl.insert( std::make_pair( std::string( "w32_mouse" ), std::string("DISCL_NONEXCLUSIVE") ) );
m_hWnd = hWnd;
m_ois = OIS::InputManager::createInputSystem( pl );
mMouse = static_cast<OIS::Mouse*>(m_ois->createInputObject( OIS::OISMouse, true ));
mKeyboard = static_cast<OIS::Keyboard*>(m_ois->createInputObject( OIS::OISKeyboard, true));
mMouse->setEventCallback(this);
mKeyboard->setEventCallback(this);
mRotate = 0.13;
mMove = 0;
mContinue = true;
m_simulation = sim;
mDirection = cam->getPosition();
camNode = cameraNode;
srand((unsigned)time(0));
targetNode = sMan->getSceneNode("playerRob");
}
InputHandler::~InputHandler() {
if (mMouse)
delete mMouse;
if (mKeyboard)
delete mKeyboard;
OIS::InputManager::destroyInputSystem(m_ois);
}
void InputHandler::capture() {
mMouse->capture();
mKeyboard->capture();
}
bool InputHandler::movePlayerCam(float ft) {
frameTime = (ft);
if (mKeyboard->isKeyDown(OIS::KC_UP) || mKeyboard->isKeyDown(OIS::KC_W) || mKeyboard->isKeyDown(OIS::KC_DOWN) || mKeyboard->isKeyDown(OIS::KC_S)) {
mDirection = (0,0,mMove);
didMove = true;
}
if (mKeyboard->isKeyDown(OIS::KC_LEFT) || mKeyboard->isKeyDown(OIS::KC_A)) {
targetNode->yaw(Ogre:

egree(2*frameTime),Ogre::Node::TS_WORLD);
camNode->yaw(Ogre:

egree(2*frameTime),Ogre::Node::TS_WORLD);
didMove = true;
}
else if (mKeyboard->isKeyDown(OIS::KC_RIGHT) || mKeyboard->isKeyDown(OIS::KC_D)) {
targetNode->yaw(Ogre:

egree(-2*frameTime),Ogre::Node::TS_WORLD);
camNode->yaw(Ogre:

egree(-2*frameTime),Ogre::Node::TS_WORLD);
didMove = true;
}
if (didMove != true) {
return false;
} else {
didMove = false;
}
mMoveTo = (targetNode->getOrientation() * ((mDirection * frameTime) - targetNode->getPosition()));
//mDirection.x = 0;
//mDirection.y = 0;
//mDirection.z = 0;
targetNode->translate(mMoveTo);
cam->lookAt(targetNode->getPosition());
//cam->setPosition();
std::cout << mMoveTo << "\n" << targetNode->getPosition() << "\n";
return true;
}
void InputHandler::setWindowExtents(int width, int height){
//Set Mouse Region.. if window resizes, we should alter this to reflect as well
const OIS::MouseState &ms = mMouse->getMouseState();
ms.width = width;
ms.height = height;
}
/*bool InputHandler::frameStarted(const Ogre::FrameEvent &evt) {
camNode->translate(mDirection * evt.timeSinceLastFrame, Ogre::Node::TS_LOCAL);
return true;
}*/
// MouseListener
bool InputHandler::mouseMoved(const OIS::MouseEvent &evt) {
if (evt.state.buttonDown(OIS::MB_Right)) {
camNode->pitch(Ogre:

egree(-mRotate * evt.state.Y.rel), Ogre::Node::TS_LOCAL);
camNode->yaw(Ogre:

egree(-mRotate * evt.state.X.rel), Ogre::Node::TS_WORLD);
cam->lookAt(targetNode->getPosition());
}
return true;
}
bool InputHandler::mousePressed(const OIS::MouseEvent &evt, OIS::MouseButtonID btn) {
return true;
}
bool InputHandler::mouseReleased(const OIS::MouseEvent &evt, OIS::MouseButtonID btn) {
return true;
}
// KeyListener
bool InputHandler::keyPressed(const OIS::KeyEvent &evt) {
if (evt.key == OIS::KC_UP || evt.key == OIS::KC_W) {
mMove = 100;
}
if (evt.key == OIS::KC_DOWN || evt.key == OIS::KC_S) {
mMove = -50;
}
return true;
}
bool InputHandler::keyReleased(const OIS::KeyEvent &evt) {
if (evt.key == OIS::KC_ESCAPE)
m_simulation->requestStateChange(SHUTDOWN);
if (evt.key == OIS::KC_F5) {
robotI++;
std::stringstream stream;
stream << "robot" << robotI;
std::string robotID = stream.str();
Ogre::Entity* ent1 = sMan->createEntity( robotID, "robot.mesh" );
Ogre::SceneNode* secondary = bNode->createChildSceneNode( robotID, Ogre::Vector3( randomNum(0,20), randomNum(0,20), (randomNum(0,20) - 500) ) );
std:: cout << robotID << "\n";
secondary->attachObject( ent1 );
}
if (evt.key == OIS::KC_F6) {
camNode->setOrientation(Ogre::Quaternion(-1,0,-.5,0));
}
if (evt.key == OIS::KC_UP || evt.key == OIS::KC_W) {
mMove = 0;
}
if (evt.key == OIS::KC_DOWN || evt.key == OIS::KC_S) {
mMove = 0;
}
return true;
}
// JoyStickListener
bool InputHandler::buttonPressed(const OIS::JoyStickEvent &evt, int index) {
return true;
}
bool InputHandler::buttonReleased(const OIS::JoyStickEvent &evt, int index) {
return true;
}
bool InputHandler::axisMoved(const OIS::JoyStickEvent &evt, int index) {
return true;
}
bool InputHandler:

ovMoved(const OIS::JoyStickEvent &evt, int index) {
return true;
}[/Highlight]