#!/usr/bin/perl
#
# MarathonRunner
# Nowral
# 99.1.26
#
$pi = 3.14159265358979;
$rd = $pi / 180;      #radian / degree

$fs = 60; #移動速度 <秒
$ft = 10; #転回速度 <度

#初期座標 <美幌駅
$lat =  43*3600 + 50*60 +  4.7;
$lon = 144*3600 +  6*60 + 34.5;

$ylat = &s2ydms($lat); #<>d.ms形式
$ylon = &s2ydms($lon);
&MacPerl'DoAppleScript(<<END_SCRIPT0);
tell application "やまおたく for Mac 0.8.3"
 activate
  set MsgStrs to {"ようこそ", "MarathonRunnerへ", "ただ今、降下中……"}
  repeat with i from 36 to 0 by -1
    set aHigh to round (i ^ 2 * 400)
    set aHead to round ((i * 20) mod 360)
    set aMsg to item ((i mod (number of items of MsgStrs)) + 1) of MsgStrs
    Set Position1 aMsg latitude $ylat longitude $ylon height aHigh azimuth aHead elevationAngle 0
  end repeat
  Set Position0 "着地しました。" latitude $ylat longitude $ylon height 100
end tell
tell current application to activate
END_SCRIPT0

$newargv = "GPS Track Data";
open(ARGVOUT, ">$newargv");
&MacPerl'SetFileInfo('GPSy', 'TEXT', $newargv);
print ARGVOUT "GPSy3 Track     (version 3.20)\tNUMBER\tNAME\tCOMMENT\tDATE\tLAT/NORTHING\tLON/EASTING\tZONE\tALT\tCOORD\tDATUM\tICON\tPROX\n";
print ARGVOUT "#\tRecords:\t0\n";

$yhd = 0; #方位 < 0 .. 360
$ynk = 0; #仰角 < -90 .. 90
$tl = 0;
$glat = &s2gdms($lat);
$glon = &s2gdms($lon);

print "OK, Let's go!\n";
`stty raw`;

while(!/[\t\s]/) {
    undef $_;
    $_ = getc until /./;
    print $_,"\n"; #エコーバック

    if(/i/) { #前進
        $rad = $yhd * $rd;
        $lat += $fs * cos($rad);
        $lon += $fs * sin($rad);
        $mf = 1;
    }
    elsif(/k/) { #後退
        $rad = $yhd * $rd;
        $lat -= $fs * cos($rad);
        $lon -= $fs * sin($rad);
        $mf = 1;
    }
    elsif(/l/) { #右旋回
        $yhd += $ft;
        $yhd %= 360;
    }
    elsif(/j/) { #左旋回
        $yhd -= $ft;
        $yhd %= 360;
    }
    elsif(/x/) { #右スライド
        $rad = ($yhd+90) % 360 * $rd;
        $lat += $fs * cos($rad);
        $lon += $fs * sin($rad);
        $mf = 1;
     }
    elsif(/z/) { #左スライド
        $rad = ($yhd+90) % 360 * $rd;
        $lat -= $fs * cos($rad);
        $lon -= $fs * sin($rad);
        $mf = 1;
     }
    elsif(/c/) { #下向き
        $ynk -= $ft;
        if($ynk < -90) { $ynk = -90; }
    }
    elsif(/d/) { #上向き
        $ynk += $ft;
        if($ynk > 90) { $ynk = 90; }
    }
    elsif(/v/) { #前向き
        $ynk = 0;
    }
    
    if($mf==1) {
        $mf = 0;
        $tl++;
        $ylat = &s2ydms($lat);
        $ylon = &s2ydms($lon);
    
        ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime();
        $gdate = sprintf("%02d/%02d/19%02d %02d:%02d:%02d",++$mon,$mday,$year,$hour,$min,$sec);
        $glat = &s2gdms($lat);
        $glon = &s2gdms($lon);
        print ARGVOUT "T\t1\tTRACK\t\t$gdate\t$glat\t$glon\t-\t 0.0\tDMS\tTokyo\t0\t0.00\n";
    }

    &MacPerl'DoAppleScript(<<END_SCRIPT1);
tell application "やまおたく for Mac 0.8.3"
  Set Position1 latitude $ylat longitude $ylon height 100 azimuth $yhd elevationAngle $ynk
end tell
END_SCRIPT1

}

`stty sane`;
close(ARGVOUT);

&MacPerl'DoAppleScript(<<END_SCRIPT2);
tell application "やまおたく for Mac 0.8.3"
  beep 1
  display dialog "Your Total Steps" default answer $tl buttons "Wow!" default button 1 with icon -20024
end tell
END_SCRIPT2

&MacPerl'Quit(3);
die "The Unhappy End";



sub s2ydms {
    local($s) = @_;
    $d = int($s / 3600);
    $m = int($s / 60);
    $s -= $m * 60;
    $m %= 60;
    $d + $m/100 + $s/10000;
}

sub s2gdms {
    local($sf) = @_;
    $d  = int($sf / 3600);
    $m  = ($sf/60) % 60;
    $s  = $sf % 60;
    $sf = int($sf*10 + 0.5) % 10;
    sprintf("%d。%02d'%02d.%d\"",$d,$m,$s,$sf);