Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 465 was 465, checked in by landauf, 16 years ago

include

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