Pol  Revision:3cfda13
guildmod.cpp
Go to the documentation of this file.
1 
11 #include "guildmod.h"
12 
13 #include <stddef.h>
14 
15 #include "../../bscript/berror.h"
16 #include "../../bscript/bobject.h"
17 #include "../../bscript/executor.h"
18 #include "../../bscript/objmembers.h"
19 #include "../../bscript/objmethods.h"
20 #include "../../clib/compilerspecifics.h"
21 #include "../../clib/rawtypes.h"
22 #include "../fnsearch.h"
23 #include "../globals/uvars.h"
24 #include "../guilds.h"
25 #include "../mobile/charactr.h"
26 #include "../uoexhelp.h"
27 #include "../uoscrobj.h"
28 
29 namespace Pol
30 {
31 namespace Bscript
32 {
33 using namespace Module;
34 template <>
37  {"ListGuilds", &GuildExecutorModule::mf_ListGuilds},
38  {"CreateGuild", &GuildExecutorModule::mf_CreateGuild},
39  {"FindGuild", &GuildExecutorModule::mf_FindGuild},
40  {"DestroyGuild", &GuildExecutorModule::mf_DestroyGuild},
41 };
42 }
43 namespace Module
44 {
45 using namespace Bscript;
46 
71 class EGuildRefObjImp : public BApplicObj<Core::GuildRef>
72 {
73 public:
75  virtual const char* typeOf() const POL_OVERRIDE;
76  virtual u8 typeOfInt() const POL_OVERRIDE;
77  virtual BObjectImp* copy() const POL_OVERRIDE;
78  virtual bool isTrue() const POL_OVERRIDE;
79  virtual bool operator==( const BObjectImp& objimp ) const POL_OVERRIDE;
80 
81  virtual BObjectRef get_member( const char* membername ) POL_OVERRIDE;
82  virtual BObjectRef get_member_id( const int id ) POL_OVERRIDE; // id test
83  virtual BObjectImp* call_method( const char* methodname, Executor& ex ) POL_OVERRIDE;
84  virtual BObjectImp* call_method_id( const int id, Executor& ex,
85  bool forcebuiltin = false ) POL_OVERRIDE;
86 };
87 
89 
90 
92  : BApplicObj<Core::GuildRef>( &guild_type, gref ){};
93 
94 const char* EGuildRefObjImp::typeOf() const
95 {
96  return "GuildRef";
97 }
99 {
100  return OTGuildRef;
101 }
102 
104 {
105  return new EGuildRefObjImp( obj_ );
106 }
107 
109 {
110  return ( !obj_->_disbanded );
111 }
112 
113 bool EGuildRefObjImp::operator==( const BObjectImp& objimp ) const
114 {
115  if ( objimp.isa( BObjectImp::OTApplicObj ) )
116  {
117  const BApplicObjBase* aob =
118  Clib::explicit_cast<const BApplicObjBase*, const BObjectImp*>( &objimp );
119 
120  if ( aob->object_type() == &guild_type )
121  {
122  const EGuildRefObjImp* guildref_imp =
123  Clib::explicit_cast<const EGuildRefObjImp*, const BApplicObjBase*>( aob );
124 
125  return ( guildref_imp->obj_->_guildid == obj_->_guildid );
126  }
127  else
128  return false;
129  }
130  else if ( objimp.isa( BObjectImp::OTBoolean ) )
131  return isTrue() == static_cast<const BBoolean&>( objimp ).isTrue();
132  else
133  return false;
134 }
135 
137 {
138  return new EGuildRefObjImp( ref_ptr<Core::Guild>( guild ) );
139 }
140 
141 bool getGuildParam( Executor& exec, unsigned param, Core::Guild*& guild, BError*& err )
142 {
143  BApplicObjBase* aob = NULL;
144  if ( exec.hasParams( param + 1 ) )
145  aob = exec.getApplicObjParam( param, &guild_type );
146 
147  if ( aob == NULL )
148  {
149  err = new BError( "Invalid parameter type" );
150  return false;
151  }
152 
153  EGuildRefObjImp* gr = static_cast<EGuildRefObjImp*>( aob );
154  guild = gr->value().get();
155  if ( guild->disbanded() )
156  {
157  err = new BError( "Guild has disbanded" );
158  return false;
159  }
160  return true;
161 }
162 
163 BObjectRef EGuildRefObjImp::get_member_id( const int id ) // id test
164 {
165  if ( obj_->_disbanded )
166  return BObjectRef( new BError( "Guild has disbanded" ) );
167 
168  switch ( id )
169  {
170  case MBR_MEMBERS:
171  {
172  std::unique_ptr<ObjArray> arr;
173  arr.reset( new ObjArray );
174  for ( Core::SerialSet::iterator itr = obj_->_member_serials.begin();
175  itr != obj_->_member_serials.end();
176  /* do this earlier */ )
177  {
178  unsigned int mserial = ( *itr );
179  Core::SerialSet::iterator last_itr = itr;
180  ++itr;
181 
183  if ( chr != NULL )
184  {
185  arr->addElement( new EOfflineCharacterRefObjImp( chr ) );
186  }
187  else
188  {
189  obj_->_member_serials.erase( last_itr );
190  }
191  }
192  return BObjectRef( arr.release() );
193  }
194 
195  case MBR_ALLYGUILDS:
196  {
197  std::unique_ptr<ObjArray> arr;
198  arr.reset( new ObjArray );
199  for ( Core::SerialSet::iterator itr = obj_->_allyguild_serials.begin();
200  itr != obj_->_allyguild_serials.end();
201  /* do this earlier */ )
202  {
203  unsigned int gserial = ( *itr );
204  Core::SerialSet::iterator last_itr = itr;
205  ++itr;
206 
207  Core::Guild* guild = Core::Guild::FindGuild( gserial );
208 
209  if ( guild != NULL )
210  {
211  arr->addElement( new EGuildRefObjImp( ref_ptr<Core::Guild>( guild ) ) );
212  }
213  else
214  {
215  obj_->_allyguild_serials.erase( last_itr );
216  }
217  }
218  return BObjectRef( arr.release() );
219  }
220 
221  case MBR_ENEMYGUILDS:
222  {
223  std::unique_ptr<ObjArray> arr;
224  arr.reset( new ObjArray );
225  for ( Core::SerialSet::iterator itr = obj_->_enemyguild_serials.begin();
226  itr != obj_->_enemyguild_serials.end();
227  /* do this earlier */ )
228  {
229  unsigned int gserial = ( *itr );
230  Core::SerialSet::iterator last_itr = itr;
231  ++itr;
232 
233  Core::Guild* guild = Core::Guild::FindGuild( gserial );
234 
235  if ( guild != NULL )
236  {
237  arr->addElement( new EGuildRefObjImp( ref_ptr<Core::Guild>( guild ) ) );
238  }
239  else
240  {
241  obj_->_enemyguild_serials.erase( last_itr );
242  }
243  }
244  return BObjectRef( arr.release() );
245  }
246 
247  case MBR_GUILDID:
248  return BObjectRef( new BLong( obj_->_guildid ) );
249  default:
250  return BObjectRef( UninitObject::create() );
251  }
252 }
253 BObjectRef EGuildRefObjImp::get_member( const char* membername )
254 {
255  if ( obj_->_disbanded )
256  return BObjectRef( new BError( "Guild has disbanded" ) );
257 
258  ObjMember* objmember = getKnownObjMember( membername );
259  if ( objmember != NULL )
260  return this->get_member_id( objmember->id );
261  else
262  return BObjectRef( UninitObject::create() );
263 }
264 
265 BObjectImp* EGuildRefObjImp::call_method_id( const int id, Executor& ex, bool /*forcebuiltin*/ )
266 {
267  if ( obj_->_disbanded )
268  return new BError( "Guild has disbanded" );
269 
270  switch ( id )
271  {
272  case MTH_ISMEMBER:
273  {
274  if ( !ex.hasParams( 1 ) )
275  return new BError( "Not enough parameters" );
276  Mobile::Character* chr;
277  if ( !getCharacterParam( ex, 0, chr ) )
278  return new BError( "Invalid parameter type" );
279 
280  return new BLong( static_cast<int>( obj_->_member_serials.count( chr->serial ) ) );
281  }
282 
283  case MTH_ISALLYGUILD:
284  {
285  if ( !ex.hasParams( 1 ) )
286  return new BError( "Not enough parameters" );
287  Core::Guild* allyguild;
288  BError* err;
289  if ( getGuildParam( ex, 0, allyguild, err ) )
290  return new BLong(
291  static_cast<int>( obj_->_allyguild_serials.count( allyguild->guildid() ) ) );
292  else
293  return err;
294  }
295 
296  case MTH_ISENEMYGUILD:
297  {
298  if ( !ex.hasParams( 1 ) )
299  return new BError( "Not enough parameters" );
300  Core::Guild* enemyguild;
301  BError* err;
302  if ( getGuildParam( ex, 0, enemyguild, err ) )
303  return new BLong(
304  static_cast<int>( obj_->_enemyguild_serials.count( enemyguild->guildid() ) ) );
305  else
306  return err;
307  }
308 
309  case MTH_ADDMEMBER:
310  {
311  if ( !ex.hasParams( 1 ) )
312  return new BError( "Not enough parameters" );
313  Mobile::Character* chr;
314  if ( !getCharacterParam( ex, 0, chr ) )
315  return new BError( "Invalid parameter type" );
316  if ( chr->guildid() )
317  return new BError( "Character already belongs to a guild" );
318 
319  chr->guild( obj_.get() );
320  obj_->_member_serials.insert( chr->serial );
321 
322  // MuadDib Added to update online members when status changes.
323  obj_->update_online_members();
324 
325  return new BLong( 1 );
326  }
327 
328  case MTH_ADDALLYGUILD:
329  {
330  if ( !ex.hasParams( 1 ) )
331  return new BError( "Not enough parameters" );
332  Core::Guild* allyguild;
333  BError* err;
334  if ( getGuildParam( ex, 0, allyguild, err ) )
335  {
336  if ( obj_->_enemyguild_serials.count( allyguild->guildid() ) )
337  return new BError( "That is an enemy guild" );
338  if ( allyguild->guildid() == obj_->guildid() )
339  return new BError( "Passed self as new ally guild" );
340 
341  obj_->_allyguild_serials.insert( allyguild->guildid() );
342  allyguild->_allyguild_serials.insert( obj_->_guildid );
343 
344  // MuadDib Added to update online members when status changes.
345  obj_->update_online_members();
346  allyguild->update_online_members();
347 
348  return new BLong( 1 );
349  }
350  else
351  return err;
352  }
353 
354  case MTH_ADDENEMYGUILD:
355  {
356  if ( !ex.hasParams( 1 ) )
357  return new BError( "Not enough parameters" );
358  Core::Guild* enemyguild;
359  BError* err;
360  if ( getGuildParam( ex, 0, enemyguild, err ) )
361  {
362  if ( obj_->_allyguild_serials.count( enemyguild->guildid() ) )
363  return new BError( "That is an ally guild" );
364  if ( enemyguild->guildid() == obj_->guildid() )
365  return new BError( "Passed self as new enemy guild" );
366 
367  obj_->_enemyguild_serials.insert( enemyguild->guildid() );
368  enemyguild->_enemyguild_serials.insert( obj_->_guildid );
369 
370  // MuadDib Added to update online members when status changes.
371  obj_->update_online_members();
372  enemyguild->update_online_members();
373 
374  return new BLong( 1 );
375  }
376  else
377  return err;
378  }
379 
380  case MTH_REMOVEMEMBER:
381  {
382  if ( !ex.hasParams( 1 ) )
383  return new BError( "Not enough parameters" );
384  Mobile::Character* chr;
385  if ( !getCharacterParam( ex, 0, chr ) )
386  return new BError( "Invalid parameter type" );
387  if ( chr->guildid() != obj_->_guildid )
388  return new BError( "Character does not belong to this guild" );
389 
390  chr->guild( NULL );
391  obj_->_member_serials.erase( chr->serial );
392 
393  // MuadDib Added to update online members when status changes.
394  obj_->update_online_members_remove( chr );
395 
396  return new BLong( 1 );
397  }
398 
399  case MTH_REMOVEALLYGUILD:
400  {
401  if ( !ex.hasParams( 1 ) )
402  return new BError( "Not enough parameters" );
403  Core::Guild* allyguild;
404  BError* err;
405  if ( getGuildParam( ex, 0, allyguild, err ) )
406  {
407  if ( !obj_->_allyguild_serials.count( allyguild->guildid() ) )
408  return new BError( "That is not an ally guild" );
409 
410  obj_->_allyguild_serials.erase( allyguild->guildid() );
411  allyguild->_allyguild_serials.erase( obj_->_guildid );
412 
413  // MuadDib Added to update online members when status changes.
414  obj_->update_online_members();
415  allyguild->update_online_members();
416 
417  return new BLong( 1 );
418  }
419  else
420  return err;
421  }
422 
424  {
425  if ( !ex.hasParams( 1 ) )
426  return new BError( "Not enough parameters" );
427  Core::Guild* enemyguild;
428  BError* err;
429  if ( getGuildParam( ex, 0, enemyguild, err ) )
430  {
431  if ( !obj_->_enemyguild_serials.count( enemyguild->guildid() ) )
432  return new BError( "That is not an enemy guild" );
433 
434  obj_->_enemyguild_serials.erase( enemyguild->guildid() );
435  enemyguild->_enemyguild_serials.erase( obj_->_guildid );
436 
437  // MuadDib Added to update online members when status changes.
438  obj_->update_online_members();
439  enemyguild->update_online_members();
440 
441  return new BLong( 1 );
442  }
443  else
444  return err;
445  }
446  default:
447  {
448  bool changed = false;
449  return CallPropertyListMethod_id( obj_->_proplist, id, ex, changed );
450  }
451  }
452 }
453 
454 BObjectImp* EGuildRefObjImp::call_method( const char* methodname, Executor& ex )
455 {
456  if ( obj_->_disbanded )
457  return new BError( "Guild has disbanded" );
458 
459  ObjMethod* objmethod = getKnownObjMethod( methodname );
460  if ( objmethod != NULL )
461  return this->call_method_id( objmethod->id, ex );
462  else
463  {
464  bool changed = false;
465  return CallPropertyListMethod( obj_->_proplist, methodname, ex, changed );
466  }
467 }
468 
469 
471  : TmplExecutorModule<GuildExecutorModule>( "Guilds", exec )
472 {
473 }
474 
477 {
478  std::unique_ptr<ObjArray> result( new ObjArray );
479  for ( Core::Guilds::iterator itr = Core::gamestate.guilds.begin(),
480  end = Core::gamestate.guilds.end();
481  itr != end; ++itr )
482  {
483  Core::Guild* guild = ( *itr ).second.get();
484  result->addElement( new EGuildRefObjImp( Core::GuildRef( guild ) ) );
485  }
486  return result.release();
487 }
488 
491 {
492  Core::Guild* guild = new Core::Guild( Core::gamestate.nextguildid++ );
493 
494  Core::gamestate.guilds[guild->guildid()].set( guild );
495 
496  return new EGuildRefObjImp( ref_ptr<Core::Guild>( guild ) );
497 }
498 
500 {
501  Core::Guild* guild;
502  BError* err;
503  if ( getGuildParam( exec, 0, guild, err ) )
504  {
505  if ( guild->hasMembers() )
506  return new BError( "Guild has members" );
507  if ( guild->hasAllies() )
508  return new BError( "Guild has allies" );
509  if ( guild->hasEnemies() )
510  return new BError( "Guild has enemies" );
511 
512  guild->disband();
513  Core::gamestate.guilds.erase( guild->guildid() );
514  return new BLong( 1 );
515  }
516  else
517  {
518  return err;
519  }
520 }
521 
524 {
525  int guildid;
526  if ( getParam( 0, guildid ) )
527  {
528  Core::Guilds::iterator itr = Core::gamestate.guilds.find( guildid );
529  if ( itr != Core::gamestate.guilds.end() )
530  return new EGuildRefObjImp( ref_ptr<Core::Guild>( ( *itr ).second.get() ) );
531  else
532  return new BError( "Guild not found" );
533  }
534  else
535  {
536  return new BError( "Invalid parameter type" );
537  }
538 }
539 }
540 }
static Guild * FindGuild(unsigned int guildid)
Definition: guilds.cpp:254
unsigned char u8
Definition: rawtypes.h:25
#define POL_OVERRIDE
unsigned int guildid() const
Definition: charactr.cpp:4164
Bscript::BObjectImp * mf_DestroyGuild()
Definition: guildmod.cpp:499
virtual const char * typeOf() const POL_OVERRIDE
Definition: guildmod.cpp:94
bool isa(BObjectType type) const
Definition: bobject.h:353
virtual BObjectImp * copy() const POL_OVERRIDE
Definition: guildmod.cpp:103
BApplicObjType guild_type
Definition: guildmod.cpp:88
virtual BObjectRef get_member(const char *membername) POL_OVERRIDE
Definition: guildmod.cpp:253
T * get() const
Definition: refptr.h:175
ObjMember * getKnownObjMember(const char *token)
Definition: parser.cpp:483
bool getParam(unsigned param, int &value)
Definition: execmodl.cpp:62
bool operator==(T *ptr, const ref_ptr< T > &rptr)
Definition: refptr.h:310
Bscript::BObjectImp * mf_ListGuilds()
ListGuilds returns an array of Guild objects.
Definition: guildmod.cpp:476
unsigned int guildid() const
Definition: guilds.cpp:123
EGuildRefObjImp(Core::GuildRef gref)
Definition: guildmod.cpp:91
GuildExecutorModule(Bscript::Executor &exec)
Definition: guildmod.cpp:470
virtual BObjectImp * call_method(const char *methodname, Executor &ex) POL_OVERRIDE
Definition: guildmod.cpp:454
ObjMethod * getKnownObjMethod(const char *token)
Definition: parser.cpp:665
Guild Object Properties: guild.guildid : integer guild.members : array of offline mobilerefs guild...
Definition: guildmod.cpp:71
const BApplicObjType * object_type() const
Definition: bobject.h:897
bool getCharacterParam(Bscript::Executor &exec, unsigned param, Mobile::Character *&chrptr)
Definition: uoexhelp.cpp:140
Bscript::BObjectImp * CallPropertyListMethod_id(PropertyList &proplist, const int id, Bscript::Executor &ex, bool &changed)
Definition: proplist.cpp:520
bool hasEnemies() const
Definition: guilds.cpp:152
bool disbanded() const
Definition: guilds.cpp:128
Bscript::BObjectImp * mf_CreateGuild()
CreateGuild returns a new Guild object.
Definition: guildmod.cpp:490
Definition: refptr.h:64
SerialSet _allyguild_serials
Definition: guilds.h:68
static UninitObject * create()
Definition: bobject.h:482
bool hasAllies() const
Definition: guilds.cpp:143
GameState gamestate
Definition: uvars.cpp:74
bool getGuildParam(Executor &exec, unsigned param, Core::Guild *&guild, BError *&err)
Definition: guildmod.cpp:141
Mobile::Character * system_find_mobile(u32 serial)
Definition: fnsearch.cpp:32
void update_online_members()
Definition: guilds.cpp:83
BApplicObjBase * getApplicObjParam(unsigned param, const BApplicObjType *object_type)
Definition: executor.cpp:488
virtual bool isTrue() const POL_OVERRIDE
Definition: guildmod.cpp:108
bool hasMembers() const
Definition: guilds.cpp:138
void disband()
Definition: guilds.cpp:133
D explicit_cast(const S &s)
Definition: stlutil.h:40
Bscript::BObjectImp * mf_FindGuild()
FindGuild( guildid );.
Definition: guildmod.cpp:523
SerialSet _enemyguild_serials
Definition: guilds.h:69
bool hasParams(unsigned howmany) const
Definition: executor.h:144
virtual u8 typeOfInt() const POL_OVERRIDE
Definition: guildmod.cpp:98
Bscript::BObjectImp * CallPropertyListMethod(PropertyList &proplist, const char *methodname, Bscript::Executor &ex, bool &changed)
Definition: proplist.cpp:591
virtual BObjectRef get_member_id(const int id) POL_OVERRIDE
Definition: guildmod.cpp:163
static Bscript::BObjectImp * CreateGuildRefObjImp(Core::Guild *guild)
Definition: guildmod.cpp:136
virtual BObjectImp * call_method_id(const int id, Executor &ex, bool forcebuiltin=false) POL_OVERRIDE
Definition: guildmod.cpp:265
Definition: berror.cpp:12
virtual bool operator==(const BObjectImp &objimp) const POL_OVERRIDE
Definition: guildmod.cpp:113