Add some code to hook into Mumble's Link plugin and provide Mumble with positional information about the player's current viewing position in Warzone

git-svn-id: svn+ssh://svn.gna.org/svn/warzone/trunk@5768 4a71c877-e1ca-e34f-864e-861f7616d084
master
Giel van Schijndel 2008-08-03 20:53:14 +00:00
parent af224be8bc
commit d54e360aa7
8 changed files with 221 additions and 2 deletions

View File

@ -36,7 +36,7 @@ noinst_HEADERS = action.h advvis.h ai.h aiexperience.h anim_id.h \
message.h messagedef.h messagely.h message_parser.tab.h miscimd.h \ message.h messagedef.h messagely.h message_parser.tab.h miscimd.h \
mission.h missiondef.h move.h movedef.h \ mission.h missiondef.h move.h movedef.h \
multigifts.h multiint.h multijoin.h multilimit.h multimenu.h multiplay.h multirecv.h \ multigifts.h multiint.h multijoin.h multilimit.h multimenu.h multiplay.h multirecv.h \
multistat.h objectdef.h objects.h objmem.h oprint.h parsetest.h positiondef.h order.h \ multistat.h mumblelink.h objectdef.h objects.h objmem.h oprint.h parsetest.h positiondef.h order.h \
orderdef.h power.h projectile.h radar.h raycast.h research.h \ orderdef.h power.h projectile.h radar.h raycast.h research.h \
researchdef.h scores.h scriptai.h scriptcb.h scriptextern.h scriptfuncs.h \ researchdef.h scores.h scriptai.h scriptcb.h scriptextern.h scriptfuncs.h \
scriptobj.h scripttabs.h scriptvals.h selection.h seqdisp.h stats.h stats-db2.h statsdef.h \ scriptobj.h scripttabs.h scriptvals.h selection.h seqdisp.h stats.h stats-db2.h statsdef.h \
@ -53,7 +53,7 @@ warzone2100_SOURCES = scriptvals_parser.tab.c scriptvals_lexer.lex.c \
loop.c main.c map.c mapdisplay.c mapgrid.c mechanics.c message.c \ loop.c main.c map.c mapdisplay.c mapgrid.c mechanics.c message.c \
message_lexer.lex.c message_parser.tab.c miscimd.c \ message_lexer.lex.c message_parser.tab.c miscimd.c \
move.c multiint.c multimenu.c multiopt.c multisync.c multibot.c multistat.c \ move.c multiint.c multimenu.c multiopt.c multisync.c multibot.c multistat.c \
objmem.c objects.c order.c parsetest.c radar.c \ mumblelink.c objmem.c objects.c order.c parsetest.c radar.c \
raycast.c research.c scores.c scriptai.c scriptcb.c scriptextern.c scriptfuncs.c \ raycast.c research.c scores.c scriptai.c scriptcb.c scriptextern.c scriptfuncs.c \
scriptobj.c scripttabs.c scriptvals.c selection.c stats.c text.c texture.c \ scriptobj.c scripttabs.c scriptvals.c selection.c stats.c text.c texture.c \
transporter.c visibility.c warcam.c wrappers.c aud.c \ transporter.c visibility.c warcam.c wrappers.c aud.c \

View File

@ -78,6 +78,7 @@
#include "modding.h" #include "modding.h"
#include "multigifts.h" #include "multigifts.h"
#include "multiplay.h" #include "multiplay.h"
#include "mumblelink.h"
#include "projectile.h" #include "projectile.h"
#include "radar.h" #include "radar.h"
#include "lib/framework/cursors.h" #include "lib/framework/cursors.h"
@ -435,6 +436,8 @@ BOOL systemInitialise(void)
return false; return false;
} }
InitMumbleLink();
// Initialize the iVis text rendering module // Initialize the iVis text rendering module
iV_TextInit(); iV_TextInit();
@ -481,6 +484,8 @@ void systemShutdown(void)
fpathShutdown(); fpathShutdown();
CloseMumbleLink();
return; return;
} }

View File

