Author |
Message |
Assaulter
Warrant Officer
Joined: Thu May 09, 2013 2:21 pm Posts: 73
|
 My Very First Attempt to Write a script
I have looked through script source files before and tried to translate what they mean but I have never just started playing with some code and see what happens. Well, this morning i decided to give it a whirl. I have created a very simple user input "menu". I feel like I am using way to much code to create such a simple menu. Is there something I can do with arrays? I have read over the stuff listed here: viewtopic.php?f=15&t=21264&view=previousBut I just can't wrap my head around how to shorten this code up to keep from repeating the same thing over and over. Here is what I have so far: Code: ####Find out which planets we want to neg
echo "*" &ANSI_12& "Which planets would you like to Negotiate a Planetary Trade Agreement with?" echo "*" echo "*"
####Get the first planet :GetPlanet1 getInput $planet1 "Planet 1"
####Get the second planet :GetPlanet2 getInput $planet2 "Planet 2"
####Make sure second planet is unique if ($planet2 = $planet1) Echo "*" &ANSI_6& "You have entered the same planet twice." Echo "*" &ANSI_12& "Please Reenter a Unique Planet number." gosub :GetPlanet2 end
####Get the third planet :GetPlanet3 getInput $planet3 "Planet 3"
####Make sure the third planet is unique if ($planet3 = $planet1) Echo "*" &ANSI_6& "You have entered the same planet twice." Echo "*" &ANSI_12& "Please Reenter a Unique Planet number." gosub :GetPlanet3 end if ($planet3 = $planet2) Echo "*" &ANSI_6& "You have entered the same planet twice." Echo "*" &ANSI_12& "Please Reenter a Unique Planet number." gosub :GetPlanet3 end ####Get the fourth planet :GetPlanet4 getInput $planet4 "Planet 4"
####Make sure the fourth planet is unique if ($planet4 = $planet1) Echo "*" &ANSI_6& "You have entered the same planet twice." Echo "*" &ANSI_12& "Please Reenter a Unique Planet number." gosub :GetPlanet4 end if ($planet4 = $planet2) Echo "*" &ANSI_6& "You have entered the same planet twice." Echo "*" &ANSI_12& "Please Reenter a Unique Planet number." gosub :GetPlanet4 end if ($planet4 = $planet3) Echo "*" &ANSI_6& "You have entered the same planet twice." Echo "*" &ANSI_12& "Please Reenter a Unique Planet number." gosub :GetPlanet4 end ####Get the fifth planet :GetPlanet5 getInput $planet5 "Planet 5"
####Make sure the fifth planet is unique if ($planet5 = $planet1) Echo "*" &ANSI_6& "You have entered the same planet twice." Echo "*" &ANSI_12& "Please Reenter a Unique Planet number." gosub :GetPlanet5 end if ($planet5 = $planet2) Echo "*" &ANSI_6& "You have entered the same planet twice." Echo "*" &ANSI_12& "Please Reenter a Unique Planet number." gosub :GetPlanet5 end if ($planet5 = $planet3) Echo "*" &ANSI_6& "You have entered the same planet twice." Echo "*" &ANSI_12& "Please Reenter a Unique Planet number." gosub :GetPlanet5 end
if ($planet5 = $planet4) Echo "*" &ANSI_6& "You have entered the same planet twice." Echo "*" &ANSI_12& "Please Reenter a Unique Planet number." gosub :GetPlanet5 end
####Get the sixth planet :GetPlanet6 getInput $planet6 "Planet 6"
#Make sure the sixth planet is unique if ($planet6 = $planet1) Echo "*" &ANSI_6& "You have entered the same planet twice." Echo "*" &ANSI_12& "Please Reenter a Unique Planet number." gosub :GetPlanet6 end if ($planet6 = $planet2) Echo "*" &ANSI_6& "You have entered the same planet twice." Echo "*" &ANSI_12& "Please Reenter a Unique Planet number." gosub :GetPlanet6 end if ($planet6 = $planet3) Echo "*" &ANSI_6& "You have entered the same planet twice." Echo "*" &ANSI_12& "Please Reenter a Unique Planet number." gosub :GetPlanet6 end
if ($planet6 = $planet4) Echo "*" &ANSI_6& "You have entered the same planet twice." Echo "*" &ANSI_12& "Please Reenter a Unique Planet number." gosub :GetPlanet6 end
if ($planet6 = $planet5) Echo "*" &ANSI_6& "You have entered the same planet twice." Echo "*" &ANSI_12& "Please Reenter a Unique Planet number." gosub :GetPlanet6 end
####Get the seventh planet :GetPlanet7 getInput $planet7 "Planet 7"
####Make sure the seventh planet is unique if ($planet7 = $planet1) Echo "*" &ANSI_6& "You have entered the same planet twice." Echo "*" &ANSI_12& "Please Reenter a Unique Planet number." gosub :GetPlanet7 end if ($planet7 = $planet2) Echo "*" &ANSI_6& "You have entered the same planet twice." Echo "*" &ANSI_12& "Please Reenter a Unique Planet number." gosub :GetPlanet7 end if ($planet7 = $planet3) Echo "*" &ANSI_6& "You have entered the same planet twice." Echo "*" &ANSI_12& "Please Reenter a Unique Planet number." gosub :GetPlanet7 end
if ($planet7 = $planet4) Echo "*" &ANSI_6& "You have entered the same planet twice." Echo "*" &ANSI_12& "Please Reenter a Unique Planet number." gosub :GetPlanet7 end
if ($planet7 = $planet5) Echo "*" &ANSI_6& "You have entered the same planet twice." Echo "*" &ANSI_12& "Please Reenter a Unique Planet number." gosub :GetPlanet7 end
if ($planet7 = $planet6) Echo "*" &ANSI_6& "You have entered the same planet twice." Echo "*" &ANSI_12& "Please Reenter a Unique Planet number." gosub :GetPlanet7 end
All comments and suggestion are appreciated. Thanks Edit: It does what I want it to. I am just wondering if there is a need for me to keep repeating the same information.
_________________ -Zoom
Zoom's Team Speak3 Server @ ts3server://vs26.tserverhq.com:7214 TradeWars2002: The Basics @ http://playtwars.altervista.org
|
Thu Mar 20, 2014 11:27 am |
|
 |
