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