Added ai command setEquipment
[ryzomcore.git] / ryzom / server / src / ai_service / ais_actions_fauna.cpp
blob0d8f059d87cb83fbab5756be4e4325375d74fdc9
1 // Ryzom - MMORPG Framework <http://dev.ryzom.com/projects/ryzom/>
2 // Copyright (C) 2010 Winch Gate Property Limited
3 //
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.
8 //
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 //----------------------------------------------------------------------------
22 #include "stdpch.h"
23 #include "ais_actions.h"
24 #include "ai_grp_fauna.h"
26 #include "continent_inline.h"
28 using namespace NLMISC;
29 using namespace NLNET;
30 using namespace std;
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())
53 return;
55 // get hold of the parameters and check their validity
56 float x,y;
57 sint32 r;
58 uint32 alias;
59 uint32 verticalPos;
60 if (!getArgs(args,name(), alias, x, y, r, verticalPos))
61 return;
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));
65 if (!place)
67 nlwarning("Unable to set placeXYR coords (%.3f,%.3f,%d) as place alias %u not found in manager",x,y,r,alias);
68 return;
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())
82 return;
84 // get hold of the parameters and check their validity
85 uint32 alias;
86 bool flagSpawn;
87 bool flagRest;
88 bool flagFood;
89 if (!getArgs(args,name(), alias, flagSpawn, flagRest, flagFood))
90 return;
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));
94 if (!place)
96 nlwarning("Unable to set placeXYRFauna flags as place alias %u not found in manager", alias);
97 return;
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())
112 return;
114 // get hold of the parameters and check their validity
115 uint32 alias;
116 uint32 stayTimeMin, stayTimeMax;
117 if (!getArgs(args,name(), alias, stayTimeMin, stayTimeMax))
118 return;
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));
122 if (!place)
124 nlwarning("Unable to set placeXYRFauna stay times as place alias %u not found in manager", alias);
125 return;
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())
139 return;
141 // get hold of the parameters and check their validity
142 uint32 alias;
143 uint32 index;
144 std::string indexNext;
145 if (!getArgs(args,name(), alias, index, indexNext))
146 return;
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));
149 if (!place)
151 nlwarning("Unable to set placeXYRFauna arcs as place alias %u not found in manager", alias);
152 return;
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);
164 int currentIndex;
165 if (sscanf(reachableIndicesStr[k].c_str(), "%d", &currentIndex) != 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())
181 return;
183 // get hold of the parameters and check their validity
184 uint32 alias;
185 bool active;
186 bool timeDriven;
187 std::string timeInterval;
188 std::string dayInterval;
189 if (!getArgs(args,name(), alias, active, timeDriven, timeInterval, dayInterval))
190 return;
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));
194 if (!place)
196 nlwarning("Unable to set placeXYRFauna stay times as place alias %u not found in manager", alias);
197 return;
199 // set activation parameters
200 place->setTimeDriven(timeDriven);
201 if (!timeDriven)
203 place->setActive(active);
205 else
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())
220 return;
222 // get hold of the parameters and check their validity
223 uint32 alias;
224 std::string type;
225 if (!getArgs(args,name(),alias))
226 return;
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)
233 continue;
235 // setup the working group pointer and exit
236 CWorkPtr::grp(grp);
237 // push the group context onto the context stack
238 CContextStack::setContext(ContextFaunaGrp);
239 return;
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);
244 CWorkPtr::grp(NULL);
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())
257 return;
259 uint32 autoSpawn;
260 if (!getArgs(args, name(), autoSpawn))
261 return;
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())
272 return;
274 // get hold of the parameters and check their validity
275 double time0, time1;
276 if (!getArgs(args,name(),time0,time1))
277 return;
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
290 // args: bool assist
292 if(!CWorkPtr::grpFauna())
293 return;
295 // get hold of the parameters and check their validity
296 uint32 assist;
297 if (!getArgs(args,name(),assist))
298 return;
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())
310 return;
312 // get hold of the parameters and check their validity
313 std::string cycles;
314 if (!getArgs(args,name(),cycles))
315 return;
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())
329 return;
331 // get hold of the parameters and check their validity
332 double time0, time1, time2= -1;
333 if(args.size()>=3)
335 if (!getArgs(args,name(),time0,time1,time2))
336 return;
338 else
340 if (!getArgs(args,name(),time0,time1))
341 return;
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
347 if(time2>=0)
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);
352 else
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())
364 return;
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());
370 return;
373 std::string spawnTypeStr;
374 std::string cycles;
375 uint32 alias;
376 uint32 weight;
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");
384 return;
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());
392 return;
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)
403 std::string sheet;
404 uint32 count;
406 if ( !args[i].get(sheet)
407 || !args[i+1].get(count))
409 nlwarning("POPVER Add Record FAILED due to bad arguments");
410 continue;
413 CSheetId sheetId(sheet);
414 if (sheetId==CSheetId::Unknown)
416 nlwarning("POPVER Add Record Invalid sheet: %s",sheet.c_str());
417 continue;
420 AISHEETS::ICreatureCPtr const sheetPtr = AISHEETS::CSheets::getInstance()->lookup(sheetId);
421 if (!sheetPtr)
423 nlwarning("POPVER Add Record Invalid sheet: %s",sheet.c_str());
424 continue;
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","")
439 if(args.size()>1)
440 return false;
442 if(args.size()==1)
444 if(args[0]==string("on")||args[0]==string("ON")||args[0]==string("true")||args[0]==string("TRUE")||args[0]==string("1"))
445 VerboseLog=true;
447 if(args[0]==string("off")||args[0]==string("OFF")||args[0]==string("false")||args[0]==string("FALSE")||args[0]==string("0"))
448 VerboseLog=false;
451 nlinfo("verboseLogging is %s",VerboseLog?"ON":"OFF");
452 return true;
455 //---------------------------------------------------------------------------------------