Assaulter
Warrant Officer
Joined: Thu May 09, 2013 2:21 pm Posts: 73
|
 Re: My Very First Attempt to Write a script
I have talked to Vid about the code.
Thanks
_________________ -Zoom
Zoom's Team Speak3 Server @ ts3server://vs26.tserverhq.com:7214 TradeWars2002: The Basics @ http://playtwars.altervista.org
|
Thu Mar 20, 2014 8:28 pm |
|
 |
ElderProphet
Commander
Joined: Tue Oct 07, 2003 2:00 am Posts: 1131 Location: Augusta, GA
|
 Re: My Very First Attempt to Write a script
Great, this is how you learn to script - struggle with a code snippet and ask for input. So I submit the code below, not to solve your problem, but to show an example of how to bring arrays to bear on the problem. I'm really using 2 types of arrays; an array of values, and an array of booleans (meaning true or false). Have a look and see what questions we can answer. Code: getInput $count "How many planets will there be?" setArray $planets $count setArray $used 0 // this line effectively nulls the array setVar $i 1 while ($i <= $count) getInput $input "What is the number for Planet " & $i & "?" isNumber $isNumber $input if ($isNumber = FALSE) echo "*Um, try putting in a number." elseif ($used[$input] = TRUE) // undefined array elements have a value of 0 or FALSE echo "*That planet number isn't unique. Try again." else setVar $planets[$i] $input setVar $used[$input] TRUE add $i 1 end end Also, here's my short thesis on arrays: http://www.twxscripts.com/twxarrays1+EP+
_________________ Claim to Fame: only guy to ever crack the TW haggle algorithm, and fig/shield/hold price formula, twice.
|
Thu Mar 20, 2014 10:14 pm |
|
 |
LoneStar
Commander
Joined: Fri Jun 09, 2006 2:00 am Posts: 1393 Location: Canada
|
 Re: My Very First Attempt to Write a script
