00001 /* 00002 Copyright (C) 2004-2007 The Pentagram team 00003 00004 This program is free software; you can redistribute it and/or 00005 modify it under the terms of the GNU General Public License 00006 as published by the Free Software Foundation; either version 2 00007 of the License, or (at your option) any later version. 00008 00009 This program is distributed in the hope that it will be useful, 00010 but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00012 GNU General Public License for more details. 00013 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 #include "pent_include.h" 00020 00021 #include "HealProcess.h" 00022 #include "MainActor.h" 00023 #include "Kernel.h" 00024 #include "getObject.h" 00025 00026 #include "IDataSource.h" 00027 #include "ODataSource.h" 00028 00029 // p_dynamic_cast stuff 00030 DEFINE_RUNTIME_CLASSTYPE_CODE(HealProcess,Process); 00031 00032 HealProcess::HealProcess() : Process() 00033 { 00034 hungerCounter = 0; 00035 healCounter = 0; 00036 item_num = 0; 00037 type = 0x222; // CONSTANT! 00038 } 00039 00040 bool HealProcess::run(const uint32 /*framenum*/) 00041 { 00042 MainActor *avatar = getMainActor(); 00043 00044 if (!avatar || avatar->isDead()) { 00045 // dead? 00046 terminate(); 00047 return false; 00048 } 00049 00050 // heal one hitpoint and one manapoint every minute (1800 frames) 00051 00052 healCounter++; 00053 00054 if (healCounter == 900) { 00055 sint16 mana = avatar->getMana(); 00056 if (mana < avatar->getMaxMana()) { 00057 mana++; 00058 avatar->setMana(mana); 00059 } 00060 } 00061 00062 if (healCounter == 1800) { 00063 uint16 hp = avatar->getHP(); 00064 if (hp < avatar->getMaxHP()) { 00065 hp++; 00066 avatar->setHP(hp); 00067 } 00068 healCounter = 0; 00069 00070 if (hungerCounter < 200) 00071 hungerCounter++; 00072 } 00073 00074 return false; 00075 } 00076 00077 void HealProcess::feedAvatar(uint16 food) 00078 { 00079 MainActor *avatar = getMainActor(); 00080 00081 if (!avatar || avatar->isDead()) { 00082 // dead? 00083 terminate(); 00084 return; 00085 } 00086 00087 if (food > hungerCounter) 00088 food = hungerCounter; 00089 00090 if (food == 0) return; 00091 00092 uint16 oldCounter = hungerCounter; 00093 hungerCounter -= food; 00094 00095 uint16 hp = avatar->getHP() - (hungerCounter / 4) + (oldCounter / 4); 00096 if (hp > avatar->getMaxHP()) hp = avatar->getMaxHP(); 00097 00098 avatar->setHP(hp); 00099 } 00100 00101 uint32 HealProcess::I_feedAvatar(const uint8* args, unsigned int /*argsize*/) 00102 { 00103 ARG_UINT16(food); 00104 00105 Process* p = Kernel::get_instance()->findProcess(0, 0x222); // CONSTANT! 00106 HealProcess* hp = p_dynamic_cast<HealProcess*>(p); 00107 if (!hp) { 00108 perr << "I_feedAvatar: unable to find HealProcess!" << std::endl; 00109 return 0; 00110 } 00111 00112 hp->feedAvatar(food); 00113 00114 return 0; 00115 } 00116 00117 00118 void HealProcess::saveData(ODataSource* ods) 00119 { 00120 Process::saveData(ods); 00121 00122 ods->write2(healCounter); 00123 ods->write2(hungerCounter); 00124 } 00125 00126 bool HealProcess::loadData(IDataSource* ids, uint32 version) 00127 { 00128 if (!Process::loadData(ids, version)) return false; 00129 00130 healCounter = ids->read2(); 00131 hungerCounter = ids->read2(); 00132 00133 return true; 00134 }