Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix vectors, required and optional parameters handling in ColShape.Polygon APIs (client and server) #163

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
28 changes: 24 additions & 4 deletions Client/mods/deathmatch/logic/luadefs/CLuaColShapeDefs.cpp
Expand Up @@ -219,10 +219,23 @@ int CLuaColShapeDefs::CreateColRectangle ( lua_State* luaVM )

int CLuaColShapeDefs::CreateColPolygon ( lua_State* luaVM )
{
// colshape createColPolygon ( float fX, float fY, float fX1, float fY1, float fX2, float fY2, float fX3, float fY3, ... )

CVector2D vecPosition;
std::vector < CVector2D > vecPointList;
CScriptArgReader argStream ( luaVM );

// Get position
argStream.ReadVector2D ( vecPosition );

// Get first 3 required points
for ( uint i = 0; i < 3; i++ )
{
CVector2D vecPoint;
argStream.ReadVector2D ( vecPoint );
vecPointList.push_back ( vecPoint );
}

if ( !argStream.HasErrors () )
{
CLuaMain* pLuaMain = m_pLuaManager->GetVirtualMachine ( luaVM );
Expand All @@ -235,11 +248,18 @@ int CLuaColShapeDefs::CreateColPolygon ( lua_State* luaVM )
CClientColPolygon* pShape = CStaticFunctionDefinitions::CreateColPolygon ( *pResource, vecPosition );
if ( pShape )
{
// Get the points
while ( argStream.NextCouldBeNumber () && argStream.NextCouldBeNumber ( 1 ) )
// Get additional points
while ( argStream.NextIsVector2D () )
{
CVector2D vecPoint;
argStream.ReadVector2D ( vecPoint );
vecPointList.push_back ( vecPoint );
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why does vecPointList need to exist if its sole purpose is to perform pShape->AddPoint( vecPointList[i] );?

Can't this line be replaced with pShape->AddPoint( vecPoint );?

(This is an honest question. There's probably a very good reason as to why you're doing this!)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is needed to store first 4 required vectors (position + 3 points),. An alternative would be to declare 4 separate variables. And since the list already exists and must be iterated over anyway, I thought it's reasonable to reuse it by pushing optional points to it too. This way AddPoint is called only in one loop instead of two which is more clean IMHO :) I don't know much about performance aspects here though.

}

// Add all points
for( uint i = 0 ; i < vecPointList.size() ; i++ )
{
argStream.ReadVector2D ( vecPosition );
pShape->AddPoint ( vecPosition );
pShape->AddPoint( vecPointList[i] );
}

CElementGroup * pGroup = pResource->GetElementGroup ();
Expand Down
12 changes: 11 additions & 1 deletion Server/mods/deathmatch/logic/luadefs/CLuaColShapeDefs.cpp
Expand Up @@ -215,10 +215,12 @@ int CLuaColShapeDefs::CreateColRectangle ( lua_State* luaVM )
int CLuaColShapeDefs::CreateColPolygon ( lua_State* luaVM )
{
// colshape createColPolygon ( float fX, float fY, float fX1, float fY1, float fX2, float fY2, float fX3, float fY3, ... )

std::vector < CVector2D > vecPointList;

// Get position and first 3 required points
CScriptArgReader argStream ( luaVM );
for ( uint i = 0; i < 4 || argStream.NextCouldBeNumber (); i++ )
for ( uint i = 0; i < 4; i++ )
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why didn't (i < 4) || argStream.NextCouldBeNumber(); work before? It looks like it should work (when clearly it didn't).

Did you test with (i < 4) || argStream.NextIsVector2D(); first?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The former did work fine with numbers only, it couldn't work with more than 3 points passed as vectors. The latter worked fine, I indeed tested it first. I guess I then decided to make Client and Server side code look more alike.
I could of course do it by simply using that for loop (with fixed conditon) in Client side code too instead of while 馃槃 But I think it's just a cosmetic thing and the main difference is direction of code sync. Will change this if needed :)

{
CVector2D vecPoint;
argStream.ReadVector2D ( vecPoint );
Expand All @@ -233,6 +235,14 @@ int CLuaColShapeDefs::CreateColPolygon ( lua_State* luaVM )
CResource* pResource = pLuaMain->GetResource ();
if ( pResource )
{
// Get additional points
while ( argStream.NextIsVector2D () )
{
CVector2D vecPoint;
argStream.ReadVector2D ( vecPoint );
vecPointList.push_back ( vecPoint );
}

CColPolygon* pShape = CStaticFunctionDefinitions::CreateColPolygon ( pResource, vecPointList );
if ( pShape )
{
Expand Down