SplitItemProcess.cpp

Go to the documentation of this file.
00001 /*
00002 Copyright (C) 2004-2005 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 "SplitItemProcess.h"
00022 #include "Item.h"
00023 #include "ShapeInfo.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(SplitItemProcess,Process);
00031 
00032 SplitItemProcess::SplitItemProcess() : Process()
00033 {
00034 
00035 }
00036 
00037 SplitItemProcess::SplitItemProcess(Item* original, Item* target_)
00038 {
00039         assert(original);
00040         assert(target_);
00041 
00042         assert(original->getShapeInfo()->hasQuantity());
00043         assert(target_->getShapeInfo()->hasQuantity());
00044 
00045         item_num = original->getObjId();
00046         target = target_->getObjId();
00047 
00048         // type = TODO
00049 }
00050 
00051 bool SplitItemProcess::run(const uint32 /*framenum*/)
00052 {
00053         Item* original = getItem(item_num);
00054         Item* targetitem = getItem(target);
00055 
00056         assert(original);
00057         assert(targetitem);
00058         assert(original->getShapeInfo()->hasQuantity());
00059         assert(targetitem->getShapeInfo()->hasQuantity());
00060 
00061         uint16 movecount = static_cast<uint16>(result);
00062 
00063         assert(movecount <= original->getQuality());
00064 
00065         uint16 origcount = original->getQuality() - movecount;
00066         uint16 targetcount = targetitem->getQuality() + movecount;
00067 
00068         pout << "SplitItemProcess splitting: " << movecount << ": "
00069                  << origcount << "-" << targetcount << std::endl;
00070                 
00071         if (targetcount > 0) {
00072                 targetitem->setQuality(targetcount);
00073                 targetitem->callUsecodeEvent_combine();
00074         } else {
00075                 targetitem->destroy();
00076                 targetitem = 0;
00077         }
00078 
00079         if (origcount > 0) {
00080                 original->setQuality(origcount);
00081                 original->callUsecodeEvent_combine();
00082         } else {
00083                 original->destroy(); // note: this terminates us
00084                 original = 0;
00085         }
00086 
00087         result = 0;
00088 
00089         if (!is_terminated())
00090                 terminate();
00091 
00092         return true;
00093 }
00094 
00095 void SplitItemProcess::saveData(ODataSource* ods)
00096 {
00097         Process::saveData(ods);
00098 
00099         ods->write2(target);
00100 }
00101 
00102 bool SplitItemProcess::loadData(IDataSource* ids, uint32 version)
00103 {
00104         if (!Process::loadData(ids, version)) return false;
00105 
00106         target = ids->read2();
00107 
00108         return true;
00109 }

Generated on Fri Jul 27 22:27:40 2007 for pentagram by  doxygen 1.4.7