From NEAT Portal Wiki
Jump to: navigation, search
(Page created)
 
(lvl10 demonstration)
Line 35: Line 35:
 
===A working example===
 
===A working example===
 
Scripts take up memory.  If you have 3 accounts with 9 cities each, with all of them running a 150 line script, you will find yourself hard-pressed for memory real quick.
 
Scripts take up memory.  If you have 3 accounts with 9 cities each, with all of them running a 150 line script, you will find yourself hard-pressed for memory real quick.
 +
 +
 +
BigBill wrote an excellen script to convert a lvl10 npc.  It ran 172 lines. Here is the same concept (ty, BigBill, the concept is the hard part) reduced to 1/5 the size.
 +
 +
<blockquote><font color="green">
 +
//  city can be iron or wood<br>
 +
set city iron<br>
 +
<br>
 +
//  barx = # of cottages to convert<br>
 +
set barx 6<br>
 +
<br>
 +
//  lvl10_barx = how many to turn lvl10<br>
 +
set lvl10_barx 9<br>
 +
<br>
 +
1: goto %city%<br>
 +
<br>
 +
2: label build_bldg<br>
 +
3: create %new_bldg%<br>
 +
4: upgrade %new_bldg%<br>
 +
5: repeat 8<br>
 +
6: gosubreturn<br>
 +
<br>
 +
7: label demo_bldg<br>
 +
8: demo %old_bldg% level10<br>
 +
9: demo %old_bldg%<br>
 +
10: repeat 9<br>
 +
11: gosubreturn<br>
 +
<br>
 +
12: label doit<br>
 +
13: gosub demo_bldg<br>
 +
14: gosub build_bldg<br>
 +
15: gosubreturn<br>
 +
<br>
 +
16: label iron<br>
 +
set new_bldg i<br>
 +
set old_bldg s<br>
 +
17: goto fields<br>
 +
<br>
 +
18: label wood<br>
 +
set new_bldg s<br>
 +
set old_bldg i<br>
 +
<br>
 +
19: label fields<br>
 +
20: gosub doit<br>
 +
set old_bldg q<br>
 +
21: gosub doit<br>
 +
set old_bldg f<br>
 +
22: gosub doit<br>
 +
23: repeat 37<br>
 +
<br>
 +
set new_bldg b<br>
 +
set old_bldg fo<br>
 +
24: gosub doit<br>
 +
set old_bldg ws<br>
 +
25: gosub doit<br>
 +
set old_bldg st<br>
 +
26: gosub doit<br>
 +
27: ifgoto ( %barx% == 0 ) upbarx<br>
 +
set old_bldg c<br>
 +
28: gosub doit<br>
 +
29: repeat %barx%<br>
 +
<br>
 +
30: label upbarx<br>
 +
31: ifgoto ( %lvl10_barx% == 0 ) end<br>
 +
32: upgrade b level10<br>
 +
33: repeat %lvl10_barx%<br>
 +
<br>
 +
</font></blockquote>
 +
 +
There are certain caseswhich do not support changing variables 'on-the-fly'.  The most notable that I have found so far are the city manager arrays.  This is demonsrtated in the following example:
 +
 +
 +
<blockquote>
 +
<font color="green">
 +
1: goto start<br>
 +
2: label output<br>
 +
3: print x %n%<br>
 +
4: print x ( m_city.cityManager.heroes[%n%].name )<br>
 +
5: gosubreturn<br>
 +
<br>
 +
6: label start<br>
 +
set n 0<br>
 +
7: gosub output<br>
 +
set n 1<br>
 +
8: gosub output<br>
 +
set n 2<br>
 +
9: gosub output<br>
 +
<br><font color="brown">
 +
19:15:49 Running line 1<br>
 +
19:15:49 Going to line 6<br>
 +
19:15:51 Running line 7<br>
 +
19:15:51 Going to line 2<br>
 +
19:15:53 Running line 3<br>
 +
19:15:53 %n% <br>
 +
19:15:55 Running line 4<br>
 +
19:15:55 ( ace ) <br>
 +
