Skip to content

Commit

Permalink
ADAPTED the item loading system to groups
Browse files Browse the repository at this point in the history
CREATED groupstest.py
  • Loading branch information
Frederico authored and Frederico committed Oct 7, 2009
1 parent ceb0f42 commit 6d11d42
Show file tree
Hide file tree
Showing 9 changed files with 268 additions and 43 deletions.
14 changes: 9 additions & 5 deletions src/rpg/faction.cc
Expand Up @@ -21,7 +21,7 @@

using namespace rpg;

std::set<faction *, order_by_name> faction::Factions;
std::vector<faction *> faction::Factions;

bool faction::verify_requirements() const
{
Expand Down Expand Up @@ -50,9 +50,13 @@ void faction::cleanup()

faction * faction::get_faction(const std::string & name)
{
faction tmp(name);

std::set<faction *>::iterator i = Factions.find(&tmp);
std::vector<faction *>::iterator i;

for (i = Factions.begin(); i != Factions.end(); i++)
{
if ((*i)->name() == name)
return (*i);
}

return (i != Factions.end()) ? (*i) : NULL;
return NULL;
}
6 changes: 3 additions & 3 deletions src/rpg/faction.h
Expand Up @@ -28,7 +28,7 @@
#ifndef FACTION_H
#define FACTION_H

#include <set>
#include <vector>
#include "rpg/group.h"

namespace rpg
Expand All @@ -46,7 +46,7 @@ namespace rpg
// Update the path to the python modules
set_group_package("groups.factions.");

Factions.insert(this);
Factions.push_back(this);
}

/**
Expand All @@ -71,7 +71,7 @@ namespace rpg
private:

/// List of all available factions
static std::set<faction *, order_by_name> Factions;
static std::vector<faction *> Factions;
};
}
#endif
79 changes: 79 additions & 0 deletions src/rpg/group.cc
Expand Up @@ -64,3 +64,82 @@ s_int32 group::estimate_speed(const std::string & terrain) const

return ret_int;
}

bool group::put_state (const string & file, const base::diskio::file_format & format) const
{
// try to save group
base::diskio record (format);
if (!put_state (record))
{
fprintf (stderr, "*** group::put_state: saving '%s' failed!\n", file.c_str ());
return false;
}

// write group to disk
return record.put_record (file);
}


bool group::put_state (base::flat & file) const
{
base::flat record;

// save the attributes
record.put_string ("gnm", Name);

// save the template this group uses
record.put_string ("gcn", class_name ());

// pass record
PyObject *args = PyTuple_New (1);
PyTuple_SetItem (args, 0, python::pass_instance ((base::flat*) &record));

// save the actual group data
call_method ("put_state", args);
Py_DECREF (args);


file.put_flat ("grp", record);
return true;
}

// load a single group from file
bool group::get_state(const string & file)
{
// try to load group
base::diskio record (base::diskio::BY_EXTENSION);

if (record.get_record (file))
return get_state (record);

return false;
}

bool group::get_state(base::flat & file)
{
base::flat record = file.get_flat ("grp");
if (!file.success ()) return false;


// get attributes
set_name(record.get_string("gnm"));

// get template to use for group
std::string tmpl = record.get_string("gcn");

// instanciate
if (!create_instance (tmpl)) return false;

// pass file
PyObject *args = PyTuple_New (1);
PyTuple_SetItem (args, 0, python::pass_instance (&record));

// load actual group data
call_method ("get_state", args);
Py_DECREF (args);

// add reference to item
set_attribute ("this", python::pass_instance (this));

return record.success ();
}
58 changes: 50 additions & 8 deletions src/rpg/group.h
Expand Up @@ -28,17 +28,10 @@
#define GROUP_H

#include "python/script.h"
#include "base/diskio.h"

namespace rpg
{
struct order_by_name
{
template <class T>
bool operator()(T * i, T * a) const
{
return (i->name() < a->name());
}
};

/**
* This class is the base for every other class that represents a group of
Expand Down Expand Up @@ -103,6 +96,55 @@ namespace rpg
{
Group_Package = group_package;
}

/**
* @name Loading/Saving
*/
//@{
/**
* Load group from named file. This will first load the %group template
* to instanciate the underlying Python group class. Then it will
* restore the actual %group data. If an group is already instanciated,
* it will be replaced.
*
* @param file name of the file to load %group from.
* @return \b true if loading successful, \b false otherwise.
*/
bool get_state (base::flat & file);

