Planeshift

psengine.h

Go to the documentation of this file.
00001 /*
00002  * psengine.h
00003  *
00004  * Copyright (C) 2001 Atomic Blue ([email protected], http://www.atomicblue.org)
00005  *
00006  *
00007  * This program is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU General Public License
00009  * as published by the Free Software Foundation (version 2 of the License)
00010  * This program is distributed in the hope that it will be useful,
00011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013  * GNU General Public License for more details.
00014  * You should have received a copy of the GNU General Public License
00015  * along with this program; if not, write to the Free Software
00016  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00017  *
00018  */
00019 #ifndef __ENGINE_H__
00020 #define __ENGINE_H__
00021 
00022 #include <csutil/csstring.h>
00023 #include <csutil/strhashr.h>
00024 #include <csutil/randomgen.h>
00025 #include <csutil/refarr.h>
00026 #include <csutil/sysfunc.h>
00027 #include <csutil/leakguard.h>
00028 #include <csutil/eventhandlers.h>
00029 #include <csutil/weakrefarr.h>
00030 #include <csutil/weakreferenced.h>
00031 #include <ivaria/profile.h>
00032 #include <ivideo/graph3d.h>
00033 #include <iutil/eventh.h>
00034 
00035 #include "psnetmanager.h"
00036 #include "isoundmngr.h"
00037 
00038 #include "util/slots.h"
00039 
00040 class psCelClient;
00041 class ClientMsgHandler;
00042 class psClientCharManager;
00043 struct iBgLoader;
00044 struct iConfigManager;
00045 struct iDialogManager;
00046 struct iDocumentSystem;
00047 struct iEngine;
00048 struct iEvent;
00049 struct iEventQueue;
00050 struct iFont;
00051 struct iGraphics2D;
00052 struct iGraphics3D;
00053 struct iSceneManipulate;
00054 struct iTextureManager;
00055 struct iThreadReturn;
00056 struct iVFS;
00057 struct iVirtualClock;
00058 class ModeHandler;
00059 class ActionHandler;
00060 class ZoneHandler;
00061 class psCharController;
00062 class psCamera;
00063 class psEffectManager;
00064 class psChatBubbles;
00065 class psCal3DCallbackLoader;
00066 class psMainWidget;
00067 class psQuestionClient;
00068 class psOptions;
00069 class psCSSetup;
00070 class psMouseBinds;
00071 class psInventoryCache;
00072 class PawsManager;
00073 class Autoexec;
00074 
00075 // Networking classes
00076 class ClientMsgHandler;
00077 class psNetManager;
00078 class psSlotManager;
00079 class ClientSongManager;
00080 
00081 class GUIHandler;
00082 
00083 struct DelayedLoader : public CS::Utility::WeakReferenced
00084 {
00085     virtual bool CheckLoadStatus() = 0;
00086     virtual ~DelayedLoader() { };
00087 };
00088 
00094 class psEngine
00095 {
00096 public:
00097     enum LoadState
00098     {
00099         LS_NONE,
00100         LS_LOAD_SCREEN,
00101         LS_INIT_ENGINE,
00102         LS_REQUEST_WORLD,
00103         LS_SETTING_CHARACTERS,
00104         LS_CREATE_GUI,
00105         LS_DONE,
00106         LS_ERROR
00107     };
00108 
00110     psEngine(iObjectRegistry* object_reg, psCSSetup* CSSetup);
00111 
00113     virtual ~psEngine();
00114 
00135     bool Initialize(int level);
00136 
00142     void Cleanup();
00143 
00147     void ResetEngine();
00148 
00150     void UpdatePerFrame();
00151 
00153     void FinishFrame()
00154     {
00155         g3d->FinishDraw();
00156         g3d->Print(NULL);
00157     }
00158 
00164     bool ProcessLogic(iEvent &Event);
00165     bool Process2D(iEvent &Event);
00166     bool Process3D(iEvent &Event);
00167     bool ProcessFrame(iEvent &Event);
00168 
00170     void LoadGame();
00171 
00173     bool IsGameLoaded()
00174     {
00175         return gameLoaded;
00176     };
00177 
00178     // References to plugins
00179     iObjectRegistry*      GetObjectRegistry()
00180     {
00181         return object_reg;
00182     }
00183     iEventNameRegistry*   GetEventNameRegistry()
00184     {
00185         return nameRegistry;
00186     }
00187     iEngine*              GetEngine()
00188     {
00189         return engine;
00190     }
00191     iGraphics3D*          GetG3D()
00192     {
00193         return g3d;
00194     }
00195     iGraphics2D*          GetG2D()
00196     {
00197         return g2d;
00198     }
00199     iTextureManager*      GetTextureManager()
00200     {
00201         return txtmgr;
00202     }
00203     iVFS*                 GetVFS()
00204     {
00205         return vfs;
00206     }
00207     iVirtualClock*        GetVirtualClock()
00208     {
00209         return vc;
00210     }
00211     iDocumentSystem*      GetXMLParser()
00212     {
00213         return xmlparser;
00214     }
00215     iConfigManager*       GetConfig()
00216     {
00217         return cfgmgr;    
00218     }
00219     iBgLoader*            GetLoader()
00220     {
00221         return loader;
00222     }
00223     iSceneManipulate*     GetSceneManipulator()
00224     {
00225         return scenemanipulator;
00226     }
00227     iSoundManager*        GetSoundManager()
00228     {
00229         return SoundManager;
00230     }
00231 
00232     csRandomGen &GetRandomGen()
00233     {
00234         return random;
00235     }
00236     float GetRandom()
00237     {
00238         return random.Get();
00239     }
00240 
00241     ClientMsgHandler*    GetMsgHandler()
00242     {
00243         return netmanager->GetMsgHandler();
00244     }
00245     CmdHandler*            GetCmdHandler()
00246     {
00247         return netmanager->GetCmdHandler();
00248     }
00249     psSlotManager*         GetSlotManager()
00250     {
00251         return slotManager;
00252     }
00253     ClientSongManager*     GetSongManager()
00254     {
00255         return songManager;
00256     }
00257     psClientCharManager*   GetCharManager()
00258     {
00259         return charmanager;
00260     }
00261     psCelClient*           GetCelClient()
00262     {
00263         return celclient;
00264     };
00265     psMainWidget*          GetMainWidget()
00266     {
00267         return mainWidget;
00268     }
00269     psEffectManager*       GetEffectManager()
00270     {
00271         return effectManager;
00272     }
00273     psChatBubbles*         GetChatBubbles()
00274     {
00275         return chatBubbles;
00276     }
00277     psOptions*             GetOptions()
00278     {
00279         return options;
00280     }
00281     ModeHandler*           GetModeHandler()
00282     {
00283         return modehandler;
00284     }
00285     ActionHandler*         GetActionHandler()
00286     {
00287         return actionhandler;
00288     }
00289     psCharController*      GetCharControl()
00290     {
00291         return charController;
00292     }
00293     psMouseBinds*          GetMouseBinds();
00294     psCamera*              GetPSCamera()
00295     {
00296         return camera;
00297     }
00298     psNetManager*          GetNetManager()
00299     {
00300         return netmanager;
00301     }
00302     psCSSetup*             GetCSSetup()
00303     {
00304         return CS_Setup;
00305     }
00306     ZoneHandler*           GetZoneHandler()
00307     {
00308         return zonehandler;
00309     }
00310     Autoexec*              GetAutoexec()
00311     {
00312         return autoexec;
00313     }
00314 
00318     void SetTargetPetitioner(const char* pet)
00319     {
00320         targetPetitioner = pet;
00321     }
00322     
00323     const char* GetTargetPetitioner()
00324     {
00325         return targetPetitioner;
00326     }
00327 
00331     void QuitClient();
00332 
00336     void Disconnect();
00337 
00341     void StartLoad()
00342     {
00343         loadstate = LS_LOAD_SCREEN;
00344     }
00345     LoadState loadstate;
00346 
00347     size_t GetTime();
00348 
00352     float GetBrightnessCorrection()
00353     {
00354         return BrightnessCorrection;
00355     }
00356     
00360     void SetBrightnessCorrection(float B);
00361 
00365     void AdjustBrightnessCorrectionUp();
00366 
00370     void AdjustBrightnessCorrectionDown();
00371 
00375     void ResetBrightnessCorrection();
00376     
00377     void UpdateLights();
00378 
00379     float GetKFactor()
00380     {
00381         return KFactor;
00382     }
00383     void SetKFactor(float K)
00384     {
00385         KFactor = K;
00386     }
00387 
00388     iEvent* GetLastEvent()
00389     {
00390         return lastEvent;
00391     }
00392 
00393     // Functions to set and get frameLimit converted into a value of FPS for users
00394     void setLimitFPS(int);
00395     int getLimitFPS()
00396     {
00397         return maxFPS;
00398     }
00399     float getFPS()
00400     {
00401         return currFPS;
00402     }
00403 
00404     bool toggleFPS()
00405     {
00406         showFPS = !showFPS;
00407         return showFPS;
00408     }
00409 
00416     inline void SetDuelConfirm(int confirmType)
00417     {
00418         duelConfirmation = confirmType;
00419         WriteConfirmationSettings();
00420     }
00425     inline int GetDuelConfirm() const
00426     {
00427         return duelConfirmation;
00428     }
00434     inline void SetMarriageProposal(bool ignore)
00435     {
00436         marriageProposal = ignore;
00437         WriteConfirmationSettings();
00438     }
00443     inline bool GetMarriageProposal() const
00444     {
00445         return marriageProposal;
00446     }
00447     
00452     void WriteConfirmationSettings();
00453     
00459     bool LoadConfirmationSettings();
00460 
00467     bool LoadSoundSettings(bool forceDef);
00468 
00470     inline bool HasLoadedMap() const
00471     {
00472         return loadedMap;
00473     }
00474 
00476     void SetLoadedMap(bool value)
00477     {
00478         loadedMap = value;
00479     }
00480 
00482     bool LoadingError()
00483     {
00484         return loadError;
00485     }
00486 
00488     void LoadError(bool value)
00489     {
00490         loadError = value;
00491     }
00492 
00500     bool LoadPawsWidget(const char* title, const char* filename);
00501 
00507     bool LoadCustomPawsWidgets(const char* filename);
00508 
00509     SlotNameHash slotName;
00510 
00511     void AddLoadingWindowMsg(const csString &msg);
00512 
00518     void FatalError(const char* msg);
00519 
00521     bool IsLoggedIn()
00522     {
00523         return loggedIn;
00524     }
00525     void SetLoggedIn(bool v)
00526     {
00527         loggedIn = v;
00528     }
00529 
00535     void SetNumChars(int chars)
00536     {
00537         numOfChars = chars;
00538     }
00539 
00541     int GetNumChars()
00542     {
00543         return numOfChars;
00544     }
00545 
00547     const char* GetMainPlayerName();
00548 
00550     bool UpdateWindowTitleInformations();
00551 
00553     void ForceRefresh()
00554     {
00555         elapsed = 0;
00556     }
00557 
00559     void SetGuildName(const char* name)
00560     {
00561         guildname = name;
00562     }
00563 
00565     const char* GetGuildName()
00566     {
00567         return guildname;
00568     }
00569 
00573     bool GetMuteSoundsOnFocusLoss(void) const
00574     {
00575         return muteSoundsOnFocusLoss;
00576     }
00580     void SetMuteSoundsOnFocusLoss(bool value)
00581     {
00582         muteSoundsOnFocusLoss = value;
00583     }
00584 
00586     void MuteAllSounds(void);
00588     void UnmuteAllSounds(void);
00589 
00593     const char* FindCommonString(unsigned int cstr_id);
00594 
00600     csStringID FindCommonStringId(const char* str);
00601 
00603     csStringHashReversible* GetMsgStrings();
00604 
00606     bool GetSoundStatus()
00607     {
00608         return soundOn;
00609     }
00610 
00612     psInventoryCache* GetInventoryCache(void)
00613     {
00614         return inventoryCache;
00615     }
00616 
00618     bool BackgroundWorldLoading()
00619     {
00620         return backgroundWorldLoading;
00621     }
00622 
00623     void RegisterDelayedLoader(DelayedLoader* obj)
00624     {
00625         delayedLoaders.PushSmart(obj);
00626     }
00627     void UnregisterDelayedLoader(DelayedLoader* obj)
00628     {
00629         delayedLoaders.Delete(obj);
00630     }
00631 
00632     static csString hwRenderer;
00633     static csString hwVersion;
00634     static csString playerName;
00635 #ifdef WIN32
00636     static HWND hwnd;
00637 #endif
00638 
00639 private:
00641     void LoadLogSettings();
00642 
00644     bool QueryPlugins();
00645 
00647     void DeclareExtraFactories();
00648 
00649     void HideWindow(const csString &widgetName);
00650 
00652     void FrameLimit();
00653 
00654     /* plugins we're using... */
00655     iObjectRegistry*          object_reg;     
00656     csRef<iEventNameRegistry> nameRegistry;   
00657     csRef<iEngine>            engine;         
00658     csRef<iConfigManager>     cfgmgr;         
00659     csRef<iTextureManager>    txtmgr;         
00660     csRef<iVFS>               vfs;            
00661     csRef<iGraphics2D>        g2d;            
00662     csRef<iGraphics3D>        g3d;            
00663     csRef<iEventQueue>        queue;          
00664     csRef<iVirtualClock>      vc;             
00665     csRef<iDocumentSystem>    xmlparser;      
00666     csRef<iSoundManager>      SoundManager;   
00667     csRef<iStringSet>         stringset;
00668     csRandomGen               random;
00669 
00670     csRef<psNetManager>       netmanager;   
00671     csRef<psCelClient>        celclient;    
00672     csRef<ModeHandler>        modehandler;  
00673     csRef<ActionHandler>      actionhandler;
00674     csRef<ZoneHandler>        zonehandler;  
00675     csRef<psCal3DCallbackLoader> cal3DCallbackLoader;
00676     psClientCharManager*      charmanager;  
00677     GUIHandler*               guiHandler;
00678     psCharController*         charController;
00679     psMouseBinds*             mouseBinds;
00680     psCamera*                 camera;
00681     csRef<psEffectManager>    effectManager;
00682     psChatBubbles*            chatBubbles;
00683     psOptions*                options;
00684     psSlotManager*            slotManager;
00685     ClientSongManager*        songManager;
00686     psQuestionClient*         questionclient;
00687     PawsManager*              paws;          
00688     psMainWidget*             mainWidget;    
00689     psInventoryCache*         inventoryCache;
00690     psCSSetup*                CS_Setup;
00691     csRef<iBgLoader>          loader;
00692     csRef<iSceneManipulate>   scenemanipulator;
00693     Autoexec*                 autoexec;
00694 
00695     /* status, misc. vars */
00696     bool gameLoaded; 
00697     bool modalMenu;
00698     bool loggedIn;
00699 
00700     csTicks loadtimeout;
00701 
00702     csString lasterror;
00703 
00704     csString guildname;
00705 
00707     bool drawScreen;
00708 
00709     // For splash progress.
00710     csRefArray<iThreadReturn> precaches;
00711     csRef<iStringArray> meshes;
00712     csRef<iStringArray> maps;
00713     size_t lastLoadingCount;
00714 
00715     // True if we've requested the main actor.
00716     bool actorRequested;
00717 
00718 private:
00719 
00720     csString targetPetitioner;
00721 
00722     float BrightnessCorrection;
00723 
00724     float KFactor;           
00725 
00726     iEvent* lastEvent;
00727 
00734     int duelConfirmation;
00735     bool marriageProposal; 
00736 
00737     bool loadedMap;
00738     bool loadError; 
00739     csArray<csString> wdgProblems; 
00740 
00742     int numOfChars;
00743 
00745     csTicks elapsed;
00746 
00748     csTicks frameLimit;
00749 
00751     int maxFPS;
00752 
00753     float currFPS;
00754     unsigned int countFPS;
00755     unsigned int timeFPS;
00756 
00757     bool showFPS;
00758 
00759     csRef<iFont> font;
00760 
00762     bool muteSoundsOnFocusLoss;
00763 
00765     bool soundOn;
00766 
00767     bool backgroundWorldLoading;
00768 
00769     // Event ID cache
00770     csEventID event_frame;
00771     csEventID event_canvashidden;
00772     csEventID event_canvasexposed;
00773     csEventID event_focusgained;
00774     csEventID event_focuslost;
00775     csEventID event_mouse;
00776     csEventID event_keyboard;
00777     csEventID event_joystick;
00778     csEventID event_quit;
00779 
00780 #if defined(CS_PLATFORM_UNIX) && defined(INCLUDE_CLIPBOARD)
00781     csEventID event_selectionnotify;
00782 #endif
00783 
00788     class LogicEventHandler : public scfImplementation1<LogicEventHandler, iEventHandler>
00789     {
00790     public:
00791         LogicEventHandler(psEngine* parent) : scfImplementationType(this), parent(parent)
00792         {
00793         }
00794 
00795         virtual ~LogicEventHandler()
00796         {
00797         }
00798 
00799         virtual bool HandleEvent(iEvent &ev)
00800         {
00801             return parent->ProcessLogic(ev);
00802         }
00803 
00804         virtual const csHandlerID* GenericPrec(csRef<iEventHandlerRegistry> &,
00805                                                csRef<iEventNameRegistry> &, csEventID) const;
00806         virtual const csHandlerID* GenericSucc(csRef<iEventHandlerRegistry> &,
00807                                                csRef<iEventNameRegistry> &, csEventID) const;
00808 
00809         CS_EVENTHANDLER_NAMES("planeshift.client.frame.logic")
00810         CS_EVENTHANDLER_DEFAULT_INSTANCE_CONSTRAINTS
00811 
00812     private:
00813         psEngine* parent;
00814     };
00815 
00820     class EventHandler3D : public scfImplementation1<EventHandler3D, iEventHandler>
00821     {
00822     public:
00823         EventHandler3D(psEngine* parent) : scfImplementationType(this), parent(parent)
00824         {
00825         }
00826 
00827         virtual ~EventHandler3D()
00828         {
00829         }
00830 
00831         virtual bool HandleEvent(iEvent &ev)
00832         {
00833             return parent->Process3D(ev);
00834         }
00835 
00836         CS_EVENTHANDLER_PHASE_3D("planeshift.client.frame.3d");
00837 
00838     private:
00839         psEngine* parent;
00840     };
00841 
00846     class EventHandler2D : public scfImplementation1<EventHandler2D, iEventHandler>
00847     {
00848     public:
00849         EventHandler2D(psEngine* parent) : scfImplementationType(this), parent(parent)
00850         {
00851         }
00852 
00853         virtual ~EventHandler2D()
00854         {
00855         }
00856 
00857         virtual bool HandleEvent(iEvent &ev)
00858         {
00859             return parent->Process2D(ev);
00860         }
00861 
00862         CS_EVENTHANDLER_PHASE_2D("planeshift.client.frame.2d");
00863 
00864     private:
00865         psEngine* parent;
00866     };
00867 
00872     class FrameEventHandler : public scfImplementation1<FrameEventHandler, iEventHandler>
00873     {
00874     public:
00875         FrameEventHandler(psEngine* parent) : scfImplementationType(this), parent(parent)
00876         {
00877         }
00878 
00879         virtual ~FrameEventHandler()
00880         {
00881         }
00882 
00883         virtual bool HandleEvent(iEvent &ev)
00884         {
00885             return parent->ProcessFrame(ev);
00886         }
00887 
00888         CS_EVENTHANDLER_PHASE_FRAME("planeshift.client.frame");
00889 
00890     private:
00891         psEngine* parent;
00892     };
00893 
00894     csRef<LogicEventHandler> eventHandlerLogic;
00895     csRef<EventHandler3D> eventHandler3D;
00896     csRef<EventHandler2D> eventHandler2D;
00897     csRef<FrameEventHandler> eventHandlerFrame;
00898 
00899     csWeakRefArray<DelayedLoader> delayedLoaders;
00900 };
00901 
00902 #endif