2007-01-15 12:09:25 -08:00
|
|
|
/*
|
|
|
|
This file is part of Warzone 2100.
|
|
|
|
Copyright (C) 1999-2004 Eidos Interactive
|
2010-07-26 16:36:29 -07:00
|
|
|
Copyright (C) 2005-2010 Warzone 2100 Project
|
2007-01-15 12:09:25 -08:00
|
|
|
|
|
|
|
Warzone 2100 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.
|
|
|
|
|
|
|
|
Warzone 2100 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 Warzone 2100; if not, write to the Free Software
|
|
|
|
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*/
|
2007-06-28 10:47:08 -07:00
|
|
|
/*
|
|
|
|
* ScriptVals.c
|
|
|
|
*
|
|
|
|
* Script data value functions
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2006-11-06 06:40:07 -08:00
|
|
|
#include <string.h>
|
2007-06-28 10:47:08 -07:00
|
|
|
|
2006-05-27 09:37:17 -07:00
|
|
|
#include "lib/framework/frame.h"
|
|
|
|
#include "lib/script/script.h"
|
2007-06-28 10:47:08 -07:00
|
|
|
#include "objects.h"
|
2007-07-12 12:56:16 -07:00
|
|
|
#include "basedef.h"
|
2007-06-28 10:47:08 -07:00
|
|
|
#include "scripttabs.h"
|
|
|
|
#include "scriptvals.h"
|
2006-05-27 09:37:17 -07:00
|
|
|
#include "lib/gamelib/gtime.h"
|
2007-06-28 10:47:08 -07:00
|
|
|
#include "group.h"
|
|
|
|
|
|
|
|
// Keep all the loaded script contexts
|
|
|
|
typedef struct _scrv_store
|
|
|
|
{
|
|
|
|
SCRV_TYPE type;
|
2006-11-03 13:35:50 -08:00
|
|
|
char *pIDString;
|
2007-06-28 10:47:08 -07:00
|
|
|
SCRIPT_CONTEXT *psContext;
|
|
|
|
|
|
|
|
struct _scrv_store *psNext;
|
|
|
|
} SCRV_STORE;
|
|
|
|
|
|
|
|
// The list of script contexts
|
|
|
|
static SCRV_STORE *psContextStore=NULL;
|
|
|
|
|
|
|
|
// keep a note of all base object pointers
|
2007-05-05 08:28:21 -07:00
|
|
|
#define MAX_BASEPOINTER 2000 //200 - local variables require more of these ("run" error)
|
2007-06-28 10:47:08 -07:00
|
|
|
static INTERP_VAL *asBasePointers[MAX_BASEPOINTER];
|
|
|
|
|
|
|
|
// Initialise the script value module
|
|
|
|
BOOL scrvInitialise(void)
|
|
|
|
{
|
|
|
|
psContextStore = NULL;
|
|
|
|
memset(asBasePointers, 0, sizeof(asBasePointers));
|
|
|
|
|
2008-03-24 09:51:17 -07:00
|
|
|
return true;
|
2007-06-28 10:47:08 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Shut down the script value module
|
|
|
|
void scrvShutDown(void)
|
|
|
|
{
|
|
|
|
SCRV_STORE *psCurr;
|
|
|
|
while (psContextStore)
|
|
|
|
{
|
|
|
|
psCurr = psContextStore;
|
|
|
|
psContextStore = psContextStore->psNext;
|
|
|
|
|
2007-04-15 03:43:05 -07:00
|
|
|
free(psCurr->pIDString);
|
|
|
|
free(psCurr);
|
2007-06-28 10:47:08 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// reset the script value module
|
|
|
|
void scrvReset(void)
|
|
|
|
{
|
|
|
|
SCRV_STORE *psCurr;
|
|
|
|
while (psContextStore)
|
|
|
|
{
|
|
|
|
psCurr = psContextStore;
|
|
|
|
psContextStore = psContextStore->psNext;
|
|
|
|
|
2007-04-15 03:43:05 -07:00
|
|
|
free(psCurr->pIDString);
|
|
|
|
free(psCurr);
|
2007-06-28 10:47:08 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
psContextStore = NULL;
|
|
|
|
memset(asBasePointers, 0, sizeof(asBasePointers));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Add a new context to the list
|
2006-11-03 13:35:50 -08:00
|
|
|
BOOL scrvAddContext(char *pID, SCRIPT_CONTEXT *psContext, SCRV_TYPE type)
|
2007-06-28 10:47:08 -07:00
|
|
|
{
|
|
|
|
SCRV_STORE *psNew;
|
|
|
|
|
2007-04-15 03:43:05 -07:00
|
|
|
psNew = (SCRV_STORE*)malloc(sizeof(SCRV_STORE));
|
2007-06-28 10:47:08 -07:00
|
|
|
if (!psNew)
|
|
|
|
{
|
2009-10-30 20:57:44 -07:00
|
|
|
debug( LOG_FATAL, "scrvAddContext: Out of memory" );
|
2006-08-22 07:28:49 -07:00
|
|
|
abort();
|
2008-03-24 09:51:17 -07:00
|
|
|
return false;
|
2007-06-28 10:47:08 -07:00
|
|
|
}
|
2007-04-15 03:43:05 -07:00
|
|
|
psNew->pIDString = (char*)malloc(strlen(pID) + 1);
|
2007-06-28 10:47:08 -07:00
|
|
|
if (!psNew->pIDString)
|
|
|
|
{
|
2009-10-30 20:57:44 -07:00
|
|
|
debug( LOG_FATAL, "scrvAddContext: Out of memory" );
|
2006-08-22 07:28:49 -07:00
|
|
|
abort();
|
2008-03-24 09:51:17 -07:00
|
|
|
return false;
|
2007-06-28 10:47:08 -07:00
|
|
|
}
|
|
|
|
strcpy(psNew->pIDString, pID);
|
|
|
|
psNew->type = type;
|
|
|
|
psNew->psContext = psContext;
|
|
|
|
|
|
|
|
psNew->psNext = psContextStore;
|
|
|
|
psContextStore = psNew;
|
|
|
|
|
2008-03-24 09:51:17 -07:00
|
|
|
return true;
|
2007-06-28 10:47:08 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Add a new base pointer variable
|
|
|
|
BOOL scrvAddBasePointer(INTERP_VAL *psVal)
|
|
|
|
{
|
|
|
|
SDWORD i;
|
|
|
|
|
|
|
|
for(i=0; i<MAX_BASEPOINTER; i++)
|
|
|
|
{
|
|
|
|
if (asBasePointers[i] == NULL)
|
|
|
|
{
|
|
|
|
asBasePointers[i] = psVal;
|
2008-03-24 09:51:17 -07:00
|
|
|
return true;
|
2007-06-28 10:47:08 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-03-24 09:51:17 -07:00
|
|
|
ASSERT(false, "scrvAddBasePointer: not enough base pointers left (total :%d)", MAX_BASEPOINTER);
|
|
|
|
return false;
|
2007-06-28 10:47:08 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// remove a base pointer from the list
|
|
|
|
void scrvReleaseBasePointer(INTERP_VAL *psVal)
|
|
|
|
{
|
|
|
|
SDWORD i;
|
|
|
|
|
|
|
|
for(i=0; i<MAX_BASEPOINTER; i++)
|
|
|
|
{
|
|
|
|
if (asBasePointers[i] == psVal)
|
|
|
|
{
|
|
|
|
asBasePointers[i] = NULL;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Check all the base pointers to see if they have died
|
|
|
|
void scrvUpdateBasePointers(void)
|
|
|
|
{
|
2008-03-25 17:27:56 -07:00
|
|
|
unsigned int i;
|
2007-06-28 10:47:08 -07:00
|
|
|
|
2008-03-25 17:27:56 -07:00
|
|
|
for(i = 0; i < MAX_BASEPOINTER; i++)
|
2007-06-28 10:47:08 -07:00
|
|
|
{
|
|
|
|
if (asBasePointers[i] != NULL)
|
|
|
|
{
|
2008-03-25 17:27:56 -07:00
|
|
|
INTERP_VAL *psVal = asBasePointers[i];
|
|
|
|
BASE_OBJECT *psObj = (BASE_OBJECT *)psVal->v.oval;
|
2007-06-28 10:47:08 -07:00
|
|
|
|
2007-08-11 02:31:16 -07:00
|
|
|
if (psObj && isDead(psObj))
|
2007-06-28 10:47:08 -07:00
|
|
|
{
|
|
|
|
psVal->v.oval = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// create a group structure for a ST_GROUP variable
|
|
|
|
BOOL scrvNewGroup(INTERP_VAL *psVal)
|
|
|
|
{
|
|
|
|
DROID_GROUP *psGroup;
|
|
|
|
|
|
|
|
if (!grpCreate(&psGroup))
|
|
|
|
{
|
2008-03-24 09:51:17 -07:00
|
|
|
return false;
|
2007-06-28 10:47:08 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// increment the refcount so the group doesn't get automatically freed when empty
|
|
|
|
grpJoin(psGroup, NULL);
|
|
|
|
|
|
|
|
psVal->v.oval = psGroup;
|
|
|
|
|
2008-03-24 09:51:17 -07:00
|
|
|
return true;
|
2007-06-28 10:47:08 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// release a ST_GROUP variable
|
|
|
|
void scrvReleaseGroup(INTERP_VAL *psVal)
|
|
|
|
{
|
|
|
|
DROID_GROUP *psGroup;
|
|
|
|
|
2007-01-08 05:40:12 -08:00
|
|
|
psGroup = (DROID_GROUP*)psVal->v.oval;
|
2007-06-28 10:47:08 -07:00
|
|
|
grpReset(psGroup);
|
|
|
|
|
2006-08-23 05:58:48 -07:00
|
|
|
ASSERT( psGroup->refCount == 1,
|
|
|
|
"scrvReleaseGroup: ref count is wrong" );
|
2007-06-28 10:47:08 -07:00
|
|
|
|
|
|
|
// do a final grpLeave to free the group
|
|
|
|
grpLeave(psGroup, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Get a context from the list
|
2006-11-03 13:35:50 -08:00
|
|
|
BOOL scrvGetContext(char *pID, SCRIPT_CONTEXT **ppsContext)
|
2007-06-28 10:47:08 -07:00
|
|
|
{
|
|
|
|
SCRV_STORE *psCurr;
|
|
|
|
|
|
|
|
for(psCurr=psContextStore; psCurr; psCurr=psCurr->psNext)
|
|
|
|
{
|
|
|
|
if (strcmp(psCurr->pIDString, pID) == 0)
|
|
|
|
{
|
|
|
|
*ppsContext = psCurr->psContext;
|
2008-03-24 09:51:17 -07:00
|
|
|
return true;
|
2007-06-28 10:47:08 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-10-30 20:57:44 -07:00
|
|
|
debug( LOG_FATAL, "scrvGetContext: couldn't find context for id: %s", pID );
|
2006-08-22 07:28:49 -07:00
|
|
|
abort();
|
2008-03-24 09:51:17 -07:00
|
|
|
return false;
|
2007-06-28 10:47:08 -07:00
|
|
|
}
|