src/tron/zone/zMonitor.cpp

Go to the documentation of this file.
00001 #include <iostream>
00002 #include <vector>
00003 
00004 #include "zMonitor.h"
00005 #include "tMath.h"
00006 #include "defs.h"
00007 #include "nNetwork.h"
00008 
00009 /*
00010  * Keep track of everybody contributing to the monitor (for a tic atm)
00011  */
00012 void
00013 zMonitor::affectSlide(gCycle* user, tPolynomial<nMessage> triggererInfluence, Triad marked) {
00014     Triggerer triggerer;
00015     triggerer.who = user;
00016     // TODO:
00017     //triggerer.positive = triggererInfluence > 0.0 ? _true : _false;
00018     triggerer.marked = marked;
00019 
00020     contributors.push_back(triggerer);
00021     totalInfluence += triggererInfluence;
00022 
00023 }
00024 
00025 
00026 // *******************************************************************************
00027 // *
00028 // *    Timestep
00029 // *
00030 // *******************************************************************************
00034 // *******************************************************************************
00035 
00036 bool zMonitor::Timestep( REAL time )
00037 {
00038     tPolynomial<nMessage> prevValueEq = valueEq;
00039 
00040     if ( totalInfluence != previousTotalInfluenceSlide ) {
00041 
00042         for (int i=1; i<totalInfluence.Len(); i++) {
00043             valueEq.changeRate(totalInfluence[i], i, time);
00044         }
00045         // Set to null any remainding elements not reassigned in the previous loop
00046         for (int i=totalInfluence.Len(); i<valueEq.Len(); i++) {
00047             valueEq.changeRate(0.0, i, time);
00048         }
00049 
00050         previousTotalInfluenceSlide = totalInfluence;
00051     }
00052 
00053     valueEq = valueEq.clamp(minValue, maxValue, time);
00054 
00055     // Assemble all the contributors in a single list
00056     // Nota: this also protect the list for further modification
00057     // by recursives rules. Should all list be merged in a later version
00058     // still do make a copy at this step
00059 
00060 
00061     totalInfluence = drift;
00062 
00063     // Only update if value has changed enough
00064     // TODO:
00065     //    if( value < previousValue - EPS || previousValue + EPS < value) {
00066     // go through all the rules and find the ones to apply
00067     zMonitorRulePtrs::const_iterator iter;
00068     for (iter = rules.begin();
00069             iter != rules.end();
00070             ++iter)
00071     {
00072         // Go through all the rules of the monitor and see wich need to be activated
00073         if ((*iter)->isValid(valueEq.evaluate(time))) {
00074             (*iter)->applyRule(contributors, time, valueEq);
00075         }
00076     }
00077     //    }
00078 
00079     // Remove the information about who contributed in the last tic
00080     contributors.erase(contributors.begin(), contributors.end());
00081 
00082     // update time
00083     lastTime = time;
00084 
00085     return false;
00086 }
00087 
00088 
00089 void
00090 zMonitor::addRule(zMonitorRulePtr aRule) {
00091     /*
00092      * HACK
00093      * This only works for a single rule! fix this
00094      */
00095     rules.push_back(aRule);
00096 }
00097 
00098 
00099 
00100 
00101 void zMonitorRule::applyRule(triggerers &contributors, REAL time, const tPolynomial<nMessage> &valueEq) {
00102     /* We take all the contributors */
00103     /* And apply the proper effect */
00104 
00105     // Go through all effectgroups (owners of some action)
00106     std::vector<zEffectGroupPtr>::iterator iter;
00107     for (iter = effectGroupList.begin();
00108             iter != effectGroupList.end();
00109             ++iter)
00110     {
00111         // Go through all the categories of triggerer (ie: people who have right to trigger an action)
00112         triggerers::const_iterator iter2;
00113         for (iter2 = contributors.begin();
00114                 iter2 != contributors.end();
00115                 ++iter2)
00116         {
00117             (*iter)->apply(*iter2, time, valueEq);
00118         }
00119     }
00120 
00121     gVectorExtra< nNetObjectID > owners;
00122     gVectorExtra< nNetObjectID > teamOwners;
00123 
00124     zMonitorInfluencePtrs::const_iterator iterMonitorInfluence;
00125     for (iterMonitorInfluence=monitorInfluences.begin();
00126             iterMonitorInfluence!=monitorInfluences.end();
00127             ++iterMonitorInfluence)
00128     {
00129         // TODO: pass the whole valueEq
00130         (*iterMonitorInfluence)->apply(owners, teamOwners, (gCycle*)0, valueEq);
00131     }
00132 
00133     zZoneInfluencePtrs::const_iterator iterZoneInfluence;
00134     for (iterZoneInfluence=zoneInfluences.begin();
00135             iterZoneInfluence!=zoneInfluences.end();
00136             ++iterZoneInfluence)
00137     {
00138         (*iterZoneInfluence)->apply(valueEq);
00139     }
00140 }
00141 
00142 /*
00143  * Triggers when the monitor is over a value
00144  */
00145 bool
00146 zMonitorRuleOver::isValid(float monitorValue) {
00147     return (monitorValue>=limit);
00148 }
00149 
00150 /*
00151  * Triggers when the monitor is under a value
00152  */
00153 bool
00154 zMonitorRuleUnder::isValid(float monitorValue) {
00155     return (monitorValue<=limit);
00156 }
00157 
00158 
00159 /*
00160  * Triggers only in a range
00161  */
00162 bool
00163 zMonitorRuleInRange::isValid(float monitorValue) {
00164     return (lowLimit<= monitorValue && monitorValue<=highLimit);
00165 }
00166 
00167 /*
00168  * Triggers only when the monitor is outside of a range
00169  */
00170 bool
00171 zMonitorRuleOutsideRange::isValid(float monitorValue) {
00172     return (monitorValue <= lowLimit  || highLimit <= monitorValue);
00173 }
00174 
00175 void
00176 zMonitorInfluence::apply(gVectorExtra< nNetObjectID > &owners, gVectorExtra< nNetObjectID > &teamOwners, gCycle * user, const tPolynomial<nMessage> &valueEq) {
00177     // Currently, we discard ownership information
00178 
00179     tPolynomial<nMessage> tf = influence.marshal(valueEq);
00180     monitor->affectSlide(user, tf, marked);
00181 
00182 }

Generated on Sat Mar 15 22:56:12 2008 for Armagetron Advanced by  doxygen 1.5.4