Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 426 was 426, checked in by motth, 16 years ago

added actual flocking code and two frameworks

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