Would like to point out that Planet Nego is more profitable when you sell as close to %100 of the asking amount as you can. That is to say if the port is buying 3000 Equipment; you stand to make more profit selling 3000 units, rather than a few hundred at a time. It is often worth the few turns to consolidate product (up to the asking amount), and negotiating with the port (Ore, Org, and or Equip), in one turn. In which case, gathering planet numbers is kind of moot.
_________________ ---------------------------- -= QUANTUM Computing 101: 15 = 3 x 5 ... 48% of the time. -= There are 10 types of people in the world: Those that understand Binary and those who do not -= If Oil is made from Dinosaurs, and Plastic is made from Oil... are plastic Dinosaurs made from real Dinosaurs? -= I like to keep my friends and my enemies rich, and wait to see which is which - Tony Stark (R.I.P.)
|
Thu Mar 20, 2014 10:16 pm |
|
 |
Assaulter
Warrant Officer
Joined: Thu May 09, 2013 2:21 pm Posts: 73
|
 Re: My Very First Attempt to Write a script
ElderProphet Yes. This is starting to clear things up. I am having trouble with a few things though. Code: getInput $count "How many planets will there be?" setArray $planets $count
#### The following line represents planet numbers already entered, ####correct? How does it get that if you are giving it no value by nulling ####it?
setArray $used 0
####By setting $i to 1. Are you giving it a starting point? Then it will ####continue with the rest of the array? setVar $i 1
while ($i <= $count) getInput $input "What is the number for Planet " & $i & "?" isNumber $isNumber $input if ($isNumber = FALSE) echo "*Um, try putting in a number."
####Does a variable followed directly by a variable in brackets mean if ####they are equal? elseif ($used[$input] = TRUE) // undefined array elements have a value of 0 or FALSE
echo "*That planet number isn't unique. Try again." else setVar $planets[$i] $input setVar $used[$input] TRUE
####This is where you are telling it to move to the next planet? add $i 1 end end
I have no programming experience as you can see. I might as well be trying to read German. LoneStar I understand. My inspiration for this comes from a game where the product on a planet is Restricted until you upgrade it and you can sell off each planet seperately to cash. Thanks for the help
_________________ -Zoom
Zoom's Team Speak3 Server @ ts3server://vs26.tserverhq.com:7214 TradeWars2002: The Basics @ http://playtwars.altervista.org
|
Fri Mar 21, 2014 9:24 am |
|
 |
ElderProphet
Commander
Joined: Tue Oct 07, 2003 2:00 am Posts: 1131 Location: Augusta, GA
|
 Re: My Very First Attempt to Write a script
To help clarify variable and array names, the common way to refer to an array is with brackets at the end, so that is the convention I'll use here. An array who's size is specified upon creation is referred to as a static array. It's size (or length, as it's also called) is known, and can't grow. You create a static arrays as follows: setArray $array 100 The other kind of array is referred to as a variable array, and doesn't need to be created. You simply start adding values to a dynamic array, and it can grow to any size. In the code I posted, the $planets[] array is static. But the next array, the $used[] array, is dynamic. In TWX, if you create an array with a size of zero, all you've really done is erased it's prior contents, which is the point of my line: setArray $used 0 But later, I'll add values to the $used[] array, even though I've not created it with a specific size, and this is what makes it a dynamic array. Assaulter wrote: ####By setting $i to 1. Are you giving it a starting point? Then it will ####continue with the rest of the array? setVar $i 1 The variable $i is commonly used to iterate, or step through a sequence. There's nothing special about $i, it's just a variable. But anytime you see $i, you should expect that it will be used to step through a series of items. Assaulter wrote: ####Does a variable followed directly by a variable in brackets mean if ####they are equal? elseif ($used[$input] = TRUE) No, that's an array. Read my short thesis I mentioned in that first post. Assaulter wrote: ####This is where you are telling it to move to the next planet? add $i 1 This is where I increment the value of $i, so that I can step through to the next element in the array, when the loop returns back to the top of the loop.
_________________ Claim to Fame: only guy to ever crack the TW haggle algorithm, and fig/shield/hold price formula, twice.
|
Sat Mar 22, 2014 6:50 pm |
|
 |
Assaulter
Warrant Officer
Joined: Thu May 09, 2013 2:21 pm Posts: 73
|
 Re: My Very First Attempt to Write a script
Ok. This clears things up. Thanks for your help!
_________________ -Zoom
Zoom's Team Speak3 Server @ ts3server://vs26.tserverhq.com:7214 TradeWars2002: The Basics @ http://playtwars.altervista.org
|
Sun Mar 23, 2014 1:05 pm |
|
 |
Assaulter
Warrant Officer
Joined: Thu May 09, 2013 2:21 pm Posts: 73
|
 Re: My Very First Attempt to Write a script
