Skip to content

Commit

Permalink
Scripting: Added more World and Game bindings
Browse files Browse the repository at this point in the history
The following functions were added:
- World.consolePlayer()
- World.Thing.id()
- Game.setMessage()
- Game.setYellowMessage() (for Hexen only)
  • Loading branch information
skyjake committed Nov 16, 2019
1 parent 7372ed3 commit 9870aa3
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 2 deletions.
18 changes: 18 additions & 0 deletions doomsday/apps/client/src/world/base/bindings_world.cpp
Expand Up @@ -20,6 +20,7 @@
#include "world/clientserverworld.h"
#include "world/map.h"
#include "world/thinkers.h"
#include "world/p_players.h"
#include "audio/audiosystem.h"
#include "dd_main.h"

Expand All @@ -31,6 +32,16 @@ using namespace de;

namespace world {

static Value *Function_World_ConsolePlayer(Context &, const Function::ArgumentValues &)
{
return new NumberValue(consolePlayer);
}

static Value *Function_Thing_Id(Context &ctx, const Function::ArgumentValues &)
{
return new NumberValue(ClientServerWorld::contextMobj(ctx).thinker.id);
}

static Value *Function_Thing_Health(Context &ctx, const Function::ArgumentValues &)
{
return new NumberValue(ClientServerWorld::contextMobj(ctx).health);
Expand All @@ -54,6 +65,12 @@ static Value *Function_Thing_StartSound(Context &ctx, const Function::ArgumentVa

void initBindings(Binder &binder, Record &worldModule)
{
// Global functions.
{
binder.init(worldModule)
<< DENG2_FUNC_NOARG(World_ConsolePlayer, "consolePlayer");
}

// Thing
{
Record &thing = worldModule.addSubrecord("Thing");
Expand All @@ -62,6 +79,7 @@ void initBindings(Binder &binder, Record &worldModule)
startSoundArgs["volume"] = new NumberValue(1.0);

binder.init(thing)
<< DENG2_FUNC_NOARG(Thing_Id, "id")
<< DENG2_FUNC_NOARG(Thing_Health, "health")
<< DENG2_FUNC_DEFS (Thing_StartSound, "startSound", "id" << "volume", startSoundArgs);

Expand Down
59 changes: 57 additions & 2 deletions doomsday/apps/plugins/common/src/common.cpp
Expand Up @@ -30,7 +30,9 @@
#include <de/Context>
#include <de/NoneValue>
#include <de/ScriptSystem>
#include <doomsday/DoomsdayApp>
#include <doomsday/defs/definition.h>
#include <doomsday/players.h>
#include <doomsday/world/map.h>

int Common_GetInteger(int id)
Expand Down Expand Up @@ -129,6 +131,7 @@ void Common_Register()
//-------------------------------------------------------------------------------------------------

static de::Binder *gameBindings;
static de::Record *gameModule;

static mobj_t &instanceMobj(const de::Context &ctx)
{
Expand Down Expand Up @@ -198,10 +201,42 @@ static de::Value *Function_Thing_Attack(de::Context &ctx, const de::Function::Ar
}
#endif

static int playerNumberArgument(const de::Value &arg)
{
if (de::is<de::NoneValue>(arg))
{
return CONSOLEPLAYER;
}
const int plrNum = arg.asInt();
if (plrNum < 0 || plrNum >= MAXPLAYERS)
{
throw de::Error("playerNumberArgument", "Player index out of bounds");
}
return plrNum;
}

static de::Value *Function_SetMessage(de::Context &, const de::Function::ArgumentValues &args)
{
const int plrNum = playerNumberArgument(*args.at(1));
P_SetMessage(&players[plrNum], args.at(0)->asText().toLatin1());
return nullptr;
}

#if defined (__JHEXEN__)
static de::Value *Function_SetYellowMessage(de::Context &, const de::Function::ArgumentValues &args)
{
const int plrNum = playerNumberArgument(*args.at(1));
P_SetYellowMessage(&players[plrNum], args.at(0)->asText().toLatin1());
return nullptr;
}
#endif

void Common_Load()
{
using namespace de;

gameModule = new Record;

Function::Defaults spawnMissileArgs;
spawnMissileArgs["angle"] = new NoneValue;
spawnMissileArgs["momz"] = new NumberValue(0.0);
Expand All @@ -210,18 +245,38 @@ void Common_Load()
attackArgs["damage"] = new NumberValue(0.0);
attackArgs["missile"] = new NoneValue;

Function::Defaults setMessageArgs;
setMessageArgs["player"] = new NoneValue;

DENG2_ASSERT(gameBindings == nullptr);
gameBindings = new Binder(nullptr, Binder::FunctionsOwned); // must delete when plugin unloaded
gameBindings->init(ScriptSystem::get().builtInClass("World", "Thing"))
#if defined(__JHERETIC__)
<< DENG2_FUNC_DEFS(Thing_Attack, "attack", "damage" << "missile", attackArgs)
<< DENG2_FUNC_DEFS(Thing_Attack, "attack", "damage" << "missile", attackArgs)
#endif
<< DENG2_FUNC_DEFS(Thing_SpawnMissile, "spawnMissile", "id" << "angle" << "momz", spawnMissileArgs);
<< DENG2_FUNC_DEFS(Thing_SpawnMissile, "spawnMissile", "id" << "angle" << "momz", spawnMissileArgs);

gameBindings->init(*gameModule)
<< DENG2_FUNC_DEFS(SetMessage, "setMessage", "message" << "player", setMessageArgs);

#if defined(__JHEXEN__)
{
Function::Defaults setYellowMessageArgs;
setYellowMessageArgs["player"] = new NoneValue;
*gameBindings
<< DENG2_FUNC_DEFS(SetYellowMessage, "setYellowMessage", "message" << "player", setYellowMessageArgs);
}
#endif

ScriptSystem::get().addNativeModule("Game", *gameModule);
}

void Common_Unload()
{
DENG2_ASSERT(gameBindings != nullptr);
de::ScriptSystem::get().removeNativeModule("Game");
delete gameBindings;
gameBindings = nullptr;
delete gameModule;
gameModule = nullptr;
}

0 comments on commit 9870aa3

Please sign in to comment.