diff --git a/xwords4/android/XWords4/jni/Android.mk b/xwords4/android/XWords4/jni/Android.mk index 3b1640337..6277a98a6 100644 --- a/xwords4/android/XWords4/jni/Android.mk +++ b/xwords4/android/XWords4/jni/Android.mk @@ -10,7 +10,7 @@ local_C_INCLUDES+= \ local_LDLIBS += -llog -# local_DEBUG = -DMEM_DEBUG -DDEBUG -DENABLE_LOGGING +local_DEBUG = -DMEM_DEBUG -DDEBUG -DENABLE_LOGGING local_DEFINES += \ $(local_DEBUG) \ -DXWFEATURE_RELAY \ @@ -21,6 +21,7 @@ local_DEFINES += \ -DPLATFORM_ANDROID \ -DXWFEATURE_CROSSHAIRS \ -DPOINTER_SUPPORT \ + -DXWFEATURE_RANDOM_VIA_UTILS \ -DSCROLL_DRAG_THRESHHOLD=1 \ -DDROP_BITMAPS \ -DDISABLE_EMPTYTRAY_UNDO \ diff --git a/xwords4/android/XWords4/jni/utilwrapper.c b/xwords4/android/XWords4/jni/utilwrapper.c index 4b14f5b4f..58a33f9eb 100644 --- a/xwords4/android/XWords4/jni/utilwrapper.c +++ b/xwords4/android/XWords4/jni/utilwrapper.c @@ -364,6 +364,18 @@ and_util_remSelected(XW_UtilCtxt* uc) UTIL_CBK_TAIL(); } +#ifdef XWFEATURE_RANDOM_VIA_UTILS +static XP_U16 +and_util_rand( XW_UtilCtxt* uc ) +{ + jint result = 0; + UTIL_CBK_HEADER("rand", "()I" ); + result = (*env)->CallIntMethod( env, util->jutil, mid ); + UTIL_CBK_TAIL(); + return result; +} +#endif + static void and_util_setIsServer(XW_UtilCtxt* uc, XP_Bool isServer ) { @@ -456,6 +468,9 @@ makeUtil( MPFORMAL JNIEnv** envp, jobject jutil, CurGameInfo* gi, SET_PROC(warnIllegalWord); SET_PROC(showChat); SET_PROC(remSelected); +#ifdef XWFEATURE_RANDOM_VIA_UTILS + SET_PROC(rand); +#endif SET_PROC(setIsServer); #ifndef XWFEATURE_STANDALONE_ONLY diff --git a/xwords4/android/XWords4/jni/xptypes.h b/xwords4/android/XWords4/jni/xptypes.h index bc5e63dde..49696b86a 100644 --- a/xwords4/android/XWords4/jni/xptypes.h +++ b/xwords4/android/XWords4/jni/xptypes.h @@ -48,7 +48,7 @@ typedef XP_U32 XP_Time; #define XP_CR "\n" #define XP_LD "%ld" -#define XP_RANDOM() and_rand() +/* #define XP_RANDOM() and_rand() */ #ifdef MEM_DEBUG # define XP_PLATMALLOC(nbytes) malloc(nbytes) diff --git a/xwords4/android/XWords4/src/org/eehouse/android/xw4/BoardActivity.java b/xwords4/android/XWords4/src/org/eehouse/android/xw4/BoardActivity.java index 3ec77a176..5ab980ce6 100644 --- a/xwords4/android/XWords4/src/org/eehouse/android/xw4/BoardActivity.java +++ b/xwords4/android/XWords4/src/org/eehouse/android/xw4/BoardActivity.java @@ -32,6 +32,7 @@ import android.view.Window; import android.os.Handler; import android.os.Message; import android.content.Intent; +import java.util.Random; import java.util.concurrent.Semaphore; import android.net.Uri; import android.app.Dialog; @@ -73,6 +74,7 @@ public class BoardActivity extends XWActivity implements UtilCtxt { private String m_path; private int m_currentOrient; private Toolbar m_toolbar; + private Random m_rgen; private String m_dlgBytes = null; private EditText m_passwdEdit = null; @@ -290,6 +292,8 @@ public class BoardActivity extends XWActivity implements UtilCtxt { Utils.logf( "BoardActivity::onCreate()" ); super.onCreate( savedInstanceState ); + m_rgen = new Random(); + if ( CommonPrefs.getHideTitleBar( this ) ) { requestWindowFeature( Window.FEATURE_NO_TITLE ); } @@ -713,6 +717,11 @@ public class BoardActivity extends XWActivity implements UtilCtxt { R.string.tiles_left_title ); } + public int rand() + { + return m_rgen.nextInt(); + } + public void setIsServer( boolean isServer ) { Utils.logf( "setIsServer(%s)", isServer?"true":"false" ); diff --git a/xwords4/android/XWords4/src/org/eehouse/android/xw4/jni/UtilCtxt.java b/xwords4/android/XWords4/src/org/eehouse/android/xw4/jni/UtilCtxt.java index 4210461e9..293fd6c36 100644 --- a/xwords4/android/XWords4/src/org/eehouse/android/xw4/jni/UtilCtxt.java +++ b/xwords4/android/XWords4/src/org/eehouse/android/xw4/jni/UtilCtxt.java @@ -48,6 +48,7 @@ public interface UtilCtxt { void requestTime(); void remSelected(); + int rand(); void setIsServer( boolean isServer ); static final int STRD_ROBOT_TRADED = 1; diff --git a/xwords4/common/comms.c b/xwords4/common/comms.c index 8d961cc8a..d21f12f13 100644 --- a/xwords4/common/comms.c +++ b/xwords4/common/comms.c @@ -927,7 +927,7 @@ XP_U16 comms_getChannelSeed( CommsCtxt* comms ) { while ( comms->channelSeed == 0 ) { - comms->channelSeed = XP_RANDOM(); + comms->channelSeed = util_rand(comms->util); XP_LOGF( "%s: channelSeed: %.4X", __func__, comms->channelSeed ); } return comms->channelSeed; diff --git a/xwords4/common/nwgamest.c b/xwords4/common/nwgamest.c index 8063e2f28..e57953784 100644 --- a/xwords4/common/nwgamest.c +++ b/xwords4/common/nwgamest.c @@ -309,7 +309,7 @@ newg_juggle( NewGameCtx* ngc ) /* Get a randomly juggled array of numbers 0..nPlayers-1. Then the number at pos[n] inicates where the entry currently at n should be. */ - changed = randIntArray( pos, nPlayers ); + changed = randIntArray( ngc->util, pos, nPlayers ); if ( changed ) { /* Deep-copy off to tmp storage. But skip lines that won't be moved diff --git a/xwords4/common/pool.c b/xwords4/common/pool.c index bf8a55eea..81d4603af 100644 --- a/xwords4/common/pool.c +++ b/xwords4/common/pool.c @@ -18,6 +18,7 @@ */ /* #include */ +#include "util.h" #include "pool.h" #include "dictnry.h" #include "xwstream.h" @@ -27,6 +28,7 @@ // #define BLANKS_FIRST 1 struct PoolContext { + XW_UtilCtxt* util; XP_U8* lettersLeft; XP_U16 numTilesLeft; XP_U16 numFaces; @@ -37,13 +39,14 @@ struct PoolContext { }; PoolContext* -pool_make( MPFORMAL_NOCOMMA ) +pool_make( MPFORMAL XW_UtilCtxt* uc ) { PoolContext* result = (PoolContext*)XP_MALLOC(mpool, sizeof(*result) ); if ( result != NULL ) { XP_MEMSET( result, 0, sizeof( *result ) ); MPASSIGN(result->mpool, mpool); + result->util = uc; #ifdef BLANKS_FIRST result->blankIndex = -1; @@ -66,9 +69,9 @@ pool_writeToStream( PoolContext* pool, XWStreamCtxt* stream ) } /* pool_writeToStream */ PoolContext* -pool_makeFromStream( MPFORMAL XWStreamCtxt* stream ) +pool_makeFromStream( MPFORMAL XW_UtilCtxt* uc, XWStreamCtxt* stream ) { - PoolContext* pool = pool_make( MPPARM_NOCOMMA(mpool) ); + PoolContext* pool = pool_make( MPPARM(mpool) uc ); pool->numTilesLeft = stream_getU16( stream ); pool->numFaces = stream_getU16( stream ); @@ -133,9 +136,9 @@ getRandomTile( PoolContext* pool ) #if defined PLATFORM_PALM && ! defined XW_TARGET_PNO XP_U16 rr = XP_RANDOM(); #elif defined PLATFORM_ANDROID - XP_U16 rr = XP_RANDOM(); + XP_U16 rr = util_rand(pool->util); #else - XP_U16 rr = (XP_U16)(XP_RANDOM()>>16); + XP_U16 rr = util_rand(pool->util); #endif XP_U16 index = rr % pool->numTilesLeft; Tile result = getNthPoolTile( pool, index ); diff --git a/xwords4/common/pool.h b/xwords4/common/pool.h index c55387250..e8453635a 100644 --- a/xwords4/common/pool.h +++ b/xwords4/common/pool.h @@ -32,12 +32,13 @@ void pool_removeTiles( PoolContext* pool, TrayTileSet* tiles ); XP_U16 pool_getNTilesLeft( PoolContext* pool ); XP_U16 pool_getNTilesLeftFor( PoolContext* pool, Tile tile ); -PoolContext* pool_make( MPFORMAL_NOCOMMA ); +PoolContext* pool_make( MPFORMAL XW_UtilCtxt* uc ); void pool_destroy( PoolContext* pool ); void pool_initFromDict( PoolContext* pool, DictionaryCtxt* dict ); void pool_writeToStream( PoolContext* pool, XWStreamCtxt* stream ); -PoolContext* pool_makeFromStream( MPFORMAL XWStreamCtxt* stream ); +PoolContext* pool_makeFromStream( MPFORMAL XW_UtilCtxt* uc, + XWStreamCtxt* stream ); #endif diff --git a/xwords4/common/server.c b/xwords4/common/server.c index 16a7c3882..a5389820a 100644 --- a/xwords4/common/server.c +++ b/xwords4/common/server.c @@ -315,7 +315,7 @@ server_makeFromStream( MPFORMAL XWStreamCtxt* stream, ModelCtxt* model, getNV( stream, &server->nv, nPlayers ); if ( stream_getBits(stream, 1) != 0 ) { - server->pool = pool_makeFromStream( MPPARM(mpool) stream ); + server->pool = pool_makeFromStream( MPPARM(mpool) util, stream ); } for ( i = 0; i < nPlayers; ++i ) { @@ -435,7 +435,7 @@ figureSleepTime( const ServerCtxt* server ) XP_U16 max = server->nv.robotThinkMax; if ( min < max ) { int diff = max - min + 1; - result = XP_RANDOM() % diff; + result = util_rand(server->vol.util) % diff; } result += min; @@ -663,7 +663,7 @@ figureTargetScore( ServerCtxt* server, XP_U16 turn ) } result = (XP_S16)(highScore - model_getPlayerScore( model, turn ) - + (FUDGE_RANGE-(XP_RANDOM() % (FUDGE_RANGE*2)))); + + (FUDGE_RANGE-(util_rand(server->vol.util) % (FUDGE_RANGE*2)))); if ( result < 0 ) { result = MINIMUM_SCORE; } @@ -1155,7 +1155,8 @@ client_readInitialMessage( ServerCtxt* server, XWStreamCtxt* stream ) } XP_ASSERT( !server->pool ); - pool = server->pool = pool_make( MPPARM_NOCOMMA(server->mpool) ); + pool = server->pool = pool_make( MPPARM(server->mpool) + server->vol.util ); pool_initFromDict( server->pool, model_getDictionary(model)); /* now read the assigned tiles for each player from the stream, and @@ -1624,7 +1625,7 @@ assignTilesToAll( ServerCtxt* server ) XP_ASSERT( server->vol.gi->serverRole != SERVER_ISCLIENT ); XP_ASSERT( model_getDictionary(model) != NULL ); if ( server->pool == NULL ) { - server->pool = pool_make( MPPARM_NOCOMMA(server->mpool) ); + server->pool = pool_make( MPPARM(server->mpool) server->vol.util ); XP_STATUSF( "initing pool" ); pool_initFromDict( server->pool, model_getDictionary(model)); } diff --git a/xwords4/common/strutils.c b/xwords4/common/strutils.c index d24c33a2d..13accdf01 100644 --- a/xwords4/common/strutils.c +++ b/xwords4/common/strutils.c @@ -18,6 +18,7 @@ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ +#include "util.h" #include "strutils.h" #include "xwstream.h" #include "mempool.h" @@ -206,7 +207,7 @@ emptyStringIfNull( XP_UCHAR* str ) } /* emptyStringIfNull */ XP_Bool -randIntArray( XP_U16* rnums, XP_U16 count ) +randIntArray( XW_UtilCtxt* util, XP_U16* rnums, XP_U16 count ) { XP_Bool changed = XP_FALSE; XP_U16 i; @@ -216,7 +217,7 @@ randIntArray( XP_U16* rnums, XP_U16 count ) } for ( i = count; i > 0 ; ) { - XP_U16 rIndex = ((XP_U16)XP_RANDOM()) % i; + XP_U16 rIndex = util_rand(util) % i; if ( --i != rIndex ) { XP_U16 tmp = rnums[rIndex]; rnums[rIndex] = rnums[i]; diff --git a/xwords4/common/strutils.h b/xwords4/common/strutils.h index b91d6bbc5..ae155c7c4 100644 --- a/xwords4/common/strutils.h +++ b/xwords4/common/strutils.h @@ -84,7 +84,7 @@ void p_replaceStringIfDifferent( MPFORMAL XP_UCHAR** curLoc, XP_UCHAR* emptyStringIfNull( XP_UCHAR* str ); /* Produce an array of ints 0..count-1, juggled */ -XP_Bool randIntArray( XP_U16* rnums, XP_U16 count ); +XP_Bool randIntArray( XW_UtilCtxt* util, XP_U16* rnums, XP_U16 count ); #ifdef XWFEATURE_SMS void binToSms( XP_UCHAR* out, XP_U16* outlen, const XP_U8* in, XP_U16 inlen ); diff --git a/xwords4/common/tray.c b/xwords4/common/tray.c index c86993459..97e30d050 100644 --- a/xwords4/common/tray.c +++ b/xwords4/common/tray.c @@ -523,7 +523,7 @@ board_juggleTray( BoardCtxt* board ) XP_U16 newT[MAX_TRAY_TILES]; /* loop until there'll be change */ - while ( !randIntArray( newT, nTiles ) ) { + while ( !randIntArray( board->util, newT, nTiles ) ) { } /* save copies of the tiles in juggled order */ diff --git a/xwords4/common/util.h b/xwords4/common/util.h index 0259de7d5..9dd2f10d2 100644 --- a/xwords4/common/util.h +++ b/xwords4/common/util.h @@ -157,6 +157,10 @@ typedef struct UtilVtable { void (*m_util_remSelected)(XW_UtilCtxt* uc); +#ifdef XWFEATURE_RANDOM_VIA_UTILS + XP_U16 (*m_util_rand)(XW_UtilCtxt* uc); +#endif + #ifndef XWFEATURE_STANDALONE_ONLY void (*m_util_addrChange)( XW_UtilCtxt* uc, const CommsAddrRec* oldAddr, const CommsAddrRec* newAddr ); @@ -257,6 +261,11 @@ struct XW_UtilCtxt { #define util_remSelected( uc ) \ (uc)->vtable->m_util_remSelected((uc)) +#ifdef XWFEATURE_RANDOM_VIA_UTILS +# define util_rand( uc ) \ + (uc)->vtable->m_util_rand((uc)) +#endif + #ifndef XWFEATURE_STANDALONE_ONLY # define util_addrChange( uc, addro, addrn ) \ (uc)->vtable->m_util_addrChange((uc), (addro), (addrn)) diff --git a/xwords4/linux/Makefile b/xwords4/linux/Makefile index c0ba9545a..979a92fda 100644 --- a/xwords4/linux/Makefile +++ b/xwords4/linux/Makefile @@ -85,6 +85,7 @@ DEFINES += -DFEATURE_TRAY_EDIT #DEFINES += -DDRAW_WITH_PRIMITIVES DEFINES += -DXWFEATURE_CROSSHAIRS DEFINES += -DXWFEATURE_CHAT +DEFINES += -DXWFEATURE_RANDOM_VIA_UTILS ifdef CURSES_CELL_HT DEFINES += -DCURSES_CELL_HT=$(CURSES_CELL_HT) diff --git a/xwords4/linux/linuxutl.c b/xwords4/linux/linuxutl.c index 354c6511f..3787f206a 100644 --- a/xwords4/linux/linuxutl.c +++ b/xwords4/linux/linuxutl.c @@ -175,6 +175,17 @@ linux_util_getUserString( XW_UtilCtxt* XP_UNUSED(uc), XP_U16 code ) } } /* linux_util_getUserString */ +#ifdef XWFEATURE_RANDOM_VIA_UTILS +static XP_U16 +linux_util_rand( XW_UtilCtxt* XP_UNUSED(util) ) +{ + XP_U16 result = 0; + result = random() >> 16; + LOG_RETURNF( "%d", result ); + return result; +} +#endif + void linux_util_vt_init( MPFORMAL XW_UtilCtxt* util ) { @@ -184,7 +195,9 @@ linux_util_vt_init( MPFORMAL XW_UtilCtxt* util ) util->vtable->m_util_getSquareBonus = linux_util_getSquareBonus; util->vtable->m_util_getCurSeconds = linux_util_getCurSeconds; util->vtable->m_util_getUserString = linux_util_getUserString; - +#ifdef XWFEATURE_RANDOM_VIA_UTILS + util->vtable->m_util_rand = linux_util_rand; +#endif } void diff --git a/xwords4/linux/xptypes.h b/xwords4/linux/xptypes.h index 19fa0b698..f49656707 100644 --- a/xwords4/linux/xptypes.h +++ b/xwords4/linux/xptypes.h @@ -102,7 +102,7 @@ extern void linux_debugf(const char*, ...) #define XP_STRNCMP(s1,s2,len) strncmp((s1),(s2),(len)) #define XP_STRNCPY(s1,s2,len) strncpy((s1),(s2),(len)) #define XP_STRCMP(s1,s2) strcmp((s1),(s2)) -#define XP_RANDOM() random() +/* #define XP_RANDOM() random() */ #define XP_SNPRINTF snprintf #define XP_MIN(a,b) ((a)<(b)?(a):(b))