/**
* Load %group from stream. This will first load the %group template
* to instanciate the underlying Python group class. Then it will
* restore the actual %group data. If an group is already instanciated,
* it will be replaced.
*
* @param file stream to load group from.
* @return \b true if loading successful, \b false otherwise.
*/
bool get_state (const std::string & file);

/**
* Save %group to named file. This will save both the grou+ template
* plus the actual data to the given file. The file will be replaced
* if it already exists.
*
* @param file name of the file to save %group to.
* @param format whether to save as XML or compressed binary.
* @return \b true if saving successful, \b false otherwise.
*/
bool put_state (const string & file, const base::diskio::file_format & format = base::diskio::BY_EXTENSION) const;

/**
* Save %group to stream. This will save both the %group template
* plus the actual data to the given file. The file will be replaced
* if it already exists.
*
* @param file stream to save %group to.
* @return \b true if saving successful, \b false otherwise.
*/
bool put_state (base::flat & file) const;


private:

/// The name of this group
Expand Down
15 changes: 9 additions & 6 deletions src/rpg/specie.cc
Expand Up @@ -21,7 +21,7 @@

using namespace rpg;

std::set<specie *, order_by_name> specie::Species;
std::vector<specie *> specie::Species;

s_int8 specie::alignment() const
{
Expand All @@ -39,9 +39,12 @@ void specie::cleanup()

specie * specie::get_specie(const std::string & name)
{
specie tmp(name);

std::set<specie *>::iterator i = Species.find(&tmp);

return (i != Species.end()) ? (*i) : NULL;
std::vector<specie *>::iterator i;

for (i = Species.begin(); i != Species.end(); i++)
{
if ((*i)->name() == name)
return (*i);
}
return NULL;
}
10 changes: 6 additions & 4 deletions src/rpg/specie.h
Expand Up @@ -28,7 +28,7 @@
#ifndef SPECIE_H
#define SPECIE_H

#include <set>
#include <vector>
#include "rpg/group.h"

namespace rpg
Expand All @@ -45,8 +45,10 @@ namespace rpg
{
// Update the path to the python modules
set_group_package("groups.species.");

Species.insert(this);

#ifndef SWIG
Species.push_back(this);
#endif
}

/**
Expand Down Expand Up @@ -79,7 +81,7 @@ namespace rpg
s_int8 Alignment;

/// List of all available species
static std::set<specie *, order_by_name> Species;
static std::vector<specie *> Species;
};
}
#endif
51 changes: 37 additions & 14 deletions test/data/groups/group.py
Expand Up @@ -16,6 +16,7 @@
# along with Adonthell; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#
from adonthell.base import flat

class group (object):
"""
Expand All @@ -28,24 +29,46 @@ def __init__ (self):
"""ctor"""
# -- reference to the underlying rpg.group instance
self.this = None

# -- Group Name
self.Name = ""

# -- Dictionary with relation between the terrain and respective
# -- imapct in speed
self.Dic = {}


def destroy (self):
"""properly delete a group"""
if self.this != None:
self.this.destroy ()

def estimate_speed (self, terrain):
""" returns a percentage depending on how nicely this group
and the terrain interact.
@param terrain the type of terrain we want to interact with
"""
return 0 # -- there's no effect

def estimate_speed_for_pathfinding (self, terrain):
""" similar to estimate_speed but now the result is going to
be used in all the pathfinding searchs. Therefore adding
special cases here can mold the behaviour of a NPC.
For example a noble NPC won't cross a swamp unless he is under attack
@param terrain the type of terrain we want to interact with
"""
return self.estimate_speed(terrain)
try:
return self.Dic[terrain]
except: return 0

# -- save item to disk
# record needs to be of type base.flat
def put_state (self, record):
record.put_string("pgpn", self.Name)

terrains = flat()
for terrain in self.Dic:
terrains.put_sint8(terrain, self.Dic[terrain])

record.put_flat ("gTerr", terrains)

# -- load item from disk
# record needs to be of type base.flat
def get_state (self, record):
self.Name = record.get_string("pgpn")

terrains = record.get_flat ("gTerr")

type, value, unused, key = terrains.next ()
while type == flat.T_SINT8:
self.Dic[key] = value
type, value, unused, key = terrains.next ()


0 comments on commit 6d11d42

Please sign in to comment.