templeos-info/public/Wb/Demo/Games/RawHide.HC.HTML

623 lines
54 KiB
HTML
Executable File

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="generator" content="TempleOS V5.03">
<meta name="viewport" content="width=device-width">
<link rel="stylesheet" href="/style/templeos.css">
<script src="/script/templeos.js"></script>
<style type="text/css">
.cF0{color:#000000;background-color:#ffffff;}
.cF1{color:#0000aa;background-color:#ffffff;}
.cF2{color:#00aa00;background-color:#ffffff;}
.cF3{color:#00aaaa;background-color:#ffffff;}
.cF4{color:#aa0000;background-color:#ffffff;}
.cF5{color:#aa00aa;background-color:#ffffff;}
.cF6{color:#aa5500;background-color:#ffffff;}
.cF7{color:#aaaaaa;background-color:#ffffff;}
.cF8{color:#555555;background-color:#ffffff;}
.cF9{color:#5555ff;background-color:#ffffff;}
.cFA{color:#55ff55;background-color:#ffffff;}
.cFB{color:#55ffff;background-color:#ffffff;}
.cFC{color:#ff5555;background-color:#ffffff;}
.cFD{color:#ff55ff;background-color:#ffffff;}
.cFE{color:#ffff55;background-color:#ffffff;}
.cFF{color:#ffffff;background-color:#ffffff;}
</style>
</head>
<body>
<pre id="content">
<a name="l1"></a><span class=cF2>//Uses </span><a href="/Wb/Demo/Lectures/FixedPoint.HC.HTML#l1"><span class=cF4>fixed-point</span></a><span class=cF2>.</span><span class=cF0>
<a name="l2"></a>
<a name="l3"></a></span><span class=cF5>RegDft</span><span class=cF0>(</span><span class=cF6>&quot;TempleOS/RawHide&quot;</span><span class=cF0>,</span><span class=cF6>&quot;F64 best_score=9999;\n&quot;</span><span class=cF0>);
<a name="l4"></a></span><span class=cF5>RegExe</span><span class=cF0>(</span><span class=cF6>&quot;TempleOS/RawHide&quot;</span><span class=cF0>);
<a name="l5"></a>
<a name="l6"></a></span><span class=cF1>F64</span><span class=cF0> t0,tf;
<a name="l7"></a></span><span class=cF9>I64</span><span class=cF0> outside_cnt;
<a name="l8"></a>
<a name="l9"></a>#</span><span class=cF1>define</span><span class=cF0> MAP_WIDTH 1000
<a name="l10"></a>#</span><span class=cF1>define</span><span class=cF0> MAP_HEIGHT 1000
<a name="l11"></a>#</span><span class=cF1>define</span><span class=cF0> FENCE_WIDTH 320
<a name="l12"></a>#</span><span class=cF1>define</span><span class=cF0> FENCE_HEIGHT 200
<a name="l13"></a>#</span><span class=cF1>define</span><span class=cF0> MAP_BORDER 3
<a name="l14"></a></span><span class=cF9>CDC</span><span class=cF0> *map_dc;
<a name="l15"></a>
<a name="l16"></a>#</span><span class=cF1>define</span><span class=cF0> GATE_WIDTH 22
<a name="l17"></a>#</span><span class=cF1>define</span><span class=cF0> GATE_HEIGHT 7
<a name="l18"></a></span><span class=cF1>F64</span><span class=cF0> gate_theta,gate_t;
<a name="l19"></a>
<a name="l20"></a>
<a name="l21"></a></span><span class=cFA>
<a name="l22"></a>&lt;1&gt;/* Graphics Not Rendered in HTML */</span><span class=cF0> </span><span class=cFA>&lt;2&gt;/* Graphics Not Rendered in HTML */</span><span class=cF0> </span><span class=cFA>&lt;3&gt;/* Graphics Not Rendered in HTML */</span><span class=cF0>
<a name="l23"></a>
<a name="l24"></a></span><span class=cFA>
<a name="l25"></a>&lt;4&gt;/* Graphics Not Rendered in HTML */</span><span class=cF0> </span><span class=cFA>&lt;5&gt;/* Graphics Not Rendered in HTML */</span><span class=cF0> </span><span class=cFA>&lt;6&gt;/* Graphics Not Rendered in HTML */</span><span class=cF0>
<a name="l26"></a>
<a name="l27"></a></span><span class=cFA>
<a name="l28"></a>&lt;7&gt;/* Graphics Not Rendered in HTML */</span><span class=cF0> </span><span class=cFA>&lt;8&gt;/* Graphics Not Rendered in HTML */</span><span class=cF0>
<a name="l29"></a>
<a name="l30"></a></span><span class=cFA>
<a name="l31"></a>&lt;9&gt;/* Graphics Not Rendered in HTML */</span><span class=cF0>
<a name="l32"></a> </span><span class=cFA>&lt;10&gt;/* Graphics Not Rendered in HTML */</span><span class=cF0>
<a name="l33"></a>
<a name="l34"></a>
<a name="l35"></a>
<a name="l36"></a>
<a name="l37"></a>
<a name="l38"></a>
<a name="l39"></a>
<a name="l40"></a>
<a name="l41"></a>#</span><span class=cF1>define</span><span class=cF0> ANIMAL_WIDTH 20
<a name="l42"></a>#</span><span class=cF1>define</span><span class=cF0> ANIMAL_HEIGHT 16
<a name="l43"></a>
<a name="l44"></a></span><span class=cF1>U8</span><span class=cF0> *cow_imgs[4] ={</span><span class=cFA>&lt;1&gt;</span><span class=cF0>,</span><span class=cFA>&lt;2&gt;</span><span class=cF0>,</span><span class=cFA>&lt;3&gt;</span><span class=cF0>,</span><span class=cFA>&lt;2&gt;</span><span class=cF0>},
<a name="l45"></a> *bull_imgs[4] ={</span><span class=cFA>&lt;4&gt;</span><span class=cF0>,</span><span class=cFA>&lt;5&gt;</span><span class=cF0>,</span><span class=cFA>&lt;6&gt;</span><span class=cF0>,</span><span class=cFA>&lt;5&gt;</span><span class=cF0>},
<a name="l46"></a> *horse_imgs[4]={</span><span class=cFA>&lt;7&gt;</span><span class=cF0>,</span><span class=cFA>&lt;8&gt;</span><span class=cF0>,</span><span class=cFA>&lt;7&gt;</span><span class=cF0>,</span><span class=cFA>&lt;8&gt;</span><span class=cF0>};
<a name="l47"></a>
<a name="l48"></a>#</span><span class=cF1>define</span><span class=cF0> ANIMALS_NUM 100
<a name="l49"></a>
<a name="l50"></a></span><span class=cF1>class</span><span class=cF0> Animal
<a name="l51"></a>{
<a name="l52"></a> </span><span class=cF9>I64</span><span class=cF0> num,x,y,dx,dy;
<a name="l53"></a> </span><span class=cF1>U8</span><span class=cF0> **imgs;
<a name="l54"></a> </span><span class=cF9>U32</span><span class=cF0> buddy;
<a name="l55"></a> </span><span class=cF1>U8</span><span class=cF0> type,frame0;
<a name="l56"></a> </span><span class=cF1>Bool</span><span class=cF0> dead,pad;
<a name="l57"></a>} *a;
<a name="l58"></a>
<a name="l59"></a></span><span class=cF2>//************************************</span><span class=cF0>
<a name="l60"></a>#</span><span class=cF1>define</span><span class=cF0> WATERFALL_HEIGHT (MAP_HEIGHT/20)
<a name="l61"></a>#</span><span class=cF1>define</span><span class=cF0> WATERFALL_DROPS 512
<a name="l62"></a>#</span><span class=cF1>define</span><span class=cF0> WATERFALL_ACCELERATION 10
<a name="l63"></a></span><span class=cF9>I32</span><span class=cF0> *r_x,*r_width,*wfd_x;
<a name="l64"></a></span><span class=cF1>F64</span><span class=cF0> *wfd_t0,waterfall_tf;
<a name="l65"></a></span><span class=cF9>I64</span><span class=cF0> waterfall_x,waterfall_y,waterfall_width;
<a name="l66"></a>
<a name="l67"></a></span><span class=cF1>U0</span><span class=cF0> RiverNew()
<a name="l68"></a>{
<a name="l69"></a> r_x=</span><span class=cF5>MAlloc</span><span class=cF0>(MAP_HEIGHT*</span><span class=cF1>sizeof</span><span class=cF7>(</span><span class=cF9>I32</span><span class=cF7>)</span><span class=cF0>);
<a name="l70"></a> r_width=</span><span class=cF5>MAlloc</span><span class=cF0>(MAP_HEIGHT*</span><span class=cF1>sizeof</span><span class=cF7>(</span><span class=cF9>I32</span><span class=cF7>)</span><span class=cF0>);
<a name="l71"></a> wfd_x=</span><span class=cF5>MAlloc</span><span class=cF0>(WATERFALL_DROPS*</span><span class=cF1>sizeof</span><span class=cF7>(</span><span class=cF9>I32</span><span class=cF7>)</span><span class=cF0>);
<a name="l72"></a> wfd_t0=</span><span class=cF5>MAlloc</span><span class=cF0>(WATERFALL_DROPS*</span><span class=cF1>sizeof</span><span class=cF7>(</span><span class=cF1>F64</span><span class=cF7>)</span><span class=cF0>);
<a name="l73"></a> waterfall_tf=</span><span class=cF5>Sqrt</span><span class=cF0>(2*WATERFALL_HEIGHT/WATERFALL_ACCELERATION);
<a name="l74"></a>}
<a name="l75"></a>
<a name="l76"></a></span><span class=cF1>U0</span><span class=cF0> RiverMake()
<a name="l77"></a>{
<a name="l78"></a> </span><span class=cF9>I64</span><span class=cF0> i,x=2*MAP_WIDTH&lt;&lt;32/3,y,dx=0,w=15&lt;&lt;32;
<a name="l79"></a> waterfall_y=(MAP_HEIGHT-WATERFALL_HEIGHT)/2*</span><span class=cF5>Rand</span><span class=cF0>+
<a name="l80"></a> (MAP_HEIGHT-WATERFALL_HEIGHT)/4;
<a name="l81"></a> </span><span class=cF1>for</span><span class=cF0> (y=MAP_BORDER;y&lt;MAP_HEIGHT-MAP_BORDER;y++) </span><span class=cF7>{</span><span class=cF0>
<a name="l82"></a> r_x[y]=x.i32[1]; r_width[y]=w.i32[1];
<a name="l83"></a> </span><span class=cF1>if</span><span class=cF0> (waterfall_y-5&lt;y&lt;waterfall_y+WATERFALL_HEIGHT+5) {
<a name="l84"></a> waterfall_width=r_width[y];
<a name="l85"></a> waterfall_x=r_x[y]-waterfall_width/2;
<a name="l86"></a> } </span><span class=cF1>else</span><span class=cF0> {
<a name="l87"></a> dx=</span><span class=cF5>ClampI64</span><span class=cF0>(dx+</span><span class=cF5>RandI32</span><span class=cF0>/64,-</span><span class=cF3>I32_MAX</span><span class=cF0>,</span><span class=cF3>I32_MAX</span><span class=cF0>)-</span><span class=cF3>I32_MAX</span><span class=cF0>/256;
<a name="l88"></a> w=</span><span class=cF5>ClampI64</span><span class=cF0>(w+</span><span class=cF5>RandI32</span><span class=cF0>*2,10*</span><span class=cF3>U32_MAX</span><span class=cF0>,150*</span><span class=cF3>U32_MAX</span><span class=cF0>);
<a name="l89"></a> x+=dx;
<a name="l90"></a> }
<a name="l91"></a> </span><span class=cF7>}</span><span class=cF0>
<a name="l92"></a> </span><span class=cF1>for</span><span class=cF0> (i=0;i&lt;WATERFALL_DROPS;i++) </span><span class=cF7>{</span><span class=cF0>
<a name="l93"></a> wfd_x[i]=</span><span class=cF5>Rand</span><span class=cF0>*waterfall_width+waterfall_x;
<a name="l94"></a> wfd_t0[i]=</span><span class=cF5>tS</span><span class=cF0>-</span><span class=cF5>Rand</span><span class=cF0>*waterfall_tf;
<a name="l95"></a> </span><span class=cF7>}</span><span class=cF0>
<a name="l96"></a>
<a name="l97"></a> </span><span class=cF2>//Plot waterfall cliff</span><span class=cF0>
<a name="l98"></a> </span><span class=cF5>Sprite3B</span><span class=cF0>(map_dc,waterfall_x,waterfall_y,0,</span><span class=cFA>&lt;10&gt;</span><span class=cF0>);
<a name="l99"></a>
<a name="l100"></a> </span><span class=cF2>//Plot sand bar</span><span class=cF0>
<a name="l101"></a> x=0;
<a name="l102"></a> </span><span class=cF1>for</span><span class=cF0> (y=MAP_BORDER;y&lt;MAP_HEIGHT-MAP_BORDER;y++) </span><span class=cF7>{</span><span class=cF0>
<a name="l103"></a> </span><span class=cF1>if</span><span class=cF0> (!</span><span class=cF7>(</span><span class=cF0>waterfall_y-9&lt;y&lt;waterfall_y+WATERFALL_HEIGHT+9</span><span class=cF7>)</span><span class=cF0>) {
<a name="l104"></a> map_dc-&gt;color=</span><span class=cF3>YELLOW</span><span class=cF0>;
<a name="l105"></a> map_dc-&gt;thick=r_width[y]+10;
<a name="l106"></a> </span><span class=cF5>GrPlot3</span><span class=cF0>(map_dc,r_x[y]+x.i32[1],y,0);
<a name="l107"></a> }
<a name="l108"></a> x=</span><span class=cF5>ClampI64</span><span class=cF0>(x+</span><span class=cF5>RandI32</span><span class=cF0>,-6*</span><span class=cF3>U32_MAX</span><span class=cF0>,6*</span><span class=cF3>U32_MAX</span><span class=cF0>);
<a name="l109"></a> </span><span class=cF7>}</span><span class=cF0>
<a name="l110"></a>
<a name="l111"></a> </span><span class=cF2>//Plot water</span><span class=cF0>
<a name="l112"></a> </span><span class=cF1>for</span><span class=cF0> (y=MAP_BORDER;y&lt;MAP_HEIGHT-MAP_BORDER;y++) </span><span class=cF7>{</span><span class=cF0>
<a name="l113"></a> map_dc-&gt;color=</span><span class=cF3>BLUE</span><span class=cF0>;
<a name="l114"></a> map_dc-&gt;thick=r_width[y];
<a name="l115"></a> </span><span class=cF5>GrPlot3</span><span class=cF0>(map_dc,r_x[y],y,0);
<a name="l116"></a> </span><span class=cF7>}</span><span class=cF0>
<a name="l117"></a>}
<a name="l118"></a>
<a name="l119"></a></span><span class=cF1>U0</span><span class=cF0> RiverDel()
<a name="l120"></a>{
<a name="l121"></a> </span><span class=cF5>Free</span><span class=cF0>(r_x);
<a name="l122"></a> </span><span class=cF5>Free</span><span class=cF0>(r_width);
<a name="l123"></a> </span><span class=cF5>Free</span><span class=cF0>(wfd_x);
<a name="l124"></a> </span><span class=cF5>Free</span><span class=cF0>(wfd_t0);
<a name="l125"></a>}
<a name="l126"></a>
<a name="l127"></a></span><span class=cF2>//************************************</span><span class=cF0>
<a name="l128"></a></span><span class=cF1>class</span><span class=cF0> RiverDrop
<a name="l129"></a>{
<a name="l130"></a> RiverDrop *next,*last;
<a name="l131"></a> </span><span class=cF9>I64</span><span class=cF0> y,dx,dy;
<a name="l132"></a>} rd_head;
<a name="l133"></a></span><span class=cF1>Bool</span><span class=cF0> rd_lock;
<a name="l134"></a>
<a name="l135"></a></span><span class=cF1>U0</span><span class=cF0> RiverDropsDel()
<a name="l136"></a>{
<a name="l137"></a> </span><span class=cF1>while</span><span class=cF0> (</span><span class=cF5>LBts</span><span class=cF7>(</span><span class=cF0>&amp;rd_lock,0</span><span class=cF7>)</span><span class=cF0>)
<a name="l138"></a> </span><span class=cF5>Yield</span><span class=cF0>;
<a name="l139"></a> </span><span class=cF5>QueDel</span><span class=cF0>(&amp;rd_head,</span><span class=cF3>TRUE</span><span class=cF0>);
<a name="l140"></a> </span><span class=cF5>QueInit</span><span class=cF0>(&amp;rd_head);
<a name="l141"></a> </span><span class=cF5>LBtr</span><span class=cF0>(&amp;rd_lock,0);
<a name="l142"></a>}
<a name="l143"></a>
<a name="l144"></a></span><span class=cF1>U0</span><span class=cF0> RiverDropsNext(</span><span class=cF9>CTask</span><span class=cF0> *mem_task)
<a name="l145"></a>{
<a name="l146"></a> RiverDrop *tmpr,*tmpr1;
<a name="l147"></a> </span><span class=cF1>while</span><span class=cF0> (</span><span class=cF5>LBts</span><span class=cF7>(</span><span class=cF0>&amp;rd_lock,0</span><span class=cF7>)</span><span class=cF0>)
<a name="l148"></a> </span><span class=cF5>Yield</span><span class=cF0>;
<a name="l149"></a> tmpr=rd_head.next;
<a name="l150"></a> </span><span class=cF1>while</span><span class=cF0> (tmpr!=&amp;rd_head) </span><span class=cF7>{</span><span class=cF0>
<a name="l151"></a> tmpr1=tmpr-&gt;next;
<a name="l152"></a> </span><span class=cF1>if</span><span class=cF0> (++tmpr-&gt;y&gt;=MAP_HEIGHT-MAP_BORDER) {
<a name="l153"></a> </span><span class=cF5>QueRem</span><span class=cF0>(tmpr);
<a name="l154"></a> </span><span class=cF5>Free</span><span class=cF0>(tmpr);
<a name="l155"></a> } </span><span class=cF1>else</span><span class=cF0> {
<a name="l156"></a> </span><span class=cF1>do</span><span class=cF0> </span><span class=cF7>{</span><span class=cF0>
<a name="l157"></a> </span><span class=cF1>if</span><span class=cF0> (</span><span class=cF5>RandU16</span><span class=cF0>&amp;1 &amp;&amp; </span><span class=cF5>GrPeek</span><span class=cF7>(</span><span class=cF0>map_dc,r_x[tmpr-&gt;y]+tmpr-&gt;dx,
<a name="l158"></a> tmpr-&gt;y+tmpr-&gt;dy</span><span class=cF7>)</span><span class=cF0>==</span><span class=cF3>BLUE</span><span class=cF0>)
<a name="l159"></a> </span><span class=cF1>break</span><span class=cF0>;
<a name="l160"></a> tmpr-&gt;dx=</span><span class=cF5>ClampI64</span><span class=cF0>(tmpr-&gt;dx+</span><span class=cF5>RandU16</span><span class=cF0>%3-1,-r_width[tmpr-&gt;y]/2,
<a name="l161"></a> r_width[tmpr-&gt;y]/2);
<a name="l162"></a> tmpr-&gt;dy=</span><span class=cF5>ClampI64</span><span class=cF0>(tmpr-&gt;dy+</span><span class=cF5>RandU16</span><span class=cF0>%3-1,-r_width[tmpr-&gt;y]/2,
<a name="l163"></a> r_width[tmpr-&gt;y]/2);
<a name="l164"></a> </span><span class=cF7>}</span><span class=cF0> </span><span class=cF1>while</span><span class=cF0> (</span><span class=cF5>GrPeek</span><span class=cF7>(</span><span class=cF0>map_dc,r_x[tmpr-&gt;y]+tmpr-&gt;dx,
<a name="l165"></a> tmpr-&gt;y+tmpr-&gt;dy</span><span class=cF7>)</span><span class=cF0>!=</span><span class=cF3>BLUE</span><span class=cF0> &amp;&amp;
<a name="l166"></a> </span><span class=cF5>GrPeek</span><span class=cF7>(</span><span class=cF0>map_dc,r_x[tmpr-&gt;y],tmpr-&gt;y</span><span class=cF7>)</span><span class=cF0>==</span><span class=cF3>BLUE</span><span class=cF0>);</span><span class=cF2>//Might be reiniting</span><span class=cF0>
<a name="l167"></a> }
<a name="l168"></a> tmpr=tmpr1;
<a name="l169"></a> </span><span class=cF7>}</span><span class=cF0>
<a name="l170"></a> tmpr=</span><span class=cF5>MAlloc</span><span class=cF0>(</span><span class=cF1>sizeof</span><span class=cF7>(</span><span class=cF0>RiverDrop</span><span class=cF7>)</span><span class=cF0>,mem_task);
<a name="l171"></a> tmpr-&gt;y=MAP_BORDER;
<a name="l172"></a> tmpr-&gt;dx=0;
<a name="l173"></a> tmpr-&gt;dy=0;
<a name="l174"></a> </span><span class=cF5>QueIns</span><span class=cF0>(tmpr,rd_head.last);
<a name="l175"></a> </span><span class=cF5>LBtr</span><span class=cF0>(&amp;rd_lock,0);
<a name="l176"></a>}
<a name="l177"></a>
<a name="l178"></a></span><span class=cF1>U0</span><span class=cF0> RiverDropsDraw(</span><span class=cF9>CDC</span><span class=cF0> *dc,</span><span class=cF9>I64</span><span class=cF0> cx,</span><span class=cF9>I64</span><span class=cF0> cy)
<a name="l179"></a>{
<a name="l180"></a> </span><span class=cF9>I64</span><span class=cF0> i;
<a name="l181"></a> </span><span class=cF1>F64</span><span class=cF0> t=</span><span class=cF5>tS</span><span class=cF0>;
<a name="l182"></a> RiverDrop *tmpr;
<a name="l183"></a> </span><span class=cF1>while</span><span class=cF0> (</span><span class=cF5>LBts</span><span class=cF7>(</span><span class=cF0>&amp;rd_lock,0</span><span class=cF7>)</span><span class=cF0>)
<a name="l184"></a> </span><span class=cF5>Yield</span><span class=cF0>;
<a name="l185"></a> tmpr=rd_head.next;
<a name="l186"></a> dc-&gt;color=</span><span class=cF3>LTBLUE</span><span class=cF0>;
<a name="l187"></a> </span><span class=cF1>while</span><span class=cF0> (tmpr!=&amp;rd_head) </span><span class=cF7>{</span><span class=cF0>
<a name="l188"></a> </span><span class=cF5>GrPlot</span><span class=cF0>(dc,r_x[tmpr-&gt;y]+tmpr-&gt;dx-cx,tmpr-&gt;y+tmpr-&gt;dy-cy);
<a name="l189"></a> tmpr=tmpr-&gt;next;
<a name="l190"></a> </span><span class=cF7>}</span><span class=cF0>
<a name="l191"></a> </span><span class=cF5>LBtr</span><span class=cF0>(&amp;rd_lock,0);
<a name="l192"></a>
<a name="l193"></a> dc-&gt;color=</span><span class=cF3>WHITE</span><span class=cF0>;
<a name="l194"></a> </span><span class=cF1>for</span><span class=cF0> (i=0;i&lt;WATERFALL_DROPS;i++)
<a name="l195"></a> </span><span class=cF5>GrPlot</span><span class=cF0>(dc,wfd_x[i]-cx,waterfall_y+0.5*WATERFALL_ACCELERATION*
<a name="l196"></a> </span><span class=cF5>Sqr</span><span class=cF7>(</span><span class=cF0>waterfall_tf*</span><span class=cF5>Saw</span><span class=cF0>(t-wfd_t0[i],waterfall_tf)</span><span class=cF7>)</span><span class=cF0>-cy);
<a name="l197"></a>}
<a name="l198"></a>
<a name="l199"></a></span><span class=cF2>//************************************</span><span class=cF0>
<a name="l200"></a></span><span class=cF1>U0</span><span class=cF0> </span><span class=cF5>DrawIt</span><span class=cF0>(</span><span class=cF9>CTask</span><span class=cF0> *task,</span><span class=cF9>CDC</span><span class=cF0> *dc)
<a name="l201"></a>{
<a name="l202"></a> </span><span class=cF1>static</span><span class=cF0> </span><span class=cF9>I64</span><span class=cF0> last_pos_x=0;
<a name="l203"></a> </span><span class=cF1>static</span><span class=cF0> </span><span class=cF1>Bool</span><span class=cF0> left=</span><span class=cF3>TRUE</span><span class=cF0>;
<a name="l204"></a> </span><span class=cF1>F64</span><span class=cF0> t;
<a name="l205"></a> </span><span class=cF9>I64</span><span class=cF0> i,frame=4*</span><span class=cF5>tS</span><span class=cF0>,
<a name="l206"></a> cx=(MAP_WIDTH -task-&gt;pix_width)/2,
<a name="l207"></a> cy=(MAP_HEIGHT-task-&gt;pix_height)/2;
<a name="l208"></a> </span><span class=cF1>if</span><span class=cF0> (task-&gt;scroll_x+cx&lt;0)
<a name="l209"></a> task-&gt;scroll_x=-cx;
<a name="l210"></a> </span><span class=cF1>if</span><span class=cF0> (task-&gt;scroll_x+cx&gt;MAP_WIDTH-task-&gt;pix_width)
<a name="l211"></a> task-&gt;scroll_x=MAP_WIDTH-task-&gt;pix_width-cx;
<a name="l212"></a> </span><span class=cF1>if</span><span class=cF0> (task-&gt;scroll_y+cy&lt;0)
<a name="l213"></a> task-&gt;scroll_y=-cy;
<a name="l214"></a> </span><span class=cF1>if</span><span class=cF0> (task-&gt;scroll_y+cy&gt;MAP_HEIGHT-task-&gt;pix_height)
<a name="l215"></a> task-&gt;scroll_y=MAP_HEIGHT-task-&gt;pix_height-cy;
<a name="l216"></a>
<a name="l217"></a> map_dc-&gt;flags|=</span><span class=cF3>DCF_NO_TRANSPARENTS</span><span class=cF0>;
<a name="l218"></a> </span><span class=cF5>GrBlot</span><span class=cF0>(dc,-cx,-cy,map_dc);
<a name="l219"></a>
<a name="l220"></a> RiverDropsDraw(dc,cx,cy);
<a name="l221"></a>
<a name="l222"></a> </span><span class=cF1>for</span><span class=cF0> (i=0;i&lt;ANIMALS_NUM;i++)
<a name="l223"></a> </span><span class=cF1>if</span><span class=cF0> (!a[i].dead) </span><span class=cF7>{</span><span class=cF0>
<a name="l224"></a> </span><span class=cF1>if</span><span class=cF0> (a[i].dx&lt;0) {
<a name="l225"></a> dc-&gt;flags|=</span><span class=cF3>DCF_JUST_MIRROR</span><span class=cF0>|</span><span class=cF3>DCF_SYMMETRY</span><span class=cF0>;
<a name="l226"></a> </span><span class=cF5>DCSymmetrySet</span><span class=cF0>(dc,a[i].x.i32[1]-cx,0,a[i].x.i32[1]-cx,1);
<a name="l227"></a> }
<a name="l228"></a> </span><span class=cF5>Sprite3</span><span class=cF0>(dc,a[i].x.i32[1]-cx,a[i].y.i32[1]-cy,0,
<a name="l229"></a> a[i].imgs[</span><span class=cF7>(</span><span class=cF0>frame+a[i].frame0</span><span class=cF7>)</span><span class=cF0>&amp;3]);
<a name="l230"></a> dc-&gt;flags&amp;=~(</span><span class=cF3>DCF_JUST_MIRROR</span><span class=cF0>|</span><span class=cF3>DCF_SYMMETRY</span><span class=cF0>);
<a name="l231"></a> </span><span class=cF7>}</span><span class=cF0>
<a name="l232"></a>
<a name="l233"></a> </span><span class=cF1>if</span><span class=cF0> (</span><span class=cFB>ms</span><span class=cF0>.pos.x-last_pos_x&gt;0)
<a name="l234"></a> left=</span><span class=cF3>FALSE</span><span class=cF0>;
<a name="l235"></a> </span><span class=cF1>else</span><span class=cF0> </span><span class=cF1>if</span><span class=cF0> (</span><span class=cFB>ms</span><span class=cF0>.pos.x-last_pos_x&lt;0)
<a name="l236"></a> left=</span><span class=cF3>TRUE</span><span class=cF0>;
<a name="l237"></a> </span><span class=cF1>if</span><span class=cF0> (left) </span><span class=cF7>{</span><span class=cF0>
<a name="l238"></a> dc-&gt;flags|=</span><span class=cF3>DCF_JUST_MIRROR</span><span class=cF0>|</span><span class=cF3>DCF_SYMMETRY</span><span class=cF0>;
<a name="l239"></a> </span><span class=cF5>DCSymmetrySet</span><span class=cF0>(dc,</span><span class=cFB>ms</span><span class=cF0>.pos.x-task-&gt;pix_left-task-&gt;scroll_x,0,
<a name="l240"></a> </span><span class=cFB>ms</span><span class=cF0>.pos.x-task-&gt;pix_left-task-&gt;scroll_x,1);
<a name="l241"></a> </span><span class=cF7>}</span><span class=cF0>
<a name="l242"></a> </span><span class=cF5>Sprite3</span><span class=cF0>(dc,</span><span class=cFB>ms</span><span class=cF0>.pos.x-task-&gt;pix_left-task-&gt;scroll_x,
<a name="l243"></a> </span><span class=cFB>ms</span><span class=cF0>.pos.y-task-&gt;pix_top -task-&gt;scroll_y,0,horse_imgs[frame&amp;3]);
<a name="l244"></a> dc-&gt;flags&amp;=~(</span><span class=cF3>DCF_JUST_MIRROR</span><span class=cF0>|</span><span class=cF3>DCF_SYMMETRY</span><span class=cF0>);
<a name="l245"></a> last_pos_x=</span><span class=cFB>ms</span><span class=cF0>.pos.x;
<a name="l246"></a>
<a name="l247"></a> </span><span class=cF1>if</span><span class=cF0> (tf) </span><span class=cF7>{</span><span class=cF0>
<a name="l248"></a> dc-&gt;color=</span><span class=cF3>RED</span><span class=cF0>;
<a name="l249"></a> t=tf-t0;
<a name="l250"></a> </span><span class=cF1>if</span><span class=cF0> (</span><span class=cF5>Blink</span><span class=cF0>)
<a name="l251"></a> </span><span class=cF5>GrPrint</span><span class=cF0>(dc,</span><span class=cF7>(</span><span class=cF0>task-&gt;pix_width-</span><span class=cF3>FONT_WIDTH</span><span class=cF0>*14</span><span class=cF7>)</span><span class=cF0>&gt;&gt;1-task-&gt;scroll_x,
<a name="l252"></a> </span><span class=cF7>(</span><span class=cF0>task-&gt;pix_height-</span><span class=cF3>FONT_HEIGHT</span><span class=cF7>)</span><span class=cF0>&gt;&gt;1-task-&gt;scroll_y,
<a name="l253"></a> </span><span class=cF6>&quot;Game Completed&quot;</span><span class=cF0>);
<a name="l254"></a> </span><span class=cF7>}</span><span class=cF0> </span><span class=cF1>else</span><span class=cF0> </span><span class=cF7>{</span><span class=cF0>
<a name="l255"></a> dc-&gt;color=</span><span class=cF3>BLACK</span><span class=cF0>;
<a name="l256"></a> t=</span><span class=cF5>tS</span><span class=cF0>-t0;
<a name="l257"></a> </span><span class=cF7>}</span><span class=cF0>
<a name="l258"></a> </span><span class=cF5>GrPrint</span><span class=cF0>(dc,-task-&gt;scroll_x,-task-&gt;scroll_y,
<a name="l259"></a> </span><span class=cF6>&quot;Outside:%03d Time:%7.2fs Best:%7.2fs&quot;</span><span class=cF0>,
<a name="l260"></a> outside_cnt,t,best_score);
<a name="l261"></a>}
<a name="l262"></a>
<a name="l263"></a></span><span class=cF1>U0</span><span class=cF0> BuddySel(</span><span class=cF9>I64</span><span class=cF0> i)
<a name="l264"></a>{
<a name="l265"></a> </span><span class=cF9>I64</span><span class=cF0> b,best_b=i,score,best_score=</span><span class=cF3>I64_MAX</span><span class=cF0>;
<a name="l266"></a> </span><span class=cF1>for</span><span class=cF0> (b=0;b&lt;ANIMALS_NUM;b++) </span><span class=cF7>{</span><span class=cF0>
<a name="l267"></a> </span><span class=cF1>if</span><span class=cF0> (b!=i &amp;&amp; !a[b].dead) {
<a name="l268"></a> score=</span><span class=cF5>RandU32</span><span class=cF0>%(512*512)+
<a name="l269"></a> </span><span class=cF5>SqrI64</span><span class=cF0>(a[b].x.i32[1]-a[i].x.i32[1])+
<a name="l270"></a> </span><span class=cF5>SqrI64</span><span class=cF0>(a[b].y.i32[1]-a[i].y.i32[1]);
<a name="l271"></a> </span><span class=cF1>if</span><span class=cF0> (score&lt;best_score) </span><span class=cF7>{</span><span class=cF0>
<a name="l272"></a> best_score=score;
<a name="l273"></a> best_b=b;
<a name="l274"></a> </span><span class=cF7>}</span><span class=cF0>
<a name="l275"></a> }
<a name="l276"></a> </span><span class=cF7>}</span><span class=cF0>
<a name="l277"></a> a[i].buddy=best_b;
<a name="l278"></a>}
<a name="l279"></a>
<a name="l280"></a>
<a name="l281"></a></span><span class=cF1>U0</span><span class=cF0> RedrawGate()
<a name="l282"></a>{
<a name="l283"></a> </span><span class=cF1>F64</span><span class=cF0> tt=</span><span class=cF5>tS</span><span class=cF0>-gate_t;
<a name="l284"></a> </span><span class=cF9>I64</span><span class=cF0> x1=FENCE_WIDTH-63,y1=FENCE_HEIGHT-1,dx,dy;
<a name="l285"></a>
<a name="l286"></a> </span><span class=cF1>if</span><span class=cF0> (tt&lt;0.5)
<a name="l287"></a> gate_theta=</span><span class=cF5>Clamp</span><span class=cF0>(gate_theta+0.02,0,</span><span class=cF3>pi</span><span class=cF0>/2);
<a name="l288"></a> </span><span class=cF1>else</span><span class=cF0> </span><span class=cF1>if</span><span class=cF0> (tt&gt;5.0)
<a name="l289"></a> gate_theta=</span><span class=cF5>Clamp</span><span class=cF0>(gate_theta-0.02,0,</span><span class=cF3>pi</span><span class=cF0>/2);
<a name="l290"></a>
<a name="l291"></a> dx=GATE_WIDTH*</span><span class=cF5>Cos</span><span class=cF0>(gate_theta); dy=-0.8*GATE_WIDTH*</span><span class=cF5>Sin</span><span class=cF0>(gate_theta);
<a name="l292"></a>
<a name="l293"></a> map_dc-&gt;color=</span><span class=cF3>LTGREEN</span><span class=cF0>;
<a name="l294"></a> </span><span class=cF5>GrRect</span><span class=cF0>(map_dc,x1,y1-0.8*GATE_WIDTH-GATE_HEIGHT,
<a name="l295"></a> 46,0.8*GATE_WIDTH+GATE_HEIGHT+3);
<a name="l296"></a>
<a name="l297"></a> map_dc-&gt;color=</span><span class=cF3>BLACK</span><span class=cF0>;
<a name="l298"></a>
<a name="l299"></a> </span><span class=cF5>GrLine</span><span class=cF0>(map_dc,x1,y1,x1+dx,y1+dy);
<a name="l300"></a> </span><span class=cF5>GrLine</span><span class=cF0>(map_dc,x1,y1,x1,y1-GATE_HEIGHT);
<a name="l301"></a> </span><span class=cF5>GrLine</span><span class=cF0>(map_dc,x1+dx,y1+dy,x1+dx,y1+dy-GATE_HEIGHT);
<a name="l302"></a> </span><span class=cF5>GrLine</span><span class=cF0>(map_dc,x1,y1-GATE_HEIGHT,x1+dx,y1+dy-GATE_HEIGHT);
<a name="l303"></a> </span><span class=cF5>GrLine</span><span class=cF0>(map_dc,x1,y1,x1+dx,y1+dy-GATE_HEIGHT);
<a name="l304"></a> </span><span class=cF5>GrLine</span><span class=cF0>(map_dc,x1,y1-GATE_HEIGHT,x1+dx,y1+dy);
<a name="l305"></a>
<a name="l306"></a> </span><span class=cF5>GrLine</span><span class=cF0>(map_dc,x1+45,y1,x1+45-dx,y1+dy);
<a name="l307"></a> </span><span class=cF5>GrLine</span><span class=cF0>(map_dc,x1+45,y1,x1+45,y1-GATE_HEIGHT);
<a name="l308"></a> </span><span class=cF5>GrLine</span><span class=cF0>(map_dc,x1+45-dx,y1+dy,x1+45-dx,y1+dy-GATE_HEIGHT);
<a name="l309"></a> </span><span class=cF5>GrLine</span><span class=cF0>(map_dc,x1+45,y1-GATE_HEIGHT,x1+45-dx,y1+dy-GATE_HEIGHT);
<a name="l310"></a> </span><span class=cF5>GrLine</span><span class=cF0>(map_dc,x1+45,y1,x1+45-dx,y1+dy-GATE_HEIGHT);
<a name="l311"></a> </span><span class=cF5>GrLine</span><span class=cF0>(map_dc,x1+45,y1-GATE_HEIGHT,x1+45-dx,y1+dy);
<a name="l312"></a>}
<a name="l313"></a>
<a name="l314"></a></span><span class=cF1>Bool</span><span class=cF0> CheckMap(</span><span class=cF9>I64</span><span class=cF0> x,</span><span class=cF9>I64</span><span class=cF0> y)
<a name="l315"></a>{
<a name="l316"></a> </span><span class=cF9>I64</span><span class=cF0> i,j,c;
<a name="l317"></a> </span><span class=cF1>if</span><span class=cF0> (</span><span class=cF5>SqrI64</span><span class=cF7>(</span><span class=cF0>x-(waterfall_x+waterfall_width/2)</span><span class=cF7>)</span><span class=cF0>&gt;&gt;1+
<a name="l318"></a> </span><span class=cF5>SqrI64</span><span class=cF7>(</span><span class=cF0>y-(waterfall_y+WATERFALL_HEIGHT/2)</span><span class=cF7>)</span><span class=cF0>&lt;2500)
<a name="l319"></a> </span><span class=cF1>return</span><span class=cF0> </span><span class=cF3>FALSE</span><span class=cF0>;
<a name="l320"></a> </span><span class=cF1>for</span><span class=cF0> (j=-4;j&lt;=2;j++)
<a name="l321"></a> </span><span class=cF1>for</span><span class=cF0> (i=-4;i&lt;=4;i++) </span><span class=cF7>{</span><span class=cF0>
<a name="l322"></a> c=</span><span class=cF5>GrPeek</span><span class=cF0>(map_dc,x+i,y+j);
<a name="l323"></a> </span><span class=cF1>if</span><span class=cF0> (c==</span><span class=cF3>LTGRAY</span><span class=cF0> || c==</span><span class=cF3>BLACK</span><span class=cF0>)
<a name="l324"></a> </span><span class=cF1>return</span><span class=cF0> </span><span class=cF3>FALSE</span><span class=cF0>;
<a name="l325"></a> </span><span class=cF7>}</span><span class=cF0>
<a name="l326"></a> </span><span class=cF1>return</span><span class=cF0> </span><span class=cF3>TRUE</span><span class=cF0>;
<a name="l327"></a>}
<a name="l328"></a>
<a name="l329"></a></span><span class=cF1>U0</span><span class=cF0> AnimateTask(</span><span class=cF9>CTask</span><span class=cF0> *parent)
<a name="l330"></a>{
<a name="l331"></a> </span><span class=cF9>I64</span><span class=cF0> i,cx,cy,cursor_x,cursor_y,dd,ddx,ddy,cnt,max_speed=</span><span class=cF3>I64_MAX</span><span class=cF0>,updates=0,
<a name="l332"></a> my_outside_cnt;
<a name="l333"></a> </span><span class=cF1>F64</span><span class=cF0> f,d,dx,dy,s,stress;
<a name="l334"></a> Animal *tmpa,*tmpa1;
<a name="l335"></a> </span><span class=cF1>while</span><span class=cF0> (</span><span class=cF3>TRUE</span><span class=cF0>) </span><span class=cF7>{</span><span class=cF0>
<a name="l336"></a> max_speed=</span><span class=cF5>ClampU64</span><span class=cF0>(max_speed,</span><span class=cF3>U32_MAX</span><span class=cF0>/3,200*</span><span class=cF3>U32_MAX</span><span class=cF0>);
<a name="l337"></a> cx=(MAP_WIDTH -parent-&gt;pix_width)/2,
<a name="l338"></a> cy=(MAP_HEIGHT-parent-&gt;pix_height)/2;
<a name="l339"></a> cursor_x=</span><span class=cFB>ms</span><span class=cF0>.pos.x+cx-parent-&gt;pix_left-parent-&gt;scroll_x;
<a name="l340"></a> cursor_y=</span><span class=cFB>ms</span><span class=cF0>.pos.y+cy-parent-&gt;pix_top -parent-&gt;scroll_y;
<a name="l341"></a> cnt=0;stress=0;
<a name="l342"></a> my_outside_cnt=0;
<a name="l343"></a> </span><span class=cF1>if</span><span class=cF0> (cursor_x&lt;FENCE_WIDTH &amp;&amp; cursor_y&lt;FENCE_HEIGHT)
<a name="l344"></a> gate_t=</span><span class=cF5>tS</span><span class=cF0>;
<a name="l345"></a> RedrawGate;
<a name="l346"></a> </span><span class=cF1>for</span><span class=cF0> (i=0;i&lt;ANIMALS_NUM;i++) {
<a name="l347"></a> tmpa=&amp;a[i];
<a name="l348"></a> </span><span class=cF1>if</span><span class=cF0> (!tmpa-&gt;dead) </span><span class=cF7>{</span><span class=cF0>
<a name="l349"></a></span><span class=cF2>//Move away from horse</span><span class=cF0>
<a name="l350"></a> ddx=tmpa-&gt;x.i32[1]-cursor_x;
<a name="l351"></a> ddy=tmpa-&gt;y.i32[1]-cursor_y;
<a name="l352"></a> </span><span class=cF1>if</span><span class=cF0> (dd=</span><span class=cF5>SqrI64</span><span class=cF7>(</span><span class=cF0>ddx</span><span class=cF7>)</span><span class=cF0>+</span><span class=cF5>SqrI64</span><span class=cF7>(</span><span class=cF0>ddy</span><span class=cF7>)</span><span class=cF0>) {
<a name="l353"></a> d=</span><span class=cF5>Sqrt</span><span class=cF0>(dd);
<a name="l354"></a> dx=ddx/d;
<a name="l355"></a> dy=ddy/d;
<a name="l356"></a> f=5.0e2*</span><span class=cF3>U32_MAX</span><span class=cF0>/dd;
<a name="l357"></a> tmpa-&gt;dx+=f*dx;
<a name="l358"></a> tmpa-&gt;dy+=f*dy;
<a name="l359"></a> }
<a name="l360"></a>
<a name="l361"></a> </span><span class=cF2>//Resel buddy about every ANIMALS_NUM*10ms=5.12 seconds</span><span class=cF0>
<a name="l362"></a> tmpa1=&amp;a[tmpa-&gt;buddy];
<a name="l363"></a> </span><span class=cF1>if</span><span class=cF0> (tmpa1-&gt;dead || i==updates%ANIMALS_NUM) {
<a name="l364"></a> BuddySel(i);
<a name="l365"></a> tmpa1=&amp;a[tmpa-&gt;buddy];
<a name="l366"></a> }
<a name="l367"></a>
<a name="l368"></a> </span><span class=cF2>//Move toward buddy</span><span class=cF0>
<a name="l369"></a> ddx=tmpa-&gt;x.i32[1]-tmpa1-&gt;x.i32[1];
<a name="l370"></a> ddy=tmpa-&gt;y.i32[1]-tmpa1-&gt;y.i32[1];
<a name="l371"></a> </span><span class=cF1>if</span><span class=cF0> (dd=</span><span class=cF5>SqrI64</span><span class=cF7>(</span><span class=cF0>ddx</span><span class=cF7>)</span><span class=cF0>+</span><span class=cF5>SqrI64</span><span class=cF7>(</span><span class=cF0>ddy</span><span class=cF7>)</span><span class=cF0>) {
<a name="l372"></a> d=</span><span class=cF5>Sqrt</span><span class=cF0>(dd);
<a name="l373"></a> s=d`1.25-80;
<a name="l374"></a> stress+=</span><span class=cF5>Abs</span><span class=cF0>(s);
<a name="l375"></a> dx=ddx/d;
<a name="l376"></a> dy=ddy/d;
<a name="l377"></a> f=-0.001*s*</span><span class=cF3>U32_MAX</span><span class=cF0>;
<a name="l378"></a> tmpa-&gt;dx+=f*dx;
<a name="l379"></a> tmpa-&gt;dy+=f*dy;
<a name="l380"></a> }
<a name="l381"></a>
<a name="l382"></a> </span><span class=cF2>//Make velocity similar to buddy</span><span class=cF0>
<a name="l383"></a> tmpa-&gt;dx+=0.1*(tmpa1-&gt;dx-tmpa-&gt;dx);
<a name="l384"></a> tmpa-&gt;dy+=0.1*(tmpa1-&gt;dy-tmpa-&gt;dy);
<a name="l385"></a>
<a name="l386"></a> </span><span class=cF2>//Add random movement, limit speed and dampen speed</span><span class=cF0>
<a name="l387"></a> tmpa-&gt;dx=0.995*</span><span class=cF5>ClampI64</span><span class=cF0>(tmpa-&gt;dx+</span><span class=cF5>RandI32</span><span class=cF0>/32,-max_speed,max_speed);
<a name="l388"></a> tmpa-&gt;dy=0.995*</span><span class=cF5>ClampI64</span><span class=cF0>(tmpa-&gt;dy+</span><span class=cF5>RandI32</span><span class=cF0>/32,-max_speed,max_speed);
<a name="l389"></a>
<a name="l390"></a> </span><span class=cF2>//Slow in river</span><span class=cF0>
<a name="l391"></a> </span><span class=cF1>if</span><span class=cF0> (</span><span class=cF5>GrPeek</span><span class=cF7>(</span><span class=cF0>map_dc,tmpa-&gt;x.i32[1],tmpa-&gt;y.i32[1]</span><span class=cF7>)</span><span class=cF0>!=</span><span class=cF3>LTGREEN</span><span class=cF0>) {
<a name="l392"></a> tmpa-&gt;dx/=2;
<a name="l393"></a> tmpa-&gt;dy/=2;
<a name="l394"></a> }
<a name="l395"></a>
<a name="l396"></a> </span><span class=cF1>if</span><span class=cF0> (CheckMap</span><span class=cF7>(</span><span class=cF0>(tmpa-&gt;x+tmpa-&gt;dx)&gt;&gt;32,(tmpa-&gt;y+tmpa-&gt;dy)&gt;&gt;32</span><span class=cF7>)</span><span class=cF0>) {
<a name="l397"></a> tmpa-&gt;x+=tmpa-&gt;dx;
<a name="l398"></a> tmpa-&gt;y+=tmpa-&gt;dy;
<a name="l399"></a> }
<a name="l400"></a>
<a name="l401"></a> </span><span class=cF2>//Keep on map</span><span class=cF0>
<a name="l402"></a> </span><span class=cF1>if</span><span class=cF0> (!</span><span class=cF7>(</span><span class=cF0>MAP_BORDER+ANIMAL_WIDTH/2
<a name="l403"></a> &lt;=tmpa-&gt;x.i32[1]&lt;MAP_WIDTH-MAP_BORDER-ANIMAL_WIDTH/2</span><span class=cF7>)</span><span class=cF0>) {
<a name="l404"></a> tmpa-&gt;x -=tmpa-&gt;dx;
<a name="l405"></a> tmpa-&gt;dx=-tmpa-&gt;dx;
<a name="l406"></a> }
<a name="l407"></a> </span><span class=cF1>if</span><span class=cF0> (!</span><span class=cF7>(</span><span class=cF0>MAP_BORDER+ANIMAL_HEIGHT
<a name="l408"></a> &lt;=tmpa-&gt;y.i32[1]&lt;MAP_HEIGHT-MAP_BORDER</span><span class=cF7>)</span><span class=cF0>) {
<a name="l409"></a> tmpa-&gt;y -=tmpa-&gt;dy;
<a name="l410"></a> tmpa-&gt;dy=-tmpa-&gt;dy;
<a name="l411"></a> }
<a name="l412"></a> cnt++;
<a name="l413"></a> </span><span class=cF1>if</span><span class=cF0> (tmpa-&gt;x&gt;&gt;32&gt;=FENCE_WIDTH || tmpa-&gt;y&gt;&gt;32&gt;=FENCE_HEIGHT)
<a name="l414"></a> my_outside_cnt++;
<a name="l415"></a> </span><span class=cF7>}</span><span class=cF0>
<a name="l416"></a> }
<a name="l417"></a> outside_cnt=my_outside_cnt;
<a name="l418"></a>
<a name="l419"></a> </span><span class=cF1>if</span><span class=cF0> (!</span><span class=cF7>(</span><span class=cF0>updates&amp;15</span><span class=cF7>)</span><span class=cF0>)
<a name="l420"></a> RiverDropsNext(parent);
<a name="l421"></a>
<a name="l422"></a> </span><span class=cF1>if</span><span class=cF0> (!tf &amp;&amp; !outside_cnt) {
<a name="l423"></a> tf=</span><span class=cF5>tS</span><span class=cF0>;
<a name="l424"></a> </span><span class=cFB>music</span><span class=cF0>.mute=</span><span class=cF3>TRUE</span><span class=cF0>;
<a name="l425"></a> </span><span class=cF5>Snd</span><span class=cF0>(86);</span><span class=cF5>Sleep</span><span class=cF0>(200);</span><span class=cF5>Snd</span><span class=cF0>;</span><span class=cF5>Sleep</span><span class=cF0>(100);
<a name="l426"></a> </span><span class=cF1>if</span><span class=cF0> (tf-t0&lt;best_score) </span><span class=cF7>{</span><span class=cF0>
<a name="l427"></a> best_score=tf-t0;
<a name="l428"></a> </span><span class=cF5>Snd</span><span class=cF0>(86);</span><span class=cF5>Sleep</span><span class=cF0>(200);</span><span class=cF5>Snd</span><span class=cF0>;</span><span class=cF5>Sleep</span><span class=cF0>(100);
<a name="l429"></a> </span><span class=cF7>}</span><span class=cF0>
<a name="l430"></a> </span><span class=cFB>music</span><span class=cF0>.mute=</span><span class=cF3>FALSE</span><span class=cF0>;
<a name="l431"></a> }
<a name="l432"></a>
<a name="l433"></a> updates++;
<a name="l434"></a>
<a name="l435"></a> </span><span class=cF1>if</span><span class=cF0> (cnt)
<a name="l436"></a> stress/=cnt;
<a name="l437"></a> </span><span class=cF1>else</span><span class=cF0>
<a name="l438"></a> stress=0;
<a name="l439"></a> </span><span class=cF1>if</span><span class=cF0> (stress&gt;100.0) {
<a name="l440"></a> </span><span class=cF5>Yield</span><span class=cF0>;
<a name="l441"></a> max_speed=stress/5.0*</span><span class=cF3>U32_MAX</span><span class=cF0>; </span><span class=cF2>//Converge faster at start-up</span><span class=cF0>
<a name="l442"></a> } </span><span class=cF1>else</span><span class=cF0> {
<a name="l443"></a> </span><span class=cF5>Sleep</span><span class=cF0>(10);
<a name="l444"></a> max_speed=0; </span><span class=cF2>//Will be set to normal max speed</span><span class=cF0>
<a name="l445"></a> }
<a name="l446"></a> </span><span class=cF7>}</span><span class=cF0>
<a name="l447"></a>}
<a name="l448"></a>
<a name="l449"></a></span><span class=cF1>U0</span><span class=cF0> SongTask(</span><span class=cF9>I64</span><span class=cF0>)
<a name="l450"></a>{</span><span class=cF2>//Randomly generated (by God :-)</span><span class=cF0>
<a name="l451"></a> </span><span class=cF5>Fs</span><span class=cF0>-&gt;task_end_cb=&amp;</span><span class=cF5>SndTaskEndCB</span><span class=cF0>;
<a name="l452"></a> </span><span class=cF5>MusicSettingsRst</span><span class=cF0>;
<a name="l453"></a> </span><span class=cF1>while</span><span class=cF0> (</span><span class=cF3>TRUE</span><span class=cF0>) </span><span class=cF7>{</span><span class=cF0>
<a name="l454"></a> </span><span class=cF5>Play</span><span class=cF0>(</span><span class=cF6>&quot;5qC4etG5DC4B5DCECFqFC4sA5D4A5D4qB&quot;</span><span class=cF0>);
<a name="l455"></a> </span><span class=cF5>Play</span><span class=cF0>(</span><span class=cF6>&quot;5C4etG5DC4B5DCECFqFC4sA5D4A5D4qB&quot;</span><span class=cF0>);
<a name="l456"></a> </span><span class=cF5>Play</span><span class=cF0>(</span><span class=cF6>&quot;4sGAGA5qG4etG5GD4eBBqB5F4eBA5qE&quot;</span><span class=cF0>);
<a name="l457"></a> </span><span class=cF5>Play</span><span class=cF0>(</span><span class=cF6>&quot;4sGAGA5qG4etG5GD4eBBqB5F4eBA5qE&quot;</span><span class=cF0>);
<a name="l458"></a> </span><span class=cF7>}</span><span class=cF0>
<a name="l459"></a>}
<a name="l460"></a>
<a name="l461"></a></span><span class=cF1>U0</span><span class=cF0> ReInit()
<a name="l462"></a>{
<a name="l463"></a> </span><span class=cF9>I64</span><span class=cF0> i;
<a name="l464"></a>
<a name="l465"></a> RiverDropsDel;
<a name="l466"></a> map_dc-&gt;color=</span><span class=cF3>LTGREEN</span><span class=cF0>;
<a name="l467"></a> </span><span class=cF5>GrRect</span><span class=cF0>(map_dc,2,2,MAP_WIDTH-4,MAP_HEIGHT-4);
<a name="l468"></a>
<a name="l469"></a> RiverMake;
<a name="l470"></a>
<a name="l471"></a> </span><span class=cF2>//Plot fence</span><span class=cF0>
<a name="l472"></a> </span><span class=cF1>for</span><span class=cF0> (i=FENCE_WIDTH;i&gt;0;i-=16)
<a name="l473"></a> </span><span class=cF5>Sprite3</span><span class=cF0>(map_dc,i,FENCE_HEIGHT,0,</span><span class=cFA>&lt;9&gt;</span><span class=cF0>);
<a name="l474"></a> map_dc-&gt;thick=1;
<a name="l475"></a> map_dc-&gt;color=</span><span class=cF3>BROWN</span><span class=cF0>;
<a name="l476"></a> </span><span class=cF1>for</span><span class=cF0> (i=0;i&lt;FENCE_HEIGHT-16;i+=16)
<a name="l477"></a> </span><span class=cF5>GrLine</span><span class=cF0>(map_dc,FENCE_WIDTH-1,i,FENCE_WIDTH-1,i+7);
<a name="l478"></a> map_dc-&gt;color=</span><span class=cF3>LTGRAY</span><span class=cF0>;
<a name="l479"></a> </span><span class=cF5>GrLine</span><span class=cF0>(map_dc,FENCE_WIDTH,0,FENCE_WIDTH,FENCE_HEIGHT-6);
<a name="l480"></a> RedrawGate;
<a name="l481"></a>
<a name="l482"></a> map_dc-&gt;thick=MAP_BORDER;
<a name="l483"></a> map_dc-&gt;color=</span><span class=cF3>RED</span><span class=cF0>;
<a name="l484"></a> </span><span class=cF5>GrBorder</span><span class=cF0>(map_dc,MAP_BORDER/2,MAP_BORDER/2,
<a name="l485"></a> MAP_WIDTH-</span><span class=cF7>(</span><span class=cF0>MAP_BORDER+1</span><span class=cF7>)</span><span class=cF0>/2,MAP_HEIGHT-</span><span class=cF7>(</span><span class=cF0>MAP_BORDER+1</span><span class=cF7>)</span><span class=cF0>/2);
<a name="l486"></a>
<a name="l487"></a> </span><span class=cF1>for</span><span class=cF0> (i=MAP_BORDER;i&lt;=MAP_HEIGHT-MAP_BORDER;i++)
<a name="l488"></a> RiverDropsNext(</span><span class=cF5>Fs</span><span class=cF0>);
<a name="l489"></a>
<a name="l490"></a> </span><span class=cF5>MemSet</span><span class=cF0>(a,0,ANIMALS_NUM*</span><span class=cF1>sizeof</span><span class=cF7>(</span><span class=cF0>Animal</span><span class=cF7>)</span><span class=cF0>);
<a name="l491"></a> </span><span class=cF1>for</span><span class=cF0> (i=0;i&lt;ANIMALS_NUM;i++) </span><span class=cF7>{</span><span class=cF0>
<a name="l492"></a> a[i].num=i;
<a name="l493"></a> </span><span class=cF1>do</span><span class=cF0> {
<a name="l494"></a> a[i].x=(64+</span><span class=cF5>RandU32</span><span class=cF0>%</span><span class=cF7>(</span><span class=cF0>MAP_WIDTH-128</span><span class=cF7>)</span><span class=cF0>)&lt;&lt;32;
<a name="l495"></a> a[i].y=(64+</span><span class=cF5>RandU32</span><span class=cF0>%</span><span class=cF7>(</span><span class=cF0>MAP_WIDTH-128</span><span class=cF7>)</span><span class=cF0>)&lt;&lt;32;
<a name="l496"></a> } </span><span class=cF1>while</span><span class=cF0> (!CheckMap</span><span class=cF7>(</span><span class=cF0>a[i].x&gt;&gt;32,a[i].y&gt;&gt;32</span><span class=cF7>)</span><span class=cF0>);
<a name="l497"></a> </span><span class=cF1>if</span><span class=cF0> (i&amp;1)
<a name="l498"></a> a[i].imgs=cow_imgs;
<a name="l499"></a> </span><span class=cF1>else</span><span class=cF0>
<a name="l500"></a> a[i].imgs=bull_imgs;
<a name="l501"></a> a[i].frame0=</span><span class=cF5>RandU16</span><span class=cF0>&amp;3;
<a name="l502"></a> BuddySel(i);
<a name="l503"></a> </span><span class=cF7>}</span><span class=cF0>
<a name="l504"></a> outside_cnt=ANIMALS_NUM;
<a name="l505"></a> gate_t=0;
<a name="l506"></a> gate_theta=0;
<a name="l507"></a> t0=</span><span class=cF5>tS</span><span class=cF0>;
<a name="l508"></a> tf=0;
<a name="l509"></a>}
<a name="l510"></a>
<a name="l511"></a></span><span class=cF1>U0</span><span class=cF0> Init()
<a name="l512"></a>{
<a name="l513"></a> RiverNew;
<a name="l514"></a> rd_lock=0;
<a name="l515"></a> </span><span class=cF5>QueInit</span><span class=cF0>(&amp;rd_head);
<a name="l516"></a> map_dc=</span><span class=cF5>DCNew</span><span class=cF0>(MAP_WIDTH,MAP_HEIGHT);
<a name="l517"></a> a=</span><span class=cF5>MAlloc</span><span class=cF0>(ANIMALS_NUM*</span><span class=cF1>sizeof</span><span class=cF7>(</span><span class=cF0>Animal</span><span class=cF7>)</span><span class=cF0>);
<a name="l518"></a> ReInit;
<a name="l519"></a>}
<a name="l520"></a>
<a name="l521"></a></span><span class=cF1>U0</span><span class=cF0> CleanUp()
<a name="l522"></a>{
<a name="l523"></a> </span><span class=cF5>DCDel</span><span class=cF0>(map_dc);
<a name="l524"></a> </span><span class=cF5>Free</span><span class=cF0>(a);
<a name="l525"></a> RiverDropsDel;
<a name="l526"></a> RiverDel;
<a name="l527"></a>}
<a name="l528"></a>
<a name="l529"></a></span><span class=cF1>U0</span><span class=cF0> RawHide()
<a name="l530"></a>{
<a name="l531"></a> </span><span class=cF9>I64</span><span class=cF0> msg_code,arg1,arg2;
<a name="l532"></a> </span><span class=cF5>SettingsPush</span><span class=cF0>; </span><span class=cF2>//See </span><a href="/Wb/Adam/TaskSettings.HC.HTML#l3"><span class=cF4>SettingsPush</span></a><span class=cF0>
<a name="l533"></a> </span><span class=cF5>MenuPush</span><span class=cF0>(
<a name="l534"></a> </span><span class=cF6>&quot;File {&quot;</span><span class=cF0>
<a name="l535"></a> </span><span class=cF6>&quot; Abort(,CH_SHIFT_ESC);&quot;</span><span class=cF0>
<a name="l536"></a> </span><span class=cF6>&quot; Exit(,CH_ESC);&quot;</span><span class=cF0>
<a name="l537"></a> </span><span class=cF6>&quot;}&quot;</span><span class=cF0>
<a name="l538"></a> </span><span class=cF6>&quot;Play {&quot;</span><span class=cF0>
<a name="l539"></a> </span><span class=cF6>&quot; Restart(,'\n');&quot;</span><span class=cF0>
<a name="l540"></a> </span><span class=cF6>&quot;}&quot;</span><span class=cF0>
<a name="l541"></a> );
<a name="l542"></a> </span><span class=cF5>Fs</span><span class=cF0>-&gt;song_task=</span><span class=cF5>Spawn</span><span class=cF0>(&amp;SongTask,</span><span class=cF3>NULL</span><span class=cF0>,</span><span class=cF6>&quot;Song&quot;</span><span class=cF0>,,</span><span class=cF5>Fs</span><span class=cF0>);
<a name="l543"></a>
<a name="l544"></a> </span><span class=cF5>PopUpOk</span><span class=cF0>(
<a name="l545"></a> </span><span class=cF6>&quot;Coral the cattle. The coral is in the\n&quot;</span><span class=cF0>
<a name="l546"></a> </span><span class=cF6>&quot;upper-left corner if you scroll.\n\n&quot;</span><span class=cF0>
<a name="l547"></a> </span><span class=cF6>&quot;Keep holding the $GREEN$&lt;CTRL&gt;$FG$ key and\n&quot;</span><span class=cF0>
<a name="l548"></a> </span><span class=cF6>&quot;scroll with $GREEN${CTRL-Left Grab}$FG$.&quot;</span><span class=cF0>);
<a name="l549"></a>
<a name="l550"></a> </span><span class=cF5>Fs</span><span class=cF0>-&gt;win_inhibit=</span><span class=cF3>WIG_TASK_DFT</span><span class=cF0>-</span><span class=cF3>WIF_SELF_FOCUS</span><span class=cF0>
<a name="l551"></a> -</span><span class=cF3>WIF_SELF_BORDER</span><span class=cF0>-</span><span class=cF3>WIF_SELF_GRAB_SCROLL</span><span class=cF0>-</span><span class=cF3>WIF_FOCUS_TASK_MENU</span><span class=cF0>;
<a name="l552"></a> </span><span class=cF5>AutoComplete</span><span class=cF0>;
<a name="l553"></a> </span><span class=cF5>WinBorder</span><span class=cF0>;
<a name="l554"></a> </span><span class=cF5>WinMax</span><span class=cF0>;
<a name="l555"></a> </span><span class=cF5>DocCursor</span><span class=cF0>;
<a name="l556"></a> </span><span class=cF5>DocClear</span><span class=cF0>;
<a name="l557"></a> Init;
<a name="l558"></a> </span><span class=cF5>Fs</span><span class=cF0>-&gt;animate_task=</span><span class=cF5>Spawn</span><span class=cF0>(&amp;AnimateTask,</span><span class=cF5>Fs</span><span class=cF0>,</span><span class=cF6>&quot;Animate&quot;</span><span class=cF0>,,</span><span class=cF5>Fs</span><span class=cF0>);
<a name="l559"></a> </span><span class=cF5>Fs</span><span class=cF0>-&gt;draw_it=&amp;</span><span class=cF5>DrawIt</span><span class=cF0>;
<a name="l560"></a> </span><span class=cF1>try</span><span class=cF0> </span><span class=cF7>{</span><span class=cF0>
<a name="l561"></a> </span><span class=cF1>while</span><span class=cF0> (</span><span class=cF3>TRUE</span><span class=cF0>) {
<a name="l562"></a> msg_code=</span><span class=cF5>GetMsg</span><span class=cF0>(&amp;arg1,&amp;arg2,
<a name="l563"></a> 1&lt;&lt;</span><span class=cF3>MSG_KEY_DOWN</span><span class=cF0>+1&lt;&lt;</span><span class=cF3>MSG_MS_L_DOWN</span><span class=cF0>+1&lt;&lt;</span><span class=cF3>MSG_MS_R_DOWN</span><span class=cF0>);
<a name="l564"></a> </span><span class=cF1>switch</span><span class=cF0> (msg_code) </span><span class=cF7>{</span><span class=cF0>
<a name="l565"></a> </span><span class=cF1>case</span><span class=cF0> </span><span class=cF3>MSG_MS_L_DOWN</span><span class=cF0>: </span><span class=cF2>//Doesn't do anything, yet.</span><span class=cF0>
<a name="l566"></a> </span><span class=cF1>break</span><span class=cF0>;
<a name="l567"></a> </span><span class=cF1>case</span><span class=cF0> </span><span class=cF3>MSG_MS_R_DOWN</span><span class=cF0>: </span><span class=cF2>//Doesn't do anything, yet.</span><span class=cF0>
<a name="l568"></a> </span><span class=cF1>break</span><span class=cF0>;
<a name="l569"></a> </span><span class=cF1>case</span><span class=cF0> </span><span class=cF3>MSG_KEY_DOWN</span><span class=cF0>:
<a name="l570"></a> </span><span class=cF1>switch</span><span class=cF0> (arg1) {
<a name="l571"></a> </span><span class=cF1>case</span><span class=cF0> </span><span class=cF6>'\n'</span><span class=cF0>:
<a name="l572"></a> ReInit;
<a name="l573"></a> </span><span class=cF1>break</span><span class=cF0>;
<a name="l574"></a> </span><span class=cF1>case</span><span class=cF0> </span><span class=cF3>CH_SHIFT_ESC</span><span class=cF0>:
<a name="l575"></a> </span><span class=cF1>case</span><span class=cF0> </span><span class=cF3>CH_ESC</span><span class=cF0>:
<a name="l576"></a> </span><span class=cF1>goto</span><span class=cF0> rh_done;
<a name="l577"></a> }
<a name="l578"></a> </span><span class=cF1>break</span><span class=cF0>;
<a name="l579"></a> </span><span class=cF7>}</span><span class=cF0>
<a name="l580"></a> }
<a name="l581"></a>rh_done:
<a name="l582"></a> </span><span class=cF5>GetMsg</span><span class=cF0>(,,1&lt;&lt;</span><span class=cF3>MSG_KEY_UP</span><span class=cF0>);
<a name="l583"></a> </span><span class=cF7>}</span><span class=cF0> </span><span class=cF1>catch</span><span class=cF0>
<a name="l584"></a> </span><span class=cF5>PutExcept</span><span class=cF0>;
<a name="l585"></a> </span><span class=cF5>SettingsPop</span><span class=cF0>;
<a name="l586"></a> CleanUp;
<a name="l587"></a> </span><span class=cF5>MenuPop</span><span class=cF0>;
<a name="l588"></a> </span><span class=cF5>RegWrite</span><span class=cF0>(</span><span class=cF6>&quot;TempleOS/RawHide&quot;</span><span class=cF0>,</span><span class=cF6>&quot;F64 best_score=%5.4f;\n&quot;</span><span class=cF0>,best_score);
<a name="l589"></a>}
<a name="l590"></a>
<a name="l591"></a>RawHide;
</span></pre></body>
</html>