Skip to content

Commit

Permalink
Scripting|World: Added the World.Thing class for mobj bindings
Browse files Browse the repository at this point in the history
MobjThinkerData's info record is now an object derived from World.Thing.
It identifies the mobj using its ID number, so native functions called
via World.Thing can look up the native mobj.
  • Loading branch information
skyjake committed Oct 28, 2015
1 parent 726462f commit f52aa56
Show file tree
Hide file tree
Showing 10 changed files with 144 additions and 6 deletions.
31 changes: 31 additions & 0 deletions doomsday/apps/client/include/world/bindings_world.h
@@ -0,0 +1,31 @@
/** @file bindings_world.h
*
* @authors Copyright (c) 2015 Jaakko Keränen <jaakko.keranen@iki.fi>
*
* @par License
* GPL: http://www.gnu.org/licenses/gpl.html
*
* <small>This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2 of the License, or (at your
* option) any later version. This program is distributed in the hope that it
* will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
* Public License for more details. You should have received a copy of the GNU
* General Public License along with this program; if not, see:
* http://www.gnu.org/licenses</small>
*/

#ifndef DENG_CLIENT_WORLD_BINDINGS_H
#define DENG_CLIENT_WORLD_BINDINGS_H

#include <de/Binder>

namespace world {

void initBindings(de::Binder &binder, de::Record &worldModule);

} // namespace world

#endif // DENG_CLIENT_WORLD_BINDINGS_H

59 changes: 59 additions & 0 deletions doomsday/apps/client/src/world/bindings_world.cpp
@@ -0,0 +1,59 @@
/** @file bindings_world.cpp World related Doomsday Script bindings.
*
* @authors Copyright (c) 2015 Jaakko Keränen <jaakko.keranen@iki.fi>
*
* @par License
* GPL: http://www.gnu.org/licenses/gpl.html
*
* <small>This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2 of the License, or (at your
* option) any later version. This program is distributed in the hope that it
* will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
* Public License for more details. You should have received a copy of the GNU
* General Public License along with this program; if not, see:
* http://www.gnu.org/licenses</small>
*/

#include "world/bindings_world.h"
#include "world/worldsystem.h"
#include "world/map.h"
#include "world/thinkers.h"
#include "dd_main.h"

#include <doomsday/world/mobj.h>
#include <de/Context>

using namespace de;

namespace world {

static mobj_t &instanceMobj(Context const &ctx)
{
/// @todo Not necessarily always the current map. -jk
int const id = ctx.selfInstance().geti(QStringLiteral("__id__"), 0);
mobj_t *mo = App_WorldSystem().map().thinkers().mobjById(id);
if(!mo)
{
throw ::Map::MissingObjectError("instanceMobj", QString("Mobj %1 does not exist").arg(id));
}
return *mo;
}

static Value *Function_Thing_Health(Context &ctx, Function::ArgumentValues const &)
{
return new NumberValue(instanceMobj(ctx).health);
}

void initBindings(Binder &binder, Record &worldModule)
{
// Thing
{
Record &thing = worldModule.addRecord("Thing");
binder.init(thing)
<< DENG2_FUNC_NOARG(Thing_Health, "health");
}
}

} // namespace world
2 changes: 2 additions & 0 deletions doomsday/apps/client/src/world/clientmobjthinkerdata.cpp
Expand Up @@ -199,6 +199,8 @@ ClientMobjThinkerData::ClientMobjThinkerData(ClientMobjThinkerData const &other)

void ClientMobjThinkerData::think()
{
MobjThinkerData::think();

d->initOnce();
if(d->flags.testFlag(StateChanged))
{
Expand Down
7 changes: 6 additions & 1 deletion doomsday/apps/client/src/world/thinkers.cpp
Expand Up @@ -429,7 +429,12 @@ void Thinker_InitPrivateData(thinker_t *th)
th->d = new ThinkerData;
}

if(th->d) THINKER_DATA(*th, ThinkerData).setThinker(th);
if(th->d)
{
auto &thinkerData = THINKER_DATA(*th, ThinkerData);
thinkerData.setThinker(th);
thinkerData.initBindings();
}
}

/**
Expand Down
9 changes: 9 additions & 0 deletions doomsday/apps/client/src/world/worldsystem.cpp
Expand Up @@ -29,6 +29,8 @@
#include <de/Error>
#include <de/Log>
#include <de/Time>
#include <de/Binder>
#include <de/ScriptSystem>
#include <doomsday/doomsdayapp.h>
#include <doomsday/console/cmd.h>
#include <doomsday/console/exec.h>
Expand Down Expand Up @@ -61,6 +63,7 @@
#include "world/p_ticker.h"
#include "world/sky.h"
#include "world/thinkers.h"
#include "world/bindings_world.h"
#include "edit_map.h"
#include "Plane"
#include "Sector"
Expand Down Expand Up @@ -298,6 +301,9 @@ static String cacheIdForMap(String const &sourcePath)

DENG2_PIMPL(WorldSystem)
{
Binder binder; ///< Doomsday Script bindings for the World.
Record worldModule;

Map *map = nullptr; ///< Current map.
Record fallbackMapInfo; ///< Used when no effective MapInfo definition.

Expand All @@ -308,6 +314,9 @@ DENG2_PIMPL(WorldSystem)

Instance(Public *i) : Base(i)
{
world::initBindings(binder, worldModule);
ScriptSystem::get().addNativeModule("World", worldModule);

// One time init of the fallback MapInfo definition.
defn::MapInfo(fallbackMapInfo).resetToDefaults();
}
Expand Down
Expand Up @@ -35,11 +35,13 @@ class LIBDOOMSDAY_PUBLIC MobjThinkerData : public ThinkerData
MobjThinkerData();
MobjThinkerData(MobjThinkerData const &other);

IData *duplicate() const;
IData *duplicate() const override;

mobj_t *mobj();
mobj_t const *mobj() const;

void initBindings() override;

/**
* Called whenever the current state of the mobj has changed.
*
Expand Down
Expand Up @@ -49,6 +49,13 @@ class LIBDOOMSDAY_PUBLIC ThinkerData : public Thinker::IData
de::Record &info();
de::Record const &info() const;

/**
* Initializes Doomsday Script bindings for the thinker. This is called
* when the thinker is added to the world, so mobjs have been assigned
* their IDs.
*/
virtual void initBindings();

