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/>.
20 //----------------------------------------------------------------------------
23 #include "ais_actions.h"
24 #include "ai_grp_fauna.h"
26 #include "continent_inline.h"
28 using namespace NLMISC
;
29 using namespace NLNET
;
31 using namespace CAISActionEnums
;
32 using namespace AITYPES
;
35 //---------------------------------------------------------------------------------------
36 // Stuff used for management of log messages
38 static bool VerboseLog
=false;
39 #define LOG if (!VerboseLog) {} else nlinfo
42 //----------------------------------------------------------------------------
43 // The FAUNA_MGR context
44 //----------------------------------------------------------------------------
46 // DEFINE_ACTION(ContextFaunaMgr,PLACEXYR)
47 DEFINE_ACTION(ContextFaunaGrp
,PLACEXYR
)
49 // set the working place slot
50 // args: string name float x, float y, sint r
52 if (!CWorkPtr::grpFauna())
55 // get hold of the parameters and check their validity
60 if (!getArgs(args
,name(), alias
, x
, y
, r
, verticalPos
))
63 // try to get a pointer to the place and create a new place if need be
64 CAIPlaceXYR
*const place
=dynamic_cast<CAIPlaceXYR
*>(CWorkPtr::grpFauna()->places().getChildByAlias(alias
));
67 nlwarning("Unable to set placeXYR coords (%.3f,%.3f,%d) as place alias %u not found in manager",x
,y
,r
,alias
);
71 // set the place coordinates
72 place
->setPosAndRadius((TVerticalPos
)verticalPos
, CAIPos(x
,y
,0,0.0f
),r
);
75 // faune places specifics
76 DEFINE_ACTION(ContextFaunaGrp
,PLXYRFAF
)
78 // set the working place slot
79 // args: string name float x, float y, sint r
81 if (!CWorkPtr::grpFauna())
84 // get hold of the parameters and check their validity
89 if (!getArgs(args
,name(), alias
, flagSpawn
, flagRest
, flagFood
))
92 // try to get a pointer to the place and create a new place if need be
93 CAIPlaceXYRFauna
*const place
=dynamic_cast<CAIPlaceXYRFauna
*>(CWorkPtr::grpFauna()->places().getChildByAlias(alias
));
96 nlwarning("Unable to set placeXYRFauna flags as place alias %u not found in manager", alias
);
100 // set the place coordinates
101 place
->setFlag(CAIPlaceXYRFauna::FLAG_SPAWN
, flagSpawn
);
102 place
->setFlag(CAIPlaceXYRFauna::FLAG_REST
, flagRest
);
103 place
->setFlag(CAIPlaceXYRFauna::FLAG_EAT
, flagFood
);
106 DEFINE_ACTION(ContextFaunaGrp
,PLXYRFAS
)
108 // set the working place slot
109 // args: string name float x, float y, sint r
111 if (!CWorkPtr::grpFauna())
114 // get hold of the parameters and check their validity
116 uint32 stayTimeMin
, stayTimeMax
;
117 if (!getArgs(args
,name(), alias
, stayTimeMin
, stayTimeMax
))
120 // try to get a pointer to the place and create a new place if need be
121 CAIPlaceXYRFauna
*const place
=dynamic_cast<CAIPlaceXYRFauna
*>(CWorkPtr::grpFauna()->places().getChildByAlias(alias
));
124 nlwarning("Unable to set placeXYRFauna stay times as place alias %u not found in manager", alias
);
128 // set the place coordinates
129 place
->setMinStayTime(stayTimeMin
);
130 place
->setMaxStayTime(stayTimeMax
);
133 DEFINE_ACTION(ContextFaunaGrp
,PLXYRFAI
)
135 // set the working place slot
136 // args: string name float x, float y, sint r
138 if (!CWorkPtr::grpFauna())
141 // get hold of the parameters and check their validity
144 std::string indexNext
;
145 if (!getArgs(args
,name(), alias
, index
, indexNext
))
147 // try to get a pointer to the place and create a new place if need be
148 CAIPlaceXYRFauna
*place
= dynamic_cast<CAIPlaceXYRFauna
*>(CWorkPtr::grpFauna()->places().getChildByAlias(alias
));
151 nlwarning("Unable to set placeXYRFauna arcs as place alias %u not found in manager", alias
);
155 std::vector
<std::string
> reachableIndicesStr
;
156 NLMISC::explode(indexNext
, string(","), reachableIndicesStr
);
157 std::set
<sint32
> arcs
;
158 for(uint k
= 0; k
< reachableIndicesStr
.size(); ++k
)
160 if (NLMISC::nlstricmp(reachableIndicesStr
[k
], "next") == 0)
162 place
->setReachNext(true);
165 if (sscanf(reachableIndicesStr
[k
].c_str(), "%d", ¤tIndex
) != 1) continue;
166 arcs
.insert((sint32
) currentIndex
);
168 std::vector
<sint32
> arcVect(arcs
.begin(), arcs
.end());
169 place
->setIndex(index
);
170 place
->setArcs(arcVect
);
174 // activation related parameters
175 DEFINE_ACTION(ContextFaunaGrp
,PLXYRFAA
)
177 // set the working place slot
178 // args: string name float x, float y, sint r
180 if (!CWorkPtr::grpFauna())
183 // get hold of the parameters and check their validity
187 std::string timeInterval
;
188 std::string dayInterval
;
189 if (!getArgs(args
,name(), alias
, active
, timeDriven
, timeInterval
, dayInterval
))
192 // try to get a pointer to the place and create a new place if need be
193 CAIPlaceXYRFauna
*const place
=dynamic_cast<CAIPlaceXYRFauna
*>(CWorkPtr::grpFauna()->places().getChildByAlias(alias
));
196 nlwarning("Unable to set placeXYRFauna stay times as place alias %u not found in manager", alias
);
199 // set activation parameters
200 place
->setTimeDriven(timeDriven
);
203 place
->setActive(active
);
207 place
->setTimeInterval(timeInterval
);
208 place
->setDayInterval(dayInterval
);
214 DEFINE_ACTION(ContextFaunaMgr
,GRPFAUNA
)
216 // set the working grp
217 // args: uint slot_in_manager [, string name [, string type]]
219 if (!CWorkPtr::mgrFauna())
222 // get hold of the parameters and check their validity
225 if (!getArgs(args
,name(),alias
))
228 // scan the manager for a group with the right alias
229 for (uint i
=0;i
<CWorkPtr::mgrFauna()->groups().size();++i
)
231 CGroup
*const grp
= CWorkPtr::mgrFauna()->groups()[i
]; //getGrpFauna(i);
232 if (grp
->getAlias()!=alias
)
235 // setup the working group pointer and exit
237 // push the group context onto the context stack
238 CContextStack::setContext(ContextFaunaGrp
);
242 // no match found so throw a warning and invalidate grp pointer
243 nlwarning("GRPFAUNA failed because no group with alias %u found in manager",alias
);
247 //----------------------------------------------------------------------------
248 // The FAUNA_GRP context
249 //----------------------------------------------------------------------------
251 DEFINE_ACTION(ContextFaunaGrp
,AUTOSPWN
)
253 // set the feed and rest times
254 // args: float time0, float time1
256 if(!CWorkPtr::grpFauna())
260 if (!getArgs(args
, name(), autoSpawn
))
262 CWorkPtr::grpFauna()->setAutoSpawn(autoSpawn
!= 0);
263 LOG("AutoSpawn : %s", autoSpawn
? "true" : "false");
266 DEFINE_ACTION(ContextFaunaGrp
,SETTIMES
)
268 // set the feed and rest times
269 // args: float time0, float time1
271 if(!CWorkPtr::grpFauna())
274 // get hold of the parameters and check their validity
276 if (!getArgs(args
,name(),time0
,time1
))
279 time0
*=10; // quick hack !
280 time1
*=10; // quick hack !
282 CWorkPtr::grpFauna()->setTimer (CGrpFauna::EAT_TIME
, (uint32
) time0
);
283 CWorkPtr::grpFauna()->setTimer (CGrpFauna::REST_TIME
, (uint32
) time1
);
284 LOG("Set timers: eat=%i sleep=%i",(uint32
) time0
, (uint32
) time1
);
287 DEFINE_ACTION(ContextFaunaGrp
, ASSIST
)
289 // set the assist mode for the group
292 if(!CWorkPtr::grpFauna())
295 // get hold of the parameters and check their validity
297 if (!getArgs(args
,name(),assist
))
300 // CWorkPtr::grpFauna()->setSolidarity(assist!=0);
304 DEFINE_ACTION(ContextFaunaGrp
, STCYCLES
)
306 // set the respawn and corpse time
307 // args: float time0, float time1
309 if (!CWorkPtr::grpFauna())
312 // get hold of the parameters and check their validity
314 if (!getArgs(args
,name(),cycles
))
317 CWorkPtr::grpFauna()->setCyles(cycles
);
319 LOG("Set Cycles: %s", cycles
.c_str());
323 DEFINE_ACTION(ContextFaunaGrp
, SPAWTIME
)
325 // set the respawn and corpse time
326 // args: float time0, float time1
328 if(!CWorkPtr::grpFauna())
331 // get hold of the parameters and check their validity
332 double time0
, time1
, time2
= -1;
335 if (!getArgs(args
,name(),time0
,time1
,time2
))
340 if (!getArgs(args
,name(),time0
,time1
))
344 CWorkPtr::grpFauna()->setTimer ( CGrpFauna::SPAWN_TIME
, (uint32
) time0
);
345 CWorkPtr::grpFauna()->setTimer ( CGrpFauna::CORPSE_TIME
, (uint32
) time1
);
346 // If the LevelDesigner has specified a correct respawn time
349 CWorkPtr::grpFauna()->setTimer ( CGrpFauna::RESPAWN_TIME
, (uint32
) time2
);
350 LOG("Set spawn timers: spawn=%i corpse=%i respawn=%i",(uint32
) time0
, (uint32
) time1
, (uint32
) time2
);
354 LOG("Set spawn timers: spawn=%i corpse=%i respawn=45",(uint32
) time0
, (uint32
) time1
);
358 DEFINE_ACTION(ContextFaunaGrp
,POPVER
)
360 // add a population version for a group
361 // args: uint32 alias, string spawn_type, uint weight, (string sheet, uint32 count)+
363 if(!CWorkPtr::grpFauna())
366 const uint32 fixedArgsCount
=3;
367 if (args
.size()<fixedArgsCount
+2 || ((args
.size()-fixedArgsCount
)&1)==1)
369 nlwarning("POPVER action FAILED due to bad number of arguments (%d)",args
.size());
373 std::string spawnTypeStr
;
377 TSpawnType spawntype
;
379 if ( args
[0].get(alias
)==false
380 || args
[1].get(spawnTypeStr
)==false
381 || args
[2].get(weight
)==false)
383 nlwarning("POPVER FAILED due to failure to read alias, spawn type & weight at arguments 0, 1 & 2");
387 // convert the spawn type to an enum value and check validity
388 getType(spawntype
,spawnTypeStr
.c_str());
389 if (spawntype
==SpawnTypeBadType
)
391 nlwarning("POP_ADD action FAILED due to unknown spawn type: '%s'",spawnTypeStr
.c_str());
395 CPopulation
*pop
= new CPopulation(CWorkPtr::grpFauna(),alias
,"Population");
396 pop
->setSpawnType(spawntype
);
398 pop
->setWeight(weight
);
400 // get hold of the parameters and check their validity
401 for (uint i
=fixedArgsCount
;i
+1<args
.size();i
+=2)
406 if ( !args
[i
].get(sheet
)
407 || !args
[i
+1].get(count
))
409 nlwarning("POPVER Add Record FAILED due to bad arguments");
413 CSheetId
sheetId(sheet
);
414 if (sheetId
==CSheetId::Unknown
)
416 nlwarning("POPVER Add Record Invalid sheet: %s",sheet
.c_str());
420 AISHEETS::ICreatureCPtr
const sheetPtr
= AISHEETS::CSheets::getInstance()->lookup(sheetId
);
423 nlwarning("POPVER Add Record Invalid sheet: %s",sheet
.c_str());
426 pop
->addPopRecord(CPopulationRecord(sheetPtr
,count
));
429 // create the population
430 CWorkPtr::grpFauna()->setPopulation(pop
);
433 //---------------------------------------------------------------------------------------
434 // Control over verbose nature of logging
435 //---------------------------------------------------------------------------------------
437 NLMISC_COMMAND(verboseFaunaParseLog
,"Turn on or off or check the state of verbose parser activity logging","")
444 if(args
[0]==string("on")||args
[0]==string("ON")||args
[0]==string("true")||args
[0]==string("TRUE")||args
[0]==string("1"))
447 if(args
[0]==string("off")||args
[0]==string("OFF")||args
[0]==string("false")||args
[0]==string("FALSE")||args
[0]==string("0"))
451 nlinfo("verboseLogging is %s",VerboseLog
?"ON":"OFF");
455 //---------------------------------------------------------------------------------------