I spent two hours working on how to access the input we stored and put it to use. Don't laugh. Code:
getWordPos CURRENTLINE $pos "Command [TL=" if ($pos <> 1) echo "**" &ANSI_14& " Must Start at Command Prompt. Exiting..." halt end
getInput $count "***" &ANSI_11& "How many planets will there be?" setArray $planets $count setArray $used 0 setVar $i 1 while ($i <= $count) getInput $input "**" &ANSI_11& "What is the number for Planet " & $i & "?" isNumber $isNumber $input if ($isNumber = FALSE) echo "**" &ANSI_14& "Um, try putting in a number." elseif ($used[$input] = TRUE) echo "**" &ANSI_14& "That planet number isn't unique. Try again." else setVar $planets[$i] $input setVar $used[$input] TRUE add $i 1 end end
setVar $i 1 while ($i <= $count) send "p n" $planets[$i] "*" waiton "How many units of" send "*" waiton "Your offer" send "*" waiton "How many units of" send "*" waiton "Your offer" send "*" add $i 1 end
It maybe juvenile but it is performing its assigned task in game.  Obviously you wouldn't want to use it in a game unless you don't want the max profit. Just practice.
_________________ -Zoom
Zoom's Team Speak3 Server @ ts3server://vs26.tserverhq.com:7214 TradeWars2002: The Basics @ http://playtwars.altervista.org
|
Sun Mar 23, 2014 10:38 pm |
|
 |
Kaus
Gameop
Joined: Tue Nov 19, 2002 3:00 am Posts: 1050 Location: USA
|
 Re: My Very First Attempt to Write a script
If you feel like challenging yourself you could condense that into 1 while statement. Part of what I used to love about scripting for TW was attempting to get the fewest lines possible to do what I want.
_________________ Dark Dominion TWGS Telnet://twgs.darkworlds.org:23 ICQ#31380757, -=English 101 pwns me=- "This one claims to have been playing since 1993 and didn't know upgrading a port would raise his alignment."
|
Mon Mar 24, 2014 11:40 am |
|
 |
Assaulter
Warrant Officer
Joined: Thu May 09, 2013 2:21 pm Posts: 73
|
 Re: My Very First Attempt to Write a script
Awesome. Yes. I will try that. Then I guess it is on to negotiating for max profit.
_________________ -Zoom
Zoom's Team Speak3 Server @ ts3server://vs26.tserverhq.com:7214 TradeWars2002: The Basics @ http://playtwars.altervista.org
|
Mon Mar 24, 2014 12:24 pm |
|
 |
ElderProphet
Commander
Joined: Tue Oct 07, 2003 2:00 am Posts: 1131 Location: Augusta, GA
|
 Re: My Very First Attempt to Write a script
Nice work, that is exactly how you should iterate through the array. I'm not sure your waitOn and send will succeed at every port, but the iteration looks good. Another important piece that I was hoping you'd catch is that the $count variable needs to be tested to make sure it's a number. You should add that test, following the same steps used elsewhere in that script. You might even make sure that it is less than or equal to the number of planets in the sector - which is available querying the database like so: SECTOR.PLANETCOUNT[CURRENTSECTOR] Assaulter wrote: Awesome. Yes. I will try that. Then I guess it is on to negotiating for max profit. My haggle script runs in the background and makes that easy.
_________________ Claim to Fame: only guy to ever crack the TW haggle algorithm, and fig/shield/hold price formula, twice.
|
Mon Mar 24, 2014 8:05 pm |
|
 |
Assaulter
Warrant Officer
Joined: Thu May 09, 2013 2:21 pm Posts: 73
|
 Re: My Very First Attempt to Write a script
No I didn't think of that. I will look into adding that as well as making sure it is equal to number of planets in sector. Thanks for the input.
Yes. Having your haggle script running did come to mind. That would make it really easy to do. I am currently trying to load planet nego and communicate with it ,though. for learning purposes.
Thanks again
_________________ -Zoom
Zoom's Team Speak3 Server @ ts3server://vs26.tserverhq.com:7214 TradeWars2002: The Basics @ http://playtwars.altervista.org
|
Tue Mar 25, 2014 12:55 am |
|
 |
Assaulter
Warrant Officer
Joined: Thu May 09, 2013 2:21 pm Posts: 73
|
 Re: My Very First Attempt to Write a script
