data dirs renamed
[k8-i-v-a-n.git] / doc / script_course / lecture4.html
blob89c670f42322fbf154a2e18da4df64ec97a9ac47
1 <html>
2 <head>
3 <title>IVAN script lecture 4</title>
4 </head>
5 <body>
7 <h2>holybanana's IVAN script course, lecture IV</h2>
8 <h4>Basic syntax structure of the script</h4>
10 <p>Browsing through he script, you may note that most of the commands
11 there are of form X { A=B; C=D; E=F; ... }. Let's call this form a <i>structure
12 definition</i> and the commands A=B; C=D; and so on <i>member value
13 assignments</i>. In other words, the script consists of <i>structures</i> which
14 have <i>members</i> to which you can assign <i>values</i>. The order
15 which you present structure definitions and member value assignments
16 has usually no relevance. (There are some rare exceptions.) The script also
17 works whether you divide your script to separate lines or not. You
18 can also insert extra spaces and tabs between words without changing the
19 meaning of the script. However, you cannot cut a line or insert
20 a space or tab inside a word, for instance write "darkmage" as "dark mage".
21 Even though it is not syntactically necessary, you should follow the
22 intendation used previously in the script. So write like this:</p>
24 <pre>
27 A=B;
28 C=D;
29 E=F;
30 ...
32 </pre>
34 And please avoid intending like this, for instance:
36 <pre>
37 X {
38 A=B;
39 C=D;
40 E=F;
41 ...
43 </pre>
45 <p>Each member of a structure has a <i>type</i>. (Remember "type" also
46 has another meaning introduced in <a href="lecture1.html">lecture I</a>.
47 We use the phrases "entity type" and "script member type" to distinguish
48 between these two meanings, if there's some possibility of confusion.)
49 The most common are other structures, <i>integers</i>, <i>vectors</i>,
50 <i>strings</i>, <i>contentscripts</i>
51 and <i>arrays of elements of a certain type</i>.</p>
53 <p>Integers are presented like this: 5, 1000000, -532489, 6*527, 5*(-6+62/7)+9
54 and so on. Division is rounded down. One special class of integers are colors,
55 which are presented like this: rgb16(100,0,100) or rgb24(255,0,0). These
56 are functions which transform triples (red,green,blue) into integers.
57 The elements of these triples are in the range from 0 to 255. So rgb16(0,255,0)
58 and rgb24(0,255,0) both mean bright green. rgb16 is used for colors of material
59 coloring and rgb24 is used for lights.</p>
61 <p>Vectors represent positions in two-dimensional spaces, for instance in
62 bitmaps or levels. They are presented as X, Y where X and Y are integers.</p>
64 <p>Any word is a string. So is a phrase surrounded by quotation marks. So
65 Alfred is a string and so is "Alex the Great", but the words Alex the Great
66 are three separate strings. It is customary to always use quotation marks to
67 help readers recognize strings, so you should use "Alfred" instead of Alfred.
68 You may use the symbols \" to insert a quotation mark inside a string.
69 Example: "The guard says: \"Buzz off!\"".</p>
71 <p>Contentscripts are the commands which represent instantiations of entities,
72 explained in <a href="lecture2.html">lecture II</a>, for instance
73 OMMEL_HAIR cloak(CLOAK_OF_FIRE_RESISTANCE). There are character, item,
74 ground terrain and over terrain contentscripts.</p>
76 <p>An array of elements of a certain type is presented as { n, A, B, C, ..., X; },
77 where A, B, C, ..., X is a list of n elements which are all of that type. For instance,
78 let's consider an array of item contentscripts, which are quite common. Let's choose
79 a structure member of such an array type: the Inventory member of character databases
80 of char.dat. As an example, here's the basic inventory of Golgor Dhan:</p>
82 <pre>
83 Inventory = { 2, scrollofenchantarmor { Times = 2; }, scrollofenchantweapon { Times = 2; } }
84 </pre>
86 <p>Btw, here we learn a new parameter for instantiations, Times. It is a member of
87 integer type and determines how many times the instantiation is repeated. So the above
88 script means exactly the same as</p>
90 <pre>
91 Inventory = { 4, scrollofenchantarmor, scrollofenchantarmor, scrollofenchantweapon, scrollofenchantweapon; }
92 </pre>
94 <p>Often, you want the array to have only one element. Here's an example:</p>
96 <pre>
97 Square, Random;
99 Items == mine(BIG_MINE) { Team = MONSTER_TEAM; IsActive = true; Chance = 50; }
101 </pre>
103 <p>This creates a big mine at a random location which is visible to monsters but
104 not other teams, is active and has 50% chance of appearing. Note that you use the
105 syntax == X; instead of = { 1, X; }. The latter works also, but is uglier.</p>
107 <p>A structure may have a member of its own type as a member or member of a member, etc.
108 This allows recursion. For instance, you can determine the contents of chest as an
109 array of item contentscripts even though the chest is presented as an item contentscript
110 itself. Example:</p>
112 <pre>
113 Inventory == itemcontainer(LARGE_CHEST) { ItemsInside == itemcontainer(CHEST) { ItemsInside == itemcontainer(SMALL_CHEST); } }
114 </pre>
116 <p>If you add this to the player, he will get a chest inside a chest inside a chest.</p>
118 <p>Exercise: Suppose the player's bananagrower friends give him a farewell gift at the start
119 of each journey. That is, a random amount of bananas is generated at the start of the game
120 in the player's inventory. One banana is always generated. Five to six bananas are most
121 common. The maximum of ten bananas is generated only rarely. Bananagrowers are generous
122 to their friends but ten is already a bit too much, as they have to feed their children.
123 How do you implement this feature using just the script?</p>
125 <p>Exercise II: Is there any way to have an item appear one time in ten thousand games in
126 a specific place, using just the script? Answer yes or no. If you answered yes, present an example.</p>
128 <hr>
131 <a href="lecture3.html">Previous lecture</a>
132 <a href="lecture5.html">Next lecture</a>
133 <a href="index.html">Back to main</a>
134 </p>
136 <hr>
138 <p>Last modified September 1 2006.</p>
140 </body>
141 </html>