1
0
Fork 0
mirror of https://github.com/ganelson/inform.git synced 2024-07-08 18:14:21 +03:00
inform7/docs/standard_rules/S-lt2.html

487 lines
26 KiB
HTML

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>S/lt</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta http-equiv="Content-Language" content="en-gb">
<link href="inweb.css" rel="stylesheet" rev="stylesheet" type="text/css">
</head>
<body>
<!--Weave of 'S/lt2' generated by 7-->
<ul class="crumbs"><li><a href="../webs.html">&#9733;</a></li><li><a href="index.html">standard_rules Template Library</a></li><li><b>Light Template</b></li></ul><p class="purpose">The determination of light, visibility and physical access.</p>
<ul class="toc"><li><a href="#SP1">&#167;1. Darkness</a></li><li><a href="#SP2">&#167;2. Light Measurement</a></li><li><a href="#SP3">&#167;3. Invariant</a></li><li><a href="#SP4">&#167;4. Adjust Light Rule</a></li><li><a href="#SP5">&#167;5. Silent Light Consideration</a></li><li><a href="#SP6">&#167;6. Translucency</a></li><li><a href="#SP7">&#167;7. Visibility Parent</a></li><li><a href="#SP8">&#167;8. Find Visibility Levels</a></li><li><a href="#SP9">&#167;9. Touchability Ceiling</a></li><li><a href="#SP10">&#167;10. Scope Ceiling</a></li><li><a href="#SP11">&#167;11. Object Is Untouchable</a></li><li><a href="#SP12">&#167;12. Access Through Barriers Rule</a></li><li><a href="#SP13">&#167;13. Can't Reach Inside Closed Containers Rule</a></li><li><a href="#SP14">&#167;14. Can't Reach Outside Closed Containers Rule</a></li><li><a href="#SP15">&#167;15. Can't Reach Inside Rooms Rule</a></li></ul><hr class="tocbar">
<p class="inwebparagraph"><a id="SP1"></a><b>&#167;1. Darkness. </b>"Darkness" is not really a place: but in I6 it has to be an object so that
the location-name on the status line can be "Darkness". In I7 we use it as
little as possible: note that it has no properties.
</p>
<pre class="display">
<span class="plain">Object thedark "(darkness object)";</span>
</pre>
<p class="inwebparagraph"></p>
<p class="inwebparagraph"><a id="SP2"></a><b>&#167;2. Light Measurement. </b>These two routines, <code class="display"><span class="extract">OffersLight</span></code> and <code class="display"><span class="extract">HasLightSource</span></code>, are largely unchanged
from their I6 definitions; see the Inform Designer's Manual, 4th edition,
for a commentary. In terms of how they are used in I7, <code class="display"><span class="extract">OffersLight</span></code> is called
only by the two rules below; <code class="display"><span class="extract">HasLightSource</span></code> is called also in determining
the scope, that is, what is visible to the player.
</p>
<pre class="display">
<span class="plain">[ OffersLight obj j;</span>
<span class="plain">while (obj) {</span>
<span class="plain">if (obj has light) rtrue;</span>
<span class="plain">objectloop (j in obj) if (HasLightSource(j)) rtrue;</span>
<span class="plain">if ((obj has container) &amp;&amp; (obj hasnt open) &amp;&amp; (obj hasnt transparent)) rfalse;</span>
<span class="plain">if ((obj provides component_parent) &amp;&amp; (obj.component_parent))</span>
<span class="plain">obj = obj.component_parent;</span>
<span class="plain">else</span>
<span class="plain">obj = parent(obj);</span>
<span class="plain">}</span>
<span class="plain">rfalse;</span>
<span class="plain">];</span>
<span class="plain">[ HasLightSource i j ad sr po;</span>
<span class="plain">if (i == 0) rfalse;</span>
<span class="plain">if (i has light) rtrue;</span>
<span class="plain">if ((IsSeeThrough(i)) &amp;&amp; (~~(HidesLightSource(i))))</span>
<span class="plain">objectloop (j in i)</span>
<span class="plain">if (HasLightSource(j)) rtrue;</span>
<span class="plain">ad = i.&amp;add_to_scope;</span>
<span class="plain">if (parent(i) ~= 0 &amp;&amp; ad ~= 0) {</span>
<span class="plain">if (metaclass(ad--&gt;0) == Routine) {</span>
<span class="plain">ats_hls = 0; ats_flag = 1;</span>
<span class="plain">sr = scope_reason; po = parser_one;</span>
<span class="plain">scope_reason = LOOPOVERSCOPE_REASON; parser_one = 0;</span>
<span class="plain">RunRoutines(i, add_to_scope);</span>
<span class="plain">scope_reason = sr; parser_one = po;</span>
<span class="plain">ats_flag = 0; if (ats_hls == 1) rtrue;</span>
<span class="plain">}</span>
<span class="plain">else {</span>
<span class="plain">for (j=0 : (WORDSIZE*j)&lt;i.#add_to_scope : j++)</span>
<span class="plain">if ((ad--&gt;j) &amp;&amp; (HasLightSource(ad--&gt;j) == 1)) rtrue;</span>
<span class="plain">}</span>
<span class="plain">}</span>
<span class="plain">if (ComponentHasLight(i)) rtrue;</span>
<span class="plain">rfalse;</span>
<span class="plain">];</span>
<span class="plain">[ ComponentHasLight o obj next_obj;</span>
<span class="plain">if (o provides component_child) {</span>
<span class="plain">obj = o.component_child;</span>
<span class="plain">while (obj) {</span>
<span class="plain">next_obj = obj.component_sibling;</span>
<span class="plain">if (obj has light) rtrue;</span>
<span class="plain">if (HasLightSource(obj)) rtrue;</span>
<span class="plain">if ((obj provides component_child) &amp;&amp; (ComponentHasLight(obj))) rtrue;</span>
<span class="plain">obj = next_obj;</span>
<span class="plain">}</span>
<span class="plain">}</span>
<span class="plain">rfalse;</span>
<span class="plain">];</span>
<span class="plain">[ HidesLightSource obj;</span>
<span class="plain">if (obj == player) rfalse;</span>
<span class="plain">if (obj has transparent or supporter) rfalse;</span>
<span class="plain">if (obj has animate) rfalse;</span>
<span class="plain">if (obj has container) return (obj hasnt open);</span>
<span class="plain">return (obj hasnt enterable);</span>
<span class="plain">];</span>
</pre>
<p class="inwebparagraph"></p>
<p class="inwebparagraph"><a id="SP3"></a><b>&#167;3. Invariant. </b>The following routines maintain two variables about the light condition
of the player:
</p>
<p class="inwebparagraph"></p>
<ul class="items"><li>(a) If on the most recent check the player was in light, then <code class="display"><span class="extract">location</span></code>
equals <code class="display"><span class="extract">real_location</span></code> and <code class="display"><span class="extract">lightflag</span></code> is false.
</li><li>(b) If on the most recent check the player was in darkness, then <code class="display"><span class="extract">location</span></code>
equals <code class="display"><span class="extract">thedark</span></code> and <code class="display"><span class="extract">lightflag</span></code> is true.
</li></ul>
<p class="inwebparagraph">Note that they are not allowed to alter <code class="display"><span class="extract">real_location</span></code>, whose definition
has nothing to do with light.
</p>
<pre class="display">
<span class="plain">Global lightflag = false;</span>
</pre>
<p class="inwebparagraph"></p>
<p class="inwebparagraph"><a id="SP4"></a><b>&#167;4. Adjust Light Rule. </b>This rule fires at least once a turn, and more often when the player moves
location, since that's likely to invalidate any previous assumptions. It
compares the state of light now with the last time it ran, and gives
instructions on what to do in each of the four possibilities.
</p>
<pre class="display">
<span class="plain">[ ADJUST_LIGHT_R previous_light_condition;</span>
<span class="plain">previous_light_condition = lightflag;</span>
<span class="plain">lightflag = OffersLight(parent(player));</span>
<span class="plain">if ((previous_light_condition == false) &amp;&amp; (lightflag == false)) {</span>
<span class="plain">location = thedark;</span>
<span class="plain">rfalse;</span>
<span class="plain">}</span>
<span class="plain">if ((previous_light_condition == false) &amp;&amp; (lightflag == true)) {</span>
<span class="plain">location = real_location;</span>
<span class="plain">CarryOutActivity(PRINTING_NEWS_OF_LIGHT_ACT);</span>
<span class="plain">rfalse;</span>
<span class="plain">}</span>
<span class="plain">if ((previous_light_condition == true) &amp;&amp; (lightflag == false)) {</span>
<span class="plain">location = thedark;</span>
<span class="plain">DivideParagraphPoint();</span>
<span class="plain">BeginActivity(PRINTING_NEWS_OF_DARKNESS_ACT);</span>
<span class="plain">if (ForActivity(PRINTING_NEWS_OF_DARKNESS_ACT) == false) {</span>
<span class="plain">ADJUST_LIGHT_RM('A'); new_line;</span>
<span class="plain">}</span>
<span class="plain">EndActivity(PRINTING_NEWS_OF_DARKNESS_ACT);</span>
<span class="plain">rfalse;</span>
<span class="plain">}</span>
<span class="plain">if ((previous_light_condition == true) &amp;&amp; (lightflag == true)) {</span>
<span class="plain">location = real_location;</span>
<span class="plain">rfalse;</span>
<span class="plain">}</span>
<span class="plain">rfalse;</span>
<span class="plain">];</span>
</pre>
<p class="inwebparagraph"></p>
<p class="inwebparagraph"><a id="SP5"></a><b>&#167;5. Silent Light Consideration. </b>The Adjust Light Rule makes a fuss when light changes: it prints messages,
for instance. This rule is silent instead, and simply does the minimum
necessary to maintain the light invariant. It is used in only four
circumstances:
</p>
<p class="inwebparagraph"></p>
<ul class="items"><li>(a) To determine the initial light condition at start of play.
</li><li>(b) When the player moves from one room to another via the "going" action.
</li><li>(c) When the player moves via <code class="display"><span class="extract">PlayerTo</span></code>, which is used by "now the player is in ...".
</li><li>(d) When the player changes from one persona to another.
</li></ul>
<p class="inwebparagraph">Perhaps case (b) is surprising. Why not simply use the adjust light rule,
as we do on an instance of "move player to ...", for instance? The
answer is that the going action is just about to print details of the
new location anyway, so it would be redundant to have the adjust light
rule print out details as well.
</p>
<pre class="display">
<span class="plain">[ SilentlyConsiderLight;</span>
<span class="plain">lightflag = OffersLight(parent(player));</span>
<span class="plain">if (lightflag) location = real_location; else location = thedark;</span>
<span class="plain">rfalse;</span>
<span class="plain">];</span>
</pre>
<p class="inwebparagraph"></p>
<p class="inwebparagraph"><a id="SP6"></a><b>&#167;6. Translucency. </b><code class="display"><span class="extract">IsSeeThrough</span></code> is used at various places: roughly speaking, it determines
whether <code class="display"><span class="extract">obj</span></code> being in scope means that the object-tree contents of <code class="display"><span class="extract">obj</span></code>
are in scope.
</p>
<pre class="display">
<span class="plain">[ IsSeeThrough obj;</span>
<span class="plain">if ((obj has supporter)</span>
<span class="plain">|| (obj has transparent)</span>
<span class="plain">|| (obj has animate)</span>
<span class="plain">|| ((obj has container) &amp;&amp; (obj has open)))</span>
<span class="plain">rtrue;</span>
<span class="plain">rfalse;</span>
<span class="plain">];</span>
</pre>
<p class="inwebparagraph"></p>
<p class="inwebparagraph"><a id="SP7"></a><b>&#167;7. Visibility Parent. </b>The idea of <code class="display"><span class="extract">VisibilityParent</span></code> is that it takes us from a given position in
the object tree, <code class="display"><span class="extract">o</span></code>, to the next visible position above. Note that
</p>
<ul class="items"><li>(1) A container has an inside and an outside: this routine calculates from
the "inside of <code class="display"><span class="extract">o</span></code>", which is why it returns <code class="display"><span class="extract">nothing</span></code> from an opaque
closed container;
</li><li>(2) Component parts are (for purposes of this routine) attached to the outside
surface of a container, so that if <code class="display"><span class="extract">o</span></code> is part of a closed opaque container
then the visibility parent of <code class="display"><span class="extract">o</span></code> is its actual parent.
</li></ul>
<pre class="display">
<span class="plain">[ VisibilityParent o;</span>
<span class="plain">if (o &amp;&amp; (o has container) &amp;&amp; (o hasnt open) &amp;&amp; (o hasnt transparent)) return nothing;</span>
<span class="plain">if (o) o = CoreOfParentOfCoreOf(o);</span>
<span class="plain">return o;</span>
<span class="plain">];</span>
</pre>
<p class="inwebparagraph"></p>
<p class="inwebparagraph"><a id="SP8"></a><b>&#167;8. Find Visibility Levels. </b>The following routine sets the pair of variables <code class="display"><span class="extract">visibility_ceiling</span></code>, the
highest visible point in the object tree above the player &mdash; or <code class="display"><span class="extract">thedark</span></code>
if the player cannot see at all &mdash; and <code class="display"><span class="extract">visibility_levels</span></code>, the number of
steps of <code class="display"><span class="extract">VisibilityParent</span></code> needed to reach this ceiling; or 0 if the
player cannot see at all.
</p>
<pre class="display">
<span class="plain">[ FindVisibilityLevels lc up;</span>
<span class="plain">if (location == thedark) {</span>
<span class="plain">visibility_ceiling = thedark;</span>
<span class="plain">visibility_levels = 0;</span>
<span class="plain">} else {</span>
<span class="plain">visibility_ceiling = player;</span>
<span class="plain">while (true) {</span>
<span class="plain">up = VisibilityParent(visibility_ceiling);</span>
<span class="plain">if (up == 0) break;</span>
<span class="plain">visibility_ceiling = up;</span>
<span class="plain">lc++;</span>
<span class="plain">}</span>
<span class="plain">visibility_levels = lc;</span>
<span class="plain">}</span>
<span class="plain">];</span>
</pre>
<p class="inwebparagraph"></p>
<p class="inwebparagraph"><a id="SP9"></a><b>&#167;9. Touchability Ceiling. </b>Analogously:
</p>
<pre class="display">
<span class="plain">[ TouchabilityCeiling original o p;</span>
<span class="plain">o = original;</span>
<span class="plain">while (o) {</span>
<span class="plain">p = CoreOfParentOfCoreOf(o);</span>
<span class="plain">if (p ofclass K1_room) return p;</span>
<span class="plain">if (p == nothing) return o;</span>
<span class="plain">if ((FollowRulebook(REACHING_OUTSIDE_RB, p)) &amp;&amp; (RulebookFailed()))</span>
<span class="plain">return p;</span>
<span class="plain">o = p;</span>
<span class="plain">}</span>
<span class="plain">return o;</span>
<span class="plain">];</span>
</pre>
<p class="inwebparagraph"></p>
<p class="inwebparagraph"><a id="SP10"></a><b>&#167;10. Scope Ceiling. </b>Scope is almost the same thing as visibility, but not quite, and the following
routine does not quite duplicate the calculation of <code class="display"><span class="extract">FindVisibilityLevels</span></code>.
The difference arises in the first step, where we take the <code class="display"><span class="extract">parent</span></code> of <code class="display"><span class="extract">pos</span></code>,
not the core of the <code class="display"><span class="extract">parent</span></code> of the core of <code class="display"><span class="extract">pos</span></code>: this makes a
difference if <code class="display"><span class="extract">pos</span></code> is inside a container which is itself part of something
else.
</p>
<pre class="display">
<span class="plain">[ ScopeCeiling pos c;</span>
<span class="plain">if (pos == player &amp;&amp; location == thedark) return thedark;</span>
<span class="plain">c = parent(pos);</span>
<span class="plain">if (c == 0) return pos;</span>
<span class="plain">while (VisibilityParent(c)) c = VisibilityParent(c);</span>
<span class="plain">return c;</span>
<span class="plain">];</span>
</pre>
<p class="inwebparagraph"></p>
<p class="inwebparagraph"><a id="SP11"></a><b>&#167;11. Object Is Untouchable. </b>The following routine imitates the I6 library one of the same name, but
works instead by delegating the decision to the accessibility rulebook.
</p>
<p class="inwebparagraph">It is not easy to answer the question of whether someone other than the
player, but in another room, can touch a multiply-present object (a
two-sided door or a backdrop) and we err on the side of caution by saying
yes in such cases. The question would only be asked in the case of a
"try" action deliberately caused by the author, or by an instruction made
by the player to someone not in the same room: in the first case, we will
assume that the author knows what he is doing, and in the second case, the
circumstances in which saying yes would be a wrong call are highly
improbable.
</p>
<pre class="display">
<span class="plain">[ ObjectIsUntouchable item silent_flag p save_sp decision moving x;</span>
<span class="plain">if (LocationOf(p) ~= real_location) {</span>
<span class="plain">for (x = CoreOf(item): x: x = CoreOfParentOfCoreOf(x)) {</span>
<span class="plain">if (x ofclass K4_door or K7_backdrop) {</span>
<span class="plain">moving = true;</span>
<span class="plain">MoveFloatingObjects(LocationOf(p));</span>
<span class="plain">break;</span>
<span class="plain">}</span>
<span class="plain">}</span>
<span class="plain">}</span>
<span class="plain">untouchable_object = item; untouchable_silence = silent_flag;</span>
<span class="plain">touch_persona = p; if (p == actor) touch_persona = 0;</span>
<span class="plain">save_sp = say__p; say__p = 0;</span>
<span class="plain">@push actor; actor = p;</span>
<span class="plain">if (FollowRulebook(ACCESSIBILITY_RB, 0, true)) {</span>
<span class="plain">if (RulebookSucceeded()) decision = false;</span>
<span class="plain">else decision = true;</span>
<span class="plain">} else decision = false;</span>
<span class="plain">@pull actor;</span>
<span class="plain">if (say__p == false) say__p = save_sp;</span>
<span class="plain">if (moving) MoveFloatingObjects();</span>
<span class="plain">untouchable_silence = 0;</span>
<span class="plain">return decision;</span>
<span class="plain">];</span>
</pre>
<p class="inwebparagraph"></p>
<p class="inwebparagraph"><a id="SP12"></a><b>&#167;12. Access Through Barriers Rule. </b></p>
<pre class="display">
<span class="plain">[ ACCESS_THROUGH_BARRIERS_R ancestor i j external p;</span>
<span class="plain">p = touch_persona; if (p == 0) p = actor;</span>
<span class="plain">ancestor = CommonAncestor(p, untouchable_object);</span>
<span class="plain">if ((ancestor == 0) &amp;&amp; (LocationOf(untouchable_object) == nothing)</span>
<span class="plain">&amp;&amp; ((untouchable_object ofclass K4_door or K7_backdrop) == false)) {</span>
<span class="plain">if (touch_persona == 0) {</span>
<span class="plain">if ((actor == player) &amp;&amp; (untouchable_silence == false)) {</span>
<span class="plain">ACCESS_THROUGH_BARRIERS_RM('A', untouchable_object);</span>
<span class="plain">new_line;</span>
<span class="plain">}</span>
<span class="plain">}</span>
<span class="plain">RulebookFails();</span>
<span class="plain">rtrue;</span>
<span class="plain">}</span>
<span class="plain">! First, a barrier between the player and the ancestor.</span>
<span class="plain">if (CoreOf(p) ~= ancestor) {</span>
<span class="plain">i = parent(CoreOf(p)); j = CoreOf(i); external = false;</span>
<span class="plain">if (j ~= i) { i = j; external = true; }</span>
<span class="plain">while (i~=ancestor &amp;&amp; i) {</span>
<span class="plain">if ((external == false)</span>
<span class="plain">&amp;&amp; (FollowRulebook(REACHING_OUTSIDE_RB, i))</span>
<span class="plain">&amp;&amp; (RulebookFailed())) rtrue; ! Barrier</span>
<span class="plain">i = parent(CoreOf(i)); external = false;</span>
<span class="plain">if (~~(i ofclass K5_container)) {</span>
<span class="plain">j = CoreOf(i);</span>
<span class="plain">if (j ~= i) { i = j; external = true; }</span>
<span class="plain">}</span>
<span class="plain">}</span>
<span class="plain">}</span>
<span class="plain">! Second, a barrier between the item and the ancestor.</span>
<span class="plain">if (CoreOf(untouchable_object) ~= ancestor) {</span>
<span class="plain">! We can always get to the core of the item.</span>
<span class="plain">i = CoreOf(untouchable_object);</span>
<span class="plain">! This will be on the inside of its parent, if its parent is a</span>
<span class="plain">! container, so there should be no exemption.</span>
<span class="plain">i = parent(i); external = false;</span>
<span class="plain">while (i~=ancestor &amp;&amp; i) {</span>
<span class="plain">if ((external == false) &amp;&amp;</span>
<span class="plain">(FollowRulebook(REACHING_INSIDE_RB, i)) &amp;&amp;</span>
<span class="plain">(RulebookFailed())) rtrue; ! Barrier</span>
<span class="plain">i = CoreOf(i);</span>
<span class="plain">if (i == ancestor) break;</span>
<span class="plain">i = parent(i); external = false;</span>
<span class="plain">if (~~(i ofclass K5_container)) {</span>
<span class="plain">j = CoreOf(i);</span>
<span class="plain">if (j ~= i) { i = j; external = true; }</span>
<span class="plain">}</span>
<span class="plain">}</span>
<span class="plain">}</span>
<span class="plain">RulebookSucceeds(); ! No barrier</span>
<span class="plain">rtrue;</span>
<span class="plain">];</span>
</pre>
<p class="inwebparagraph"></p>
<p class="inwebparagraph"><a id="SP13"></a><b>&#167;13. Can't Reach Inside Closed Containers Rule. </b></p>
<pre class="display">
<span class="plain">[ CANT_REACH_INSIDE_CLOSED_R;</span>
<span class="plain">if (parameter_value has container &amp;&amp; parameter_value hasnt open) {</span>
<span class="plain">if (touch_persona == 0) {</span>
<span class="plain">if ((actor == player) &amp;&amp; (untouchable_silence == false)) {</span>
<span class="plain">CANT_REACH_INSIDE_CLOSED_RM('A', parameter_value);</span>
<span class="plain">new_line;</span>
<span class="plain">}</span>
<span class="plain">}</span>
<span class="plain">RulebookFails(); rtrue;</span>
<span class="plain">}</span>
<span class="plain">rfalse;</span>
<span class="plain">];</span>
</pre>
<p class="inwebparagraph"></p>
<p class="inwebparagraph"><a id="SP14"></a><b>&#167;14. Can't Reach Outside Closed Containers Rule. </b></p>
<pre class="display">
<span class="plain">[ CANT_REACH_OUTSIDE_CLOSED_R;</span>
<span class="plain">if (parameter_value has container &amp;&amp; parameter_value hasnt open) {</span>
<span class="plain">if (touch_persona == 0) {</span>
<span class="plain">if ((actor == player) &amp;&amp; (untouchable_silence == false)) {</span>
<span class="plain">CANT_REACH_OUTSIDE_CLOSED_RM('A', parameter_value);</span>
<span class="plain">new_line;</span>
<span class="plain">}</span>
<span class="plain">}</span>
<span class="plain">RulebookFails(); rtrue;</span>
<span class="plain">}</span>
<span class="plain">rfalse;</span>
<span class="plain">];</span>
</pre>
<p class="inwebparagraph"></p>
<p class="inwebparagraph"><a id="SP15"></a><b>&#167;15. Can't Reach Inside Rooms Rule. </b></p>
<pre class="display">
<span class="plain">[ CANT_REACH_INSIDE_ROOMS_R;</span>
<span class="plain">if (parameter_value &amp;&amp; parameter_value ofclass K1_room) {</span>
<span class="plain">if (touch_persona == 0) {</span>
<span class="plain">if ((actor == player) &amp;&amp; (untouchable_silence == false)) {</span>
<span class="plain">CANT_REACH_INSIDE_ROOMS_RM('A', parameter_value);</span>
<span class="plain">new_line;</span>
<span class="plain">}</span>
<span class="plain">}</span>
<span class="plain">RulebookFails(); rtrue;</span>
<span class="plain">}</span>
<span class="plain">rfalse;</span>
<span class="plain">];</span>
</pre>
<p class="inwebparagraph"></p>
<hr class="tocbar">
<ul class="toc"><li><a href="S-lt.html">Back to 'Language Template'</a></li><li><a href="S-lt3.html">Continue with 'ListWriter Template'</a></li></ul><hr class="tocbar">
<!--End of weave-->
</body>
</html>