MCPcopy Create free account
hub / github.com/OpenStarbound/OpenStarbound / spawnNpc

Method spawnNpc

source/game/StarCommandProcessor.cpp:446–479  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

444}
445
446String CommandProcessor::spawnNpc(ConnectionId connectionId, String const& argumentString) {
447 if (auto errorMsg = adminCheck(connectionId, "spawn NPCs"))
448 return *errorMsg;
449
450 auto arguments = m_parser.tokenizeToStringList(argumentString);
451
452 try {
453 auto npcDatabase = Root::singleton().npcDatabase();
454 float npcLevel = 1;
455 uint64_t seed = Random::randu64();
456 Json overrides;
457
458 if (arguments.size() < 2)
459 return "You must specify a species and NPC type to spawn.";
460
461 if (arguments.size() >= 3)
462 npcLevel = lexicalCast<float>(arguments.at(2));
463 if (arguments.size() >= 4)
464 seed = lexicalCast<uint64_t>(arguments.at(3));
465 if (arguments.size() >= 5)
466 overrides = Json::parse(arguments.at(4)).toObject();
467
468 auto npc = npcDatabase->createNpc(npcDatabase->generateNpcVariant(arguments.at(0), arguments.at(1), npcLevel, seed, overrides));
469 bool done = m_universe->executeForClient(connectionId, [&](WorldServer* world, PlayerPtr const& player) {
470 npc->setPosition(player->aimPosition());
471 world->addEntity(npc);
472 });
473
474 return done ? "" : "Invalid client state";
475 } catch (StarException const& exception) {
476 Logger::warn("Could not spawn NPC of species '{}', exception caught: {}", argumentString, outputException(exception, true));
477 return strf("Could not spawn NPC of species '{}'", argumentString);
478 }
479}
480
481String CommandProcessor::spawnVehicle(ConnectionId connectionId, String const& argumentString) {
482 if (auto errorMsg = adminCheck(connectionId, "spawn vehicles"))

Callers

nothing calls this directly

Calls 15

singletonClass · 0.85
randu64Function · 0.85
strfFunction · 0.85
tokenizeToStringListMethod · 0.80
npcDatabaseMethod · 0.80
toObjectMethod · 0.80
createNpcMethod · 0.80
generateNpcVariantMethod · 0.80
executeForClientMethod · 0.80
parseFunction · 0.50
outputExceptionFunction · 0.50
sizeMethod · 0.45

Tested by

no test coverage detected