Planeshift

exchangemanager.h

Go to the documentation of this file.
00001 /*
00002  * exchangemanager.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 __EXCHANGEMANAGER_H__
00020 #define __EXCHANGEMANAGER_H__
00021 //=============================================================================
00022 // Crystal Space Includes
00023 //=============================================================================
00024 #include <csutil/ref.h>
00025 #include <csutil/refarr.h>
00026 #include <csutil/parray.h>
00027 
00028 //=============================================================================
00029 // Project Includes
00030 //=============================================================================
00031 #include "bulkobjects/pscharinventory.h"
00032 
00033 //=============================================================================
00034 // Local Includes
00035 //=============================================================================
00036 #include "msgmanager.h"             // Parent class
00037 
00038 class ClientConnectionSet;
00039 class psSpawnManager;
00040 class psServer;
00041 class Exchange;
00042 class ExchangeManager;
00043 class psNPCDialog;
00044 class psDatabase;
00045 
00054 class ExchangingCharacter
00055 {
00056 public:
00057     ExchangingCharacter(int client, psCharacterInventory &inv);
00058 
00066     bool OfferItem(int exchSlotNum, INVENTORY_SLOT_NUMBER invSlot, int stackcount, bool test);
00067 
00078     bool RemoveItemFromOffer(int itemNum, int count, int &remain);
00079 
00080     bool MoveOfferedItem(int fromSlot,int stackcount,int toSlot);
00081 
00085     bool IsOfferingSane();
00086 
00092     void TransferOffer(int targetClientNum);
00093 
00098     void TransferOffer(psCharacter* npc);
00099 
00106     void TransferMoney(int targetClientNum);
00107 
00111     void TransferMoney(psCharacter* target);
00112 
00113     psMoney GetOfferedMoney()
00114     {
00115         return offeringMoney;
00116     }
00117 
00124     void AdjustMoney(int coin, int delta);
00125     void AdjustMoney(const psMoney &amount);
00126 
00127     void GetOfferingCSV(csString &buff);
00128 
00140     void GetOffering(csString &buff);
00141 
00142     psItem* GetOfferedItem(int slot);
00143 
00156     void GetSimpleOffering(csString &buff, psCharacter* chr, bool exact = true);
00157 
00161     void UpdateReceivingMoney(psMoney &money);
00162 
00166     void UpdateOfferingMoney();
00167 
00168     psMoney offeringMoney;
00169 
00173     float GetSumSize();
00174 
00178     float GetSumWeight();
00179 
00180     bool GetExchangedItems(csString &text);
00181 
00182 protected:
00183 
00184     psCharacter* GetClientCharacter(int clientNum);
00185 
00189 //    psItem *offering[EXCHANGE_SLOT_COUNT];
00190     // csArray<psItem*> itemsOffered;
00191 
00192     int client;
00193     psCharacterInventory* chrinv;
00194 };
00195 
00196 //------------------------------------------------------------------------------
00197 
00204 class Exchange : public iDeleteObjectCallback
00205 {
00206 public:
00207     Exchange(Client* starter, bool automaticExchange, ExchangeManager* manager);
00208     virtual ~Exchange();
00209 
00217     virtual void HandleEnd(Client* client) = 0;
00218 
00227     virtual bool HandleAccept(Client* client) = 0;
00228     // virtual bool Involves(psCharacter * character) = 0;
00229     // virtual bool Involves(psItem * item) = 0;
00230 
00243     virtual bool AddItem(Client* fromClient, INVENTORY_SLOT_NUMBER fromSlot, int stackCount, int toSlot);
00244 
00257     virtual void MoveItem(Client* client, int fromSlot, int stackCount, int toSlot);
00258 
00271     virtual bool RemoveItem(Client* fromClient, int slot, int stackCount);
00272 
00273     virtual psMoney GetOfferedMoney(Client* client);
00274     virtual bool AdjustMoney(Client* client, int moneyType, int newMoney);
00275     virtual bool AdjustMoney(Client* client, const psMoney &money);
00276     int GetID() const
00277     {
00278         return id;
00279     }
00280     bool CheckRange(int clientNum, gemObject* ourActor, gemObject* otherActor);
00281 
00282     Client* GetStarterClient()
00283     {
00284         return starterClient;    // NILAYA: Should axe.
00285     }
00286 
00287     psItem* GetStarterOffer(int slot);
00288     virtual psItem* GetTargetOffer(int slot)
00289     {
00290         return NULL;
00291     }
00292 
00293 protected:
00294     virtual void SendAddItemMessage(Client* fromClient, int slot, psCharacterInventory::psCharacterInventoryItem* item);
00295     virtual void SendRemoveItemMessage(Client* fromClient, int slot);
00296 
00297     void SendEnd(int clientNum);
00298     static int next_id;
00299 
00301     int id;
00302 
00304     ExchangingCharacter starterChar;
00305     Client* starterClient;
00306     uint32_t player;
00307 
00308     bool exchangeEnded;    
00309     bool exchangeSuccess; 
00310 
00311     bool automaticExchange; 
00312 
00313     ExchangeManager* exchangeMgr;
00314 };
00315 
00316 //------------------------------------------------------------------------------
00317 
00318 class PlayerToPlayerExchange : public Exchange
00319 {
00320 public:
00321     PlayerToPlayerExchange(Client* client, Client* target, ExchangeManager* manager);
00322     virtual ~PlayerToPlayerExchange();
00323 
00324     bool CheckExchange(uint32_t clientNum, bool checkRange=false);
00325     bool HandleAccept(Client* client);
00326     void HandleEnd(Client* client);
00327     void SendExchangeStatusToBoth();
00328     psMoney GetOfferedMoney(Client* client);
00329     bool AdjustMoney(Client* client, int moneyType, int newMoney);
00330     bool AdjustMoney(Client* client, const psMoney &money);
00331 
00342     virtual bool AddItem(Client* fromClient, INVENTORY_SLOT_NUMBER fromSlot, int stackCount, int toSlot);
00343 
00356     virtual bool RemoveItem(Client* fromClient, int slot, int stackCount);
00357 
00358     virtual void DeleteObjectCallback(iDeleteNotificationObject* object);
00359 
00360     virtual psItem* GetTargetOffer(int slot);
00361 
00362 protected:
00363     virtual void SendAddItemMessage(Client* fromClient, int slot, psCharacterInventory::psCharacterInventoryItem* item);
00364     virtual void SendRemoveItemMessage(Client* fromClient, int slot);
00365 
00366 private:
00368     void SendRequestToBoth();
00369 
00370     Client* GetOtherClient(Client* client) const;
00371 
00372     PlayerToPlayerExchange();
00373 
00375     Client* targetClient;
00376     ExchangingCharacter targetChar;
00377     int target;
00378 
00379     bool starterAccepted, targetAccepted;
00380 };
00381 
00382 //------------------------------------------------------------------------------
00383 
00384 class PlayerToNPCExchange : public Exchange
00385 {
00386 public:
00387     PlayerToNPCExchange(Client* starter, gemObject* target, bool automaticExchange, int questID, ExchangeManager* manager);
00388     virtual ~PlayerToNPCExchange();
00389     gemObject* GetTargetGEM();
00390     bool CheckExchange(uint32_t clientNum, bool checkRange=false);
00391     virtual void HandleEnd(Client* client);
00392     virtual bool HandleAccept(Client* client);
00393     //virtual bool Involves(psCharacter * character);
00394     //virtual bool Involves(psItem * item);
00395     virtual void DeleteObjectCallback(iDeleteNotificationObject* object);
00396 
00397 protected:
00406     bool CheckXMLResponse(Client* client, psNPCDialog* dlg, csString trigger);
00407 
00408     gemObject* target;
00409     int questID;
00410 };
00411 
00412 /* Maintains a list of all the exchanges that are ongoing.
00413  */
00414 class ExchangeManager : public MessageManager<ExchangeManager>
00415 {
00416 public:
00417 
00418     ExchangeManager(ClientConnectionSet* pCCS);
00419     virtual ~ExchangeManager();
00420 
00421     static bool ExchangeCheck(Client* client, gemObject* target, csString* errorMessage = NULL);
00422 
00423     void StartExchange(Client* client, bool withPlayer, bool automaticExchange = false, int questID = -1);
00424 
00425     void HandleExchangeRequest(MsgEntry* me,Client* client);
00426     void HandleExchangeAccept(MsgEntry* me,Client* client);
00427     void HandleExchangeEnd(MsgEntry* me,Client* client);
00428     void HandleAutoGive(MsgEntry* me,Client* client);
00429 
00431     Exchange* GetExchange(int id);
00432 
00434     void AddExchange(Exchange* exchange)
00435     {
00436         exchanges.Push(exchange);
00437     }
00438 
00440     void DeleteExchange(Exchange* exchange);
00441 
00442 protected:
00443 
00444     ClientConnectionSet* clients;
00445 
00446     csPDelArray<Exchange> exchanges;
00447 };
00448 
00451 #endif