1 // Ryzom - MMORPG Framework <http://dev.ryzom.com/projects/ryzom/>
2 // Copyright (C) 2010 Winch Gate Property Limited
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU Affero General Public License as
6 // published by the Free Software Foundation, either version 3 of the
7 // License, or (at your option) any later version.
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU Affero General Public License for more details.
14 // You should have received a copy of the GNU Affero General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
19 #ifndef RYAI_AIS_ACTIONS_H
20 #define RYAI_AIS_ACTIONS_H
22 #include "ai_share/ai_actions.h"
26 #include "ai_mgr_fauna.h"
27 #include "ai_mgr_npc.h"
30 #include "ai_grp_fauna.h"
31 #include "ai_grp_npc.h"
34 #include "ai_bot_fauna.h"
35 #include "ai_bot_npc.h"
38 #include "state_chat.h"
40 #include "event_reaction_container.h"
41 #include "continent.h"
43 namespace CAISActionEnums
45 //----------------------------------------------------------------------------
46 // contexts for the command set
64 ContextEventContainer
,
67 ContextPositionalState
,
86 ContextSquadTemplateVariant
,
90 ContextOutpostSquadFamily
,
91 ContextOutpostGroupDesc
,
92 ContextOutpostBotDesc
,
97 inline TContext
parentContext(TContext context
)
99 #define NO_PARENT return ContextNumContexts;
102 case ContextGlobal
: NO_PARENT
103 case ContextEventContainer
: NO_PARENT
104 case BaseContextMgr
: return ContextEventContainer
;
105 case ContextFaunaMgr
: return BaseContextMgr
;
106 case ContextNpcMgr
: return BaseContextMgr
;
107 case ContextNpcMgrKaravan
: return ContextNpcMgr
;
108 case ContextNpcMgrTribe
: return ContextNpcMgr
;
109 case ContextFaunaGrp
: NO_PARENT
110 case ContextNpcGrp
: NO_PARENT
111 case ContextNpcGrpKaravan
: return ContextNpcGrp
;
112 case ContextNpcBot
: NO_PARENT
113 case BaseContextState
: NO_PARENT
114 case ContextPositionalState
: return BaseContextState
;
115 case ContextPunctualState
: return BaseContextState
;
116 case ContextStateProfile
: NO_PARENT
117 case ContextStateChat
: NO_PARENT
118 case ContextContinent
: NO_PARENT
119 case ContextRegion
: return ContextContinent
;
120 case ContextCellZone
: return ContextRegion
;
121 case ContextCell
: return ContextCellZone
;
122 case ContextFaunaZone
: return ContextCell
;
123 case ContextNpcZone
: return ContextCell
;
124 case ContextRoad
: return ContextNpcZone
;
125 case ContextRoadTrigger
: return ContextRoad
;
126 case ContextGroupDesc
: return ContextGroupFamily
;
127 case ContextBotDesc
: return ContextGroupDesc
;
128 case ContextOutpost
: NO_PARENT
129 case ContextOutpostCharge
: return ContextOutpost
;
130 case ContextOutpostSquadFamily
: return ContextOutpost
;
131 case ContextOutpostGroupDesc
: return ContextOutpostSquadFamily
;
132 case ContextOutpostBotDesc
: return ContextOutpostGroupDesc
;
133 case ContextGroupFamily
: return ContextRegion
;
136 nlwarning("parentContext(context): Unknown context");
145 class CAISActions
: public CAIActions::IExecutor
148 //----------------------------------------------------------------------------
153 if (CAISActions::Instance
==NULL
)
154 CAISActions::Instance
=new CAISActions
;
155 CAIActions::init(Instance
);
157 static void release()
159 CAIActions::release();
163 //----------------------------------------------------------------------------
164 // inheritted virtual interface
166 virtual void openFile(const std::string
&fileName
);
167 virtual void closeFile(const std::string
&fileName
);
169 virtual void execute(uint64 action
,const std::vector
<CAIActions::CArg
> &args
);
171 virtual void begin(uint32 contextAlias
);
172 virtual void end(uint32 contextAlias
);
176 //----------------------------------------------------------------------------
179 // the 'actionMap' (map of action ids to handlers)
180 class IActionHandler
;
181 typedef std::map
<uint64
,IActionHandler
*> TActionMap
;
182 TActionMap ActionMap
[CAISActionEnums::ContextNumContexts
];
184 //----------------------------------------------------------------------------
185 // the actoin handler strucure and a macro for defining handlers easily
189 IActionHandler(CAISActionEnums::TContext context
, const char *name
)
191 if (CAISActions::Instance
==NULL
)
192 CAISActions::Instance
=new CAISActions
;
196 for (i
=0;i
<8 && name
[i
]!=0;++i
)
197 ((char *)&id
)[i
]=name
[i
];
199 // the following assert ensures that we never try to cram >8 letters into an int64
200 nlassert(name
[i
]==0);
201 CAISActions::Instance
->ActionMap
[context
][id
]=this;
204 virtual const char *name()=0;
205 virtual void operator()(const std::vector
<CAIActions::CArg
> &args
)=0;
209 #define DEFINE_ACTION(context,cmdName) \
210 struct CActionHandler_##context##_##cmdName : public CAISActions::IActionHandler \
212 CActionHandler_##context##_##cmdName() \
213 : CAISActions::IActionHandler(CAISActionEnums::context, #cmdName) { } \
214 virtual char const* name() { return #cmdName; } \
215 virtual void operator()(std::vector<CAIActions::CArg> const& args); \
217 static CActionHandler_##context##_##cmdName ActionHandler_##context##_##cmdName; \
218 void CActionHandler_##context##_##cmdName::operator()(std::vector<CAIActions::CArg> const& args)
220 #define DEFINE_ACTION_TEMPLATE1(context,cmdName,templateTypeName1) \
221 template <typename templateTypeName1> \
222 struct CActionHandler_##context##_##cmdName : public CAISActions::IActionHandler \
225 CActionHandler_##context##_##cmdName(char const* name) \
226 : CAISActions::IActionHandler(CAISActionEnums::context, name), _Name(name) { } \
227 virtual char const* name() { return _Name; } \
228 virtual void operator()(std::vector<CAIActions::CArg> const& args); \
230 template <typename templateTypeName1> \
231 void CActionHandler_##context##_##cmdName<templateTypeName1>::operator()(std::vector<CAIActions::CArg> const& args)
232 // cmdName + typeId should not exceed 8 characters long
233 #define DEFINE_ACTION_TEMPLATE1_INSTANCE(context,cmdName,typeId,typeName1) \
234 static CActionHandler_##context##_##cmdName<typeName1> ActionHandler_##context##_##cmdName(#cmdName#typeId)
236 //----------------------------------------------------------------------------
237 // the singleton class instance
238 static CAISActions
*Instance
;
242 //----------------------------------------------------------------------------
244 //----------------------------------------------------------------------------
252 Stack
.push_back(CAISActionEnums::ContextGlobal
);
255 static void push(CAISActionEnums::TContext context
)
257 if (!initialised()) init();
258 Stack
.push_back(context
);
261 static CAISActionEnums::TContext
topContext()
263 if (!initialised()) init();
264 return context(size()-1);
267 static CAISActionEnums::TContext
context(uint i
)
269 if (!initialised()) init();
274 static void setContext(CAISActionEnums::TContext context
)
276 nlassert(initialised());
277 nlassert(!Stack
.empty());
279 Stack
.back() = context
;
284 if (!initialised()) init();
285 return (uint
)Stack
.size();
290 nlassert(Stack
.size()>1);
295 static bool initialised()
297 return !Stack
.empty();
300 static std::vector
<CAISActionEnums::TContext
> Stack
;
303 //----------------------------------------------------------------------------
304 // Pointers to managers, groups etc currently being worked on
305 //----------------------------------------------------------------------------
310 static void aiInstance(CAIInstance
*instance
) { Instance
=instance
; }
311 static CAIInstance
*aiInstance() { nlassert(Instance
); return Instance
; }
313 static void mgr(CManager
*m
) { Mgr
=m
; }
314 static CManager
*mgr() { return Mgr
; }
316 static CMgrFauna
*mgrFauna() { return dynamic_cast<CMgrFauna
*>(Mgr
); }
317 static CMgrNpc
*mgrNpc() { return dynamic_cast<CMgrNpc
*>(Mgr
); }
318 static COutpostManager
*mgrOutpost() { return dynamic_cast<COutpostManager
*>(Mgr
); }
320 static void grp(CGroup
* g
) { Grp
=g
; }
321 static CGroup
*grp() { return Grp
; }
322 static CGrpFauna
*grpFauna() { return dynamic_cast<CGrpFauna
*>(Grp
); }
323 static CGroupNpc
*grpNpc() { return dynamic_cast<CGroupNpc
*>(Grp
); }
325 static void bot(CBot
*b
) { Bot
=b
; }
326 static CBot
*bot() { return Bot
; }
327 static CBotFauna
*botFauna() { return dynamic_cast<CBotFauna
*>(Bot
); }
328 static CBotNpc
*botNpc() { return dynamic_cast<CBotNpc
*>(Bot
); }
330 static void stateState(CAIState
*s
) { State
=s
; }
331 static CAIState
*stateState() { return State
; }
333 static void eventReactionContainer(CStateMachine
*eventContainer
) { EventContainer
=eventContainer
; }
334 static CStateMachine
*eventReactionContainer() { return EventContainer
; }
337 static void stateProfile(CAIStateProfile
*p
) { Profile
=p
; }
338 static CAIStateProfile
*stateProfile() { return Profile
; }
340 static void stateChat(CAIStateChat
*p
) { Chat
=p
; }
341 static CAIStateChat
*stateChat() { return Chat
; }
343 static void continent(CContinent
*c
) { Continent
= c
;}
344 static CContinent
*continent() { return Continent
;}
345 static void region(CRegion
*r
) { Region
= r
;}
346 static CRegion
*region() { return Region
;}
348 static void groupFamily(CGroupFamily
*gf
) { GroupFamily
= gf
;}
349 static CGroupFamily
*groupFamily() { return GroupFamily
;}
352 static void cellZone(CCellZone
*c
) { CellZone
= c
;}
353 static CCellZone
*cellZone() { return CellZone
;}
354 static void cell(CCell
*c
) { Cell
= c
;}
355 static CCell
*cell() { return Cell
;}
356 static void faunaZone(CFaunaZone
*z
) { FaunaZone
= z
;}
357 static CFaunaZone
*faunaZone() { return FaunaZone
;}
358 static void npcZone(CNpcZone
*z
) { NpcZone
= z
;}
359 static CNpcZone
*npcZone() { return NpcZone
;}
360 static void road(CRoad
*r
) { Road
= r
;}
361 static CRoad
*road() { return Road
;}
362 static void roadTrigger(CRoadTrigger
*t
) { RoadTrigger
= t
;}
363 static CRoadTrigger
*roadTrigger() { return RoadTrigger
;}
365 static void groupDesc(CGroupDesc<CGroupFamily> *d) { GroupDesc = d;}
366 static CGroupDesc<CGroupFamily> *groupDesc() { return GroupDesc;}
367 static void botDesc(CBotDesc<CGroupFamily> *d) { BotDesc = d;}
368 static CBotDesc<CGroupFamily> *botDesc() { return BotDesc;}
371 static void outpost(COutpost
*o
) { Outpost
= o
;}
372 static COutpost
*outpost() { return Outpost
;}
373 // static void outpostCharge(COutpostCharge *oc) { OutpostCharge = oc;}
374 // static COutpostCharge *outpostCharge() { return OutpostCharge;}
375 static void squadVariantName(const std::string
& n
) { SquadVariantName
= n
;}
376 static const std::string
& squadVariantName() { return SquadVariantName
;}
378 static void outpostGroupDesc(CGroupDesc<COutpostSquadFamily> *d) { OutpostGroupDesc = d;}
379 static CGroupDesc<COutpostSquadFamily> *outpostGroupDesc() { return OutpostGroupDesc;}
380 static void outpostBotDesc(CBotDesc<COutpostSquadFamily> *d) { OutpostBotDesc = d;}
381 static CBotDesc<COutpostSquadFamily> *outpostBotDesc() { return OutpostBotDesc;}
383 static void groupDesc(IGroupDesc
*d
) { GroupDesc
= d
;}
384 static IGroupDesc
*groupDesc() { return GroupDesc
;}
385 static void botDesc(IBotDesc
*d
) { BotDesc
= d
;}
386 static IBotDesc
*botDesc() { return BotDesc
;}
388 static void addLogicAction(IAILogicAction
*action
, uint32 alias
) { _LogicActionMap
[alias
] = action
; }
389 // retrive logic action from alias
390 static IAILogicAction
*getLogicActionFromAlias(uint32 alias
) { return _LogicActionMap
.count(alias
) ? _LogicActionMap
[alias
] : NULL
; }
391 // clear logic action map
392 static void clearLogicActionMap() { _LogicActionMap
.clear(); }
393 // current logic action
394 static void logicAction(IAILogicAction
*action
) { _LogicAction
= action
; }
395 static IAILogicAction
*logicAction() { return _LogicAction
; }
397 static IAILogicAction
*_LogicAction
;
398 static std::map
<uint32
, IAILogicAction
*> _LogicActionMap
;
399 static CAIInstance
* Instance
;
400 static CManager
* Mgr
;
404 static CAIState
* State
;
405 static CAIStateProfile
* Profile
;
406 static CAIStateChat
* Chat
;
408 static CStateMachine
* EventContainer
;
410 static CContinent
* Continent
;
411 static CRegion
* Region
;
412 static CGroupFamily
* GroupFamily
;
413 static CCellZone
* CellZone
;
415 static CFaunaZone
* FaunaZone
;
416 static CNpcZone
* NpcZone
;
418 static CRoadTrigger
* RoadTrigger
;
419 // static CGroupDesc<CGroupFamily>* GroupDesc;
420 // static CBotDesc<CGroupFamily>* BotDesc;
422 static COutpost
* Outpost
;
423 // static COutpostCharge* OutpostCharge;
424 static std::string SquadVariantName
;
425 // static CGroupDesc<COutpostSquadFamily>* OutpostGroupDesc;
426 // static CBotDesc<COutpostSquadFamily>* OutpostBotDesc;
427 static IGroupDesc
* GroupDesc
;
428 static IBotDesc
* BotDesc
;
432 _Instance
= Instance
;
439 _EventContainer
=EventContainer
;
440 _EventReaction
= NULL
;
445 Instance
= _Instance
;
452 EventContainer
=_EventContainer
;
456 // the structure is only ever instantiated for context save purposes
457 CAIInstance
* _Instance
;
463 CAIEventReaction
* _EventReaction
;
464 CAIStateProfile
* _Profile
;
467 CStateMachine
* _EventContainer
;
471 //----------------------------------------------------------------------------
472 // Local routines for the action handlers to use
473 //----------------------------------------------------------------------------
475 template <class T0
, class T1
, class T2
, class T3
, class T4
, class T5
, class T6
>
476 inline bool getArgs(const std::vector
<CAIActions::CArg
> &args
,
477 const char *actionName
,T0
&v0
,T1
&v1
,T2
&v2
,T3
&v3
,T4
&v4
,T5
&v5
,T6
&v6
)
479 if (args
.size()!=7) { nlwarning("Action %s FAILED due to argument count",actionName
); return false; }
480 if (!args
[0].get(v0
)){ nlwarning("Action %s FAILED due to type of argument 0",actionName
); return false; }
481 if (!args
[1].get(v1
)){ nlwarning("Action %s FAILED due to type of argument 1",actionName
); return false; }
482 if (!args
[2].get(v2
)){ nlwarning("Action %s FAILED due to type of argument 2",actionName
); return false; }
483 if (!args
[3].get(v3
)){ nlwarning("Action %s FAILED due to type of argument 3",actionName
); return false; }
484 if (!args
[4].get(v4
)){ nlwarning("Action %s FAILED due to type of argument 4",actionName
); return false; }
485 if (!args
[5].get(v5
)){ nlwarning("Action %s FAILED due to type of argument 5",actionName
); return false; }
486 if (!args
[6].get(v6
)){ nlwarning("Action %s FAILED due to type of argument 6",actionName
); return false; }
490 template <class T0
, class T1
, class T2
, class T3
, class T4
, class T5
>
491 inline bool getArgs(const std::vector
<CAIActions::CArg
> &args
,
492 const char *actionName
,T0
&v0
,T1
&v1
,T2
&v2
,T3
&v3
,T4
&v4
,T5
&v5
)
494 if (args
.size()!=6) { nlwarning("Action %s FAILED due to argument count",actionName
); return false; }
495 if (!args
[0].get(v0
)){ nlwarning("Action %s FAILED due to type of argument 0",actionName
); return false; }
496 if (!args
[1].get(v1
)){ nlwarning("Action %s FAILED due to type of argument 1",actionName
); return false; }
497 if (!args
[2].get(v2
)){ nlwarning("Action %s FAILED due to type of argument 2",actionName
); return false; }
498 if (!args
[3].get(v3
)){ nlwarning("Action %s FAILED due to type of argument 3",actionName
); return false; }
499 if (!args
[4].get(v4
)){ nlwarning("Action %s FAILED due to type of argument 4",actionName
); return false; }
500 if (!args
[5].get(v5
)){ nlwarning("Action %s FAILED due to type of argument 5",actionName
); return false; }
504 template <class T0
, class T1
, class T2
, class T3
, class T4
>
505 inline bool getArgs(const std::vector
<CAIActions::CArg
> &args
,
506 const char *actionName
,T0
&v0
,T1
&v1
,T2
&v2
,T3
&v3
,T4
&v4
)
508 if (args
.size()!=5) { nlwarning("Action %s FAILED due to argument count",actionName
); return false; }
509 if (!args
[0].get(v0
)){ nlwarning("Action %s FAILED due to type of argument 0",actionName
); return false; }
510 if (!args
[1].get(v1
)){ nlwarning("Action %s FAILED due to type of argument 1",actionName
); return false; }
511 if (!args
[2].get(v2
)){ nlwarning("Action %s FAILED due to type of argument 2",actionName
); return false; }
512 if (!args
[3].get(v3
)){ nlwarning("Action %s FAILED due to type of argument 3",actionName
); return false; }
513 if (!args
[4].get(v4
)){ nlwarning("Action %s FAILED due to type of argument 4",actionName
); return false; }
517 template <class T0
, class T1
, class T2
, class T3
>
518 inline bool getArgs(const std::vector
<CAIActions::CArg
> &args
,
519 const char *actionName
,T0
&v0
,T1
&v1
,T2
&v2
,T3
&v3
)
521 if (args
.size()!=4) { nlwarning("Action %s FAILED due to argument count",actionName
); return false; }
522 if (!args
[0].get(v0
)){ nlwarning("Action %s FAILED due to type of argument 0",actionName
); return false; }
523 if (!args
[1].get(v1
)){ nlwarning("Action %s FAILED due to type of argument 1",actionName
); return false; }
524 if (!args
[2].get(v2
)){ nlwarning("Action %s FAILED due to type of argument 2",actionName
); return false; }
525 if (!args
[3].get(v3
)){ nlwarning("Action %s FAILED due to type of argument 3",actionName
); return false; }
529 template <class T0
, class T1
, class T2
>
530 inline bool getArgs(const std::vector
<CAIActions::CArg
> &args
,
531 const char *actionName
,T0
&v0
,T1
&v1
,T2
&v2
)
533 if (args
.size()!=3) { nlwarning("Action %s FAILED due to argument count",actionName
); return false; }
534 if (!args
[0].get(v0
)){ nlwarning("Action %s FAILED due to type of argument 0",actionName
); return false; }
535 if (!args
[1].get(v1
)){ nlwarning("Action %s FAILED due to type of argument 1",actionName
); return false; }
536 if (!args
[2].get(v2
)){ nlwarning("Action %s FAILED due to type of argument 2",actionName
); return false; }
540 template <class T0
, class T1
>
541 inline bool getArgs(const std::vector
<CAIActions::CArg
> &args
,
542 const char *actionName
,T0
&v0
,T1
&v1
)
544 if (args
.size()!=2) { nlwarning("Action %s FAILED due to argument count",actionName
); return false; }
545 if (!args
[0].get(v0
)){ nlwarning("Action %s FAILED due to type of argument 0",actionName
); return false; }
546 if (!args
[1].get(v1
)){ nlwarning("Action %s FAILED due to type of argument 1",actionName
); return false; }
551 inline bool getArgs(const std::vector
<CAIActions::CArg
> &args
,
552 const char *actionName
,T0
&v0
)
554 if (args
.size()!=1) { nlwarning("Action %s FAILED due to argument count",actionName
); return false; }
555 if (!args
[0].get(v0
)){ nlwarning("Action %s FAILED due to type of argument 0",actionName
); return false; }