Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/AI/src/orxonox.cc @ 2092

Last change on this file since 2092 was 495, checked in by motth, 17 years ago

fixed a memory leak, added documentation

File size: 14.4 KB
Line 
1/*
2 *   ORXONOX - the hottest 3D action shooter ever to exist
3 *
4 *
5 *   License notice:
6 *
7 *   This program is free software: you can redistribute it and/or modify
8 *   it under the terms of the GNU General Public License as published by
9 *   the Free Software Foundation, either version 3 of the License, or
10 *   (at your option) any later version.
11 *
12 *   This program is distributed in the hope that it will be useful,
13 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
14 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 *   GNU General Public License for more details.
16 *
17 *   You should have received a copy of the GNU General Public License
18 *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
19 *
20 *
21 *   Author:
22 *      Benjamin Knecht <beni_at_orxonox.net>, (C) 2007
23 *   Co-authors:
24 *      ...
25 *
26 */
27
28/**
29 @file  orxonox.cc
30 @brief Orxonox Main File
31 */
32
33#include <Ogre.h>
34#include <OIS/OIS.h>
35#include <CEGUI/CEGUI.h>
36#include <OgreCEGUIRenderer.h>
37#include <OgreMath.h>
38
39#include <string>
40#include <iostream>
41
42#include "SignalHandler.h"
43#include "xml/xmlParser.h"
44#include "loader/LevelLoader.h"
45#include "Flocking.h"
46#include "Wander.h"
47
48// some tests to see if enet works without includsion
49//#include <enet/enet.h>
50//#include <enet/protocol.h>
51
52#if OGRE_PLATFORM == OGRE_PLATFORM_APPLE
53#include <CoreFoundation/CoreFoundation.h>
54
55// This function will locate the path to our application on OS X,
56// unlike windows you can not rely on the curent working directory
57// for locating your configuration files and resources.
58std::string macBundlePath()
59{
60  char path[1024];
61  CFBundleRef mainBundle = CFBundleGetMainBundle();
62  assert(mainBundle);
63
64  CFURLRef mainBundleURL = CFBundleCopyBundleURL(mainBundle);
65  assert(mainBundleURL);
66
67  CFStringRef cfStringRef = CFURLCopyFileSystemPath( mainBundleURL, kCFURLPOSIXPathStyle);
68  assert(cfStringRef);
69
70  CFStringGetCString(cfStringRef, path, 1024, kCFStringEncodingASCII);
71
72  CFRelease(mainBundleURL);
73  CFRelease(cfStringRef);
74
75  return std::string(path);
76}
77#endif
78
79using namespace Ogre;
80
81//my-stuff
82//globale definition eines Arrays welches alle nodes enthält
83Vector3 ElementLocationArray[9];
84Vector3 ElementSpeedArray[9];
85Vector3 ElementAccelerationArray[9];
86
87Element arrayOfElements[9];
88Wander walker;
89int counter = 0;
90int times = 0;
91
92
93
94class OrxExitListener : public FrameListener
95{
96  public:
97    OrxExitListener(OIS::Keyboard *keyboard, Root* root)
98  : mKeyboard(keyboard)
99    {
100        root_ = root;
101    }
102
103    bool frameStarted(const FrameEvent& evt)
104    {
105      moving(evt);
106      mKeyboard->capture();
107      return !mKeyboard->isKeyDown(OIS::KC_ESCAPE);
108    }
109 
110    //code the movments of the nodes here
111    void moving(const FrameEvent& evt) {
112      SceneManager *mgr = root_->getSceneManager("Default SceneManager");
113
114/*    // RUN WANDER
115
116      walker.update();
117      walker.speed = walker.speed + 10*walker.acceleration*evt.timeSinceLastFrame;
118      walker.location = walker.location + 10*walker.speed*evt.timeSinceLastFrame;
119      walker.acceleration  = (0,0,0);
120      mgr->getSceneNode("HeadNode10")->setPosition(walker.location);
121
122*/   // END RUN WANDER
123
124
125
126
127     //  RUN FLOCKING
128
129      arrayOfElements[8].location = 100*Vector3(Math::Cos(Math::DegreesToRadians(counter)/10),Math::Sin(Math::DegreesToRadians(counter)/10),Math::Cos(Math::DegreesToRadians(counter+(counter-180)/2)/10));
130
131      arrayOfElements[0].update(arrayOfElements);
132      arrayOfElements[1].update(arrayOfElements);
133      arrayOfElements[2].update(arrayOfElements);
134      arrayOfElements[3].update(arrayOfElements);
135      arrayOfElements[4].update(arrayOfElements);
136      arrayOfElements[5].update(arrayOfElements);
137      arrayOfElements[6].update(arrayOfElements);
138      arrayOfElements[7].update(arrayOfElements);
139      arrayOfElements[8].update(arrayOfElements);
140
141      for(int i=0; i<9; i++) {
142        arrayOfElements[i].speed = 0.995*arrayOfElements[i].speed + arrayOfElements[i].acceleration*evt.timeSinceLastFrame;
143        arrayOfElements[i].location = arrayOfElements[i].location + arrayOfElements[i].speed*evt.timeSinceLastFrame;
144        arrayOfElements[i].acceleration  = (0,0,0);
145      }
146
147      mgr->getSceneNode("HeadNode1")->setPosition(arrayOfElements[0].location);
148      mgr->getSceneNode("HeadNode2")->setPosition(arrayOfElements[1].location);
149      mgr->getSceneNode("HeadNode3")->setPosition(arrayOfElements[2].location);
150      mgr->getSceneNode("HeadNode4")->setPosition(arrayOfElements[3].location);
151      mgr->getSceneNode("HeadNode5")->setPosition(arrayOfElements[4].location);
152      mgr->getSceneNode("HeadNode6")->setPosition(arrayOfElements[5].location);
153      mgr->getSceneNode("HeadNode7")->setPosition(arrayOfElements[6].location);
154      mgr->getSceneNode("HeadNode8")->setPosition(arrayOfElements[7].location);
155      mgr->getSceneNode("HeadNode9")->setPosition(arrayOfElements[8].location);
156
157      counter = counter + 1;
158      counter = counter%7200;
159
160      // END RUN FLOCKING
161
162    }
163
164  private:
165    OIS::Keyboard *mKeyboard;
166    Root* root_;
167};
168
169class OrxApplication
170{
171  public:
172    void go()
173    {
174      createRoot();
175      defineResources();
176      setupRenderSystem();
177      createRenderWindow();
178      initializeResourceGroups();
179      createScene();
180      setupScene();
181      setupInputSystem();
182      setupCEGUI();
183      createFrameListener();
184      startRenderLoop();
185    }
186
187    ~OrxApplication()
188    {
189      mInputManager->destroyInputObject(mKeyboard);
190      OIS::InputManager::destroyInputSystem(mInputManager);
191
192//       delete mRenderer;
193//       delete mSystem;
194
195      delete mListener;
196      delete mRoot;
197    }
198
199  private:
200    Root *mRoot;
201    OIS::Keyboard *mKeyboard;
202    OIS::Mouse *mMouse;
203    OIS::InputManager *mInputManager;
204    CEGUI::OgreCEGUIRenderer *mRenderer;
205    CEGUI::System *mSystem;
206    OrxExitListener *mListener;
207
208    void createRoot()
209    {
210#if OGRE_PLATFORM == OGRE_PLATFORM_APPLE
211      mRoot = new Root(macBundlePath() + "/Contents/Resources/plugins.cfg");
212#else
213      mRoot = new Root();
214#endif
215    }
216
217    void defineResources()
218    {
219      String secName, typeName, archName;
220      ConfigFile cf;
221#if OGRE_PLATFORM == OGRE_PLATFORM_APPLE
222      cf.load(macBundlePath() + "/Contents/Resources/resources.cfg");
223#else
224      cf.load("resources.cfg");
225#endif
226
227      ConfigFile::SectionIterator seci = cf.getSectionIterator();
228      while (seci.hasMoreElements())
229      {
230        secName = seci.peekNextKey();
231        ConfigFile::SettingsMultiMap *settings = seci.getNext();
232        ConfigFile::SettingsMultiMap::iterator i;
233        for (i = settings->begin(); i != settings->end(); ++i)
234        {
235          typeName = i->first;
236          archName = i->second;
237#if OGRE_PLATFORM == OGRE_PLATFORM_APPLE
238          ResourceGroupManager::getSingleton().addResourceLocation( String(macBundlePath() + "/" + archName), typeName, secName);
239#else
240          ResourceGroupManager::getSingleton().addResourceLocation( archName, typeName, secName);
241#endif
242        }
243      }
244    }
245
246    void setupRenderSystem()
247    {
248      if (!mRoot->restoreConfig() && !mRoot->showConfigDialog())
249        throw Exception(52, "User canceled the config dialog!", "OrxApplication::setupRenderSystem()");
250    }
251
252    void createRenderWindow()
253    {
254      mRoot->initialise(true, "Ogre Render Window");
255    }
256
257    void initializeResourceGroups()
258    {
259      TextureManager::getSingleton().setDefaultNumMipmaps(5);
260      ResourceGroupManager::getSingleton().initialiseAllResourceGroups();
261    }
262
263    void createScene(void)
264    {
265
266      string levelFile = "sp_level_moonstation.oxw";
267      loader::LevelLoader* loader = new loader::LevelLoader(levelFile);
268    }
269   
270    void setupScene()
271    {
272      SceneManager *mgr = mRoot->createSceneManager(ST_GENERIC, "Default SceneManager");
273      Camera *cam = mgr->createCamera("Camera");
274      cam->setPosition(Vector3(0,0,1000));
275      cam->lookAt(Vector3(0,0,0));
276      Viewport *vp = mRoot->getAutoCreatedWindow()->addViewport(cam);
277
278      //Invoke example to test AI
279      example();
280    }
281
282    void setupInputSystem()
283    {
284      size_t windowHnd = 0;
285      std::ostringstream windowHndStr;
286      OIS::ParamList pl;
287      RenderWindow *win = mRoot->getAutoCreatedWindow();
288
289      win->getCustomAttribute("WINDOW", &windowHnd);
290      windowHndStr << windowHnd;
291      pl.insert(std::make_pair(std::string("WINDOW"), windowHndStr.str()));
292      mInputManager = OIS::InputManager::createInputSystem(pl);
293
294      try
295      {
296        mKeyboard = static_cast<OIS::Keyboard*>(mInputManager->createInputObject(OIS::OISKeyboard, false));
297        mMouse = static_cast<OIS::Mouse*>(mInputManager->createInputObject(OIS::OISMouse, false));
298      }
299      catch (const OIS::Exception &e)
300      {
301        throw new Exception(42, e.eText, "OrxApplication::setupInputSystem");
302      }
303    }
304
305    void setupCEGUI()
306    {
307      SceneManager *mgr = mRoot->getSceneManager("Default SceneManager");
308      RenderWindow *win = mRoot->getAutoCreatedWindow();
309
310      // CEGUI setup
311//       mRenderer = new CEGUI::OgreCEGUIRenderer(win, Ogre::RENDER_QUEUE_OVERLAY, false, 3000, mgr);
312//       mSystem = new CEGUI::System(mRenderer);
313
314      // Other CEGUI setup here.
315    }
316
317    void createFrameListener()
318    {
319      mListener = new OrxExitListener(mKeyboard, mRoot);
320      mRoot->addFrameListener(mListener);
321    }
322
323    void startRenderLoop()
324    {
325      mRoot->startRendering();
326    }
327
328    void example() {
329    SceneManager *mgr = mRoot->getSceneManager("Default SceneManager");
330    mgr->setAmbientLight(ColourValue(1.0,1.0,1.0));
331
332/*  //TEST DATA WANDER
333
334    Entity* ent10 = mgr->createEntity("Head10", "ogrehead.mesh");
335    SceneNode *node10 = mgr->getRootSceneNode()->createChildSceneNode("HeadNode10", Vector3(0,0,0));
336    node10->attachObject(ent10);
337    Vector3 temp;
338    temp = (0,0,0);
339    walker.setValues(node10->getPosition(),temp,temp,true);
340
341*/  //END TEST DATA WANDER
342
343
344//   TEST DATA FLOCKING
345
346    Entity* ent1 = mgr->createEntity("Head1", "ogrehead.mesh");
347    Entity* ent2 = mgr->createEntity("Head2", "ogrehead.mesh");
348    Entity* ent3 = mgr->createEntity("Head3", "ogrehead.mesh");
349    Entity* ent4 = mgr->createEntity("Head4", "ogrehead.mesh");
350    Entity* ent5 = mgr->createEntity("Head5", "ogrehead.mesh");
351    Entity* ent6 = mgr->createEntity("Head6", "ogrehead.mesh");
352    Entity* ent7 = mgr->createEntity("Head7", "ogrehead.mesh");
353    Entity* ent8 = mgr->createEntity("Head8", "ogrehead.mesh");
354    Entity* ent9 = mgr->createEntity("Head9", "ogrehead.mesh");
355
356    SceneNode *node1 = mgr->getRootSceneNode()->createChildSceneNode("HeadNode1", Vector3(100,300,100));
357    SceneNode *node2 = mgr->getRootSceneNode()->createChildSceneNode("HeadNode2", Vector3(300,0,200));
358    SceneNode *node3 = mgr->getRootSceneNode()->createChildSceneNode("HeadNode3", Vector3(-300,0,-100));
359    SceneNode *node4 = mgr->getRootSceneNode()->createChildSceneNode("HeadNode4", Vector3(-100,-300,150));
360    SceneNode *node5 = mgr->getRootSceneNode()->createChildSceneNode("HeadNode5", Vector3(150,150,-100));
361    SceneNode *node6 = mgr->getRootSceneNode()->createChildSceneNode("HeadNode6", Vector3(150,-150,-100));
362    SceneNode *node7 = mgr->getRootSceneNode()->createChildSceneNode("HeadNode7", Vector3(-150,-150,0));
363    SceneNode *node8 = mgr->getRootSceneNode()->createChildSceneNode("HeadNode8", Vector3(-150,150,0));
364    SceneNode *node9 = mgr->getRootSceneNode()->createChildSceneNode("HeadNode9", Vector3(0,0,0));
365
366    node1->attachObject(ent1);
367    node2->attachObject(ent2);
368    node3->attachObject(ent3);
369    node4->attachObject(ent4);
370    node5->attachObject(ent5);
371    node6->attachObject(ent6);
372    node7->attachObject(ent7);
373    node8->attachObject(ent8);
374    node9->attachObject(ent9);
375
376    ElementLocationArray[0] = node1->getPosition();
377    ElementLocationArray[1] = node2->getPosition();
378    ElementLocationArray[2] = node3->getPosition();
379    ElementLocationArray[3] = node4->getPosition();
380    ElementLocationArray[4] = node5->getPosition();
381    ElementLocationArray[5] = node6->getPosition();
382    ElementLocationArray[6] = node7->getPosition();
383    ElementLocationArray[7] = node8->getPosition();
384    ElementLocationArray[8] = node9->getPosition();
385
386    ElementSpeedArray[0] = (0,0,0);
387    ElementSpeedArray[1] = (0,0,0);
388    ElementSpeedArray[2] = (0,0,0);
389    ElementSpeedArray[3] = (0,0,0);
390    ElementSpeedArray[4] = (0,0,0);
391    ElementSpeedArray[5] = (0,0,0);
392    ElementSpeedArray[6] = (0,0,0);
393    ElementSpeedArray[7] = (0,0,0);
394    ElementSpeedArray[8] = (0,0,0);
395
396    ElementAccelerationArray[0] = (0,0,0);
397    ElementAccelerationArray[1] = (0,0,0);
398    ElementAccelerationArray[2] = (0,0,0);
399    ElementAccelerationArray[3] = (0,0,0);
400    ElementAccelerationArray[4] = (0,0,0);
401    ElementAccelerationArray[5] = (0,0,0);
402    ElementAccelerationArray[6] = (0,0,0);
403    ElementAccelerationArray[7] = (0,0,0);
404    ElementAccelerationArray[8] = (0,0,0);
405
406    arrayOfElements[0].setValues( ElementLocationArray[0], ElementSpeedArray[0], ElementAccelerationArray[0], true);
407    arrayOfElements[1].setValues( ElementLocationArray[1], ElementSpeedArray[1], ElementAccelerationArray[1], true);
408    arrayOfElements[2].setValues( ElementLocationArray[2], ElementSpeedArray[2], ElementAccelerationArray[2], true);
409    arrayOfElements[3].setValues( ElementLocationArray[3], ElementSpeedArray[3], ElementAccelerationArray[3], true);
410    arrayOfElements[4].setValues( ElementLocationArray[4], ElementSpeedArray[4], ElementAccelerationArray[4], true);
411    arrayOfElements[5].setValues( ElementLocationArray[5], ElementSpeedArray[5], ElementAccelerationArray[5], true);
412    arrayOfElements[6].setValues( ElementLocationArray[6], ElementSpeedArray[6], ElementAccelerationArray[6], true);
413    arrayOfElements[7].setValues( ElementLocationArray[7], ElementSpeedArray[7], ElementAccelerationArray[7], true);
414    arrayOfElements[8].setValues( ElementLocationArray[8], ElementSpeedArray[8], ElementAccelerationArray[8], false);
415
416   // END TEST DATA FLOCKING
417
418
419    }
420};
421
422
423
424#if OGRE_PLATFORM == PLATFORM_WIN32 || OGRE_PLATFORM == OGRE_PLATFORM_WIN32
425#define WIN32_LEAN_AND_MEAN
426#include "windows.h"
427
428             INT WINAPI WinMain(HINSTANCE hInst, HINSTANCE, LPSTR strCmdLine, INT)
429#else
430             int main(int argc, char **argv)
431#endif
432{
433  try
434  {
435    SignalHandler::getInstance()->doCatch(argv[0], "orxonox.log");
436    OrxApplication orxonox;
437    orxonox.go();
438  }
439  catch(Exception& e)
440  {
441#if OGRE_PLATFORM == PLATFORM_WIN32 || OGRE_PLATFORM == OGRE_PLATFORM_WIN32
442    MessageBoxA(NULL, e.getFullDescription().c_str(), "An exception has occurred!", MB_OK | MB_ICONERROR | MB_TASKMODAL);
443#else
444    fprintf(stderr, "An exception has occurred: %s\n",
445            e.getFullDescription().c_str());
446#endif
447  }
448
449  return 0;
450}
451
Note: See TracBrowser for help on using the repository browser.