Vegastrike 0.5.1 rc1  1.0
Original sources for Vegastrike Evolved
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
msgcenter.cpp
Go to the documentation of this file.
1 /*
2  * Vega Strike
3  * Copyright (C) 2001-2002 Daniel Horn
4  *
5  * http://vegastrike.sourceforge.net/
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20  */
21 
22 /*
23  * MessageCenter written by Alexander Rawass <alexannika@users.sourceforge.net>
24  */
25 
26 #include <stdlib.h>
27 #include <stdio.h>
28 #include <errno.h>
29 #include <time.h>
30 #include <ctype.h>
31 #include <assert.h>
32 #ifndef WIN32
33 //this file isn't available on my system (all win32 machines?) i dun even know what it has or if we need it as I can compile without it
34 #include <unistd.h>
35 #endif
36 
37 #include <expat.h>
38 #include "xml_support.h"
39 #include "networking/netserver.h"
40 #include "vegastrike.h"
41 
42 #include "cmd/unit_generic.h"
43 #include "mission.h"
44 #include "easydom.h"
45 
46 #include "msgcenter.h"
47 #include <algorithm>
48 
49 void MessageCenter::add( string from, string to, string message, double delay )
50 {
51  gameMessage msg;
52 
53  msg.from = from;
54  msg.to = to;
55  msg.message = message;
56 
57  msg.time = mission->getGametime()+delay;
58  if (SERVER)
59  VSServer->sendMessage( from, to, message, (float) delay );
60  messages.push_back( msg );
61 }
62 void MessageCenter::clear( const std::vector< std::string > &who, const std::vector< std::string > &whoNOT )
63 {
64  if ( who.empty() && whoNOT.empty() )
65  messages.clear();
66  for (int i = messages.size()-1; i >= 0; i--)
67  if ( std::find( whoNOT.begin(), whoNOT.end(),
68  messages[i].to.get() ) == whoNOT.end()
69  && ( who.empty() || std::find( who.begin(), who.end(), messages[i].to.get() ) != who.end() ) )
70  messages.erase( messages.begin()+i );
71 }
72 bool MessageCenter::last( unsigned int n,
73  gameMessage &m,
74  const std::vector< std::string > &who,
75  const std::vector< std::string > &whoNOT )
76 {
77  if ( who.empty() && whoNOT.empty() ) {
78  int size = messages.size();
79 
80  int index = size-1-n;
81  if (index >= 0) {
82  m = messages[index];
83  return true;
84  } else {
85  return false;
86  }
87  } else {
88  int j = 0;
89  int i = 0;
90  for (i = messages.size()-1; i >= 0; i--)
91  if ( std::find( whoNOT.begin(), whoNOT.end(),
92  messages[i].to.get() ) == whoNOT.end()
93  && ( who.empty() || std::find( who.begin(), who.end(), messages[i].to.get() ) != who.end() ) ) {
94  if (j == (int) n)
95  break;
96  j++;
97  }
98  if (i < 0)
99  return false;
100  m = messages[i];
101  return true;
102  }
103 }
104