@ -67,6 +67,7 @@
#include "loadsave.h" #include "loadsave.h"
#include "game.h" #include "game.h"
#include "multijoin.h" #include "multijoin.h"
#include "mumblelink.h"
#include "lighting.h" #include "lighting.h"
#include "intimage.h" #include "intimage.h"
#include "lib/framework/cursors.h" #include "lib/framework/cursors.h"
@ -148,6 +149,7 @@ GAMECODE gameLoop(void)
BOOL quitting=false; BOOL quitting=false;
INT_RETVAL intRetVal; INT_RETVAL intRetVal;
int clearMode = 0; int clearMode = 0;
const Vector3f playerRot = Vector3f_ToRadians(Vector3iPSX_To3fDegree(player.r));
if (!war_GetFog()) if (!war_GetFog())
{ {
@ -174,6 +176,10 @@ GAMECODE gameLoop(void)
audio_Update(); audio_Update();
UpdateMumbleSoundPos(Vector3i_To3f(player.p),
Vector3f_EulerToForwardVector(playerRot),
Vector3f_EulerToUpVector(playerRot));
if (!paused) if (!paused)
{ {
if (!scriptPaused() && !editPaused()) if (!scriptPaused() && !editPaused())

View File

@ -59,6 +59,7 @@ SRC=ai.c \
multisync.c \ multisync.c \
multibot.c \ multibot.c \
multistat.c \ multistat.c \
mumblelink.c \
objmem.c \ objmem.c \
objects.c \ objects.c \
order.c \ order.c \

174
src/mumblelink.c Normal file
View File

@ -0,0 +1,174 @@
#include "mumblelink.h"
#include "lib/framework/frame.h"
#include "lib/ivis_common/pievector.h"
#include <wchar.h>
typedef struct
{
uint32_t uiVersion;
int32_t uiTick;
Vector3f position;
Vector3f forward;
Vector3f up;
wchar_t name[256];
} LinkedMem;
#if defined(WZ_OS_UNIX)
#include <unistd.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include <sys/time.h>
#include <time.h>
#endif
#if defined(WZ_OS_WIN)
typedef HANDLE FileHandle;
#define EMPTY_FILEHANDLE NULL
#define MAP_FAILED NULL
#elif defined(WZ_OS_UNIX)
typedef int FileHandle;
#define EMPTY_FILEHANDLE -1
#endif
static const FileHandle EmptyFileHandle = EMPTY_FILEHANDLE;
static FileHandle SharedMem = EMPTY_FILEHANDLE;
static LinkedMem* lm = NULL;
bool InitMumbleLink()
{
// When "doubly" initialising just say we're succesful
if (lm != NULL)
return true;
// Open shared memory (should be created by Mumble)
if (SharedMem == EmptyFileHandle)
{
#if defined(WZ_OS_WIN)
SharedMem = OpenFileMappingW(FILE_MAP_ALL_ACCESS, false, L"MumbleLink");
#elif defined(WZ_OS_UNIX)
char* memname;
sasprintf(&memname, "/MumbleLink.%d", (int)getuid());
SharedMem = shm_open(memname, O_RDWR, S_IRUSR | S_IWUSR);
#endif
if (SharedMem == EmptyFileHandle)
{
#if defined(WZ_OS_WIN)
debug(LOG_NEVER, "Failed to open a shared memory object: %d", GetLastError());
#elif defined(WZ_OS_UNIX)
debug(LOG_NEVER, "Failed to open a shared memory object: %s", strerror(errno));
#endif
return false;
}
}
// Map a LinkedMem instance onto shared memory
if (lm == NULL)
{
#if defined(WZ_OS_WIN)
lm = (LinkedMem *)MapViewOfFile(SharedMem, FILE_MAP_ALL_ACCESS, 0, 0, sizeof(*lm));
#elif defined(WZ_OS_UNIX)
lm = (LinkedMem *)mmap(NULL, sizeof(*lm), PROT_READ | PROT_WRITE, MAP_SHARED, SharedMem, 0);
#endif
if (lm == MAP_FAILED)
{
#if defined(WZ_OS_WIN)
debug(LOG_ERROR, "Failed to map shared memory into local memory: %d", GetLastError());
#elif defined(WZ_OS_UNIX)
debug(LOG_ERROR, "Failed to map shared memory into local memory: %s", strerror(errno));
lm = NULL;
#endif
return false;
}
}
wcsncpy(lm->name, L"Warzone 2100", sizeof(lm->name));
// Guarantee to NUL terminate
lm->name[sizeof(lm->name) - 1] = L'\0';
return true;
}
#if defined(WZ_OS_WIN)
#elif defined(WZ_OS_UNIX)
#endif
bool CloseMumbleLink()
{
if (lm != NULL)
{
#if defined(WZ_OS_WIN)
const bool success = UnmapViewOfFile(lm);
#elif defined(WZ_OS_UNIX)
const bool success = (munmap(lm, sizeof(*lm)) == 0);
#endif
if (!success)
{
#if defined(WZ_OS_WIN)
debug(LOG_ERROR, "Failed to unmap shared memory out of local memory: %d", GetLastError());
#elif defined(WZ_OS_UNIX)
debug(LOG_ERROR, "Failed to unmap shared memory out of local memory: %s", strerror(errno));
#endif
return false;
}
lm = NULL;
}
if (SharedMem != EmptyFileHandle)
{
#if defined(WZ_OS_WIN)
const bool success = CloseHandle(SharedMem);
#elif defined(WZ_OS_UNIX)
const bool success = (close(SharedMem) == 0);
#endif
if (!success)
{
#if defined(WZ_OS_WIN)
debug(LOG_ERROR, "Failed to close a shared memory object: %d", GetLastError());
#elif defined(WZ_OS_UNIX)
debug(LOG_ERROR, "Failed to close a shared memory object: %s", strerror(errno));
#endif
return false;
}
SharedMem = EmptyFileHandle;
}
return true;
}
#if defined(WZ_OS_UNIX)
static const int32_t GetTickCount(void)
{
struct timeval tv;
gettimeofday(&tv, NULL);
const int32_t milliSeconds = tv.tv_usec / 1000 + tv.tv_sec * 1000;
return milliSeconds;
}
#endif
bool UpdateMumbleSoundPos(const Vector3f pos, const Vector3f forward, const Vector3f up)
{
if (lm == NULL)
{
return false;
}
lm->position = pos;
lm->forward = Vector3f_Normalise(forward);
lm->up = Vector3f_Normalise(up);
// Protocol version of Mumble's "Link" plugin
lm->uiVersion = 1;
/* This is used to indicate that this data has been updated. Thus to
* avoid locking conflicts it must be the last change we do to this
* structure.
*/
lm->uiTick = GetTickCount();
return true;
}

12
src/mumblelink.h Normal file
View File

@ -0,0 +1,12 @@
#ifndef __INCLUDED_SRC_MUMBLELINK_H__
#define __INCLUDED_SRC_MUMBLELINK_H__
#include "lib/framework/frame.h"
#include "lib/ivis_common/pievector.h"
extern bool InitMumbleLink(void);
extern bool CloseMumbleLink(void);
extern bool UpdateMumbleSoundPos(const Vector3f pos, const Vector3f forward, const Vector3f up);
#endif // __INCLUDED_SRC_MUMBLELINK_H__

View File

@ -2385,6 +2385,19 @@
<Option target="DBGWindows" /> <Option target="DBGWindows" />
<Option target="NDBGWindows" /> <Option target="NDBGWindows" />
</Unit> </Unit>
<Unit filename="src/mumblelink.c">
<Option compilerVar="CC" />
<Option target="DBGUnix" />
<Option target="NDBGUnix" />
<Option target="DBGWindows" />
<Option target="NDBGWindows" />
</Unit>
<Unit filename="src/mumblelink.h">
<Option target="DBGUnix" />
<Option target="NDBGUnix" />
<Option target="DBGWindows" />
<Option target="NDBGWindows" />
</Unit>
<Unit filename="src/objectdef.h"> <Unit filename="src/objectdef.h">
<Option target="DBGUnix" /> <Option target="DBGUnix" />
<Option target="NDBGUnix" /> <Option target="NDBGUnix" />

View File

@ -524,6 +524,10 @@
RelativePath="..\src\multisync.c" RelativePath="..\src\multisync.c"
> >
</File> </File>
<File
RelativePath="..\src\mumblelink.c"
>
</File>
<File <File
RelativePath="..\src\objects.c" RelativePath="..\src\objects.c"
> >
@ -1027,6 +1031,10 @@
RelativePath="..\src\multistat.h" RelativePath="..\src\multistat.h"
> >
</File> </File>
<File
RelativePath="..\src\mumblelink.h"
>
</File>
<File <File
RelativePath="..\src\objectdef.h" RelativePath="..\src\objectdef.h"
> >