private:
DENG2_PRIVATE(d)

Expand Down
18 changes: 18 additions & 0 deletions doomsday/apps/libdoomsday/src/world/mobjthinkerdata.cpp
Expand Up @@ -19,8 +19,13 @@

#include "doomsday/world/mobjthinkerdata.h"

#include <de/ScriptSystem>
#include <de/RecordValue>

using namespace de;

static String const VAR_ID("__id__");

DENG2_PIMPL_NOREF(MobjThinkerData)
{};

Expand Down Expand Up @@ -48,6 +53,19 @@ mobj_t const *MobjThinkerData::mobj() const
return reinterpret_cast<mobj_t const *>(&thinker());
}

void MobjThinkerData::initBindings()
{
ThinkerData::initBindings();

// World.Thing is the class for mobjs.
info().addSuperRecord(new RecordValue(ScriptSystem::get().nativeModule(
QStringLiteral("World")).subrecord(QStringLiteral("Thing"))));

// The ID is important because this is how the object is identified in
// script functions (relied upon by World.Thing).
info().addNumber(VAR_ID, mobj()->thinker.id).setReadOnly();
}

void MobjThinkerData::stateChanged(state_t const *)
{
// overridden
Expand Down
3 changes: 3 additions & 0 deletions doomsday/apps/libdoomsday/src/world/thinkerdata.cpp
Expand Up @@ -90,6 +90,9 @@ Record const &ThinkerData::info() const
return d->info;
}

void ThinkerData::initBindings()
{}

#ifdef DENG2_DEBUG
duint32 ThinkerData::DebugCounter::total = 0;
ThinkerData::DebugValidator ensureAllPrivateDataIsReleased;
Expand Down
10 changes: 6 additions & 4 deletions doomsday/apps/server/CMakeLists.txt
Expand Up @@ -80,6 +80,7 @@ set (SHARED_WITH_CLIENT
${src}/include/ui/infine/finaletextwidget.h
${src}/include/ui/infine/finalewidget.h
${src}/include/world/dmuargs.h
${src}/include/world/bindings_world.h
${src}/include/world/blockmap.h
${src}/include/world/bsp/convexsubspaceproxy.h
${src}/include/world/bsp/edgetip.h
Expand Down Expand Up @@ -114,7 +115,7 @@ set (SHARED_WITH_CLIENT
${src}/include/world/surface.h
${src}/include/world/thinkers.h
${src}/include/world/vertex.h
${src}/include/world/worldsystem.h
${src}/include/world/worldsystem.h

${src}/src/api_console.cpp
${src}/src/api_filesys.cpp
Expand Down Expand Up @@ -174,6 +175,7 @@ set (SHARED_WITH_CLIENT
${src}/src/ui/infine/infinesystem.cpp
${src}/src/world/api_map.cpp
${src}/src/world/api_mapedit.cpp
${src}/src/world/bindings_world.cpp
${src}/src/world/blockmap.cpp
${src}/src/world/bsp/convexsubspaceproxy.cpp
${src}/src/world/bsp/hplane.cpp
Expand Down Expand Up @@ -213,7 +215,7 @@ set (SHARED_WITH_CLIENT
)

if (WIN32)
include_directories (../client/include/windows)
include_directories (../client/include/windows)
list (APPEND SHARED_WITH_CLIENT ${src}/src/windows/dd_winit.cpp)
endif ()

Expand All @@ -225,7 +227,7 @@ if (UNIX)
set (MAN_PAGE ${CMAKE_CURRENT_BINARY_DIR}/doomsday-server.6)
list (APPEND SOURCES ${MAN_PAGE})
deng_add_amedoc (MAN ${MAN_PAGE} ${DENG_SOURCE_DIR}/doc/server server.ame)
endif ()
endif ()
endif ()

add_executable (server ${SOURCES} ${HEADERS} ${API_HEADERS} ${SHARED_WITH_CLIENT})
Expand All @@ -246,7 +248,7 @@ target_link_libraries (server
lzss
)

# Server is installed as part of the "client" component instead of the
# Server is installed as part of the "client" component instead of the
# default "tools".
deng_install_tool (server client)

Expand Down

0 comments on commit f52aa56

Please sign in to comment.