Pol  Revision:3cfda13
chrituse.cpp
Go to the documentation of this file.
1 
7 #include <stddef.h>
8 
9 #include "../../bscript/bobject.h"
10 #include "../../bscript/eprog.h"
11 #include "../../clib/logfacility.h"
12 #include "../../clib/passert.h"
13 #include "../../clib/refptr.h"
14 #include "../../plib/systemstate.h"
15 #include "../item/item.h"
16 #include "../item/itemdesc.h"
17 #include "../module/osmod.h"
18 #include "../module/uomod.h"
19 #include "../scrdef.h"
20 #include "../scrsched.h"
21 #include "../scrstore.h"
22 #include "../uoexec.h"
23 #include "../uoscrobj.h"
24 #include "charactr.h"
25 
26 namespace Pol
27 {
28 namespace Mobile
29 {
30 bool Character::start_script( Bscript::EScriptProgram* prog, bool start_attached,
32  Bscript::BObjectImp* param4 )
33 {
34  if ( !( !start_attached || ( script_ex == NULL ) ) )
35  {
36  POLLOG << "Character::start_script hiccup\n"
37  << "Trying to start script " << prog->name.get() << "\n"
38  << "Script " << script_ex->scriptname() << " is already running\n";
39  return false; // if it's going to have a passert() error, just return false.
40  }
41  passert( !start_attached || ( script_ex == NULL ) );
42 
43  Bscript::BObject ob2( param2 ? param2 : Bscript::UninitObject::create() );
44  Bscript::BObject ob3( param3 ? param3 : Bscript::UninitObject::create() );
45  Bscript::BObject ob4( param4 ? param4 : Bscript::UninitObject::create() );
46 
47  std::unique_ptr<Core::UOExecutor> ex( Core::create_script_executor() );
48  auto uoemod = new Module::UOExecutorModule( *ex );
49  ex->addModule( uoemod );
50 
51  if ( prog->haveProgram )
52  {
53  if ( param4 )
54  ex->pushArg( param4 );
55  if ( param3 )
56  ex->pushArg( param3 );
57  if ( param2 )
58  ex->pushArg( param2 );
59  ex->pushArg( new Module::ECharacterRefObjImp( this ) );
60  }
61 
62  ex->os_module->priority = 100;
63 
64  if ( ex->setProgram( prog ) )
65  {
66  if ( start_attached )
67  {
68  script_ex = ex.get();
69  uoemod->attached_chr_ = this;
70  }
71  uoemod->controller_.set( this );
72  schedule_executor( ex.release() );
73  return true;
74  }
75  else
76  {
77  POLLOG << "Unable to setProgram(" << prog->name.get() << ")\n";
78  return false;
79  }
80 }
81 
83  bool start_attached )
84 {
85  return start_script( prog, start_attached, new Module::EItemRefObjImp( item ) );
86 }
87 }
88 namespace Items
89 {
91 {
92  const Items::ItemDesc& itemdesc = this->itemdesc();
93  if ( !itemdesc.walk_on_script.empty() )
94  {
96  prog = find_script2( itemdesc.walk_on_script,
97  true, // complain if not found
99  if ( prog.get() != NULL )
100  {
101  std::unique_ptr<Core::UOExecutor> ex( Core::create_script_executor() );
102  ex->addModule( new Module::UOExecutorModule( *ex ) );
103  if ( prog->haveProgram )
104  {
105  ex->pushArg( new Bscript::BLong( chr->lastz ) );
106  ex->pushArg( new Bscript::BLong( chr->lasty ) );
107  ex->pushArg( new Bscript::BLong( chr->lastx ) );
108  ex->pushArg( new Module::EItemRefObjImp( this ) );
109  ex->pushArg( new Module::ECharacterRefObjImp( chr ) );
110  }
111 
112  ex->os_module->priority = 100;
113 
114  if ( ex->setProgram( prog.get() ) )
115  {
116  schedule_executor( ex.release() );
117  }
118  }
119  }
120 }
121 }
122 }
bool empty() const
Definition: scrdef.h:41
virtual void walk_on(Mobile::Character *chr)
Definition: chrituse.cpp:90
ref_ptr< Bscript::EScriptProgram > find_script2(const ScriptDef &script, bool complain_if_not_found, bool cache_script)
Definition: scrstore.cpp:83
SystemState systemstate
Definition: systemstate.cpp:12
Core::PolConfig config
Definition: systemstate.h:43
Core::UOExecutor * script_ex
Definition: charactr.h:867
T * get() const
Definition: refptr.h:175
boost_utils::script_name_flystring name
Definition: eprog.h:91
bool start_itemuse_script(Bscript::EScriptProgram *prog, Items::Item *item, bool start_attached)
Definition: chrituse.cpp:82
UOExecutor * create_script_executor()
Definition: scrsched.cpp:644
Core::ScriptDef walk_on_script
Definition: itemdesc.h:96
static UninitObject * create()
Definition: bobject.h:482
#define passert(exp)
Definition: passert.h:62
#define POLLOG
Definition: logfacility.h:219
bool cache_interactive_scripts
Definition: polcfg.h:51
bool start_script(Bscript::EScriptProgram *prog, bool start_attached, Bscript::BObjectImp *param2=NULL, Bscript::BObjectImp *param3=NULL, Bscript::BObjectImp *param4=NULL)
Definition: chrituse.cpp:30
const std::string & scriptname() const
Definition: executor.h:413
void schedule_executor(UOExecutor *ex)
Definition: scrsched.cpp:662
Definition: berror.cpp:12