Adonthell  0.4
time_event.cc
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002/2003/2004 Kai Sterker <kai.sterker@gmail.com>
3  Part of the Adonthell Project <http://adonthell.nongnu.org>
4 
5  Adonthell is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation; either version 2 of the License, or
8  (at your option) any later version.
9 
10  Adonthell is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License
16  along with Adonthell. If not, see <http://www.gnu.org/licenses/>.
17 */
18 
19 /**
20  * @file time_event.cc
21  *
22  * @author Kai Sterker
23  * @brief Implements the time_event class.
24  */
25 
26 #include "time_event.h"
27 #include "gamedate.h"
28 
29 // create a new time event
30 time_event::time_event (const string & time, bool absolute) : event ()
31 {
32  Repeat = 1;
33  Type = TIME_EVENT;
34  Absolute = absolute;
35  Time = gamedate::parse_time (time);
36  if (!absolute) Time += gamedate::time ();
37 }
38 
39 // specify the interval between two occurances of the event
40 void time_event::set_repeat (const string & interval, s_int32 count)
41 {
42  Interval = gamedate::parse_time (interval);
43  Repeat = count;
44 }
45 
46 // execute the time event
48 {
49  // nothing needs be passed to the script; it can get the
50  // current time from the gametime class if it is needed.
51  switch (Action)
52  {
53  case ACTION_SCRIPT:
54  {
55  Script->run ();
56  break;
57  }
58 
59  case ACTION_PYFUNC:
60  {
62  break;
63  }
64 
65  case ACTION_CPPFUNC:
66  {
67  Callback ();
68  break;
69  }
70 
71  default: break;
72  }
73 
74  // when the script needs be repeated, do so.
75  if (Repeat != 0) Time += Interval;
76 
77  return do_repeat ();
78 }
79 
80 // disable the event temporarily
82 {
83  // save time 'til relative event is raised
84  if (!Absolute) Time -= gamedate::time ();
85 
86  event::pause ();
87 }
88 
89 // enable a previously paused event
91 {
92  // restore alarm time for relative event
93  if (!Absolute) Time += gamedate::time ();
94 
95  event::resume ();
96 }
97 
98 // Save time event to file
100 {
101  // save basic event data first
102  event::put_state (out);
103 
104  // save time event data
105  Time >> out;
106  Interval >> out;
107  Absolute >> out;
108 }
109 
110 // load time event from file
112 {
113  // get basic event data
114  event::get_state (in);
115 
116  // get time event data
117  Time << in;
118  Interval << in;
119  Absolute << in;
120 
121  return true;
122 }
Class to write data from a Gzip compressed file.
Definition: fileops.h:227
#define s_int32
32 bits long signed integer
Definition: types.h:50
Class to read data from a Gzip compressed file.
Definition: fileops.h:135
u_int8 Action
What happens if the event occurs - see enum above.
Definition: event.h:319
virtual void put_state(ogzstream &out) const
Saves the basic event data (such as the type or script data) to a file.
Definition: event.cc:141
void put_state(ogzstream &out) const
Saves the basic event data (such as the type or script data) to a file.
Definition: time_event.cc:99
virtual void pause()
Disable the event temporarily.
Definition: event.cc:227
Base class for events.
Definition: event.h:75
bool get_state(igzstream &in)
Loads the basic event date from a file.
Definition: time_event.cc:111
s_int32 Repeat
Defines how often the event should be repeated.
Definition: event.h:336
time_event()
Standard constructor.
Definition: time_event.h:62
void pause()
Disable the event temporarily.
Definition: time_event.cc:81
virtual bool get_state(igzstream &in)
Loads the basic event date from a file.
Definition: event.cc:178
static u_int32 time()
Get the current gametime.
Definition: gamedate.h:64
u_int8 Type
Event type - see enum above.
Definition: event.h:309
void run(PyObject *args=NULL)
Calls the run () method of this object.
Definition: py_object.h:125
virtual void resume()
Re-enable an event that has been paused.
Definition: event.cc:234
py_callback * PyFunc
Python callback that may be executed instead of the script.
Definition: event.h:353
Declares the gamedate class.
py_object * Script
The Python script accociated with this event.
Definition: event.h:342
Functor0 Callback
C++ callback that may be executed when the event gets triggered.
Definition: event.h:358
static u_int32 parse_time(const std::string &time)
convert the time string to gametime minutes.
Definition: gamedate.cc:104
Declares the time_event class.
void callback_func0()
Calls the python function without arguments.
Definition: py_callback.cc:60
s_int32 do_repeat()
Decrease the event&#39;s repeat count and return the number of repeats left.
Definition: event.cc:241
void resume()
Re-enable an event that has been paused.
Definition: time_event.cc:90
s_int32 execute(const event *evnt)
Executes the script associated with this time event.
Definition: time_event.cc:47
void set_repeat(const string &interval, s_int32 count=-1)
Set whether the event should be raised at fixed intervals.
Definition: time_event.cc:40