bzug0 Posted March 8, 2007 Posted March 8, 2007 I have this: void GivePwds( char *team, char *out ) { int i=0; int tm=0; char string[100]; char string2[100]; for(i=0; i<4; i++) { out[i] = (rand()%26)+'a'; } if ( !Q_stricmp( team, "Red" ) ) { tm = TEAM_RED; } if ( !Q_stricmp( team, "Blue" ) ) { tm = TEAM_BLUE; } Com_sprintf(string,sizeof(string),"print \"%s Team Password is: %s\n\"", team, out); Com_sprintf(string2,sizeof(string2),"cp \"%s Team Password is: %s\n\"", team, out); for ( i = 0 ; i < level.maxclients ; i++ ) { if (level.clients[i].sess.sessionTeam != tm) continue; trap_SendServerCommand( i, string); trap_SendServerCommand( i, string2); } } I use it like this: char pwd[4]; trap_Cvar_Set( "g_redLock", "1" ); GivePwds( "Red", pwd ); And then I get strange password. The first 4 characters are fine but the rest are strange characters possibly some address??! Hope someone knows, or experienced something like this... -- Forgot to say this works fine in windows sometimes.
bzug0 Posted March 9, 2007 Author Posted March 9, 2007 I think won't be problem with that... example of the biggest string this should return: Blue Team Password is: abcd 27 isn't bigger than 100 :| or the problem is that the string is too big?!?
bzug0 Posted March 10, 2007 Author Posted March 10, 2007 For more stupid this look like but forgot the end of string... '\0'
ensiform Posted March 10, 2007 Posted March 10, 2007 You shouldn't ever have to append a \0... ever... your compiler will do it for you.
ensiform Posted March 10, 2007 Posted March 10, 2007 what is the point of out char? i dont see why you have the rand loop either... const char *teamNames[] = { "Red", "Blue", "Free", "Spectator" }; void GivePwds( team_t, const char *pwd ) { int i=0; char string[100]; char string2[100]; Com_sprintf(string,sizeof(string),"print \"%s Team Password is: %s\n\"", teamNames[team], pwd); Com_sprintf(string2,sizeof(string2),"cp \"%s Team Password is: %s\n\"", teamNames[team], pwd); for ( i = 0 ; i < level.maxclients ; i++ ) { // you might want some sanity checks here... or bad things can happen... if (level.clients[i].sess.sessionTeam != tm) continue; trap_SendServerCommand( i, string); trap_SendServerCommand( i, string2); } } you may want to show more of where this func is being used if you want better help...
bzug0 Posted March 11, 2007 Author Posted March 11, 2007 Well the function is used and declared in g_main.c And in the first post I said it all , just didn't mention the g_main.c void GivePwds( char *team, char *out ) { //out - empty array to store the password and use it to set another cvar. int i=0; int tm=0; char string[100]; char string2[100]; for(i=0; i<4; i++) { //this is to generate the password out[i] = (rand()%26)+'a'; } if ( !Q_stricmp( team, "Red" ) ) { //this is dumb I will remake it tm = TEAM_RED; } if ( !Q_stricmp( team, "Blue" ) ) { tm = TEAM_BLUE; } Com_sprintf(string,sizeof(string),"print \"%s Team Password is: %s\n\"", team, out); Com_sprintf(string2,sizeof(string2),"cp \"%s Team Password is: %s\n\"", team, out); for ( i = 0 ; i < level.maxclients ; i++ ) { if (level.clients[i].sess.sessionTeam != tm) continue; trap_SendServerCommand( i, string); trap_SendServerCommand( i, string2); } } The out is the empty array the function receives to store password, initially it has other functions but now that you talk about it I can create it inside the function, and set the cvars I have to set inside it too. I think its all explained. I will remake this function and see the results.
bzug0 Posted March 11, 2007 Author Posted March 11, 2007 anyway, I tested this in different ways and if you can do this command without closing the string manually I will have the solution: if (Q_stricmp (cmd, "test") == 0) { int i=0; char pwd[5]; for(i=0; i<4; i++) { pwd[i] = (rand()%26)+'a'; } pwd[4]='\0'; G_Printf("password=%s\n", pwd); } I have added this in g_svcmds.c for testing purposes. And it only works if I close the string manually. Edit: Checked and C closes strings like "hi", but strings like { 'h', 'i' } stay simple char array.
ensiform Posted March 12, 2007 Posted March 12, 2007 Well your best bet would be to have a static char array in your function for the randomizing and have your function just return pwd like so: char *function(team_t team) { static char pwd[max_pwd_len] = {""}; randomizer of pwd here... ... send to ppl return pwd; }
bzug0 Posted March 12, 2007 Author Posted March 12, 2007 yup static just work fine. I only remember to turn functions static - nothing else... Anyway, in a way or another it's working fine now.
ensiform Posted March 12, 2007 Posted March 12, 2007 uhm no... const char *teamNames[] = { "Free", "Red", "Blue", "Spectator" }; char *GivePwds(team_t team) { static char out[5]; char pwd[5] = {"none"}; // yes use { } // not a vry gud randomizer btw :B: check net for some open source generators... for(i=0; i<4; i++) { //this is to generate the password pwd[i] = (rand()%26)+'a'; } pwd[4] = '\0'; for( i=0; i<level.numConnectedClients; i++) { if ( level.clients[i].sess.sessionTeam != team || (g_gametype.integer == GT_SIEGE && level.clients[i].sess.siegeDesiredTeam != team) ) { continue; } trap_SendServerCommand( i, va("print \"%s Team Password is: %s\n\"", teamNames[team], pwd); trap_SendServerCommand( i, va("cp \"%s Team Password is: %s\n\"", teamNames[team], pwd); } Q_strncpyz(out, pwd, sizeof(out)); return out; } also change your [4] in g_main to [5] to match up dood... because \0 is a character that gets added later :x and static functions are pretty much gay unless they are only being used inside that specific file otherwise they are useless.
bzug0 Posted March 13, 2007 Author Posted March 13, 2007 I've done something similar, and yes after the first post I've changed the array to 5 lol[when I remembered the '\0']. About the team names I added them inside my function because I wont use it outside it, and I don't want to bother much with that... And I like static function , I'm used to Java thats why I make silly mistakes in C bah Anyway thanks for you help ps: the randomizer lol, is just some simple thing I came with. But, after adding that I have searched but they are too complex for the job. I only need 4 characters.
Recommended Posts
Archived
This topic is now archived and is closed to further replies.