Adonthell  0.4
time_event.cc
Go to the documentation of this file.
1 /*
2  $Id: time_event.cc,v 1.7 2005/01/18 12:43:53 ksterker Exp $
3 
4  Copyright (C) 2002/2003/2004 Kai Sterker <kaisterker@linuxgames.com>
5  Part of the Adonthell Project http://adonthell.linuxgames.com
6 
7  Adonthell is free software; you can redistribute it and/or modify
8  it under the terms of the GNU General Public License as published by
9  the Free Software Foundation; either version 2 of the License, or
10  (at your option) any later version.
11 
12  Adonthell 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 Adonthell; if not, write to the Free Software
19  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 */
21 
22 /**
23  * @file time_event.cc
24  *
25  * @author Kai Sterker
26  * @brief Implements the time_event class.
27  */
28 
29 #include "time_event.h"
30 #include "gamedate.h"
31 
32 // create a new time event
33 time_event::time_event (const string & time, bool absolute) : event ()
34 {
35  Repeat = 1;
36  Type = TIME_EVENT;
37  Absolute = absolute;
38  Time = gamedate::parse_time (time);
39  if (!absolute) Time += gamedate::time ();
40 }
41 
42 // specify the interval between two occurances of the event
43 void time_event::set_repeat (const string & interval, s_int32 count)
44 {
45  Interval = gamedate::parse_time (interval);
46  Repeat = count;
47 }
48 
49 // execute the time event
51 {
52  // nothing needs be passed to the script; it can get the
53  // current time from the gametime class if it is needed.
54  switch (Action)
55  {
56  case ACTION_SCRIPT:
57  {
58  Script->run ();
59  break;
60  }
61 
62  case ACTION_PYFUNC:
63  {
65  break;
66  }
67 
68  case ACTION_CPPFUNC:
69  {
70  Callback ();
71  break;
72  }
73 
74  default: break;
75  }
76 
77  // when the script needs be repeated, do so.
78  if (Repeat != 0) Time += Interval;
79 
80  return do_repeat ();
81 }
82 
83 // disable the event temporarily
85 {
86  // save time 'til relative event is raised
87  if (!Absolute) Time -= gamedate::time ();
88 
89  event::pause ();
90 }
91 
92 // enable a previously paused event
94 {
95  // restore alarm time for relative event
96  if (!Absolute) Time += gamedate::time ();
97 
98  event::resume ();
99 }
100 
101 // Save time event to file
103 {
104  // save basic event data first
105  event::put_state (out);
106 
107  // save time event data
108  Time >> out;
109  Interval >> out;
110  Absolute >> out;
111 }
112 
113 // load time event from file
115 {
116  // get basic event data
117  event::get_state (in);
118 
119  // get time event data
120  Time << in;
121  Interval << in;
122  Absolute << in;
123 
124  return true;
125 }
Class to write data from a Gzip compressed file.
Definition: fileops.h:223
#define s_int32
32 bits long signed integer
Definition: types.h:44
Class to read data from a Gzip compressed file.
Definition: fileops.h:131
u_int8 Action
What happens if the event occurs - see enum above.
Definition: event.h:315
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:137
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:102
virtual void pause()
Disable the event temporarily.
Definition: event.cc:223
Base class for events.
Definition: event.h:71
bool get_state(igzstream &in)
Loads the basic event date from a file.
Definition: time_event.cc:114
s_int32 Repeat
Defines how often the event should be repeated.
Definition: event.h:332
time_event()
Standard constructor.
Definition: time_event.h:58
void pause()
Disable the event temporarily.
Definition: time_event.cc:84
virtual bool get_state(igzstream &in)
Loads the basic event date from a file.
Definition: event.cc:174
static u_int32 time()
Get the current gametime.
Definition: gamedate.h:60
u_int8 Type
Event type - see enum above.
Definition: event.h:305
void run(PyObject *args=NULL)
Calls the run () method of this object.
Definition: py_object.h:121
virtual void resume()
Re-enable an event that has been paused.
Definition: event.cc:230
py_callback * PyFunc
Python callback that may be executed instead of the script.
Definition: event.h:349
Declares the gamedate class.
py_object * Script
The Python script accociated with this event.
Definition: event.h:338
Functor0 Callback
C++ callback that may be executed when the event gets triggered.
Definition: event.h:354
static u_int32 parse_time(const std::string &time)
convert the time string to gametime minutes.
Definition: gamedate.cc:100
Declares the time_event class.
void callback_func0()
Calls the python function without arguments.
Definition: py_callback.cc:56
s_int32 do_repeat()
Decrease the event&#39;s repeat count and return the number of repeats left.
Definition: event.cc:237
void resume()
Re-enable an event that has been paused.
Definition: time_event.cc:93
s_int32 execute(const event *evnt)
Executes the script associated with this time event.
Definition: time_event.cc:50
void set_repeat(const string &interval, s_int32 count=-1)
Set whether the event should be raised at fixed intervals.
Definition: time_event.cc:43