Merge branch 'android_branch' into android_bt

This commit is contained in:
Eric House 2012-05-05 11:08:03 -07:00
commit 64199c4e64
4 changed files with 85 additions and 38 deletions

View file

@ -72,6 +72,7 @@ static void buildModelFromStack( ModelCtxt* model, StackCtxt* stack,
MovePrintFuncPost mpfpo, void* closure );
static void setPendingCounts( ModelCtxt* model, XP_S16 turn );
static XP_S16 setContains( const TrayTileSet* tiles, Tile tile );
static void sortTiles( TrayTileSet* dest, const TrayTileSet* src );
static void removeTile( TrayTileSet* tiles, XP_U16 index );
static void loadPlayerCtxt( const ModelCtxt* model, XWStreamCtxt* stream,
XP_U16 version, PlayerCtxt* pc );
@ -1765,7 +1766,9 @@ model_assignPlayerTiles( ModelCtxt* model, XP_S16 turn,
const TrayTileSet* tiles )
{
XP_ASSERT( turn >= 0 );
stack_addAssign( model->vol.stack, turn, tiles );
TrayTileSet sorted;
sortTiles( &sorted, tiles );
stack_addAssign( model->vol.stack, turn, &sorted );
assignPlayerTiles( model, turn, tiles );
} /* model_assignPlayerTiles */
@ -1773,30 +1776,17 @@ model_assignPlayerTiles( ModelCtxt* model, XP_S16 turn,
void
model_sortTiles( ModelCtxt* model, XP_S16 turn )
{
PlayerCtxt* player;
XP_S16 ii;
TrayTileSet tiles = { .nTiles = 0 };
XP_S16 nTiles;
XP_ASSERT( turn >= 0 );
player = &model->players[turn];
TrayTileSet sorted;
sortTiles( &sorted, model_getPlayerTiles( model, turn ) );
for ( nTiles = model_getNumTilesInTray( model, turn ) - 1;
nTiles >= 0; --nTiles ) {
Tile min = 0xFF;
XP_U16 minIndex = 0;
for ( ii = 0; ii <= nTiles; ++ii ) {
Tile tile = player->trayTiles.tiles[ii];
if ( tile < min ) {
min = tile;
minIndex = ii;
}
}
tiles.tiles[tiles.nTiles++] =
removePlayerTile( model, turn, minIndex );
nTiles = sorted.nTiles;
while ( nTiles > 0 ) {
removePlayerTile( model, turn, --nTiles );
}
assignPlayerTiles( model, turn, &tiles );
assignPlayerTiles( model, turn, &sorted );
} /* model_sortTiles */
XP_U16
@ -2449,6 +2439,23 @@ setContains( const TrayTileSet* tiles, Tile tile )
return result;
}
static void
sortTiles( TrayTileSet* dest, const TrayTileSet* src )
{
TrayTileSet tmp = *src;
dest->nTiles = 0;
while ( 0 < tmp.nTiles ) {
XP_U16 ii, smallest;
for ( smallest = ii = 0; ii < tmp.nTiles; ++ii ) {
if ( tmp.tiles[ii] < tmp.tiles[smallest] ) {
smallest = ii;
}
}
dest->tiles[dest->nTiles++] = tmp.tiles[smallest];
removeTile( &tmp, smallest );
}
}
#ifdef CPLUS
}
#endif

View file

