Adonthell  0.4
map_event.cc
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002/2003 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 map_event.cc
21  *
22  * @author Kai Sterker
23  * @brief Implements the different map events.
24  */
25 
26 #include "map_event.h"
27 
28 // constructor
30 {
31  submap = x = y = dir = map = -1;
32  c = NULL;
33 }
34 
35 // compare two map events
36 bool map_event::equals (const event* e)
37 {
38  // we know that we've got a map_event :)
39  map_event *t = (map_event *) e;
40 
41  if (submap != -1 && t->submap != submap) return false;
42  if (x != -1 && t->x != x) return false;
43  if (y != -1 && t->y != y) return false;
44  if (dir != -1 && t->dir != dir) return false;
45  if (map != -1 && t->map != map) return false;
46  if (c && t->c != c) return false;
47 
48  return true;
49 }
50 
51 // Execute map event's script
53 {
54  switch (Action)
55  {
56  case ACTION_SCRIPT:
57  {
58  map_event *t = (map_event *) e;
59 
60  PyObject *args = Py_BuildValue ("(i, i, i, i, s)",
61  t->submap, t->x, t->y, t->dir, t->c->get_id ().c_str ());
62 
63  Script->run (args);
64 
65  Py_DECREF (args);
66  break;
67  }
68 
69  case ACTION_PYFUNC:
70  {
72  break;
73  }
74 
75  case ACTION_CPPFUNC:
76  {
77  Callback ();
78  break;
79  }
80 
81  default: break;
82  }
83 
84  return do_repeat ();
85 }
86 
87 // Load a map event from file
89 {
90  event::get_state (f);
91 
92  string name;
93  string s;
94 
95  submap << f;
96  x << f;
97  y << f;
98 
99  dir << f;
100  map << f;
101 
102  s << f;
103  if (s != "") c = (mapcharacter*) data::characters[s.c_str ()];
104  else c = NULL;
105 
106  return true;
107 }
108 
109 // Save map event to file
111 {
112  event::put_state (out);
113 
114  submap >> out;
115  x >> out;
116  y >> out;
117  dir >> out;
118  map >> out;
119 
120  if (c) c->get_id () >> out;
121  else
122  {
123  string s = "";
124  s >> out;
125  }
126 }
127 
128 // constructor
130 {
131  Type = ENTER_EVENT;
132 }
133 
134 // constructor
136 {
137  Type = LEAVE_EVENT;
138 }
139 
140 // constructor
142 {
143  Type = ACTION_EVENT;
144 }
map_event::get_state
bool get_state(igzstream &)
Loads the basic event date from a file.
Definition: map_event.cc:88
map_event::x
s_int32 x
X position (-1 for any).
Definition: map_event.h:68
map_event::equals
bool equals(const event *evnt)
Compare two map events for equality.
Definition: map_event.cc:36
event::put_state
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
igzstream
Class to read data from a Gzip compressed file.
Definition: fileops.h:135
dictionary
Stores objects of any kind.
Definition: storage.h:231
map_event::dir
s_int8 dir
Direction where the character is looking (-1 for any).
Definition: map_event.h:78
map_event::c
mapcharacter * c
Pointer to the mapcharacter that can launch this event (NULL for any).
Definition: map_event.h:88
s_int32
#define s_int32
32 bits long signed integer
Definition: types.h:50
event::Action
u_int8 Action
What happens if the event occurs - see enum above.
Definition: event.h:319
ogzstream
Class to write data from a Gzip compressed file.
Definition: fileops.h:227
map_event
Baseclass for map enter/leave/action events.
Definition: map_event.h:36
event::get_state
virtual bool get_state(igzstream &in)
Loads the basic event date from a file.
Definition: event.cc:178
event::PyFunc
py_callback * PyFunc
Python callback that may be executed instead of the script.
Definition: event.h:353
map_event::y
s_int32 y
Y position (-1 for any).
Definition: map_event.h:73
leave_event::leave_event
leave_event()
Default constructor.
Definition: map_event.cc:135
map_event.h
Declares the different map events.
map_event::put_state
void put_state(ogzstream &) const
Saves the basic event data (such as the type or script data) to a file.
Definition: map_event.cc:110
event::Type
u_int8 Type
Event type - see enum above.
Definition: event.h:309
event::do_repeat
s_int32 do_repeat()
Decrease the event's repeat count and return the number of repeats left.
Definition: event.cc:241
map_event::submap
s_int32 submap
Submap index (-1 for any).
Definition: map_event.h:63
mapcharacter
Representation of characters on a landmap.
Definition: mapcharacter.h:139
event::Script
py_object * Script
The Python script accociated with this event.
Definition: event.h:342
enter_event::enter_event
enter_event()
Default constructor.
Definition: map_event.cc:129
character_base::get_id
string get_id()
Returns an unique identifier of the character.
Definition: character_base.h:110
py_callback::callback_func0
void callback_func0()
Calls the python function without arguments.
Definition: py_callback.cc:60
map_event::map_event
map_event()
Default constructor.
Definition: map_event.cc:29
event
Base class for events.
Definition: event.h:75
map_event::execute
s_int32 execute(const event *evnt)
Executes the script associated with this map event.
Definition: map_event.cc:52
event::Callback
Functor0 Callback
C++ callback that may be executed when the event gets triggered.
Definition: event.h:358
py_object::run
void run(PyObject *args=NULL)
Calls the run () method of this object.
Definition: py_object.h:125
map_event::map
s_int32 map
Useless (for now).
Definition: map_event.h:83
action_event::action_event
action_event()
Default constructor.
Definition: map_event.cc:141