Planeshift

workmanager.h

Go to the documentation of this file.
00001 /*
00002 * workmanager.h
00003 *
00004 * Copyright (C) 2001-2002 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 
00020 #ifndef __WORKMANAGER_H__
00021 #define __WORKMANAGER_H__
00022 
00023 //=============================================================================
00024 // Crystal Space Includes
00025 //=============================================================================
00026 #include <csutil/sysfunc.h>
00027 
00028 //=============================================================================
00029 // Project Includes
00030 //=============================================================================
00031 
00032 //=============================================================================
00033 // Local Includes
00034 //=============================================================================
00035 #include "msgmanager.h"             // Parent class
00036 #include "gem.h"
00037 
00038 class psWorkGameEvent;
00039 class WorkManager;
00040 class psItem;
00041 class Client;
00042 struct CombinationConstruction;
00043 
00044 // Define the work event types
00045 #define MANUFACTURE 0                   ///< Digging, collecting, farming
00046 #define PRODUCTION  1                   ///< Crafting
00047 #define LOCKPICKING 2                   ///< Picking of locks
00048 #define CLEANUP     3                   ///< Cleaning up of public containers
00049 #define REPAIR      4                   ///< Repairing items takes time too
00050 
00051 #define GARBAGE_PATTERNID   1           ///< Define for hard coded pattern IDs
00052 #define CLEANUP_DELAY       600         ///< Seconds to wait before performing cleanup event
00053 //#define CLEANUP_DELAY       10 ///< for testing only
00054 
00060 enum TradePatternMatch
00061 {
00062     TRANSFORM_MATCH                 = 0x0000,       
00063     TRANSFORM_UNKNOWN_PATTERN       = 0x0001,       
00064     TRANSFORM_UNKNOWN_ITEM          = 0x0002,       
00065     TRANSFORM_MISSING_ITEM          = 0x0004,       
00066     TRANSFORM_UNKNOWN_WORKITEM      = 0x0008,       
00067     TRANSFORM_MISSING_EQUIPMENT     = 0x0010,       
00068     TRANSFORM_BAD_TRAINING          = 0x0020,       
00069     TRANSFORM_BAD_SKILLS            = 0x0040,       
00070     TRANSFORM_OVER_SKILLED          = 0x0080,       
00071     TRANSFORM_FAILED_CONSTRAINTS    = 0x0100,       
00072     TRANSFORM_BAD_QUANTITY          = 0x0200,       
00073     TRANSFORM_BAD_COMBINATION       = 0x0400,       
00074     TRANSFORM_TOO_MANY_ITEMS        = 0x0800,       
00075     TRANSFORM_BAD_USE               = 0x1000,       
00076     TRANSFORM_NO_STAMINA            = 0x2000,       
00077     TRANSFORM_GONE_WRONG            = 0x4000,       
00078     TRANSFORM_GARBAGE               = 0x8000        
00079 };
00080 
00084 enum TradeTransfomType
00085 {
00086     TRANSFORMTYPE_UNKNOWN=0,        
00087     TRANSFORMTYPE_AUTO_CONTAINER,   
00088     TRANSFORMTYPE_SLOT,             
00089     TRANSFORMTYPE_CONTAINER,        
00090     TRANSFORMTYPE_SLOT_CONTAINER,   
00091     TRANSFORMTYPE_TARGETSLOT,       
00092     TRANSFORMTYPE_TARGET,           
00093     TRANSFORMTYPE_TARGET_TO_NPC,    
00094     TRANSFORMTYPE_SELF_CONTAINER    
00095 };
00096 
00097 //-----------------------------------------------------------------------------
00098 
00102 struct NaturalResource
00103 {
00104     int          sector;                
00105     csVector3    loc;                   
00106     float        radius;                
00107     float        visible_radius;        
00108     float        probability;           
00109     psSkillInfo* skill;                 
00110     int          skill_level;           
00111     unsigned int item_cat_id;           
00112     float        item_quality;          
00113     csString     anim;                  
00114     int          anim_duration_seconds; 
00115     int          reward;                
00116     csString     reward_nickname;       
00117     size_t       action;                
00118 
00119 };
00120 
00121 
00125 class NearNaturalResource
00126 {
00127 public:
00128     NearNaturalResource(NaturalResource* res, float distance) : resource(res), dist(distance) {}
00129     NaturalResource* resource;
00130     float dist;
00131     bool operator<(const NearNaturalResource &oth) const
00132     {
00133         if(oth.dist > dist)
00134             return true;
00135         if(oth.resource->probability > resource->probability)
00136             return true;
00137         if(oth.resource->skill_level > resource->skill_level)
00138             return true;
00139         return false;
00140     }
00141 };
00142 
00143 //-----------------------------------------------------------------------------
00144 
00145 struct constraint
00146 {
00147     bool (*constraintFunction)(WorkManager* that, char* param);
00148     const char* name;
00149     const char* message;
00150 };
00151 
00152 //-----------------------------------------------------------------------------
00153 
00159 class WorkManager : public MessageManager<WorkManager>
00160 {
00161 public:
00162 
00163     WorkManager(CacheManager* cachemanager, EntityManager* entitymanager);
00164     virtual ~WorkManager();
00165 
00166 //-----------------------------------------------------------------------------
00167 // Entry points
00168 //-----------------------------------------------------------------------------
00169 
00177     void HandleUse(Client* client);
00178 
00185     void HandleCombine(Client* client);
00186 
00194     void HandleConstruct(Client* client);
00195 
00208     void StartAutoWork(Client* client, gemContainer* container, psItem* autoItem, int count);
00209 
00218     bool StartScriptWork(Client* client, gemObject* target, csString pattern);
00219 
00228     void StopWork(Client* client, psItem* item);
00229 
00240     void HandleWorkEvent(psWorkGameEvent* workEvent);
00241 
00249     void HandleCleanupEvent(psWorkGameEvent* workEvent);
00250 
00258     void HandleProductionEvent(psWorkGameEvent* workEvent);
00259 
00273     void HandleRepairEvent(psWorkGameEvent* workEvent);
00274     void LockpickComplete(psWorkGameEvent* workEvent);
00275 
00281     static bool constraintTime(WorkManager* that, char* param);
00282     static bool constraintFriends(WorkManager* that,char* param);
00283     static bool constraintLocation(WorkManager* that,char* param);
00284     static bool constraintMode(WorkManager* that,char* param);
00285     static bool constraintGender(WorkManager* that,char* param);
00286     static bool constraintRace(WorkManager* that,char* param);
00290 
00291     void StartLockpick(Client* client,psItem* item);
00292 
00293 
00304     bool LoadLocalVars(Client* client, gemObject* target=NULL);
00305 
00306     /*
00307      * Send clear client view message to remove items from autocontainers.
00308      *
00309      * @param slotID The slot number to clear.
00310      * @param containerID The container ID that has item that needs to be cleared.
00311      *
00312      * @return False if there is a problem sending message.
00313      *
00314     bool SendClearUpdate( unsigned int slotID, unsigned int containerID ); */
00315 
00316 
00318     void HandleProduction(gemActor* actor,const char* type,const char* reward);
00319 
00320 protected:
00321     csPDelArray<NaturalResource> resources;       
00322 
00326     csStringArray resourcesActions;
00327     MathScript* calc_repair_rank;                 
00328     MathScript* calc_repair_time;                 
00329     MathScript* calc_repair_result;               
00330     MathScript* calc_repair_quality;              
00331     MathScript* calc_repair_exp;                  
00332     MathScript* calc_lockpicking_exp;             
00333     MathScript* calc_mining_chance;               
00334     MathScript* calc_mining_exp;                  
00335     MathScript* calc_transform_exp;               
00336     MathScript* calc_transform_practice;          
00337     MathScript* calc_lockpick_time;               
00338     MathScript* calc_transform_apply_skill;       
00339     MathScript* calc_transform_time;              
00340     MathScript* calc_combine_quality;             
00341 
00342 
00343     void HandleLockPick(MsgEntry* me,Client* client);
00344     void HandleWorkCommand(MsgEntry* me,Client* client);
00345 
00355     void StopAutoWork(Client* client, psItem* autoItem);
00356 
00364     void StopUseWork(MsgEntry* me,Client* client);
00365 
00371     void StartUseWork(Client* client);
00372 
00379     void StopCombineWork(Client* client);
00380 
00386     void StartCombineWork(Client* client);
00387 
00393     void StartConstructWork(Client* client);
00394 
00401     void StopConstructWork(Client* client);
00402 
00410     void StopCleanupWork(Client* client, psItem* cleanItem);
00411 
00422     void SendTransformError(uint32_t clientNum, unsigned int result, uint32 curItemId = 0, int CurItemQty = 0);
00423 
00436     bool IsContainerCombinable(uint32 &resultId, int &resultQty);
00437 
00450     bool IsHandCombinable(uint32 &resultId, int &resultQty);
00451 
00462     bool ValidateCombination(csArray<psItem*> itemArray, uint32 &resultId, int &resultQty);
00463 
00474     bool AnyCombination(csArray<psItem*> itemArray, uint32 &resultId, int &resultQty);
00475 
00485     bool MatchCombinations(csArray<psItem*> itemArray, CombinationConstruction* current, int &multiplier);
00486 
00497     unsigned int AnyTransform(csArray<psTradePatterns*> &patterns, float &KFactor, uint32 targetId, int targetQty);
00498 
00508     unsigned int IsTransformable(uint32 patternId, uint32 targetId, int targetQty);
00509 
00510     bool ScriptNoTarget();
00511     bool ScriptActor(gemActor* gemAct);
00512     bool ScriptItem(gemItem* gemItm);
00513     bool ScriptAction(gemActionLocation* gemAction);
00514 
00515     bool CombineWork();
00516     bool IsIngredient(uint32 patternId, uint32 targetId);
00517 
00526     psItem* TransformSelfContainerItem(psItem* oldItem, uint32 newId, int newQty, float itemQuality, psTradeProcesses* process, psTradeTransformations* trans);
00527 
00536     psItem* TransformContainedItem(psItem* oldItem, uint32 newId, int newQty, float itemQuality, psTradeProcesses* process, psTradeTransformations* trans);
00537 
00545     psItem* CombineContainedItem(uint32 newId, int newQty, float itemQuality, psItem* containerItem);
00546 
00555     psItem* TransformSlotItem(INVENTORY_SLOT_NUMBER slot, uint32 newId, int newQty, float itemQuality, psTradeProcesses* process, psTradeTransformations* trans);
00556 
00565     psItem* TransformTargetSlotItem(INVENTORY_SLOT_NUMBER slot, uint32 newId, int newQty, float itemQuality, psTradeProcesses* process, psTradeTransformations* trans);
00566 
00575     psItem* TransformTargetItem(psItem* oldItem, uint32 newId, int newQty, float itemQuality, psTradeProcesses* process, psTradeTransformations* trans);
00576 
00577     void TransformTargetItemToNpc(psItem* workItem, Client* client);
00578 
00594     void ApplyProcessScript(psItem* oldItem, psItem* newItem, gemActor* worker, psTradeProcesses* process, psTradeTransformations* trans);
00595 //    bool TransformHandItem(uint32 newId, int newQty, float itemQuality);
00596     //bool SendItemUpdate( INVENTORY_SLOT_NUMBER slotID, psItem *newItem );
00597 
00598     void StartTransformationEvent(int transType, INVENTORY_SLOT_NUMBER transSlot, int resultQty,
00599                                   float resultQuality, psItem* autoItem);
00600     void StartCleanupEvent(int transType, Client* client, psItem* item, gemActor* worker);
00601 
00602     bool ValidateTarget(Client* client);
00603     bool ValidateWork();
00604     bool ValidateMind();
00605     bool ValidateStamina(Client* client);
00606     bool IsOnHand(uint32 equipId);
00607     psItem* CreateTradeItem(uint32 newId, int newQty, float itemQuality, bool transient = false);
00608     bool ValidateTraining(psTradeTransformations* transCandidate, psTradeProcesses* processCandidate);
00609     bool ValidateSkills(psTradeTransformations* transCandidate, psTradeProcesses* processCandidate);
00610     bool ValidateNotOverSkilled(psTradeTransformations* transCandidate, psTradeProcesses* processCandidate);
00611     bool ValidateConstraints(psTradeTransformations* transCandidate, psTradeProcesses* processCandidate);
00612 
00622     int CalculateEventDuration(psTradeTransformations* trans, psTradeProcesses* process, psItem* transItem, gemActor* worker);
00623 
00624     bool CheckStamina(psCharacter* owner) const;
00625 
00626     void Initialize();
00627 
00631     bool CalculateQuality(float factor, psItem* transItem, gemActor* worker, bool amountModifier, float &currentQuality, psTradeProcesses* process, psTradeTransformations* trans, csTicks time);
00632 
00647     void HandleRepair(gemActor* actor, Client* client, const csString &repairSlotName);
00648 
00667     void HandleProduction(gemActor* actor, size_t type,const char* reward, Client* client = NULL);
00668 
00669     bool SameProductionPosition(gemActor* actor, const csVector3 &startPos);
00670 
00679     csArray<NearNaturalResource> FindNearestResource(iSector* sector, csVector3 &pos, const size_t action, const char* reward = NULL);
00680 
00681 private:
00682 
00683     csWeakRef<gemActor> worker;     
00684 
00685     uint32_t clientNum;             
00686     psItem* workItem;               
00687     psItem* autoItem;               
00688     gemActor* owner;                
00689     gemObject* gemTarget;           
00690     csArray<psTradePatterns*> patterns; 
00691     uint32 groupPatternId;          
00692     float patternKFactor;           
00693     float currentQuality;           
00694     psTradeTransformations* trans;  
00695     psTradeProcesses* process;      
00696     const char* preworkModeString;  
00697     bool secure;                    
00698 
00699     CacheManager* cacheManager;
00700     EntityManager* entityManager;
00701 };
00702 
00703 
00704 
00705 //-----------------------------------------------------------------------------
00706 
00708 class psWorkGameEvent : public psGameEvent, public iDeleteObjectCallback
00709 {
00710 public:
00711     psWorkGameEvent(WorkManager* mgr,
00712                     gemActor* worker,
00713                     int delayticks,
00714                     int cat,
00715                     csVector3 &pos,
00716                     csArray<NearNaturalResource>* natres=NULL,
00717                     Client* c=NULL,
00718                     psItem* object=NULL,
00719                     float repairAmount=0.0F);
00720     virtual ~psWorkGameEvent();
00721 
00722     void Interrupt();
00723 
00724     virtual void Trigger();  
00725 
00726     virtual void DeleteObjectCallback(iDeleteNotificationObject* object);
00727 
00729     void SetTransformation(psTradeTransformations* t)
00730     {
00731         transformation = t;
00732     }
00733 
00735     psTradeTransformations* GetTransformation()
00736     {
00737         return transformation;
00738     }
00739 
00741     void SetProcess(psTradeProcesses* p)
00742     {
00743         process = p;
00744     }
00745 
00747     psTradeProcesses* GetProcess()
00748     {
00749         return process;
00750     }
00751 
00753     int GetResultQuantity()
00754     {
00755         return resultQuantity;
00756     }
00757     void SetResultQuantity(int newQuantity)
00758     {
00759         resultQuantity = newQuantity;
00760     }
00761 
00763     float GetResultQuality()
00764     {
00765         return resultQuality;
00766     }
00767     void SetResultQuality(float newQuality)
00768     {
00769         resultQuality = newQuality;
00770     }
00771 
00773     float GetKFactor()
00774     {
00775         return KFactor;
00776     }
00777     void SetKFactor(float newFactor)
00778     {
00779         KFactor = newFactor;
00780     }
00781 
00783     INVENTORY_SLOT_NUMBER GetTransformationSlot()
00784     {
00785         return transSlot;
00786     }
00787     void SetTransformationSlot(INVENTORY_SLOT_NUMBER curSlot)
00788     {
00789         transSlot = curSlot;
00790     }
00791 
00792     psItem* GetTranformationItem()
00793     {
00794         return item;
00795     }
00796     void SetTransformationItem(psItem* i)
00797     {
00798         item = i;
00799     }
00800 
00801     psItem* GetWorkItem()
00802     {
00803         return workItem;
00804     }
00805     void SetWorkItem(psItem* w)
00806     {
00807         workItem = w;
00808     }
00809 
00810     gemObject* GetTargetGem()
00811     {
00812         return gemTarget;
00813     }
00814     void SetTargetGem(gemObject* g)
00815     {
00816         gemTarget = g;
00817     }
00818 
00820     int GetTransformationType()
00821     {
00822         return transType;
00823     }
00824     void SetTransformationType(int t)
00825     {
00826         transType = t;
00827     }
00828 
00829     WorkManager* workmanager;
00830     csWeakRef<gemActor> worker;
00831     csArray<NearNaturalResource> nrr;
00832     Client* client;
00833     gemObject* gemTarget;
00834     int category;
00835     csVector3 position;
00836     psItem*    object;
00837     psTradeTransformations* transformation;
00838     psTradeProcesses* process;
00839     float repairAmount;
00840 
00841     uint32_t effectID;      
00842     csArray<PublishDestination> multi;
00843 
00844 private:
00845     int resultQuantity;
00846     float resultQuality;
00847     float KFactor;
00848     INVENTORY_SLOT_NUMBER transSlot;
00849     psItem* item;
00850     psItem* workItem;
00851     int transType;
00852 };
00853 
00854 #endif