Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ADDED beginnings of interaction with scenery objects
- Loading branch information
Showing
11 changed files
with
450 additions
and
68 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
/* | ||
Copyright (C) 2010 Kai Sterker <kaisterker@linuxgames.com> | ||
Part of the Adonthell Project http://adonthell.linuxgames.com | ||
Adonthell is free software; you can redistribute it and/or modify | ||
it under the terms of the GNU General Public License as published by | ||
the Free Software Foundation; either version 2 of the License, or | ||
(at your option) any later version. | ||
Adonthell is distributed in the hope that it will be useful, | ||
but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
GNU General Public License for more details. | ||
You should have received a copy of the GNU General Public License | ||
along with Adonthell; if not, write to the Free Software | ||
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | ||
*/ | ||
|
||
/** | ||
* @file world/action.cc | ||
* | ||
* @author Kai Sterker | ||
* @brief Implements the object interaction class. | ||
*/ | ||
|
||
|
||
#include "world/action.h" | ||
#include "python/pool.h" | ||
#include "world/character.h" | ||
#include "world/object.h" | ||
|
||
|
||
using world::action; | ||
|
||
// ctor | ||
action::action () | ||
{ | ||
Action = NULL; | ||
Args = NULL; | ||
} | ||
|
||
// dtor | ||
action::~action() | ||
{ | ||
clear (); | ||
} | ||
|
||
// reset | ||
void action::clear () | ||
{ | ||
delete Action; | ||
Py_XDECREF(Args); | ||
|
||
Action = NULL; | ||
Args = NULL; | ||
} | ||
|
||
// init | ||
bool action::init (const std::string & script, const std::string & method, PyObject *args) | ||
{ | ||
clear (); | ||
|
||
Action = python::pool::connect (ACTION_DIR + script, script, method); | ||
if (Action == NULL) return false; | ||
|
||
Args = python::pad_tuple (args, 2); | ||
if (Args == NULL) | ||
{ | ||
delete Action; | ||
Action = NULL; | ||
|
||
return false; | ||
} | ||
|
||
return true; | ||
} | ||
|
||
// execute action | ||
void action::execute (world::character *actor, world::object *target) | ||
{ | ||
execute (python::pass_instance (actor), python::pass_instance (target)); | ||
} | ||
|
||
// execute action | ||
void action::execute (PyObject *actor, PyObject *target) | ||
{ | ||
if (Action != NULL) | ||
{ | ||
// prepare arguments | ||
PyTuple_SET_ITEM (Args, 0, actor); | ||
PyTuple_SET_ITEM (Args, 1, target); | ||
|
||
Action->execute (Args); | ||
|
||
// reset | ||
PyTuple_SET_ITEM (Args, 0, Py_None); | ||
PyTuple_SET_ITEM (Args, 1, Py_None); | ||
} | ||
} | ||
|
||
// save | ||
void action::put_state (base::flat& file) const | ||
{ | ||
if (Action != NULL) | ||
{ | ||
base::flat record; | ||
Action->put_state (record); | ||
python::put_tuple (Args, record, 2); | ||
file.put_flat ("action", record); | ||
} | ||
} | ||
|
||
// load | ||
bool action::get_state (base::flat& file) | ||
{ | ||
Action = new python::method(); | ||
if (Action->get_state (file)) | ||
{ | ||
Args = python::get_tuple (file, 2); | ||
return true; | ||
} | ||
|
||
return false; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
/* | ||
Copyright (C) 2010 Kai Sterker <kaisterker@linuxgames.com> | ||
Part of the Adonthell Project http://adonthell.linuxgames.com | ||
Adonthell is free software; you can redistribute it and/or modify | ||
it under the terms of the GNU General Public License as published by | ||
the Free Software Foundation; either version 2 of the License, or | ||
(at your option) any later version. | ||
Adonthell is distributed in the hope that it will be useful, | ||
but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
GNU General Public License for more details. | ||
You should have received a copy of the GNU General Public License | ||
along with Adonthell; if not, write to the Free Software | ||
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | ||
*/ | ||
|
||
/** | ||
* @file world/action.h | ||
* | ||
* @author Kai Sterker | ||
* @brief Declares the object interaction class. | ||
*/ | ||
|
||
#ifndef WORLD_ACTION_H | ||
#define WORLD_ACTION_H | ||
|
||
#include "python/method.h" | ||
|
||
/** | ||
* Path to the object interaction scripts. | ||
*/ | ||
#define ACTION_DIR "schedules.obj." | ||
|
||
namespace world | ||
{ | ||
class character; | ||
class object; | ||
|
||
/** | ||
* This class is used to associate an action with a scenery | ||
* object. The action itself is implemented as the method of | ||
* a python script. Since the method is loaded from the python::pool, | ||
* it should be stateless as it is probably shared by more than | ||
* one object. | ||
*/ | ||
class action | ||
{ | ||
public: | ||
/** | ||
* Create a new, empty action. | ||
*/ | ||
action (); | ||
|
||
/** | ||
* Destructor. | ||
*/ | ||
~action (); | ||
|
||
/** | ||
* Initialize the action. | ||
* @param script name of file inside the action directory. | ||
* @param method name of the method implementing the action. | ||
* @param args optional arguments when calling the method. | ||
* @return true on success, false otherwise. | ||
*/ | ||
bool init (const std::string & script, const std::string & method, PyObject *args = NULL); | ||
|
||
#ifndef SWIG | ||
/** | ||
* Execute the action with the given character and object. | ||
* @param actor the character triggering the action. | ||
* @param target the object acted on. | ||
*/ | ||
void execute (world::character *actor, world::object *target); | ||
#endif | ||
|
||
/** | ||
* Execute the action with the given character and object. | ||
* | ||
* This method is optimized for being called from a Python | ||
* script, as it avoids boxing and unboxing of its arguments. | ||
* | ||
* @param actor the character triggering the action. | ||
* @param target the object acted on. | ||
*/ | ||
void execute (PyObject *actor, PyObject *target); | ||
|
||
/** | ||
* @name Loading / Saving | ||
*/ | ||
//@{ | ||
/** | ||
* Save the %action to a stream. | ||
* @param file stream where to save the %action. | ||
*/ | ||
void put_state (base::flat& file) const; | ||
|
||
/** | ||
* Loads the %action from a stream. | ||
* @param file stream to load the %action from. | ||
* @return \e true if the %schedule was loaded successfully, \e false otherwise. | ||
*/ | ||
bool get_state (base::flat& file); | ||
//@} | ||
|
||
#ifndef SWIG | ||
/// allow %action to be passed through SWIG | ||
GET_TYPE_NAME(world::action); | ||
#endif // SWIG | ||
|
||
private: | ||
/** | ||
* Reset the action. | ||
*/ | ||
void clear (); | ||
|
||
/// Forbid copy constructor | ||
action (const action & a); | ||
/// the action implementation | ||
python::method *Action; | ||
/// additional arguments for the action | ||
PyObject *Args; | ||
}; | ||
} | ||
|
||
#endif |
Oops, something went wrong.