Add optional param unittype to ChangeUnitsOwner.

And rewrite the function using tolua++.
This commit is contained in:
kon 2011-03-05 14:02:44 +00:00
parent 9ac3a69fc3
commit 3efba8c31c
4 changed files with 144 additions and 26 deletions

View file

@ -44,6 +44,8 @@
#include "ai.h"
#include "actions.h"
#include "commands.h"
#include "tolua++.h"
#include "trigger.h"
/*----------------------------------------------------------------------------
-- Variables
@ -246,37 +248,56 @@ static int CclPlayer(lua_State *l)
**
** @param l Lua state.
*/
static int CclChangeUnitsOwner(lua_State *l)
void CclChangeUnitsOwner(
const int topLeft[2],
const int bottomRight[2],
int oldPlayer,
int newPlayer,
lua_Object unitTypeLua,
lua_State *l)
{
CUnit *table[UnitMax];
const CUnitType *triggerUnitType;
if (oldPlayer < 0 || oldPlayer >= PlayerMax) {
LuaError(l, "Player number is out of range: %d" _C_ oldPlayer);
}
if (newPlayer < 0 || newPlayer >= PlayerMax) {
LuaError(l, "Player number is out of range: %d" _C_ newPlayer);
}
// tolua++ 1.0.93 claims to define TOLUA_NIL, but doesn't,
// so we use 0 instead.
//
// If the argument is nil, behave as if it had been omitted.
// This feature is not documented in game.html because
// script authors should use "any" rather than nil.
if (unitTypeLua == 0 /* TOLUA_NIL */ || lua_isnil(l, unitTypeLua)) {
triggerUnitType = ANY_UNIT;
} else {
lua_pushvalue(l, unitTypeLua);
triggerUnitType = TriggerGetUnitType(l);
lua_pop(l, 1);
}
// Okay, now Lua won't longjmp any more.
CUnit *units[UnitMax];
int n;
int oldp;
int newp;
int x1;
int y1;
int x2;
int y2;
const int x1 = topLeft[0];
const int y1 = topLeft[1];
const int x2 = bottomRight[0];
const int y2 = bottomRight[1];
LuaCheckArgs(l, 4);
LuaCheckTableSize(l, 1, 2);
x1 = LuaToNumber(l, 1, 1);
y1 = LuaToNumber(l, 1, 2);
LuaCheckTableSize(l, 2, 2);
x2 = LuaToNumber(l, 2, 1);
y2 = LuaToNumber(l, 2, 2);
n = UnitCache.Select(x1, y1, x2 + 1, y2 + 1, table, UnitMax);
oldp = LuaToNumber(l, 3);
newp = LuaToNumber(l, 4);
n = UnitCache.Select(x1, y1, x2 + 1, y2 + 1, units, UnitMax);
while (n) {
if (table[n - 1]->Player->Index == oldp) {
table[n - 1]->ChangeOwner(&Players[newp]);
CUnit *const unit = units[n - 1];
if (unit->Player->Index == oldPlayer
&& TriggerMatchUnitType(unit, triggerUnitType)) {
unit->ChangeOwner(&Players[newPlayer]);
}
--n;
}
return 0;
}
/**
@ -505,7 +526,6 @@ static int CclDefinePlayerColorIndex(lua_State *l)
void PlayerCclRegister(void)
{
lua_register(Lua, "Player", CclPlayer);
lua_register(Lua, "ChangeUnitsOwner", CclChangeUnitsOwner);
lua_register(Lua, "SetMaxSelectable", CclSetMaxSelectable);

View file

@ -245,6 +245,9 @@
-- Declarations
----------------------------------------------------------------------------*/
typedef int lua_Object; // from tolua++.h
struct lua_State;
class CUnit;
class CUnitType;
class PlayerAi;
@ -482,6 +485,14 @@ extern void DebugPlayers(void);
/// register ccl features
extern void PlayerCclRegister(void);
extern void CclChangeUnitsOwner(
const int topLeft[2],
const int bottomRight[2],
int oldPlayer,
int newPlayer,
lua_Object unitTypeLua,
lua_State *l);
/// Allowed to select multiple units, maybe not mine
#define CanSelectMultipleUnits(player) \
((player) == ThisPlayer || ThisPlayer->IsTeamed((player)))

View file

@ -1,3 +1,5 @@
$#include "player.h"
enum PlayerTypes {
PlayerNeutral = 2,
PlayerNobody = 3,
@ -61,3 +63,16 @@ class CPlayer
extern CPlayer Players[PlayerMax];
extern CPlayer *ThisPlayer;
// tolua++ 1.0.93 claims to define TOLUA_NIL, but doesn't,
// so we use 0 instead.
//
// tolua++ 1.0.93 generates code that copies the contents
// of the arrays from the wrong Lua stack indexes if the
// lua_State * parameter is the first one, so put it last.
void CclChangeUnitsOwner @ ChangeUnitsOwner(
int topLeft[2],
int bottomRight[2],
int oldPlayer,
int newPlayer,
lua_Object unitType = 0 /* TOLUA_NIL */,
lua_State *l);

View file

@ -1,6 +1,6 @@
/*
** Lua binding: stratagus
** Generated automatically by tolua++-1.0.92 on Tue Nov 23 22:03:42 2010.
** Generated automatically by tolua++-1.0.92 on Sat Mar 5 15:52:42 2011.
*/
#ifndef __cplusplus
@ -37,6 +37,7 @@ int GetNetworkState() {return (int)NetLocalState;}
extern string NetworkMapName;
void NetworkGamePrepareGameSettings(void);
extern void InitVideo();
#include "player.h"
#include "editor.h"
bool IsReplayGame();
void StartMap(const string &str, bool clean = true);
@ -15047,6 +15048,76 @@ static int tolua_set_ThisPlayer_ptr(lua_State* tolua_S)
}
#endif //#ifndef TOLUA_DISABLE
/* function: CclChangeUnitsOwner */
#ifndef TOLUA_DISABLE_tolua_stratagus_ChangeUnitsOwner00
static int tolua_stratagus_ChangeUnitsOwner00(lua_State* tolua_S)
{
#ifndef TOLUA_RELEASE
tolua_Error tolua_err;
if (
!tolua_istable(tolua_S,1,0,&tolua_err) ||
!tolua_istable(tolua_S,2,0,&tolua_err) ||
!tolua_isnumber(tolua_S,3,0,&tolua_err) ||
!tolua_isnumber(tolua_S,4,0,&tolua_err) ||
!tolua_isnoobj(tolua_S,6,&tolua_err)
)
goto tolua_lerror;
else
#endif
{
int topLeft[2];
int bottomRight[2];
int oldPlayer = ((int) tolua_tonumber(tolua_S,3,0));
int newPlayer = ((int) tolua_tonumber(tolua_S,4,0));
lua_Object unitType = ((lua_Object) tolua_tovalue(tolua_S,5,0));
lua_State* l = tolua_S;
{
#ifndef TOLUA_RELEASE
if (!tolua_isnumberarray(tolua_S,1,2,0,&tolua_err))
goto tolua_lerror;
else
#endif
{
int i;
for(i=0; i<2;i++)
topLeft[i] = ((int) tolua_tofieldnumber(tolua_S,1,i+1,0));
}
}
{
#ifndef TOLUA_RELEASE
if (!tolua_isnumberarray(tolua_S,2,2,0,&tolua_err))
goto tolua_lerror;
else
#endif
{
int i;
for(i=0; i<2;i++)
bottomRight[i] = ((int) tolua_tofieldnumber(tolua_S,2,i+1,0));
}
}
{
CclChangeUnitsOwner(topLeft,bottomRight,oldPlayer,newPlayer,unitType,l);
}
{
int i;
for(i=0; i<2;i++)
tolua_pushfieldnumber(tolua_S,1,i+1,(lua_Number) topLeft[i]);
}
{
int i;
for(i=0; i<2;i++)
tolua_pushfieldnumber(tolua_S,2,i+1,(lua_Number) bottomRight[i]);
}
}
return 0;
#ifndef TOLUA_RELEASE
tolua_lerror:
tolua_error(tolua_S,"#ferror in function 'ChangeUnitsOwner'.",&tolua_err);
return 0;
#endif
}
#endif //#ifndef TOLUA_DISABLE
/* get function: Ident of class CUnitType */
#ifndef TOLUA_DISABLE_tolua_get_CUnitType_Ident
static int tolua_get_CUnitType_Ident(lua_State* tolua_S)
@ -20429,6 +20500,7 @@ TOLUA_API int tolua_stratagus_open (lua_State* tolua_S)
tolua_endmodule(tolua_S);
tolua_array(tolua_S,"Players",tolua_get_stratagus_Players,tolua_set_stratagus_Players);
tolua_variable(tolua_S,"ThisPlayer",tolua_get_ThisPlayer_ptr,tolua_set_ThisPlayer_ptr);
tolua_function(tolua_S,"ChangeUnitsOwner",tolua_stratagus_ChangeUnitsOwner00);
tolua_cclass(tolua_S,"CUnitType","CUnitType","",NULL);
tolua_beginmodule(tolua_S,"CUnitType");
tolua_variable(tolua_S,"Ident",tolua_get_CUnitType_Ident,tolua_set_CUnitType_Ident);