src/tron/cockpit/cCockpit.cpp

Go to the documentation of this file.
00001 /*
00002 
00003 *************************************************************************
00004 
00005 ArmageTron -- Just another Tron Lightcycle Game in 3D.
00006 Copyright (C) 2000  Manuel Moos (manuel@moosnet.de)
00007 
00008 **************************************************************************
00009 
00010 This program is free software; you can redistribute it and/or
00011 modify it under the terms of the GNU General Public License
00012 as published by the Free Software Foundation; either version 2
00013 of the License, or (at your option) any later version.
00014 
00015 This program is distributed in the hope that it will be useful,
00016 but WITHOUT ANY WARRANTY; without even the implied warranty of
00017 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018 GNU General Public License for more details.
00019 
00020 You should have received a copy of the GNU General Public License
00021 along with this program; if not, write to the Free Software
00022 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
00023 
00024 ***************************************************************************
00025 
00026 */
00027 
00028 #include "cockpit/cCockpit.h"
00029 #include "tValue.h"
00030 #include "values/vParser.h"
00031 #include "cockpit/cGauges.h"
00032 #include "cockpit/cLabel.h"
00033 #include "cockpit/cMap.h"
00034 #include "cockpit/cRectangle.h"
00035 #include "nConfig.h"
00036 
00037 #ifndef DEDICATED
00038 
00039 #include "ePlayer.h"
00040 #include "gCycle.h"
00041 #include "eTimer.h"
00042 #include "eTeam.h"
00043 #include "eCamera.h"
00044 #include "rRender.h"
00045 #include "rFont.h"
00046 #include "rScreen.h"
00047 #include "eSensor.h"
00048 #include <iostream>
00049 #include "eSoundMixer.h"
00050 
00051 #include <time.h>
00052 
00053 static void parsecockpit () {
00054     FOREACH_COCKPIT(i) {
00055         (*i)->ProcessCockpit();
00056     }
00057 }
00058 
00059 static void readjust_cockpit () {
00060     FOREACH_COCKPIT(i) {
00061         (*i)->Readjust();
00062     }
00063 }
00064 
00065 static rCallbackAfterScreenModeChange reloadft(&readjust_cockpit);
00066 
00067 static tString cockpit_file("Anonymous/standard-0.0.1.aacockpit.xml");
00068 static tConfItem<tString> cf("COCKPIT_FILE",cockpit_file,&parsecockpit);
00069 
00070 typedef std::pair<tString, tValue::Callback<cCockpit>::cb_ptr> cbpair;
00071 static const cbpair cbarray[] = {
00072                                     cbpair(tString("player_rubber")       , &cCockpit::cb_CurrentRubber),
00073                                     cbpair(tString("player_acceleration") , &cCockpit::cb_CurrentAcceleration),
00074                                     cbpair(tString("current_ping")        , &cCockpit::cb_CurrentPing),
00075                                     cbpair(tString("player_speed")        , &cCockpit::cb_CurrentSpeed),
00076                                     cbpair(tString("max_speed")           , &cCockpit::cb_MaxSpeed),
00077                                     cbpair(tString("player_brakes")       , &cCockpit::cb_CurrentBrakingReservoir),
00078                                     cbpair(tString("enemies_alive")       , &cCockpit::cb_AliveEnemies),
00079                                     cbpair(tString("friends_alive")       , &cCockpit::cb_AliveTeammates),
00080                                     cbpair(tString("current_framerate")   , &cCockpit::cb_Framerate),
00081                                     cbpair(tString("time_since_start")    , &cCockpit::cb_RunningTime),
00082                                     cbpair(tString("current_minutes")     , &cCockpit::cb_CurrentTimeMinutes),
00083                                     cbpair(tString("current_hours")       , &cCockpit::cb_CurrentTimeHours),
00084                                     cbpair(tString("current_hours12h")    , &cCockpit::cb_CurrentTimeHours12h),
00085                                     cbpair(tString("current_seconds")     , &cCockpit::cb_CurrentTimeSeconds),
00086                                     cbpair(tString("current_score")       , &cCockpit::cb_CurrentScore),
00087                                     cbpair(tString("top_score")           , &cCockpit::cb_TopScore),
00088                                     cbpair(tString("current_score_team")  , &cCockpit::cb_CurrentScoreTeam),
00089                                     cbpair(tString("top_score_team")      , &cCockpit::cb_TopScoreTeam),
00090                                     cbpair(tString("fastest_speed")       , &cCockpit::cb_FastestSpeed),
00091                                     cbpair(tString("fastest_name")        , &cCockpit::cb_FastestName),
00092                                     cbpair(tString("fastest_speed_round") , &cCockpit::cb_FastestSpeedRound),
00093                                     cbpair(tString("fastest_name_round")  , &cCockpit::cb_FastestNameRound),
00094                                     cbpair(tString("time_to_impact_front"), &cCockpit::cb_TimeToImpactFront),
00095                                     cbpair(tString("time_to_impact_right"), &cCockpit::cb_TimeToImpactRight),
00096                                     cbpair(tString("time_to_impact_left") , &cCockpit::cb_TimeToImpactLeft),
00097                                     cbpair(tString("current_song")        , &cCockpit::cb_CurrentSong),
00098                                     cbpair(tString("current_name")        , &cCockpit::cb_CurrentName),
00099                                     cbpair(tString("current_colored_name"), &cCockpit::cb_CurrentColoredName),
00100                                     cbpair(tString("current_pos_x")       , &cCockpit::cb_CurrentPosX),
00101                                     cbpair(tString("current_pos_y")       , &cCockpit::cb_CurrentPosY)
00102                                 };
00103 std::map<tString, tValue::Callback<cCockpit>::cb_ptr> const stc_callbacks(cbarray, cbarray+sizeof(cbarray)/sizeof(cbpair));
00104 
00105 std::set<tString> stc_forbiddenCallbacks;
00106 #endif
00107 static tString stc_forbiddenCallbacksString;
00108 #ifndef DEDICATED
00109 
00110 std::list<cCockpit *> cCockpit::m_Cockpits;
00111 
00112 static void reparseforbiddencallbacks(void) {
00113     stc_forbiddenCallbacks.clear();
00114 
00115     tString callbacks = stc_forbiddenCallbacksString + ":"; //add the extra separator, makes things easier
00116 
00117     size_t pos = 0;
00118     size_t next;
00119     while((next = callbacks.find(':', pos)) != tString::npos) {
00120         tString callback = callbacks.SubStr(pos, next - pos);
00121         if(stc_callbacks.count(callback)) {
00122             stc_forbiddenCallbacks.insert(callback);
00123         }
00124         pos = next+1;
00125     }
00126     parsecockpit();
00127 }
00128 
00129 static nSettingItem<tString> fcs("FORBID_COCKPIT_DATA", stc_forbiddenCallbacksString,&reparseforbiddencallbacks);
00130 #else
00131 static nSettingItem<tString> fcs("FORBID_COCKPIT_DATA", stc_forbiddenCallbacksString);
00132 #endif
00133 #ifndef DEDICATED
00134 
00135 
00136 cCockpit::~cCockpit() {
00137     ClearWidgets();
00138     std::remove_if(m_Cockpits.begin(), m_Cockpits.end(), std::bind2nd(std::equal_to<cCockpit *>(), this));
00139 }
00140 cCockpit::cCockpit(cockpit_type type) :
00141         m_Type(type),
00142         m_Cam(all),
00143         m_Player(0),
00144         m_FocusPlayer(0),
00145         m_ViewportPlayer(0),
00146 m_FocusCycle(0) {
00147     m_Cockpits.push_back(this);
00148     ProcessCockpit();
00149 }
00150 
00151 void cCockpit::ClearWidgets(void) {
00152     //while(!m_Widgets_perplayer.empty()) {
00153     //    delete m_Widgets_perplayer.front();
00154     //    m_Widgets_perplayer.pop_front();
00155     //}
00156     //while(!m_Widgets_rootwindow.empty()) {
00157     //    delete m_Widgets_rootwindow.front();
00158     //    m_Widgets_rootwindow.pop_front();
00159     //}
00160     m_Widgets.clear();
00161     //m_Widgets_perplayer.clear();
00162     //m_Widgets_cycles.clear();
00163     m_EventHandlers.clear();
00164 }
00165 
00166 void cCockpit::SetPlayer(ePlayer *player) {
00167     m_Player = player;
00168     m_ViewportPlayer = m_FocusPlayer = m_Player->netPlayer;
00169     if (player->cam) {
00170         for(int i =0 ; i< se_PlayerNetIDs.Len(); i++){
00171             ePlayerNetID *testPlayer = se_PlayerNetIDs[i];
00172             if(const eGameObject *testCycle = testPlayer->Object()) {
00173                 if(player->cam->Center() == testCycle) {
00174                     m_FocusPlayer = testPlayer;
00175                 }
00176             }
00177         }
00178     }
00179     if(m_FocusPlayer != 0) {
00180         m_FocusCycle = dynamic_cast<gCycle *>(m_FocusPlayer->Object());
00181     } else {
00182         m_FocusCycle = 0;
00183     }
00184 }
00185 
00186 //callbacks
00187 tValue::BasePtr cCockpit::cb_CurrentRubber(void) {
00188     if(m_Type == VIEWPORT_TOP) return tValue::BasePtr(new tValue::Base());
00189     return tValue::BasePtr(new tValue::Float(m_FocusCycle->GetRubber()));
00190 }
00191 tValue::BasePtr cCockpit::cb_CurrentAcceleration(void) {
00192     if(m_Type == VIEWPORT_TOP) return tValue::BasePtr(new tValue::Base());
00193     return tValue::BasePtr(new tValue::Float(m_FocusCycle->GetAcceleration()));
00194 }
00195 tValue::BasePtr cCockpit::cb_CurrentPing(void) {
00196     if(m_Type == VIEWPORT_TOP) return tValue::BasePtr(new tValue::Base());
00197     return tValue::BasePtr(new tValue::Float(static_cast<int>(m_ViewportPlayer->ping*1000)));
00198 }
00199 tValue::BasePtr cCockpit::cb_CurrentSpeed(void) {
00200     if(m_Type == VIEWPORT_TOP) return tValue::BasePtr(new tValue::Base());
00201     return tValue::BasePtr(new tValue::Float(m_FocusCycle->Speed()));
00202 }
00203 tValue::BasePtr cCockpit::cb_MaxSpeed(void) {
00204     if(m_Type == VIEWPORT_TOP) return tValue::BasePtr(new tValue::Base());
00205     return tValue::BasePtr(new tValue::Int( static_cast<int>(ceil( m_FocusCycle->MaximalSpeed() / 10.) *10)));
00206 }
00207 tValue::BasePtr cCockpit::cb_CurrentBrakingReservoir(void) {
00208     if(m_Type == VIEWPORT_TOP) return tValue::BasePtr(new tValue::Base());
00209     return tValue::BasePtr(new tValue::Float(m_FocusCycle->GetBrakingReservoir()));
00210 }
00211 tValue::BasePtr cCockpit::cb_AliveEnemies(void){
00212     if(m_Type == VIEWPORT_TOP) return tValue::BasePtr(new tValue::Base());
00213     int aliveenemies=0;
00214     unsigned short int max = se_PlayerNetIDs.Len();
00215     if(m_ViewportPlayer)
00216     {
00217         for(unsigned short int i=0;i<max;i++){
00218             ePlayerNetID *p=se_PlayerNetIDs(i);
00219             if(p->Object() && p->Object()->Alive() && p->CurrentTeam() != m_ViewportPlayer->CurrentTeam())
00220                 aliveenemies++;
00221         }
00222     }
00223     return tValue::BasePtr(new tValue::Int(aliveenemies));
00224 }
00225 tValue::BasePtr cCockpit::cb_AliveTeammates(void){
00226     if(m_Type == VIEWPORT_TOP) return tValue::BasePtr(new tValue::Base());
00227     int alivemates=0;
00228     unsigned short int max = se_PlayerNetIDs.Len();
00229     if(m_ViewportPlayer)
00230     {
00231         for(unsigned short int i=0;i<max;i++){
00232             ePlayerNetID *p=se_PlayerNetIDs(i);
00233             if(p->Object() && p->Object()->Alive() && p->CurrentTeam() == m_ViewportPlayer->CurrentTeam())
00234                 alivemates++;
00235         }
00236     }
00237     return tValue::BasePtr(new tValue::Int(alivemates));
00238 }
00239 
00240 tValue::BasePtr cCockpit::cb_Framerate(void){
00241 
00242     static int fps       = 60;
00243     static REAL lastTime = 0;
00244 
00245     const REAL newtime = tSysTimeFloat();
00246     const REAL ts      = newtime - lastTime;
00247 
00248     int newfps   = static_cast<int>(se_AverageFPS());
00249     if (fabs((newfps-fps)*ts)>4)
00250     {
00251         fps      = newfps;
00252         lastTime = newtime;
00253     }
00254     return tValue::BasePtr(new tValue::Int(fps));
00255 }
00256 
00257 tValue::BasePtr cCockpit::cb_RunningTime(void){
00258     return tValue::BasePtr(new tValue::Float(tSysTimeFloat()));
00259 }
00260 
00261 tValue::BasePtr cCockpit::cb_CurrentTimeHours(void){
00262     struct tm* thisTime;
00263     time_t rawtime;
00264 
00265     time ( &rawtime );
00266     thisTime = localtime ( &rawtime );
00267 
00268     return tValue::BasePtr(new tValue::Int(thisTime->tm_hour));
00269 }
00270 
00271 tValue::BasePtr cCockpit::cb_CurrentTimeHours12h(void){
00272     struct tm* thisTime;
00273     time_t rawtime;
00274 
00275     time ( &rawtime );
00276     thisTime = localtime ( &rawtime );
00277 
00278     return tValue::BasePtr(new tValue::Int((thisTime->tm_hour+11)%12+1));
00279 }
00280 
00281 tValue::BasePtr cCockpit::cb_CurrentTimeMinutes(void){
00282     struct tm* thisTime;
00283     time_t rawtime;
00284 
00285     time ( &rawtime );
00286     thisTime = localtime ( &rawtime );
00287 
00288     return tValue::BasePtr(new tValue::Int(thisTime->tm_min));
00289 }
00290 
00291 tValue::BasePtr cCockpit::cb_CurrentTimeSeconds(void){
00292     struct tm* thisTime;
00293     time_t rawtime;
00294 
00295     time ( &rawtime );
00296     thisTime = localtime ( &rawtime );
00297 
00298     return tValue::BasePtr(new tValue::Int(thisTime->tm_sec));
00299 }
00300 
00301 REAL stc_fastestSpeedRound = .0;
00302 REAL stc_fastestSpeed;
00303 tString stc_fastestNameRound;
00304 tString stc_fastestName;
00305 int stc_topScore;
00306 
00307 tValue::BasePtr cCockpit::cb_CurrentScore(void){
00308     if(m_Type == VIEWPORT_TOP) return tValue::BasePtr(new tValue::Base());
00309     return tValue::BasePtr(new tValue::Int(m_ViewportPlayer->TotalScore()));
00310 }
00311 tValue::BasePtr cCockpit::cb_TopScore(void){
00312     return tValue::BasePtr(new tValue::Int(stc_topScore));
00313 }
00314 
00315 tValue::BasePtr cCockpit::cb_CurrentScoreTeam(void){
00316     if(m_Type == VIEWPORT_TOP) return tValue::BasePtr(new tValue::Base());
00317     if(m_ViewportPlayer->CurrentTeam() == 0) {
00318         return tValue::BasePtr(new tValue::Base());
00319     }
00320     return tValue::BasePtr(new tValue::Int(m_ViewportPlayer->CurrentTeam()->Score()));
00321 }
00322 tValue::BasePtr cCockpit::cb_TopScoreTeam(void){
00323     int max = 0;
00324     for(int i=0;i<eTeam::teams.Len();++i){
00325         eTeam *t = eTeam::teams(i);
00326         if(t->Score() > max) max = t->Score();
00327     }
00328     return tValue::BasePtr(new tValue::Int(max));
00329 }
00330 
00331 static void stc_updateFastest() {
00332     stc_fastestSpeed = 0.;
00333     stc_topScore = 0;
00334     for(int i =0 ; i< se_PlayerNetIDs.Len(); i++){
00335         ePlayerNetID *p = se_PlayerNetIDs[i];
00336 
00337         if(gCycle *h = (gCycle*)(p->Object())){
00338             if (h->Speed()>stc_fastestSpeedRound && h->Alive()){
00339                 stc_fastestSpeedRound =  (float) h->Speed();  // changed to float for more accuracy in reporting top speed
00340                 stc_fastestNameRound = p->GetName();
00341             }
00342             if (h->Speed()>stc_fastestSpeed && h->Alive()){
00343                 stc_fastestSpeed =  (float) h->Speed();  // changed to float for more accuracy in reporting top speed
00344                 stc_fastestName = p->GetName();
00345             }
00346         }
00347         int thisscore = se_PlayerNetIDs[i]->TotalScore();
00348         if(thisscore>stc_topScore)
00349             stc_topScore=thisscore;
00350     }
00351 }
00352 
00353 static rPerFrameTask stcuf(&stc_updateFastest);
00354 
00355 tValue::BasePtr cCockpit::cb_FastestSpeedRound(void){
00356     return tValue::BasePtr(new tValue::Float(stc_fastestSpeedRound));
00357 }
00358 
00359 tValue::BasePtr cCockpit::cb_FastestNameRound(void){
00360     return tValue::BasePtr(new tValue::String(stc_fastestNameRound));
00361 }
00362 
00363 tValue::BasePtr cCockpit::cb_FastestSpeed(void){
00364     return tValue::BasePtr(new tValue::Float(stc_fastestSpeed));
00365 }
00366 
00367 tValue::BasePtr cCockpit::cb_FastestName(void){
00368     return tValue::BasePtr(new tValue::String(stc_fastestName));
00369 }
00370 
00371 tValue::BasePtr cCockpit::cb_TimeToImpactFront(void){
00372     if(m_Type == VIEWPORT_TOP) return tValue::BasePtr(new tValue::Base());
00373     eSensor test(m_FocusCycle, m_FocusCycle->Position(), m_FocusCycle->Direction());
00374     test.detect(5.*m_FocusCycle->Speed());
00375     return tValue::BasePtr(new tValue::Float(test.hit/m_FocusCycle->Speed()));
00376 }
00377 tValue::BasePtr cCockpit::cb_TimeToImpactRight(void){
00378     if(m_Type == VIEWPORT_TOP) return tValue::BasePtr(new tValue::Base());
00379     eSensor test(m_FocusCycle, m_FocusCycle->Position(), m_FocusCycle->Direction().Turn(0,-1));
00380     test.detect(5.*m_FocusCycle->Speed());
00381     return tValue::BasePtr(new tValue::Float(test.hit/m_FocusCycle->Speed()));
00382 }
00383 tValue::BasePtr cCockpit::cb_TimeToImpactLeft(void){
00384     if(m_Type == VIEWPORT_TOP) return tValue::BasePtr(new tValue::Base());
00385     eSensor test(m_FocusCycle, m_FocusCycle->Position(), m_FocusCycle->Direction().Turn(0,1));
00386     test.detect(5.*m_FocusCycle->Speed());
00387     return tValue::BasePtr(new tValue::Float(test.hit/m_FocusCycle->Speed()));
00388 }
00389 
00390 tValue::BasePtr cCockpit::cb_CurrentSong(void){
00391     return tValue::BasePtr(new tValue::String(eSoundMixer::GetMixer()->GetCurrentSong()));
00392 }
00393 
00394 tValue::BasePtr cCockpit::cb_CurrentName(void) {
00395     if(m_Type == VIEWPORT_TOP) return tValue::BasePtr(new tValue::Base());
00396     return tValue::BasePtr(new tValue::String(tString(m_FocusPlayer->GetName())));
00397 }
00398 
00399 tValue::BasePtr cCockpit::cb_CurrentColoredName(void) {
00400     if(m_Type == VIEWPORT_TOP) return tValue::BasePtr(new tValue::Base());
00401     tColoredString ret;
00402     ret << *m_FocusPlayer;
00403     return tValue::BasePtr(new tValue::String(ret));
00404 }
00405 
00406 tValue::BasePtr cCockpit::cb_CurrentPosX(void) {
00407     if(m_Type == VIEWPORT_TOP) return tValue::BasePtr(new tValue::Base());
00408     return tValue::BasePtr(new tValue::Float(m_FocusCycle->Position().x));
00409 }
00410 
00411 tValue::BasePtr cCockpit::cb_CurrentPosY(void) {
00412     if(m_Type == VIEWPORT_TOP) return tValue::BasePtr(new tValue::Base());
00413     return tValue::BasePtr(new tValue::Float(m_FocusCycle->Position().x));
00414 }
00415 
00416 cCockpit* cCockpit::_instance = 0;
00417 
00418 void cCockpit::ProcessCockpit(void) {
00419     ClearWidgets();
00420 
00421     if (!LoadWithParsing(cockpit_file)) return;
00422     node cur = GetFileContents();
00423     if(!cur) {
00424         tERR_WARN("No Cockpit node found!");
00425     }
00426     if (m_Path.Type() != "aacockpit") {
00427         tERR_WARN("Type 'aacockpit' expected, found '" << cur.GetProp("type") << "' instead");
00428         return;
00429     }
00430     if (cur.IsOfType("Cockpit")) {
00431         ProcessWidgets(cur);
00432         if(sr_screenWidth != 0) Readjust();
00433         return;
00434     } else {
00435         tERR_WARN("Found a node of type '" + cur.GetName() + "' where type 'Cockpit' was expected");
00436         return;
00437     }
00438 }
00439 
00440 void cCockpit::ProcessWidgets(node cur) {
00441     std::map<tString, node> templates;
00442     for(cur = cur.GetFirstChild(); cur; ++cur) {
00443         if (cur.IsOfType("text") || cur.IsOfType("comment")) continue;
00444         if (cur.IsOfType("WidgetTemplate")) {
00445             templates[cur.GetProp("id")] = cur;
00446             continue;
00447         }
00448         switch (m_Type) {
00449         case VIEWPORT_TOP:
00450             if(cur.GetProp("viewport") != "top") {
00451                 continue;
00452             }
00453             break;
00454         case VIEWPORT_CYCLE:
00455             if(cur.GetProp("viewport") != "cycle") {
00456                 continue;
00457             }
00458             break;
00459         case VIEWPORT_ALL:
00460             if(cur.GetProp("viewport") != "all") {
00461                 continue;
00462             }
00463             break;
00464         }
00465         cWidget::Base_ptr widget_ptr = ProcessWidgetType(cur);
00466         if(&*widget_ptr == 0) {
00467             tERR_WARN("Unknown Widget type '" + cur.GetName() + "'");
00468             continue;
00469         }
00470         cWidget::Base &widget = *widget_ptr;
00471 
00472         widget.SetCockpit(this);
00473         widget.ParseTemplate(true);
00474 
00475         //Process all templates first
00476         tString use(cur.GetProp("usetemplate"));
00477 
00478         use += " "; //add the extra seperator, makes things easier
00479         int pos = 0;
00480         int next;
00481         while((next = use.find(' ', pos)) != -1) {
00482             tString thisuse = use.SubStr(pos, next - pos);
00483             if(templates.count(thisuse)) {
00484                 ProcessWidget(templates[thisuse], widget);
00485             } else if (!thisuse.empty()) {
00486                 tERR_WARN(tString("Nothing known about template id '") + thisuse + "'.");
00487             }
00488             pos = next+1;
00489         }
00490 
00491         widget.ParseTemplate(false);
00492         ProcessWidget(cur, widget);
00493         m_Widgets.push_back(widget_ptr.release());
00494     }
00495 }
00496 
00497 void cCockpit::ProcessWidget(node cur, cWidget::Base &widget) {
00498     ProcessWidgetCamera(cur, widget);
00499     int num;
00500     cur.GetProp("toggle", num);
00501     AddEventHandler(num, &widget);
00502     widget.SetDefaultState(cur.GetPropBool("toggleDefault"));
00503     widget.SetSticky(cur.GetPropBool("toggleSticky"));
00504     ProcessWidgetCore(cur, widget);
00505 }
00506 
00507 void cCockpit::AddEventHandler(int id, cWidget::Base *widget) {
00508     m_EventHandlers.insert(std::pair<int, cWidget::Base *>(id, widget));
00509 }
00510 
00511 cWidget::Base_ptr cCockpit::ProcessWidgetType(node cur) {
00512     if(cur.IsOfType("NeedleGauge"))
00513         return cWidget::Base_ptr(new cWidget::NeedleGauge());
00514     if(cur.IsOfType("BarGauge"))
00515         return cWidget::Base_ptr(new cWidget::BarGauge());
00516     if(cur.IsOfType("VerticalBarGauge"))
00517         return cWidget::Base_ptr(new cWidget::VerticalBarGauge());
00518     if(cur.IsOfType("Label"))
00519         return cWidget::Base_ptr(new cWidget::Label());
00520     if(cur.IsOfType("Map"))
00521         return cWidget::Base_ptr(new cWidget::Map());
00522     if(cur.IsOfType("Rectangle"))
00523         return cWidget::Base_ptr(new cWidget::Rectangle());
00524     return cWidget::Base_ptr();
00525 }
00526 
00527 void cCockpit::ProcessWidgetCamera(node cur, cWidget::Base &widget) {
00528     tString cam(cur.GetProp("camera"));
00529     if(cam.size() == 0) {
00530         tERR_WARN("Empty camera string");
00531         widget.SetCam(all);
00532     }
00533     std::map<tString, unsigned> cams;
00534     cams[tString("custom")] = custom;
00535     cams[tString("follow")] = follow;
00536     cams[tString("free")] = free;
00537     cams[tString("in")] = in;
00538     cams[tString("server_custom")] = server_custom;
00539     cams[tString("smart")] = smart;
00540     cams[tString("mer")] = mer;
00541     cams[tString("all")] = all;
00542     cams[tString("*")] = all;
00543 
00544     unsigned ret=0;
00545     bool invert=false;
00546     if(cam(0) == '^') {
00547         invert=true;
00548         cam.erase(0,1);
00549     }
00550     cam += " "; //add the extra seperator, makes things easier
00551 
00552     int pos = 0;
00553     int next;
00554     while((next = cam.find(' ', pos)) != -1) {
00555         tString thiscam = cam.SubStr(pos, next - pos);
00556         if(cams.count(thiscam)) {
00557             ret |= cams[thiscam];
00558         } else {
00559             tERR_WARN(tString("Nothing known about camera type '") + thiscam + "'.");
00560         }
00561         pos = next+1;
00562     }
00563     if(invert) {
00564         ret = ~ret;
00565     }
00566     widget.SetCam(ret);
00567 }
00568 
00569 void cCockpit::ProcessWidgetCore(node cur, cWidget::Base &widget) {
00570     for (cur = cur.GetFirstChild(); cur; ++cur) {
00571         tString name = cur.GetName();
00572         if(name == "comment" || name == "text") continue;
00573         widget.Process(cur);
00574     }
00575 }
00576 
00577 
00578 //void cCockpit::RenderPlayer(float factor) {
00579 //    if(m_FocusPlayer != 0 && m_ViewportPlayer != 0) {
00580 //        Color(1,1,1);
00581 //        if(m_Player->cam) {
00582 //
00583 //            if (m_FocusCycle && ( !m_Player->netPlayer || !m_Player->netPlayer->IsChatting()) && se_GameTime()>-2){
00584 //                //h->Speed()>maxmeterspeed?maxmeterspeed+=10:1;
00585 //
00586 //                for(widget_list_t::const_iterator i=m_Widgets.begin(); i!=m_Widgets.end(); ++i)
00587 //                {
00588 //                    int cam = (*i)->GetCam();
00589 //                    switch(m_Player->cam->GetCamMode()) {
00590 //                    case CAMERA_IN:
00591 //                    case CAMERA_SMART_IN:
00592 //                        if(!(cam & in)) continue;
00593 //                        break;
00594 //                    case CAMERA_CUSTOM:
00595 //                        if(!(cam & custom)) continue;
00596 //                        break;
00597 //                    case CAMERA_FREE:
00598 //                        if(!(cam & free)) continue;
00599 //                        break;
00600 //                    case CAMERA_FOLLOW:
00601 //                        if(!(cam & follow)) continue;
00602 //                        break;
00603 //                    case CAMERA_SMART:
00604 //                        if(!(cam & smart)) continue;
00605 //                        break;
00606 //                    case CAMERA_SERVER_CUSTOM:
00607 //                        if(!(cam & server_custom)) continue;
00608 //                        break;
00609 //                    case CAMERA_MER:
00610 //                        if(!(cam & mer)) continue;
00611 //                        break;
00612 //                    case CAMERA_COUNT:
00613 //                        continue; //not handled, no sense?!
00614 //                    }
00615 //                    if ((*i)->Active()) {
00616 //                        if(cWidget::WithCoordinates *coordWidget = dynamic_cast<cWidget::WithCoordinates *>(&(*(*i)))) {
00617 //                            coordWidget->SetFactor(factor);
00618 //                        }
00619 //                        (*i)->Render();
00620 //                    }
00621 //                }
00622 //                //  bool displayfastest = true;// put into global, set via menusytem... subby to do.make sr_DISPLAYFASTESTout
00623 //
00624 //            }
00625 //        }
00626 //    }
00627 //
00628 //}
00629 //void cCockpit::RenderRootwindow() {
00630 //    if( m_Player == 0 ) return;
00631 //    if(m_ViewportPlayer == 0) return;
00632 //
00633 //    sr_ResetRenderState(true);
00634 //    glViewport (GLsizei(0),
00635 //                GLsizei(0),
00636 //                GLsizei(sr_screenWidth),
00637 //                GLsizei(sr_screenWidth));
00638 //
00639 //    //BeginLineLoop();
00640 //    //Color(1.,1.,1.,1.);
00641 //    //Vertex(-.1,-.1);
00642 //    //Vertex( .1,-.1);
00643 //    //Vertex( .1, .1);
00644 //    //Vertex(-.1, .1);
00645 //    //RenderEnd();
00646 //    for(widget_list_t::const_iterator i=m_Widgets.begin(); i!=m_Widgets.end(); ++i) {
00647 //        if((*i)->Active()) {
00648 //            (*i)->Render();
00649 //        }
00650 //    }
00651 //}
00652 //
00653 //void cCockpit::RenderCycle(gCycle const &cycle) {
00654 //    m_ViewportPlayer = m_FocusPlayer = cycle.Player();
00655 //    if(m_FocusPlayer != 0) {
00656 //        m_FocusCycle = dynamic_cast<gCycle *>(m_FocusPlayer->Object());
00657 //    } else {
00658 //        m_FocusCycle = 0;
00659 //    }
00660 //    //if( m_Player == 0 ) return;
00661 //    if(m_ViewportPlayer == 0) return;
00662 //
00663 //    bool gl_depth_test = glIsEnabled(GL_DEPTH_TEST);
00664 //    glDisable(GL_DEPTH_TEST);
00665 //
00666 //    for(widget_list_t::const_iterator i=m_Widgets.begin(); i!=m_Widgets.end(); ++i) {
00667 //        if((*i)->Active()) {
00668 //            (*i)->Render();
00669 //        }
00670 //    }
00671 //
00672 //    if(gl_depth_test) {
00673 //        glEnable(GL_DEPTH_TEST);
00674 //    }
00675 //}
00676 
00677 void cCockpit::SetCycle(gCycle const &cycle) {
00678     m_ViewportPlayer = m_FocusPlayer = cycle.Player();
00679     if(m_FocusPlayer != 0) {
00680         m_FocusCycle = dynamic_cast<gCycle *>(m_FocusPlayer->Object());
00681     } else {
00682         m_FocusCycle = 0;
00683     }
00684 }
00685 
00686 void cCockpit::Render() {
00687     switch(m_Type) {
00688     case VIEWPORT_ALL:
00689         if(m_FocusPlayer != 0 && m_ViewportPlayer != 0) {
00690             Color(1,1,1);
00691             if(m_Player->cam) {
00692 
00693                 if (m_FocusCycle && ( !m_Player->netPlayer || !m_Player->netPlayer->IsChatting()) && se_GameTime()>-2){
00694                     //h->Speed()>maxmeterspeed?maxmeterspeed+=10:1;
00695 
00696                     for(widget_list_t::const_iterator i=m_Widgets.begin(); i!=m_Widgets.end(); ++i)
00697                     {
00698                         int cam = (*i)->GetCam();
00699                         switch(m_Player->cam->GetCamMode()) {
00700                         case CAMERA_IN:
00701                         case CAMERA_SMART_IN:
00702                             if(!(cam & in)) continue;
00703                             break;
00704                         case CAMERA_CUSTOM:
00705                             if(!(cam & custom)) continue;
00706                             break;
00707                         case CAMERA_FREE:
00708                             if(!(cam & free)) continue;
00709                             break;
00710                         case CAMERA_FOLLOW:
00711                             if(!(cam & follow)) continue;
00712                             break;
00713                         case CAMERA_SMART:
00714                             if(!(cam & smart)) continue;
00715                             break;
00716                         case CAMERA_SERVER_CUSTOM:
00717                             if(!(cam & server_custom)) continue;
00718                             break;
00719                         case CAMERA_MER:
00720                             if(!(cam & mer)) continue;
00721                             break;
00722                         case CAMERA_COUNT:
00723                             continue; //not handled, no sense?!
00724                         }
00725                         if ((*i)->Active()) {
00726                             (*i)->Render();
00727                         }
00728                     }
00729                     //  bool displayfastest = true;// put into global, set via menusytem... subby to do.make sr_DISPLAYFASTESTout
00730 
00731                 }
00732             }
00733         }
00734         break;
00735     case VIEWPORT_TOP:
00736         sr_ResetRenderState(true);
00737         glViewport (GLsizei(0),
00738                     GLsizei(0),
00739                     GLsizei(sr_screenWidth),
00740                     GLsizei(sr_screenWidth));
00741 
00742         for(widget_list_t::const_iterator i=m_Widgets.begin(); i!=m_Widgets.end(); ++i) {
00743             if((*i)->Active()) {
00744                 (*i)->Render();
00745             }
00746         }
00747         break;
00748     case VIEWPORT_CYCLE: {
00749             if(m_ViewportPlayer == 0) return;
00750 
00751             bool gl_depth_test = glIsEnabled(GL_DEPTH_TEST);
00752             glDisable(GL_DEPTH_TEST);
00753 
00754             for(widget_list_t::const_iterator i=m_Widgets.begin(); i!=m_Widgets.end(); ++i) {
00755                 if((*i)->Active()) {
00756                     (*i)->Render();
00757                 }
00758             }
00759 
00760             if(gl_depth_test) {
00761                 glEnable(GL_DEPTH_TEST);
00762             }
00763         } break;
00764     }
00765 }
00766 
00767 static void display_cockpit_lucifer() {
00768     static cCockpit static_cockpit(cCockpit::VIEWPORT_TOP);
00769 
00770     sr_ResetRenderState(true);
00771 
00772     if (!(se_mainGameTimer &&
00773             se_mainGameTimer->speed > .9 &&
00774             se_mainGameTimer->speed < 1.1 &&
00775             se_mainGameTimer->IsSynced() )) return;
00776 
00777     rViewportConfiguration* viewportConfiguration = rViewportConfiguration::CurrentViewportConfiguration();
00778 
00779     for ( int viewport = viewportConfiguration->num_viewports-1; viewport >= 0; --viewport )
00780     {
00781         // get the ID of the player in the viewport
00782         int playerID = sr_viewportBelongsToPlayer[ viewport ];
00783 
00784         // get the player
00785         ePlayer* player = ePlayer::PlayerConfig( playerID );
00786 
00787         rViewport *port = viewportConfiguration->Port( viewport );
00788         tCoord dims = port->GetDimensions();
00789 
00790         // select the corrected viewport
00791         port->EqualAspectBottom().Select();
00792 
00793         cCockpit *player_cockpit;
00794         if(!(player_cockpit = dynamic_cast<cCockpit *>(&*player->cockpit))) {
00795             player_cockpit = new cCockpit(cCockpit::VIEWPORT_ALL);
00796             player->cockpit = player_cockpit;
00797             float factor = 4./3. / (static_cast<float>(sr_screenWidth)/static_cast<float>(sr_screenHeight));
00798             player_cockpit->Readjust(factor * dims.y / dims.x);
00799         }
00800 
00801         player_cockpit->SetPlayer(player);
00802         // delegate
00803         player_cockpit->Render();
00804     }
00805 
00806     static_cockpit.Render();
00807 
00808 #if 0   // Testing ground :)
00809     vValue::Expr::Core::Base *test = vValue::Parser::parse(tString("10"));
00810     std::cerr << "test: " << test->GetValue() << std::endl;
00811 #endif
00812 }
00813 
00814 static rPerFrameTask dfps(&display_cockpit_lucifer);
00815 
00816 static uActionGlobal cockpitKey1("COCKPIT_KEY_1");
00817 static uActionGlobal cockpitKey2("COCKPIT_KEY_2");
00818 static uActionGlobal cockpitKey3("COCKPIT_KEY_3");
00819 static uActionGlobal cockpitKey4("COCKPIT_KEY_4");
00820 static uActionGlobal cockpitKey5("COCKPIT_KEY_5");
00821 static uActionGlobalFunc ck1(&cockpitKey1, &cCockpit::ProcessKey1, true);
00822 static uActionGlobalFunc ck2(&cockpitKey2, &cCockpit::ProcessKey2, true);
00823 static uActionGlobalFunc ck3(&cockpitKey3, &cCockpit::ProcessKey3, true);
00824 static uActionGlobalFunc ck4(&cockpitKey4, &cCockpit::ProcessKey4, true);
00825 static uActionGlobalFunc ck5(&cockpitKey5, &cCockpit::ProcessKey5, true);
00826 
00827 bool ProcessKey(float i, int num) {
00828     bool ret = false;
00829     FOREACH_COCKPIT(j) {
00830         if((*j)->HandleEvent(num, i>0)) {
00831             ret = true;
00832         }
00833     }
00834     return ret;
00835 }
00836 
00837 bool cCockpit::ProcessKey1(float i) { return ProcessKey(i, 1); }
00838 bool cCockpit::ProcessKey2(float i) { return ProcessKey(i, 2); }
00839 bool cCockpit::ProcessKey3(float i) { return ProcessKey(i, 3); }
00840 bool cCockpit::ProcessKey4(float i) { return ProcessKey(i, 4); }
00841 bool cCockpit::ProcessKey5(float i) { return ProcessKey(i, 5); }
00842 
00843 bool cCockpit::HandleEvent(int id, bool state) {
00844     if(m_EventHandlers.count(id)){
00845         for(std::multimap<int, cWidget::Base *>::iterator iter = m_EventHandlers.find(id); iter != m_EventHandlers.end() && iter->first == id; ++iter) {
00846             iter->second->HandleEvent(state, id);
00847         }
00848         return true;
00849     }
00850     return false;
00851 }
00852 
00853 void cCockpit::Readjust(void) {
00854     if(m_Type != VIEWPORT_TOP) return;
00855     if (sr_screenWidth == 0) return;
00856     float factor = 4./3. / (static_cast<float>(sr_screenWidth)/static_cast<float>(sr_screenHeight));
00857     //float factor = 2.;
00858     Readjust(factor);
00859 }
00860 void cCockpit::Readjust(float factor) {
00861     for(widget_list_t::iterator iter = m_Widgets.begin(); iter != m_Widgets.end(); ++iter) {
00862         if(cWidget::WithCoordinates *coordWidget = dynamic_cast<cWidget::WithCoordinates *>(&(*(*iter)))) {
00863             coordWidget->SetFactor(factor);
00864         }
00865     }
00866 }
00867 
00868 gCycle* cCockpit::GetFocusCycle(void) {
00869     return m_FocusCycle;
00870 }
00871 
00872 ePlayer *cCockpit::GetPlayer() {
00873     return m_Player;
00874 }
00875 #endif

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