Adonthell 0.4
map_event.cc
Go to the documentation of this file.
00001 /*
00002    $Id: map_event.cc,v 1.4 2003/01/20 20:18:43 ksterker Exp $
00003 
00004    Copyright (C) 2002/2003 Kai Sterker <kaisterker@linuxgames.com>
00005    Part of the Adonthell Project http://adonthell.linuxgames.com
00006 
00007    This program is free software; you can redistribute it and/or modify
00008    it under the terms of the GNU General Public License.
00009    This program is distributed in the hope that it will be useful,
00010    but WITHOUT ANY WARRANTY.
00011 
00012    See the COPYING file for more details.
00013 */
00014 
00015 /**
00016  * @file map_event.cc
00017  *
00018  * @author Kai Sterker
00019  * @brief Implements the different map events.
00020  */
00021  
00022 #include "map_event.h"
00023 
00024 // constructor
00025 map_event::map_event () : event ()
00026 {
00027     submap = x = y = dir = map = -1;
00028     c = NULL;
00029 }
00030 
00031 // compare two map events
00032 bool map_event::equals (const event* e)
00033 {
00034     // we know that we've got a map_event :)
00035     map_event *t = (map_event *) e;
00036 
00037     if (submap != -1 && t->submap != submap) return false;
00038     if (x != -1 && t->x != x) return false;
00039     if (y != -1 && t->y != y) return false;
00040     if (dir != -1 && t->dir != dir) return false;
00041     if (map != -1 && t->map != map) return false;
00042     if (c && t->c != c) return false;
00043     
00044     return true;
00045 }
00046 
00047 // Execute map event's script
00048 s_int32 map_event::execute (const event* e)
00049 {
00050     switch (Action)
00051     {
00052         case ACTION_SCRIPT:
00053         {
00054             map_event *t = (map_event *) e; 
00055     
00056             PyObject *args = Py_BuildValue ("(i, i, i, i, s)", 
00057                 t->submap, t->x, t->y, t->dir, t->c->get_id ().c_str ());  
00058     
00059             Script->run (args);
00060             
00061             Py_DECREF (args);
00062             break;
00063         }
00064         
00065         case ACTION_PYFUNC:
00066         {
00067             PyFunc->callback_func0 ();
00068             break;
00069         }
00070         
00071         case ACTION_CPPFUNC:
00072         {
00073             Callback ();
00074             break;
00075         }
00076         
00077         default: break;
00078     }
00079     
00080     return do_repeat ();
00081 }
00082 
00083 // Load a map event from file
00084 bool map_event::get_state (igzstream& f)
00085 {
00086     event::get_state (f); 
00087 
00088     string name;
00089     string s; 
00090     
00091     submap << f; 
00092     x << f;
00093     y << f;
00094 
00095     dir << f;
00096     map << f;
00097 
00098     s << f;
00099     if (s != "") c = (mapcharacter*) data::characters[s.c_str ()];
00100     else c = NULL; 
00101 
00102     return true;
00103 }
00104 
00105 // Save map event to file
00106 void map_event::put_state (ogzstream& out) const
00107 {
00108     event::put_state (out);
00109     
00110     submap >> out; 
00111     x >> out;
00112     y >> out;
00113     dir >> out;
00114     map >> out;
00115     
00116     if (c) c->get_id () >> out;
00117     else 
00118     {
00119         string s = ""; 
00120         s >> out;
00121     }
00122 }  
00123 
00124 // constructor
00125 enter_event::enter_event () : map_event ()
00126 {
00127     Type = ENTER_EVENT; 
00128 }
00129 
00130 // constructor
00131 leave_event::leave_event () : map_event ()
00132 {
00133     Type = LEAVE_EVENT; 
00134 }
00135 
00136 // constructor
00137 action_event::action_event () : map_event ()
00138 {
00139     Type = ACTION_EVENT; 
00140 }