TaskList.cpp

Go to the documentation of this file.
00001 /*
00002 SagaEngine library
00003 Copyright (c) 2002-2006 Skalden Studio AS
00004 
00005 This software is provided 'as-is', without any express or implied 
00006 warranty. In no event will the authors be held liable for any 
00007 damages arising from the use of this software.
00008 
00009 Permission is granted to distribute the library under the terms of the 
00010 Q Public License version 1.0. Be sure to read and understand the license
00011 before using the library. It should be included here, or you may read it
00012 at http://www.trolltech.com/products/qt/licenses/licensing/qpl
00013 
00014 The original version of this library can be located at:
00015 http://www.sagaengine.com/
00016 
00017 Rune Myrland
00018 rune@skalden.com
00019 */
00020 
00021 
00022 #include "TaskList.hpp"
00023 
00024 namespace se_core {
00025 
00026     int TaskList
00027     ::perform(int weight) {
00028         int n = next();
00029         if(n < 0) {
00030             freePoints_ = 0;
00031             return 0;
00032         }
00033         int w = 0;
00034         freePoints_ += (n >= 0) ? weight : -freePoints_;
00035         /*
00036         if((n >= 0) && (w + tasks_[n]->weight() < freePoints_)) {
00037             tasks_[n]->perform();
00038             w += tasks_[n]->weight();
00039             tasks_[n] = tasks_[ --taskCount_ ];
00040             freePoints_ = 0;
00041         }
00042         */
00043         while(n >= 0 && w + tasks_[n]->weight() < freePoints_) {
00044             tasks_[n]->perform();
00045             w += tasks_[n]->weight();
00046             tasks_[n]->isInProgress_ = false;
00047             tasks_[n] = tasks_[ --taskCount_ ];
00048             n = next();
00049         }
00050         freePoints_ -= w;
00051         return w;
00052     }
00053 
00054     int TaskList
00055     ::performAll() {
00056         int n = next();
00057         if(n < 0) {
00058             freePoints_ = 0;
00059             return 0;
00060         }
00061         int w = 0;
00062         while(taskCount_ > 0) {
00063             tasks_[n]->perform();
00064             w += tasks_[n]->weight();
00065             tasks_[n]->isInProgress_ = false;
00066             tasks_[n] = tasks_[ --taskCount_ ];
00067             n = next();
00068         }
00069         freePoints_ = 0;
00070         return w;
00071     }
00072 
00073 
00074     void TaskList
00075     ::addFree(int weight) {
00076         freePoints_ = weight;
00077     }
00078 
00079     int TaskList
00080     ::next() {
00081         int n = -1;
00082         int p = 65536;
00083         for(int i = 0; i < taskCount_; ++i) {
00084             if(tasks_[i]->priority() < p) {
00085                 n = i;
00086                 p = tasks_[i]->priority();
00087             }
00088         }
00089         return n;
00090     }
00091 
00092     void TaskList
00093     ::add(Task& t) {
00094         Assert(&t != 0);
00095         Assert(taskCount_ < MAX_TASKS);
00096         if(t.isInProgress_) {
00097             remove(t);
00098         }
00099         t.isInProgress_ = true;
00100         tasks_[ taskCount_++ ] = &t;
00101     }
00102 
00103 
00104     void TaskList
00105     ::remove(Task& t) {
00106         if(!t.isInProgress_)
00107             return;
00108 
00109         for(int i = 0; i < taskCount_; ++i) {
00110             if(tasks_[ i ] == &t) {
00111                 tasks_[ i ]->isInProgress_ = false;
00112                 tasks_[ i ] = tasks_[ --taskCount_ ];
00113                 --i;
00114             }
00115         }
00116     }
00117 
00118     void TaskList
00119     ::reset() {
00120         for(int i = 0; i < taskCount_; ++i) {
00121             tasks_[ i ]->isInProgress_ = false;
00122         }
00123         taskCount_ = 0;
00124         freePoints_ = 0;
00125     }
00126 }

Home Page | SagaEngine trunk (updated nightly) reference generated Sun Dec 2 20:06:12 2007 by Doxygen version 1.3.9.1.

SourceForge.net Logo