Planeshift

npcclient.h

Go to the documentation of this file.
00001 /*
00002  * npcclient.h
00003  *
00004  * Copyright (C) 2003 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 __NPCCLIENT_H__
00020 #define __NPCCLIENT_H__
00021 //=============================================================================
00022 // Crystal Space Includes
00023 //=============================================================================
00024 #include <csutil/csstring.h>
00025 #include <csutil/hash.h>
00026 #include <csutil/ref.h>
00027 #include <csutil/list.h>
00028 #include <iutil/vfs.h>
00029 
00030 struct iObjectRegistry;
00031 struct iDocumentNode;
00032 struct iConfigManager;
00033 struct iCollideSystem;
00034 
00035 //=============================================================================
00036 // Library Includes
00037 //=============================================================================
00038 #include "net/pstypes.h"
00039 #include "net/npcmessages.h"
00040 
00041 #include "util/psconst.h"
00042 #include "util/serverconsole.h"
00043 #include "util/pspath.h"
00044 #include "util/pspathnetwork.h"
00045 #include "util/mathscript.h"
00046 
00047 #include "tools/celhpf.h"
00048 
00053 class  psDatabase;
00054 class  MsgHandler;
00055 class  psNetConnection;
00056 class  EventManager;
00057 class  RecipeManager;
00058 class  NetworkManager;
00059 class  NPCType;
00060 class  NPC;
00061 class  gemNPCActor;
00062 class  gemNPCObject;
00063 class  gemNPCItem;
00064 struct iVFS;
00065 class  Perception;
00066 class  Reaction;
00067 class  psWorld;
00068 class  Location;
00069 class  LocationType;
00070 class  LocationManager;
00071 class  Waypoint;
00072 //class  psPFMaps;
00073 class  Tribe;
00074 class  psPath;
00075 class  psPathNetwork;
00076 struct iCelHNavStruct;
00077 
00081 class psNPCClient : public iCommandCatcher, public iScriptableVar
00082 {
00083 public:
00084 
00087     struct DeferredNPC
00088     {
00089         PID      id;
00090         csString name;
00091     };
00092 
00093 
00099     psNPCClient();
00100 
00104     ~psNPCClient();
00105 
00118     bool Initialize(iObjectRegistry* object_reg, const char* host, const char* user, const char* pass, int port);
00119 
00123     iObjectRegistry* GetObjectReg()
00124     {
00125         return objreg;
00126     }
00127 
00131     Tribe* GetTribe(int id);
00132 
00137     void MainLoop();
00138 
00144     virtual void CatchCommand(const char* cmd);
00145 
00149     void LoadLogSettings();
00150 
00154     void SaveLogSettings();
00155 
00159     void LoadCompleted();
00160 
00164     bool IsReady();
00165 
00169     void Tick();
00170 
00178     bool LoadMap(const char* mapfile);
00179 
00184     LocationType* FindRegion(const char* regname);
00185 
00190     LocationType* FindLocation(const char* locname);
00191 
00196     Location* FindLocation(const char* loctype, const char* name);
00197 
00202     Location* FindNearestLocation(const char* loctype, csVector3 &pos, iSector* sector, float range = -1, float* found_range = NULL);
00203 
00208     Location* FindRandomLocation(const char* loctype, csVector3 &pos, iSector* sector, float range = -1, float* found_range = NULL);
00209 
00214     Waypoint* FindNearestWaypoint(const csVector3 &v, iSector* sector, float range = -1, float*  found_range = NULL);
00215 
00220     Waypoint* FindRandomWaypoint(const csVector3 &v, iSector* sector, float range = -1, float*  found_range = NULL);
00221 
00226     Waypoint* FindNearestWaypoint(const char* group, csVector3 &v, iSector* sector, float range = -1, float*  found_range = NULL);
00227 
00232     Waypoint* FindRandomWaypoint(const char* group, csVector3 &v, iSector* sector, float range = -1, float*  found_range = NULL);
00233 
00238     Waypoint* FindWaypoint(gemNPCObject* entity);
00239 
00242     Waypoint* FindNearestWaypoint(gemNPCObject* entity, float range, float* found_range);
00243 
00244 
00249     Waypoint* FindWaypoint(int id);
00250 
00255     Waypoint* FindWaypoint(const char* name, WaypointAlias** alias);
00256 
00260     csList<Waypoint*> FindWaypointRoute(Waypoint*  start, Waypoint*  end, const psPathNetwork::RouteFilter* filter);
00261 
00265     csList<Edge*> FindEdgeRoute(Waypoint*  start, Waypoint*  end, const psPathNetwork::RouteFilter* filter);
00266 
00271     NPCType* FindNPCType(const char* npctype_name);
00272 
00276     void AddRaceInfo(const psNPCRaceListMessage::NPCRaceInfo_t &raceInfo);
00277 
00281     psNPCRaceListMessage::NPCRaceInfo_t* GetRaceInfo(const char* name);
00282 
00286     bool DumpRace(const char* pattern);
00287 
00291     float GetWalkVelocity(csString &race);
00292 
00296     float GetRunVelocity(csString &race);
00297 
00301     NetworkManager* GetNetworkMgr()
00302     {
00303         return network;
00304     }
00305 
00309     psNetConnection* GetNetConnection()
00310     {
00311         return connection;
00312     }
00313 
00317     void RegisterReaction(NPC* npc, Reaction* reaction);
00318 
00333     void TriggerEvent(Perception* pcpt, float maxRange=-1.0,
00334                       csVector3* basePos=NULL, iSector* baseSector=NULL,
00335                       bool sameSector=false);
00336 
00342     EventManager* GetEventManager()
00343     {
00344         return eventmanager;
00345     }
00346 
00352     MathScriptEngine*  GetMathScriptEngine()
00353     {
00354         return mathScriptEngine;
00355     }
00356 
00360     void Disconnect();
00361 
00372     void SetEntityPos(EID id, csVector3 &pos, iSector* sector, InstanceID instance, bool force = false);
00373 
00380     NPC* FindNPCByPID(PID character_id);
00381 
00388     NPC* FindNPC(EID entid);
00389 
00396     psPath* FindPath(const char* name);
00397 
00406     void EnableDisableNPCs(const char* pattern, bool enable);
00407 
00411     void ListAllNPCs(const char* pattern);
00412 
00416     size_t GetNpcListAmount()
00417     {
00418         return npcs.GetSize();
00419     }
00420 
00424     unsigned int GetTickCounter() const
00425     {
00426         return tick_counter;
00427     }
00428 
00432     bool DumpNPC(const char* pattern);
00433 
00437     bool InfoNPC(const char* pattern);
00438 
00442     void ListAllEntities(const char* pattern, bool onlyCharacters = false);
00443 
00447     void ListTribes(const char* pattern);
00448 
00452     void ListTribeRecipes(const char* tribeID);
00453 
00457     void ListReactions(const char* pattern);
00458 
00462     void ListWaypoints(const char* pattern);
00463 
00467     void ListPaths(const char* pattern);
00468 
00472     void ListLocations(const char* pattern);
00473 
00479     void HandleDeath(NPC* who);
00480 
00481     void Add(gemNPCObject* object);
00482     void Remove(gemNPCObject* object);
00483     void RemoveAll();
00484 
00487     void Remove(NPC* npc);
00488 
00489     gemNPCObject* FindCharacterID(PID pid);
00490     gemNPCObject* FindEntityID(EID eid);
00491 
00497     gemNPCObject* FindEntityByName(const char* name);
00498 
00504     bool CheckAttachTribes(NPC* npc);
00505 
00506     void AttachNPC(gemNPCActor* actor, uint8_t DRcounter, EID ownerEID, PID masterID);
00507 
00508     iCollideSystem* GetCollDetSys()
00509     {
00510         return cdsys;
00511     }
00512 
00513     NPC* ReadSingleNPC(PID char_id, PID master_id = 0);
00514 
00531     NPC* ReadMasteredNPC(PID char_id, PID master_id);
00532 
00536     void UpdateTime(int minute, int hour, int day, int month, int year);
00537 
00538     iCelHNavStruct*  GetNavStruct()
00539     {
00540         return navStruct;
00541     }
00542 
00543     iCelHPath* ShortestPath(NPC* npc, const csVector3 &from, iSector* fromSector, const csVector3 &goal, iSector* goalSector);
00544 
00545     psWorld*  GetWorld()
00546     {
00547         return world;
00548     }
00549 
00550     iEngine*  GetEngine()
00551     {
00552         return engine;
00553     }
00554 
00555     iVFS*   GetVFS()
00556     {
00557         return vfs;
00558     }
00559 
00563 
00564 
00568     int GetGameTODMinute()
00569     {
00570         return gameMinute;
00571     }
00572 
00576     int GetGameTODHour()
00577     {
00578         return gameHour;
00579     }
00580 
00584     int GetGameTODDay()
00585     {
00586         return gameDay;
00587     }
00588 
00592     int GetGameTODMonth()
00593     {
00594         return gameMonth;
00595     }
00596 
00600     int GetGameTODYear()
00601     {
00602         return gameYear;
00603     }
00605 
00614     void AttachObject(iObject* object, gemNPCObject* gobject);
00615 
00624     void UnattachObject(iObject* object, gemNPCObject* gobject);
00625 
00633     gemNPCObject* FindAttachedObject(iObject* object);
00634 
00635 
00646     csArray<gemNPCObject*> FindNearbyEntities(iSector* sector, const csVector3 &pos, float radius, bool doInvisible = false);
00647 
00658     csArray<gemNPCActor*> FindNearbyActors(iSector* sector, const csVector3 &pos, float radius, bool doInvisible = false);
00659 
00665     csPtr<iDocumentNode> GetRootNode(const char* xmlfile);
00666 
00676     bool AddNPCType(csString newType);
00677 
00683     bool LoadNPCTypes(iDocumentNode* root);
00684 
00688     bool LoadNPCTypes();
00689 
00700     bool FirePerception(int NPCId, const char* perception);
00701 
00705     bool LoadPathNetwork();
00706 
00710     psPathNetwork* GetPathNetwork()
00711     {
00712         return pathNetwork;
00713     }
00714 
00718     LocationManager* GetLocationManager()
00719     {
00720         return locationManager;
00721     }
00722 
00726 
00727     virtual double GetProperty(MathEnvironment* env, const char* ptr);
00728     virtual double CalcFunction(MathEnvironment* env, const char* functionName, const double* params);
00729     virtual const char* ToString();
00731 
00732 protected:
00736     bool ReadNPCsFromDatabase();
00737 
00741     bool LoadLocations();
00742 
00746     bool LoadTribes();
00747 
00752     void PerceptProximityItems();
00753 
00758     void PerceptProximityLocations();
00759 
00763     void PerceptProximityTribeHome();
00764 
00765 public:
00766     static psNPCClient*             npcclient;
00767 protected:
00768     psNetConnection*                connection;
00769     iObjectRegistry*                objreg;
00770     csRef<iEngine>                  engine;
00771     csRef<iConfigManager >          configmanager;
00772     csRef<MsgHandler>               msghandler;
00773     ServerConsole*                  serverconsole;
00774     EventManager*                   eventmanager;
00775     RecipeManager*                  recipemanager;
00776     NetworkManager*                 network;
00777     psDatabase*                     database;
00778     csRef<iVFS>                     vfs;
00779 
00780     csHash<NPCType*, const char*>   npctypes;
00781     LocationManager*                locationManager;         
00782     MathScriptEngine*               mathScriptEngine;
00783     psPathNetwork*                  pathNetwork;
00784     csRef<iCelHNavStruct>           navStruct;
00785     csPDelArray<NPC>                npcs;
00786     csArray<DeferredNPC>            npcsDeferred;
00787     csPDelArray<Tribe>              tribes;
00788     csHash<gemNPCObject*, EID>      all_gem_objects_by_eid;
00789     csHash<gemNPCObject*, PID>      all_gem_objects_by_pid;
00790     csPDelArray<gemNPCObject>       all_gem_objects;
00791     csArray<gemNPCItem*>            all_gem_items;
00792     csArray<gemNPCActor*>           all_gem_actors;
00793 
00794     csHash<psNPCRaceListMessage::NPCRaceInfo_t,csString>     raceInfos; 
00795 
00796     csHash<NPC*,csString>           allReactions;     
00797     csArray<csString>               notUsedReactions; 
00798 
00799     csRef<iCollideSystem>           cdsys;
00800 
00801     psWorld*                        world;
00802 
00803     //    psPFMaps*                       PFMaps;
00804 
00806     unsigned int                    tick_counter;
00807 
00809     int                             current_long_range_perception_index;
00810     int                             current_long_range_perception_loc_index;
00811     int                             current_tribe_home_perception_index;
00812 
00813     // Game Time
00814     int                             gameMinute;
00815     int                             gameHour;
00816     int                             gameDay;
00817     int                             gameMonth;
00818     int                             gameYear;
00819     csTicks                         gameTimeUpdated;
00820 
00821 };
00822 
00825 #endif
00826