ThreadBoard ArchivesSite FeaturesActiveworlds SupportHistoric Archives |
Converting a Co-ord String to API Usable (Sdk)
Converting a Co-ord String to API Usable // SdkpercipientJul 17, 2002, 4:22pm
I'm using Borland C++Builder. I have VERY limited knowledge of C++, although
I have programming experience in other languages. I am taking a crash course, learn-as-you-go method and making a bot. I want the Co-ords for the bot's position to be entered as the usual 10n 5w 10a 90, but the SDK needs that separated. I know how to do the simple math to multiply those numbers to what the SDK needs, but what I dunno how to do is to split that up, know if it should be north or south, and whatever. Could anyone help me out? Thanks, Percipient strike rapierJul 17, 2002, 4:52pm
Hmm, im not sure, i only started learning VC++ 2 days ago but here is my bet
considering their relitivly the same, use the MSDN to help. First if it dosent have a split(x) function like VB do a for loop from 1 to the length of the string. Create a array using something allong the lines of: Int Range[4] Int SetVal For(i, strlen(section),i += 1); { If(char(i) = ""); { SetVal += 1; } Else; { Range[SetVal] = range[setval] + Char(i); } End if; } Then take the last byte of each of em and if its either N or S is Z axis, so take all the bytes except the last 1 and do the maths with em, then if its E or W then its X, all but last byte and do maths, if its a then its altitude, if its non of em is YAW. My apologies if this is a bunch of pure rubbish but ive only done a grand total of 20 min of VC++ - Mark *Gawd i hate learning this* grimbleJul 17, 2002, 6:38pm
Assuming you're changing from another language, write a parser in the one
you know, keeping to the low-level functions (no language-specific specific stuff that doesn't have a C++ equivalent) and convert the working code. That way (a) you can write the function faster as you know the language better, (b) you know the logic works when you get to the point of writing the c++ version so you know the problem is with the actual converted code and (c) its much easier to write code in a new language when you've already got a visualisation of the breakdown of the function. Grims [View Quote] ananasJul 17, 2002, 7:13pm
I'm not sure what split() in VB does, but C has more
than one function to break a string into parts, for example strtok() and sscanf(). It could look somehow like this : char SNString[30], EWString[30], World[30], AString; sscanf (InputString, "%s %s %s %s", World, SNString, EWString, AString); A function to get the coord's integer values from the broken out parts is atoi() To get the last characters (the direction), I would use array addressing together with strlen(), e.g. int SNValue; SNValue = atoi (SNString); The atoi() stops at the first non-numeric character, so there's no need to remove anything. if ( (SNString[strlen(SNString)-1] | 0x20) == 's') SNValue = -SNValue; The "| 0x20" makes the character lowercase, independant from what the user typed. strike rapierJul 17, 2002, 8:26pm
StrFunction = Split(Expression, Key (or something))
It creates a string array of StrFunction in sections as seperated by the key.. ie: StrSomething = split("me,you,him,her,baron,brant",",") you would get StrSomething(0) = "me" StrSomething(1) = "you" StrSomething(2) = "him" etc ect ect bowenJul 17, 2002, 8:55pm
Wow I don't know who taught you C++ but.. wow. If that's how VC++ works I
want none of it. Just trying to tell you you need to relearn the basic syntax of C++. :) don't take that the wrong way though. 20 mins should've taught you that much you silly willy. --Bowen-- [View Quote] o0oiiijoshiiio0oJul 17, 2002, 11:41pm
Ah, all those replies and no working code. It makes me feel wanted. The
function I use is strtok, since it's ANSI C. I believe the header on that one is string.h. Note that I almost ALWAYS use STL string... more versatile... just #include <string> using namespace std; and then you can do string MyString.... but calling my soon-to-be-listed parse function with a string of any other type that I've ever tested with would work fine. This will take a string like "1000 -1000 10 90" and fill in the soon to be shown Coordinates struct. As an excersize (oh God, it must be summer) for the student, I'm not gunna deal with the characters NSEW, and use negative numbers instead. struct Coordinates { int x; int y; int z; int yaw; }; Coordinates *GetCoordinates(string ParseFrom) { char *token; Coordinates ParseReturn; token = strtok(ParseFrom.c_str(), " "); // 1234n 5678e leaves token as // 1234n ParseReturn.x = atoi(token); token = strtok(NULL, " "); // parse between the first two spaces ParseReturn.z = atoi(token); token = strtok(NULL, " "); ParseReturn.y = atoi(token); token = strtok(NULL, " "); ParseReturn.yaw = atoi(token); } So as a quick recap, if you have a string like -1234 5678 9 10 You can do Coordinates MyCoords = GetCoordinates(string); and then access members of the structs. Of course, since I'm dealing with pointers and don't have any coffee on my desk, that's a sign that you want to test and come back during the day hours if this doesn't work. I know how to do it; I've done it before. Just ::yawn:: tired. -J [View Quote] ananasJul 18, 2002, 2:59am
ananasJul 18, 2002, 3:01am
The advantage of incomplete snippets over a
ready-made solution : The developer has to deal with the why and the how and will easier understand what he implements :) [View Quote] dionJul 18, 2002, 10:52am
I did the very same thing with VisualBasic a few weeks back. This might help
you to figure it out. Private Sub TportButt_Click() Dim teleport As String Dim intX As Long Dim intZ As Long Dim intStart As Integer 'turn coords into meters the bot can use to tport teleport = Coords.Text For X = 2 To 35 If UCase(Mid(teleport, X, 1)) = "N" Or UCase(Mid(teleport, X, 1)) = "S" Then intX = Val(Left(teleport, X - 1)) If UCase(Mid(teleport, X, 1)) = "S" Then intX = intX * (-1000) If UCase(Mid(teleport, X, 1)) = "N" Then intX = intX * 1000 Exit For End If Next X For X = 2 To 35 If Mid(teleport, X, 1) = " " Then intStart = X If UCase(Mid(teleport, X, 1)) = "E" Or UCase(Mid(teleport, X, 1)) = "W" Then intZ = Val(Mid(teleport, intStart + 1, X - 1)) If UCase(Mid(teleport, X, 1)) = "E" Then intZ = intZ * (-1000) If UCase(Mid(teleport, X, 1)) = "W" Then intZ = intZ * 1000 Exit For End If Next X rc = ChatForm.SDK.AwEnter(World.Text) If rc Then ChatForm.AwChat.SelColor = vbRed ChatForm.AwChat.SelBold = True ChatForm.AwChat.SelItalic = False ChatForm.AwChat.SelText = "Error! Reason " & rc & vbCrLf Exit Sub End If ChatForm.SDK.AwMyX = intZ ChatForm.SDK.AwMyY = 0 ChatForm.SDK.AwMyZ = intX ChatForm.SDK.AwMyYaw = 0 ChatForm.SDK.AwMyType = 0 rc = ChatForm.SDK.AwStateChange If rc Then ChatForm.AwChat.SelColor = vbRed ChatForm.AwChat.SelBold = True ChatForm.AwChat.SelItalic = False ChatForm.AwChat.SelText = "Error! Reason " & rc & vbCrLf Exit Sub End If ChatForm.AwChat.SelColor = vbBlue ChatForm.AwChat.SelBold = True ChatForm.AwChat.SelItalic = False ChatForm.AwChat.SelText = "[" & LogInForm.BotName & "] has been successfully teleported to " & teleport & "." & vbCrLf TportForm.Visible = False End Sub [View Quote] o0oiiijoshiiio0oJul 18, 2002, 11:32am
Well, of course, but I always give snippets for my ideas and no one's
mentioned strtok... -J [View Quote] ananasJul 18, 2002, 10:17pm
strike rapierJul 25, 2002, 5:41pm
20 min, 4 cups of tea, and the first 2 chapters about installing VS and
Loading consoles :) |