Page 1 of 1

Command: /settime

Posted: Wed Feb 27, 2008 8:09 pm
by John Adams
I apologize, but I have been banging my head against a wall for hours on what I assumed was something very simple; adding a new Command to set the world time (so when it gets all dark, we can make it light again :)).
If you do not have time to deal with this right now, please do not worry about it. It's just me trying to get into the coding aspect with something simple. Obviously, nothing is simple but Hello World!
Here's what I have successfully done.
Commands.h:
#define COMMAND_SETTIME 58 (next one after 57 heh)
`commands` table in DB:
added record for settime, handler 58.
Commands.cpp:

Code: Select all

		case COMMAND_SETTIME:
			{
				if(sep && sep->arg[1]) {
					client->SimpleMessage(CHANNEL_COLOR_YELLOW,"I'm in!");
				} else {
					client->SimpleMessage(CHANNEL_COLOR_YELLOW,"Usage: /settime {HH:MM}");
				}
			break;
			}
What this does is if you type /settime and hit enter, it gives the syntax - that was the easy part. The part that threw me was the arg[0], which I am pretty sure I understand but other example commands used sep->arg[0] where mine would only work with sep->arg[1].
Anyway, typing /settime 1 (anything really) now prints out my debug line. Wee! Ok, here's where it all falls apart...
world_time.hour = part of the value
world_time.minute = other part
I know how to split them up at the :, but it's the "world_time.hour" part that's got me boggled. I took the code from World.cpp (init). Since it fails completely, I am assuming that is incorrect.
Anyway, if I gain an understanding of how to access World data/variables from within the command switch, maybe I could help add a few commands here and there.
The one I really want to see is /commands [text], which will list available commands that contain [text]. So then, we can build the /{command} help features using the helptext field. That should be easy... however, C++ is definitely not phpScript. :)
Thanks for your time with the noob.

Posted: Wed Feb 27, 2008 8:54 pm
by zanifer
arg[0] is the name of the command, or program run, arg[1] and above are usually the command line arguments that follow it as separated by a space. For example ./myporgrame 12 would result in arg[0] being the string "myprograme" with arg[1] being the string "12". If you added further additions on the command line being further increments in the arg[blank] value. sep[[1] would be the value you entered for time.
I hope I understood your question, I know I am not familiar with the workings of the emulator code. Sorry if I missed your point, I am only eager to try and learn how these things work by spitting out my two cents.

Posted: Wed Feb 27, 2008 9:06 pm
by LethalEncounter
Actually sep->arg[0] is correct and you shouldnt get any problems using it. Ive never tried to send a time update when my character was already on world so I'm not sure it will even work but when I get a chance I'll try it and let you know.

Posted: Wed Feb 27, 2008 9:22 pm
by John Adams
Thanks zanifer. I believe you are correct, and that is my understanding of arg's as well. I just didn't understand why the other commands, with obvious params, were only checking for arg[0] in order to be valid.
Anyway, LE this isn't critical at all. Like I said, just trying to find a place where I can learn and try to add small bits. I got plenty going on with the DB as well, but sometimes my brain needs a little diversion. :)
fwiw, I got the thing to compile by really hacking it to bits - and I am 99% certain what I did was dead wrong. Including Variables.h so I could declare the "var" like you did in World.cpp. Tried writing a new time to variables.gametime and reading it back in. Where I gave up was trying to send the WorldTimeUpdate or whatever packet back to the client. I called it a night, after creating a new case for COMMAND_COMMANDS (411) tee hee.
Then my ADHD kicked in and I wrote my first-ever-in-my-life C# Win32 app with a dataset on it. I've never written a GUI from the ground up, but my job is pushing me that way (no more command line/console stuff heh) so why not.
I almost pooped myself when it worked!

Posted: Thu Feb 28, 2008 4:02 pm
by LethalEncounter
You guys are confusing command line arguments with the arguments passed into the command. The first argument after /command is arg[0].

Posted: Thu Feb 28, 2008 4:26 pm
by LethalEncounter
I went ahead and finished /settime and the client does change it's time when it receives the new packet. The changes will be on SVN sometime in the next few days.

Posted: Thu Feb 28, 2008 4:53 pm
by alfa
Good work LE and thx JA for idea :P

Posted: Thu Feb 28, 2008 5:22 pm
by John Adams
Heh, thank you LE. I can't wait to take a look at how you did it, so maybe I can learn something.
Sorry about the arg confusion. I guess I missed a step somewhere.

Posted: Thu Feb 28, 2008 6:19 pm
by LethalEncounter

Code: Select all

case COMMAND_SETTIME:{
			if(sep && sep->arg[0]){
				sscanf (sep->arg[0], "%d:%d", &world.GetWorldTimeStruct()->hour, &world.GetWorldTimeStruct()->minute);
				if(sep->arg[1] && sep->IsNumber(1))
					world.GetWorldTimeStruct()->month = atoi(sep->arg[1]) - 1; //zero based indexes
				if(sep->arg[2] && sep->IsNumber(2))
					world.GetWorldTimeStruct()->day = atoi(sep->arg[2]) - 1; //zero based indexes
				if(sep->arg[3] && sep->IsNumber(3))
					world.GetWorldTimeStruct()->year = atoi(sep->arg[3]);
				PacketStruct* packet = world.GetWorldTime(client->GetVersion());
				if(packet){
 					client->QueuePacket(packet->serialize());
					safe_delete(packet);
				}
			}
			else{
				client->SimpleMessage(CHANNEL_COLOR_YELLOW,"Usage: /settime [hour:minute] (month) (day) (year)");
				client->SimpleMessage(CHANNEL_COLOR_YELLOW,"Example: /settime 8:30");
			}
			break;
		}
Note that you will also need to change World.h:
Change

Code: Select all

WorldTime*			GetWorldTimeStruct()
to

Code: Select all

WorldTime*			GetWorldTimeStruct(){
		return &world_time;
	}

Posted: Thu Feb 28, 2008 7:09 pm
by John Adams
Woot! Thanks, I will try it. :)

Posted: Sat Mar 01, 2008 12:46 pm
by John Adams
Works perfectly for my client, but my second client never gets the new world time update.