<font color="blue">(Here '%n% is undefined, therefore it defaults to 0, hero[0] is ace)</font><font color="brown"><br>
 +
19:15:57 Running line 5<br>
 +
19:15:57 Returning to line 7<br>
 +
19:15:59 Running line 8<br>
 +
19:15:59 Going to line 2<br>
 +
19:16:01 Running line 3<br>
 +
19:16:01 %n% <br>
 +
19:16:03 Running line 4<br>
 +
19:16:03 ( ace ) <br>
 +
19:16:05 Running line 5<br>
 +
19:16:05 Returning to line 8<br>
 +
19:16:07 Running line 9<br>
 +
19:16:07 Going to line 2<br>
 +
19:16:09 Running line 3<br>
 +
19:16:09 %n% <br>
 +
19:16:11 Running line 4<br>
 +
19:16:11 ( ace ) <br>
 +
19:16:13 Running line 5<br>
 +
19:16:13 Returning to line 9<br>
 +
19:16:14 Finished script<br>
 +
<br>
 +
<font color="blue">(Restarting the script...)</font><font color="brown"><br>
 +
19:16:19 Running line 1<br>
 +
19:16:19 Going to line 6<br>
 +
19:16:21 Running line 7<br>
 +
19:16:21 Going to line 2<br>
 +
19:16:23 Running line 3<br>
 +
19:16:23 2 <br>
 +
<font color="blue">(Here it recalls the last value set in the script...)</font><font color="brown"><br>
 +
19:16:25 Running line 4<br>
 +
19:16:25 ( k ) <br>
 +
19:16:27 Running line 5<br>
 +
19:16:27 Returning to line 7<br>
 +
19:16:29 Running line 8<br>
 +
19:16:29 Going to line 2<br>
 +
19:16:31 Running line 3<br>
 +
19:16:31 2 <br>
 +
19:16:33 Running line 4<br>
 +
19:16:33 ( k ) <br>
 +
19:16:35 Running line 5<br>
 +
19:16:35 Returning to line 8<br>
 +
19:16:37 Running line 9<br>
 +
19:16:37 Going to line 2<br>
 +
19:16:39 Running line 3<br>
 +
19:16:39 2 <br>
 +
19:16:41 Running line 4<br>
 +
19:16:41 ( k ) <br>
 +
19:16:43 Running line 5<br>
 +
19:16:43 Returning to line 9<br>
 +
19:16:44 Finished script<br>
 +
<br>
 +
<font color="blue">(Ok, lets try changing the last set to..)</font><font color="green"><br>
 +
<br>
 +
8: gosub output<br>
 +
set n 1<br>
 +
9: gosub output<br>
 +
</font><font color="brown"><br>
 +
19:28:55 Running line 1<br>
 +
19:28:55 Going to line 6<br>
 +
19:28:57 Running line 7<br>
 +
19:28:57 Going to line 2<br>
 +
19:28:59 Running line 3<br>
 +
19:28:59 2 <br>
 +
19:29:01 Running line 4<br>
 +
19:29:01 ( k ) <br>
 +
19:29:03 Running line 5<br>
 +
19:29:03 Returning to line 7<br>
 +
19:29:05 Running line 8<br>
 +
19:29:05 Going to line 2<br>
 +
19:29:06 Script Stopped<br>
 +
<br>
 +
<font color="blue">(It still retains the previous value, and will ad ininitim except for:)</font><font color="green"><br>
 +
19:29:06 Script Stopped<br>
 +
<font color="blue">(This changes things. Watch:)</font><font color="green"><br>
 +
<br>
 +
19:32:20 Running line 1<br>
 +
19:32:20 Going to line 6<br>
 +
19:32:22 Running line 7<br>
 +
19:32:22 Going to line 2<br>
 +
19:32:24 Running line 3<br>
 +
19:32:24 1 <br>
 +
19:32:26 Running line 4<br>
 +
19:32:26 ( a ) <br>
 +
19:32:28 Running line 5<br>
 +
19:32:28 Returning to line 7<br>
 +
19:32:30 Running line 8<br>
 +
19:32:30 Going to line 2<br>
 +
</font>
 +
</blockquote>

Revision as of 21:46, 24 June 2011

Introduction

In this section we will look at some advanced techniques for saving memory, creating reusable blocks and methods of keeping things under control.

Rule #1: Test your script!
This is particularily important for attack scripts and any script that destroys a troop, building or wall defense.
Ideally you would test any such script in an empty city. It is ok to produce errors when you expect them.
Set quantities to minimum numbers until you are sure the script functions properly.
Before you run your market script to sell 9,999,999 food at a time, run it through selling 100 just to test the logic.
Rule #2: Keep your scripts as compact and simple as possible.
This includes using gosub routines and replaceable variables.
Rule #3: Be sure you have enough troops, resources, heroes, and most importantly, time.
There is nothing worse than getting to wave #8 and encountering an "Illegal camp time ignored" error.
Before running an attack script against your enemy, run it against an npc 1 mile beyond your enemy.
This will be further discussed in the attack section.

Y.A.E.B. contains space for 5 scripts per city. You should take advantage of these spaces to store scripts that you use regularly.


Kiss your script

Kiss stands for 'Keep it simple, stupid'. The best way to do this is with sub-routines. Any time your script does the same thing more than once, you should use a sub-routine. Bear in mind, however, that there is absolutely no advantage to having a sub-routine who's only function is to send you somewhere else.

Use repeat and loop whenever you want to run the same command or chunk of commands more than once. Replaceable variables are by far the most economical way to keep your script compact and make it reuseable.

The Simple Attack script is a good example. Simply changing the set xy xxx,yyy and set army xxxx lines will allow you to send any army to any city. The Schwack Attack, found immediately following the previous example is a far more complex example . I keep a copy in one of the script spaces of every city.

The Schwack Attack also demonstrates a simple way of selecting an option from multiple choices. Only the first goto command encountered is able to run, so you simply cut the and paste the attack you want to perform into the first position and run the script. It is best to set the 'always run script from line #1' option in the misc. page of the options. The script was written before this option was availible, and I have since eliminated the 'goto top' line from the end of the script.


A working example

Scripts take up memory. If you have 3 accounts with 9 cities each, with all of them running a 150 line script, you will find yourself hard-pressed for memory real quick.


BigBill wrote an excellen script to convert a lvl10 npc. It ran 172 lines. Here is the same concept (ty, BigBill, the concept is the hard part) reduced to 1/5 the size.

// city can be iron or wood
set city iron

// barx = # of cottages to convert
set barx 6

// lvl10_barx = how many to turn lvl10
set lvl10_barx 9

1: goto %city%

2: label build_bldg
3: create %new_bldg%
4: upgrade %new_bldg%
5: repeat 8
6: gosubreturn

7: label demo_bldg
8: demo %old_bldg% level10
9: demo %old_bldg%
10: repeat 9
11: gosubreturn

12: label doit
13: gosub demo_bldg
14: gosub build_bldg
15: gosubreturn

16: label iron
set new_bldg i
set old_bldg s
17: goto fields

18: label wood
set new_bldg s
set old_bldg i

19: label fields
20: gosub doit
set old_bldg q
21: gosub doit
set old_bldg f
22: gosub doit
23: repeat 37

set new_bldg b
set old_bldg fo
24: gosub doit
set old_bldg ws
25: gosub doit
set old_bldg st
26: gosub doit
27: ifgoto ( %barx% == 0 ) upbarx
set old_bldg c
28: gosub doit
29: repeat %barx%

30: label upbarx
31: ifgoto ( %lvl10_barx% == 0 ) end
32: upgrade b level10
33: repeat %lvl10_barx%

There are certain caseswhich do not support changing variables 'on-the-fly'. The most notable that I have found so far are the city manager arrays. This is demonsrtated in the following example:


1: goto start
2: label output
3: print x %n%
4: print x ( m_city.cityManager.heroes[%n%].name )
5: gosubreturn

6: label start
set n 0
7: gosub output
set n 1
8: gosub output
set n 2
9: gosub output

19:15:49 Running line 1
19:15:49 Going to line 6
19:15:51 Running line 7
19:15:51 Going to line 2
19:15:53 Running line 3
19:15:53 %n%
19:15:55 Running line 4
19:15:55 ( ace )
(Here '%n% is undefined, therefore it defaults to 0, hero[0] is ace)
19:15:57 Running line 5
19:15:57 Returning to line 7
19:15:59 Running line 8
19:15:59 Going to line 2
19:16:01 Running line 3
19:16:01 %n%
19:16:03 Running line 4
19:16:03 ( ace )
19:16:05 Running line 5
19:16:05 Returning to line 8
19:16:07 Running line 9
19:16:07 Going to line 2
19:16:09 Running line 3
19:16:09 %n%
19:16:11 Running line 4
19:16:11 ( ace )
19:16:13 Running line 5
19:16:13 Returning to line 9
19:16:14 Finished script

(Restarting the script...)
19:16:19 Running line 1
19:16:19 Going to line 6
19:16:21 Running line 7
19:16:21 Going to line 2
19:16:23 Running line 3
19:16:23 2
(Here it recalls the last value set in the script...)
19:16:25 Running line 4
19:16:25 ( k )
19:16:27 Running line 5
19:16:27 Returning to line 7
19:16:29 Running line 8
19:16:29 Going to line 2
19:16:31 Running line 3
19:16:31 2
19:16:33 Running line 4
19:16:33 ( k )
19:16:35 Running line 5
19:16:35 Returning to line 8
19:16:37 Running line 9
19:16:37 Going to line 2
19:16:39 Running line 3
19:16:39 2
19:16:41 Running line 4
19:16:41 ( k )
19:16:43 Running line 5
19:16:43 Returning to line 9
19:16:44 Finished script

(Ok, lets try changing the last set to..)

8: gosub output
set n 1
9: gosub output

19:28:55 Running line 1
19:28:55 Going to line 6
19:28:57 Running line 7
19:28:57 Going to line 2
19:28:59 Running line 3
19:28:59 2
19:29:01 Running line 4
19:29:01 ( k )
19:29:03 Running line 5
19:29:03 Returning to line 7
19:29:05 Running line 8
19:29:05 Going to line 2
19:29:06 Script Stopped

(It still retains the previous value, and will ad ininitim except for:)
19:29:06 Script Stopped
(This changes things. Watch:)

19:32:20 Running line 1
19:32:20 Going to line 6
19:32:22 Running line 7
19:32:22 Going to line 2
19:32:24 Running line 3
19:32:24 1
19:32:26 Running line 4
19:32:26 ( a )
19:32:28 Running line 5
19:32:28 Returning to line 7
19:32:30 Running line 8
19:32:30 Going to line 2
</blockquote>