Adonthell  0.4
time_event_handler.cc
Go to the documentation of this file.
1 /*
2  $Id: time_event_handler.cc,v 1.6 2003/01/20 00:15:41 ksterker Exp $
3 
4  Copyright (C) 2002/2003 Kai Sterker <kaisterker@linuxgames.com>
5  Part of the Adonthell Project http://adonthell.linuxgames.com
6 
7  This program is free software; you can redistribute it and/or modify
8  it under the terms of the GNU General Public License.
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY.
11 
12  See the COPYING file for more details.
13 */
14 
15 /**
16  * @file time_event_handler.cc
17  *
18  * @author Kai Sterker
19  * @brief Implements the time_event_handler class.
20  */
21 
22 #include <algorithm>
23 #include "gamedate.h"
24 #include "time_event.h"
25 #include "time_event_handler.h"
26 
27 
28 // See whether a matching event is registered and execute the
29 // according script(s)
31 {
32  s_int32 repeat;
33  event *evt;
34 
35  // As long as matching events are in the list
36  while (!Events.empty () && Events.front ()->equals (e))
37  {
38  evt = Events.front ();
39 
40  // we remove the event in any case, as it needs to be
41  // re-registered at a new position if it repeats
42  Events.erase (Events.begin ());
43  evt->set_registered (false);
44 
45  // events that don't repeat are destroyed automatically
46  repeat = evt->execute (e);
47 
48  // re-register event if it needs be repeated
49  if (repeat) register_event (evt);
50  else delete evt;
51  }
52 
53  return;
54 }
55 
56 // Unregister an event
58 {
59  vector<event*>::iterator i;
60 
61  // Search for the event we want to remove
62  i = find (Events.begin (), Events.end (), e);
63 
64  // found? -> get rid of it :)
65  if (i != Events.end ()) Events.erase (i);
66 }
67 
68 // register an event with the handler
70 {
71  vector<event*>::iterator i = Events.begin ();
72 
73  // search for the proper place to insert new event
74  while (i != Events.end ())
75  {
76  // skip events that are raised earlier than e
77  if (((time_event *) e)->time () <= ((time_event *) (*i))->time ()) break;
78  i++;
79  }
80 
81  Events.insert (i, e);
82 }