Well I have got the script to use planet nego with preset variables. Gonna dive into user inputted variables later. I tried to put an isNumber in for the $count variable, but keep getting a run-time error saying it is not a number. It echos "Please enter a number" but then the script terminates. Here is what i got: Code: getWordPos CURRENTLINE $pos "Command [TL=" if ($pos <> 1) echo "**" &ANSI_14& " Must Start at Command Prompt. Exiting..." halt end
getInput $count "***" &ANSI_11& "How many planets would you like to Neg?" isNumber $isNumber1 $count if ($isNumber1 = FALSE) echo "**" &ANSI_14& "Please enter a number." elseif ($isNumber1 >= "8") echo "**" &ANSI_14& "This script only supports up to 7 planets" else setvar $count TRUE end
setArray $planets $count setArray $used 0 setVar $i 1 while ($i <= $count) getInput $input "**" &ANSI_11& "What is the number for Planet " & $i & "?" isNumber $isNumber2 $input if ($isNumber2 = FALSE) echo "**" &ANSI_14& "Um, try putting in a number." elseif ($used[$input] = TRUE) echo "**" &ANSI_14& "That planet number isn't unique. Try again." else setVar $planets[$i] $input setVar $used[$input] TRUE add $i 1 end end
setVar $i 1 while ($i <= $count) SetVar $_CK_PNEGO_FUELTOSELL "-1" SetVar $_CK_PNEGO_ORGTOSELL "max" SetVar $_CK_PNEGO_EQUIPTOSELL "max" setVar $_CK_PTRADESETTING "100" SaveVar $_CK_PNEGO_FUELTOSELL SaveVar $_CK_PNEGO_ORGTOSELL SaveVar $_CK_PNEGO_EQUIPTOSELL saveVar $_CK_PTRADESETTING
send "l" $planets[$i] "*" waiton "Fuel Ore" waiton "elp) [D]" load "scripts\_ck_planet_nego.cts" waiton " --- Done with port" send "q" add $i 1 end SetVar $_CK_PNEGO_FUELTOSELL "DELETED" SetVar $_CK_PNEGO_ORGTOSELL "DELETED" SetVar $_CK_PNEGO_EQUIPTOSELL "DELETED" SaveVar $_CK_PNEGO_FUELTOSELL SaveVar $_CK_PNEGO_ORGTOSELL SaveVar $_CK_PNEGO_EQUIPTOSELL
halt
_________________ -Zoom
Zoom's Team Speak3 Server @ ts3server://vs26.tserverhq.com:7214 TradeWars2002: The Basics @ http://playtwars.altervista.org
|
Tue Mar 25, 2014 9:00 pm |
|
 |
Assaulter
Warrant Officer
Joined: Thu May 09, 2013 2:21 pm Posts: 73
|
 Re: My Very First Attempt to Write a script
Should have just spent a little longer with it. Not sure if this is the answer but it worked: Code: :HowMany getInput $howmany "***" &ANSI_11& "How many planets would you like to Neg?" isNumber $isNumber1 $howmany if ($isNumber1 = FALSE) echo "**" &ANSI_14& "Please enter a number." goto :HowMany elseif ($howmany >= "8") echo "**" &ANSI_14& "This script only supports up to 7 planets" goto :HowMany else setvar $count $howmany end
I appreciate any input. All the input thus far, has made learning this seem possible. Thanks
_________________ -Zoom
Zoom's Team Speak3 Server @ ts3server://vs26.tserverhq.com:7214 TradeWars2002: The Basics @ http://playtwars.altervista.org
|
Tue Mar 25, 2014 9:28 pm |
|
 |
ElderProphet
Commander
Joined: Tue Oct 07, 2003 2:00 am Posts: 1131 Location: Augusta, GA
|
 Re: My Very First Attempt to Write a script
Looks good.
To include the query to the TWX database, where you ensure the planet qty doesn't exceed the actual number in the sector, you would just change the one line, from this: elseif ($howmany >= "8")
...to this: elseif ($howmany > SECTOR.PLANETCOUNT[CURRENTSECTOR])
_________________ Claim to Fame: only guy to ever crack the TW haggle algorithm, and fig/shield/hold price formula, twice.
|
Wed Mar 26, 2014 9:53 pm |
|
 |
|