@ -26,6 +26,15 @@
#include "memstream.h"
#include "strutils.h"
/* HASH_STREAM: It should be possible to hash the move stack by simply hashing
the stream from the beginning to the top of the undo stack (excluding
what's in the redo area), avoiding iterating over it and doing a ton of
bitwise operations to read it into entries. But I don't currently seem to
have a XWStreamPos that corresponds to the undo-top and so I can't figure
out the length. Hashing that includes the redo part of the stack doesn't
work once there's been undo activity. (Not sure why...) */
// #define HASH_STREAM
#ifdef CPLUS
extern "C" {
#endif
@ -58,8 +67,22 @@ stack_init( StackCtxt* stack )
} /* stack_init */
#ifdef STREAM_VERS_BIGBOARD
#ifdef HASH_STREAM
static XP_U16
figureStackSize( const StackCtxt* stack )
{
XWStreamCtxt* data = stack->data;
XWStreamPos oldReadPos = stream_setPos( data, POS_READ, START_OF_STREAM );
XWStreamPos oldWritePos = stream_setPos( data, POS_WRITE, stack->cachedPos );
XP_U16 len = stream_getSize( data );
(void)stream_setPos( data, POS_READ, oldReadPos );
(void)stream_setPos( data, POS_WRITE, oldWritePos );
return len;
}
#endif
static XP_U32
augmentHash( XP_U32 hash, XP_U8* ptr, XP_U16 len )
augmentHash( XP_U32 hash, const XP_U8* ptr, XP_U16 len )
{
XP_ASSERT( 0 < len );
// see http://en.wikipedia.org/wiki/Jenkins_hash_function
@ -72,9 +95,11 @@ augmentHash( XP_U32 hash, XP_U8* ptr, XP_U16 len )
hash += (hash << 3);
hash ^= (hash >> 11);
hash += (hash << 15);
// XP_LOGF( "%s: hashed %d bytes -> %X", __func__, len, (unsigned int)hash );
return hash;
}
#ifndef HASH_STREAM
static XP_U32
augmentFor( XP_U32 hash, const StackEntry* entry )
{
@ -98,12 +123,20 @@ augmentFor( XP_U32 hash, const StackEntry* entry )
}
return hash;
}
#endif
XP_U32
stack_getHash( StackCtxt* stack )
{
XP_U32 hash = 0L;
XP_U32 hash;
#ifdef HASH_STREAM
XP_U16 len = figureStackSize( stack );
const XP_U8* ptr = stream_getPtr( stack->data );
LOG_HEX( ptr, len, __func__ );
hash = augmentHash( 0L, ptr, len );
#else
XP_U16 nn, nEntries = stack->nEntries;
hash = 0L;
for ( nn = 0; nn < nEntries; ++nn ) {
StackEntry entry;
XP_MEMSET( &entry, 0, sizeof(entry) );
@ -115,6 +148,7 @@ stack_getHash( StackCtxt* stack )
}
XP_ASSERT( 0 != hash );
// LOG_RETURNF( "%.8X", (unsigned int)hash );
#endif
return hash;
}
#endif
@ -214,7 +248,7 @@ stack_copy( const StackCtxt* stack )
static void
pushEntry( StackCtxt* stack, const StackEntry* entry )
{
XP_U16 i, bitsPerTile;
XP_U16 ii, bitsPerTile;
XWStreamPos oldLoc;
XP_U16 nTiles = entry->u.move.moveInfo.nTiles;
XWStreamCtxt* stream = stack->data;
@ -239,12 +273,12 @@ pushEntry( StackCtxt* stack, const StackEntry* entry )
stream_putBits( stream, 1, entry->u.move.moveInfo.isHorizontal );
bitsPerTile = stack->bitsPerTile;
XP_ASSERT( bitsPerTile == 5 || bitsPerTile == 6 );
for ( i = 0; i < nTiles; ++i ) {
for ( ii = 0; ii < nTiles; ++ii ) {
Tile tile;
stream_putBits( stream, 5,
entry->u.move.moveInfo.tiles[i].varCoord );
entry->u.move.moveInfo.tiles[ii].varCoord );
tile = entry->u.move.moveInfo.tiles[i].tile;
tile = entry->u.move.moveInfo.tiles[ii].tile;
stream_putBits( stream, bitsPerTile, tile & TILE_VALUE_MASK );
stream_putBits( stream, 1, (tile & TILE_BLANK_BIT) != 0 );
}
@ -270,6 +304,7 @@ pushEntry( StackCtxt* stack, const StackEntry* entry )
++stack->nEntries;
stack->highWaterMark = stack->nEntries;
stack->top = stream_setPos( stream, POS_WRITE, oldLoc );
// XP_LOGF( "after %s size now %d", __func__, figureStackSize( stack ) );
} /* pushEntry */
static void
@ -380,18 +415,18 @@ stack_addAssign( StackCtxt* stack, XP_U16 turn, const TrayTileSet* tiles )
} /* stack_addAssign */
static XP_Bool
setCacheReadyFor( StackCtxt* stack, XP_U16 n )
setCacheReadyFor( StackCtxt* stack, XP_U16 nn )
{
StackEntry dummy;
XP_U16 i;
XP_U16 ii;
stream_setPos( stack->data, POS_READ, START_OF_STREAM );
for ( i = 0; i < n; ++i ) {
for ( ii = 0; ii < nn; ++ii ) {
StackEntry dummy;
readEntry( stack, &dummy );
}
stack->cacheNext = n;
stack->cachedPos = stream_getPos( stack->data, XP_FALSE );
stack->cacheNext = nn;
stack->cachedPos = stream_getPos( stack->data, POS_READ );
return XP_TRUE;
} /* setCacheReadyFor */
@ -434,14 +469,15 @@ stack_getNthEntry( StackCtxt* stack, XP_U16 nn, StackEntry* entry )
XP_Bool
stack_popEntry( StackCtxt* stack, StackEntry* entry )
{
XP_U16 n = stack->nEntries - 1;
XP_Bool found = stack_getNthEntry( stack, n, entry );
XP_U16 nn = stack->nEntries - 1;
XP_Bool found = stack_getNthEntry( stack, nn, entry );
if ( found ) {
stack->nEntries = n;
stack->nEntries = nn;
setCacheReadyFor( stack, n ); /* set cachedPos by side-effect */
setCacheReadyFor( stack, nn ); /* set cachedPos by side-effect */
stack->top = stack->cachedPos;
}
//XP_LOGF( "after %s size now %d", __func__, figureStackSize( stack ) );
return found;
} /* stack_popEntry */

View file

@ -1706,6 +1706,10 @@ cursesmain( XP_Bool isServer, LaunchParams* params )
g_globals.cGlobals.cp.showBoardArrow = XP_TRUE;
g_globals.cGlobals.cp.showRobotScores = params->showRobotScores;
g_globals.cGlobals.cp.hideTileValues = params->hideValues;
g_globals.cGlobals.cp.skipCommitConfirm = params->skipCommitConfirm;
g_globals.cGlobals.cp.sortNewTiles = params->sortNewTiles;
g_globals.cGlobals.cp.showColors = params->showColors;
g_globals.cGlobals.cp.allowPeek = params->allowPeek;
#ifdef XWFEATURE_SLOW_ROBOT
g_globals.cGlobals.cp.robotThinkMin = params->robotThinkMin;
g_globals.cGlobals.cp.robotThinkMax = params->robotThinkMax;

View file

@ -159,7 +159,7 @@ build_cmds() {
fi
PARAMS="$(player_params $NLOCALS $NPLAYERS $DEV)"
PARAMS="$PARAMS $BOARD_SIZE --room $ROOM --trade-pct 20"
PARAMS="$PARAMS $BOARD_SIZE --room $ROOM --trade-pct 20 --sort-tiles "
PARAMS="$PARAMS --game-dict $DICT --port $PORT --host $HOST "
PARAMS="$PARAMS --file $FILE --slow-robot 1:3 --skip-confirm"
PARAMS="$PARAMS --drop-nth-packet $DROP_N $PLAT_PARMS"
@ -481,7 +481,7 @@ while [ "$#" -gt 0 ]; do
APP_NEW=$(getArg $*)
shift
;;
--dict)
--game-dict)
DICTS[${#DICTS[@]}]=$(getArg $*)
shift
;;