',{id:dialog_el_id+'_cap','class':'DiaCaption'});row.append('XXX');form.append(row);if(will_save){row=$('
',{id:dialog_el_id+'_input','class':'DiaInput'});form.append(row);el=$('
',{id:dialog_el_id+'_infield',type:'text',name:'filename'});row.append(el);}
row=$('
',{id:dialog_el_id+'_body','class':'DiaBody'});form.append(row);row=$('
',{id:dialog_el_id+'_cap2','class':'DiaCaption'});row.hide();form.append(row);row=$('
',{id:dialog_el_id+'_buttonrow','class':'DiaButtons'});{el=$('
',{id:dialog_el_id+'_cancel',type:'button'});el.append('Cancel');el.on('click',evhan_cancel_button);row.append(el);el=$('',{id:dialog_el_id+'_delete',type:'button'});el.append('Delete');el.on('click',evhan_delete_button);el.hide();row.append(el);el=$('',{id:dialog_el_id+'_display',type:'button'});el.append('Display');el.on('click',evhan_display_button);el.hide();row.append(el);el=$('',{id:dialog_el_id+'_accept',type:'submit'});el.append(will_save?'Save':'Load');el.on('click',(will_save?evhan_accept_save_button:evhan_accept_load_button));row.append(el);}
form.append(row);frame.append(dia);is_open=true;evhan_storage_changed();var focusfunc;if(will_save){focusfunc=function(){var el=$('#'+dialog_el_id+'_infield');if(el.length)
el.focus();};}
else{focusfunc=function(){var el=$('#'+dialog_el_id+'_select');if(el.length)
el.focus();};}
defer_func(focusfunc);}
function dialog_close(){var dia=$('#'+dialog_el_id);if(dia.length)
dia.remove();var frame=$('#'+dialog_el_id+'_frame');if(frame.length)
frame.remove();var screen=$('#'+dialog_el_id+'_screen');if(screen.length)
screen.remove();is_open=false;dialog_callback=null;cur_filelist=null;editing=false;editing_dirent=null;}
function set_caption(msg,isupper){var elid=(isupper?dialog_el_id+'_cap':dialog_el_id+'_cap2');var el=$('#'+elid);if(!el.length)
return;if(!msg){el.hide();}
else{el.text(msg);el.show();}}
function label_for_usage(val){switch(val){case'data':return'data file';case'save':return'save file';case'transcript':return'transcript';case'command':return'command script';default:return'file';}}
function usage_is_textual(val){return(val=='transcript'||val=='command');}
function defer_func(func)
{return window.setTimeout(func,0.01*1000);}
function evhan_select_change(){if(!is_open)
return false;if(confirming)
return false;var selel=$('#'+dialog_el_id+'_select');if(!selel.length)
return false;var pos=selel.prop('selectedIndex');if(!cur_filelist||pos<0||pos>=cur_filelist.length)
return false;var file=cur_filelist[pos];var fel=$('#'+dialog_el_id+'_infield');if(!fel.length)
return false;fel.val(file.dirent.filename);return false;}
function evhan_select_change_editing(){if(!is_open)
return false;if(!editing||editing_dirent)
return false;var butel=$('#'+dialog_el_id+'_delete');butel.prop('disabled',true);butel=$('#'+dialog_el_id+'_display');butel.prop('disabled',true);var selel=$('#'+dialog_el_id+'_select');if(!selel.length)
return false;var pos=selel.prop('selectedIndex');if(!cur_filelist||pos<0||pos>=cur_filelist.length)
return false;var file=cur_filelist[pos];if(!file||!file.dirent||!file_ref_exists(file.dirent))
return false;butel=$('#'+dialog_el_id+'_delete');butel.prop('disabled',false);butel=$('#'+dialog_el_id+'_display');butel.prop('disabled',false);}
function evhan_accept_load_button(ev){ev.preventDefault();if(!is_open)
return false;if(editing)
return false;var selel=$('#'+dialog_el_id+'_select');if(!selel.length)
return false;var pos=selel.prop('selectedIndex');if(!cur_filelist||pos<0||pos>=cur_filelist.length)
return false;var file=cur_filelist[pos];if(!file||!file.dirent||!file_ref_exists(file.dirent))
return false;var callback=dialog_callback;dialog_close();if(callback)
callback(file.dirent);return false;}
function evhan_accept_save_button(ev){ev.preventDefault();if(!is_open)
return false;if(editing)
return false;var fel=$('#'+dialog_el_id+'_infield');if(!fel.length)
return false;var filename=fel.val();filename=jQuery.trim(filename);if(!filename.length)
return false;var dirent=file_construct_ref(filename,cur_usage,cur_gameid);if(file_ref_exists(dirent)&&!confirming){confirming=true;set_caption('You already have a '+cur_usage_name+' "'
+dirent.filename+'". Do you want to replace it?',false);fel.prop('disabled',true);var butel=$('#'+dialog_el_id+'_accept');butel.text('Replace');return false;}
var callback=dialog_callback;dialog_close();if(callback)
callback(dirent);return false;}
function evhan_edit_button(ev){ev.preventDefault();if(!is_open)
return false;if(!editing){editing=true;editing_dirent=null;if(confirming){confirming=false;set_caption(null,false);var fel=$('#'+dialog_el_id+'_infield');fel.prop('disabled',false);var butel=$('#'+dialog_el_id+'_accept');butel.prop('disabled',false);butel.text('Save');}
var fel=$('#'+dialog_el_id+'_input');if(fel.length){fel.hide();}
var butel=$('#'+dialog_el_id+'_edit');butel.text('Done');butel=$('#'+dialog_el_id+'_delete');butel.show();butel=$('#'+dialog_el_id+'_display');butel.show();butel=$('#'+dialog_el_id+'_accept');butel.hide();evhan_storage_changed();return false;}
else if(!editing_dirent){editing=false;editing_dirent=null;var fel=$('#'+dialog_el_id+'_input');if(fel.length){fel.show();}
var butel=$('#'+dialog_el_id+'_edit');butel.text('Edit');butel=$('#'+dialog_el_id+'_delete');butel.hide();butel=$('#'+dialog_el_id+'_display');butel.hide();butel=$('#'+dialog_el_id+'_accept');butel.show();evhan_storage_changed();return false;}
else{editing=true;editing_dirent=null;$('#'+dialog_el_id+'_buttonrow').show();var butel=$('#'+dialog_el_id+'_edit');butel.text('Done');evhan_storage_changed();return false;}}
function evhan_delete_button(ev){ev.preventDefault();if(!is_open)
return false;if(!editing||editing_dirent)
return false;var selel=$('#'+dialog_el_id+'_select');if(!selel.length)
return false;var pos=selel.prop('selectedIndex');if(!cur_filelist||pos<0||pos>=cur_filelist.length)
return false;var file=cur_filelist[pos];if(!file||!file.dirent)
return false;file_remove_ref(file.dirent);evhan_storage_changed();return false;}
function evhan_display_button(ev){ev.preventDefault();if(!is_open)
return false;if(!editing||editing_dirent)
return false;var selel=$('#'+dialog_el_id+'_select');if(!selel.length)
return false;var pos=selel.prop('selectedIndex');if(!cur_filelist||pos<0||pos>=cur_filelist.length)
return false;var file=cur_filelist[pos];if(!file||!file.dirent||!file_ref_exists(file.dirent))
return false;$('#'+dialog_el_id+'_buttonrow').hide();var butel=$('#'+dialog_el_id+'_edit');butel.text('Close');editing_dirent=file.dirent;evhan_storage_changed();return false;}
function evhan_cancel_button(ev){ev.preventDefault();if(!is_open)
return false;if(confirming){confirming=false;set_caption(null,false);var fel=$('#'+dialog_el_id+'_infield');fel.prop('disabled',false);var butel=$('#'+dialog_el_id+'_accept');butel.prop('disabled',false);butel.text('Save');return false;}
var callback=dialog_callback;dialog_close();if(callback)
callback(null);return false;}
function evhan_storage_changed(ev){if(!is_open)
return false;var el,bodyel,butel,ls,lastusage;var changedkey=null;if(ev)
changedkey=ev.key;bodyel=$('#'+dialog_el_id+'_body');if(!bodyel.length)
return false;if(editing&&editing_dirent){if(!file_ref_exists(editing_dirent)){editing_dirent=null;$('#'+dialog_el_id+'_buttonrow').show();butel=$('#'+dialog_el_id+'_edit');butel.text('Done');}}
if(editing&&editing_dirent){bodyel.empty();var dat=file_read(editing_dirent);dat=String.fromCharCode.apply(this,dat);if(usage_is_textual(editing_dirent.usage)){var textel=$('',{'class':'DiaDisplayText'});textel.text(dat);bodyel.append(textel);set_caption('Displaying file contents...',true);}
else{var b64dat=window.btoa(dat);var linkel=$('
',{'href':'data:application/octet-stream;base64,'+b64dat,'target':'_blank','download':'data'});linkel.text(editing_dirent.filename);bodyel.append(linkel);set_caption('Use "Save As" option in your browser to download this link.',true);}
return false;}
if(editing){ls=files_list(null,cur_gameid);if(cur_gameid!=''){ls=ls.concat(files_list(null,''));}
ls.sort(function(f1,f2){if(f1.dirent.usage!=f2.dirent.usage)
return(f1.dirent.usage',{id:dialog_el_id+'_select',name:'files'});selel.prop('size','5');var ix,file,datestr;var anyselected=false;for(ix=0;ix',{name:'f'+ix});el.prop('disabled',true);el.text('-- '+label_for_usage(file.label)+'s --');selel.append(el);continue;}
el=$('',{name:'f'+ix});if(!anyselected){anyselected=true;el.selected=true;}
datestr=format_date(file.modified);el.text(file.dirent.filename+' -- '+datestr);selel.append(el);}
bodyel.append(selel);selel.on('change',evhan_select_change_editing);evhan_select_change_editing();set_caption('All stored files are now visible. You may delete them, and display files containing text. Press Done when finished.',true);return false;}
ls=files_list(cur_usage,cur_gameid);ls.sort(function(f1,f2){return f2.modified.getTime()-f1.modified.getTime();});cur_filelist=ls;if(ls.length==0){bodyel.empty();}
else{bodyel.empty();var selel=$('',{id:dialog_el_id+'_select',name:'files'});selel.prop('size','5');var ix,file,datestr;for(ix=0;ix',{name:'f'+ix});if(ix==0)
el.selected=true;datestr=format_date(file.modified);el.text(file.dirent.filename+' -- '+datestr);selel.append(el);}
bodyel.append(selel);if(will_save)
selel.on('change',evhan_select_change);}
if(will_save){set_caption('Name this '+cur_usage_name+'.',true);el=$('#'+dialog_el_id+'_accept');el.prop('disabled',false);}
else{if(ls.length==0){set_caption('You have no '+cur_usage_name+'s for this game.',true);el=$('#'+dialog_el_id+'_accept');el.prop('disabled',true);}
else{set_caption('Select a '+cur_usage_name+' to load.',true);el=$('#'+dialog_el_id+'_accept');el.prop('disabled',false);}}}
function file_construct_ref(filename,usage,gameid){if(!filename)
filename='';if(!usage)
useage='';if(!gameid)
gameid='';var key=usage+':'+gameid+':'+filename;var ref={dirent:'dirent:'+key,content:'content:'+key,filename:filename,usage:usage,gameid:gameid};return ref;}
function file_decode_ref(dirkey){if(dirkey.slice(0,7)!='dirent:')
return null;var oldpos=7;var pos=dirkey.indexOf(':',oldpos);if(pos<0)
return null;var usage=dirkey.slice(oldpos,pos);oldpos=pos+1;pos=dirkey.indexOf(':',oldpos);if(pos<0)
return null;var gameid=dirkey.slice(oldpos,pos);oldpos=pos+1;var filename=dirkey.slice(oldpos);var conkey='cont'+dirkey.slice(3);var ref={dirent:dirkey,content:conkey,filename:filename,usage:usage,gameid:gameid};return ref;}
function file_load_dirent(dirent){if(typeof(dirent)!='object'){dirent=file_decode_ref(dirent);if(!dirent)
return null;}
var statstring=localStorage.getItem(dirent.dirent);if(!statstring)
return null;var file={dirent:dirent};var ix,pos,key,val;var ls=statstring.toString().split(',');for(ix=0;ix=0){localStorage.keys=localStorage.keys.without(key);localStorage.length=localStorage.keys.length;delete localStorage.data[key];}},key:function(index){return localStorage.keys[index];},clear:function(){localStorage.data={};localStorage.keys=[];localStorage.length=0;}}}
$(window).on('storage',evhan_storage_changed);return{open:dialog_open,file_construct_ref:file_construct_ref,file_ref_exists:file_ref_exists,file_remove_ref:file_remove_ref,file_write:file_write,file_read:file_read};}();Glk=function(){var VM=null;var has_canvas;var has_exited=false;var ui_disabled=false;var ui_specialinput=null;var ui_specialcallback=null;var event_generation=0;var current_partial_inputs=null;var current_partial_outputs=null;function init(vm_options){has_canvas=(document.createElement('canvas').getContext!=undefined);VM=vm_options.vm;if(window.GiDispa)
GiDispa.set_vm(VM);vm_options.accept=accept_ui_event;GlkOte.init(vm_options);}
function accept_ui_event(obj){var box;if(ui_disabled){qlog("### ui is disabled, ignoring event");return;}
if(obj.gen!=event_generation){GlkOte.log('Input event had wrong generation number: got '+obj.gen+', currently at '+event_generation);return;}
event_generation+=1;current_partial_inputs=obj.partial;switch(obj.type){case'init':content_metrics=obj.metrics;VM.init();break;case'external':if(obj.value=='timer'){handle_timer_input();}
break;case'hyperlink':handle_hyperlink_input(obj.window,obj.value);break;case'mouse':handle_mouse_input(obj.window,obj.x,obj.y);break;case'char':handle_char_input(obj.window,obj.value);break;case'line':handle_line_input(obj.window,obj.value,obj.terminator);break;case'arrange':content_metrics=obj.metrics;box={left:content_metrics.outspacingx,top:content_metrics.outspacingy,right:content_metrics.width-content_metrics.outspacingx,bottom:content_metrics.height-content_metrics.outspacingy};if(gli_rootwin)
gli_window_rearrange(gli_rootwin,box);handle_arrange_input();break;case'redraw':handle_redraw_input();break;case'specialresponse':if(obj.response=='fileref_prompt'){gli_fileref_create_by_prompt_callback(obj);}
break;}}
function handle_arrange_input(){if(!gli_selectref)
return;gli_selectref.set_field(0,Const.evtype_Arrange);gli_selectref.set_field(1,null);gli_selectref.set_field(2,0);gli_selectref.set_field(3,0);if(window.GiDispa)
GiDispa.prepare_resume(gli_selectref);gli_selectref=null;VM.resume();}
function handle_redraw_input(){if(!gli_selectref)
return;gli_selectref.set_field(0,Const.evtype_Redraw);gli_selectref.set_field(1,null);gli_selectref.set_field(2,0);gli_selectref.set_field(3,0);if(window.GiDispa)
GiDispa.prepare_resume(gli_selectref);gli_selectref=null;VM.resume();}
function handle_timer_input(){if(!gli_selectref)
return;gli_selectref.set_field(0,Const.evtype_Timer);gli_selectref.set_field(1,null);gli_selectref.set_field(2,0);gli_selectref.set_field(3,0);if(window.GiDispa)
GiDispa.prepare_resume(gli_selectref);gli_selectref=null;VM.resume();}
function handle_hyperlink_input(disprock,val){if(!gli_selectref)
return;var win=null;for(win=gli_windowlist;win;win=win.next){if(win.disprock==disprock)
break;}
if(!win||!win.hyperlink_request)
return;gli_selectref.set_field(0,Const.evtype_Hyperlink);gli_selectref.set_field(1,win);gli_selectref.set_field(2,val);gli_selectref.set_field(3,0);win.hyperlink_request=false;if(window.GiDispa)
GiDispa.prepare_resume(gli_selectref);gli_selectref=null;VM.resume();}
function handle_mouse_input(disprock,xpos,ypos){if(!gli_selectref)
return;var win=null;for(win=gli_windowlist;win;win=win.next){if(win.disprock==disprock)
break;}
if(!win||!win.mouse_request)
return;gli_selectref.set_field(0,Const.evtype_MouseInput);gli_selectref.set_field(1,win);gli_selectref.set_field(2,xpos);gli_selectref.set_field(3,ypos);win.mouse_request=false;if(window.GiDispa)
GiDispa.prepare_resume(gli_selectref);gli_selectref=null;VM.resume();}
function handle_char_input(disprock,input){var charval;if(!gli_selectref)
return;var win=null;for(win=gli_windowlist;win;win=win.next){if(win.disprock==disprock)
break;}
if(!win||!win.char_request)
return;if(input.length==1){charval=input.charCodeAt(0);if(!win.char_request_uni)
charval=charval&0xFF;}
else{charval=KeystrokeNameMap[input];if(!charval)
charval=Const.keycode_Unknown;}
gli_selectref.set_field(0,Const.evtype_CharInput);gli_selectref.set_field(1,win);gli_selectref.set_field(2,charval);gli_selectref.set_field(3,0);win.char_request=false;win.char_request_uni=false;win.input_generation=null;if(window.GiDispa)
GiDispa.prepare_resume(gli_selectref);gli_selectref=null;VM.resume();}
function handle_line_input(disprock,input,termkey){var ix;if(!gli_selectref)
return;var win=null;for(win=gli_windowlist;win;win=win.next){if(win.disprock==disprock)
break;}
if(!win||!win.line_request)
return;if(input.length>win.linebuf.length)
input=input.slice(0,win.linebuf.length);if(win.request_echo_line_input){ix=win.style;gli_set_style(win.str,Const.style_Input);gli_window_put_string(win,input);if(win.echostr)
glk_put_jstring_stream(win.echostr,input);gli_set_style(win.str,ix);gli_window_put_string(win,"\n");if(win.echostr)
glk_put_jstring_stream(win.echostr,"\n");}
for(ix=0;ix>10),0xDC00+(val&0x3FF));}}
function TrimArrayToBytes(arr){var ix,newarr;var len=arr.length;for(ix=0;ix=0x100)
break;}
if(ix==len){return arr;}
newarr=Array(len);for(ix=0;ix=0x100)
newarr[ix]=63;else
newarr[ix]=arr[ix];}
return newarr;}
function ByteArrayToString(arr){var ix,newarr;var len=arr.length;if(len==0)
return'';for(ix=0;ix=0x100)
break;}
if(ix==len){return String.fromCharCode.apply(this,arr);}
newarr=Array(len);for(ix=0;ix=0x10000)
break;}
if(ix==len){return String.fromCharCode.apply(this,arr);}
newarr=Array(len);for(ix=0;ix>10),0xDC00+(val&0x3FF));}}
return newarr.join('');}
function qlog(msg){if(window.console&&console.log)
console.log(msg);else if(window.opera&&opera.postError)
opera.postError(msg);}
function RefBox(){this.value=undefined;this.set_value=function(val){this.value=val;}
this.get_value=function(){return this.value;}}
function RefStruct(numels){this.fields=[];this.push_field=function(val){this.fields.push(val);}
this.set_field=function(pos,val){this.fields[pos]=val;}
this.get_field=function(pos){return this.fields[pos];}
this.get_fields=function(){return this.fields;}}
var DidNotReturn={dummy:'Glk call has not yet returned'};function call_may_not_return(id){if(id==0x001||id==0x0C0||id==0x062)
return true;else
return false;}
var strtype_File=1;var strtype_Window=2;var strtype_Memory=3;var strtype_Resource=4;var gli_windowlist=null;var gli_rootwin=null;var geometry_changed=true;var content_metrics=null;var gli_streamlist=null;var gli_filereflist=null;var gli_schannellist=null;var gli_currentstr=null;var gli_selectref=null;var gli_api_display_rocks=1;var gli_timer_interval=null;var gli_timer_id=null;var gli_timer_started=null;function gli_new_window(type,rock){var win={};win.type=type;win.rock=rock;win.disprock=undefined;win.parent=null;win.str=gli_stream_open_window(win);win.echostr=null;win.style=Const.style_Normal;win.hyperlink=0;win.input_generation=null;win.linebuf=null;win.char_request=false;win.line_request=false;win.char_request_uni=false;win.line_request_uni=false;win.hyperlink_request=false;win.mouse_request=false;win.echo_line_input=true;win.line_input_terminators=[];win.request_echo_line_input=null;win.prev=null;win.next=gli_windowlist;gli_windowlist=win;if(win.next)
win.next.prev=win;if(window.GiDispa)
GiDispa.class_register('window',win);else
win.disprock=gli_api_display_rocks++;geometry_changed=true;return win;}
function gli_delete_window(win){var prev,next;if(window.GiDispa)
GiDispa.class_unregister('window',win);geometry_changed=true;win.echostr=null;if(win.str){gli_delete_stream(win.str);win.str=null;}
prev=win.prev;next=win.next;win.prev=null;win.next=null;if(prev)
prev.next=next;else
gli_windowlist=next;if(next)
next.prev=prev;win.parent=null;win.rock=null;win.disprock=null;}
function gli_windows_unechostream(str){var win;for(win=gli_windowlist;win;win=win.next){if(win.echostr===str)
win.echostr=null;}}
function gli_window_put_string(win,val){var ix,ch;switch(win.type){case Const.wintype_TextBuffer:if(win.style!=win.accumstyle||win.hyperlink!=win.accumhyperlink)
gli_window_buffer_deaccumulate(win);win.accum.push(val);break;case Const.wintype_TextGrid:for(ix=0;ix=win.gridwidth){win.cursorx=0;win.cursory++;}
if(win.cursory<0)
win.cursory=0;else if(win.cursory>=win.gridheight)
break;if(ch=="\n"){win.cursory++;win.cursorx=0;continue;}
lineobj=win.lines[win.cursory];lineobj.dirty=true;lineobj.chars[win.cursorx]=ch;lineobj.styles[win.cursorx]=win.style;lineobj.hyperlinks[win.cursorx]=win.hyperlink;win.cursorx++;}
break;}}
function gli_window_grid_canonicalize(win){if(win.cursorx<0)
win.cursorx=0;else if(win.cursorx>=win.gridwidth){win.cursorx=0;win.cursory++;}
if(win.cursory<0)
win.cursory=0;else if(win.cursory>=win.gridheight)
return true;return false;}
function gli_window_buffer_deaccumulate(win){var conta=win.content;var stylename=StyleNameMap[win.accumstyle];var text,ls,ix,obj,arr;if(win.accum.length){text=win.accum.join('');ls=text.split('\n');for(ix=0;ixwin.gridheight){win.lines.length=win.gridheight;}
else if(oldheightwin.gridwidth){lineobj.dirty=true;lineobj.chars.length=win.gridwidth;lineobj.styles.length=win.gridwidth;lineobj.hyperlinks.length=win.gridwidth;}
else if(oldwidth=max){split=min;}
else{split=Math.min(Math.max(split,min),max-splitwid);}
win.pair_splitpos=split;win.pair_splitwidth=splitwid;if(win.pair_vertical){box1={left:win.bbox.left,right:win.pair_splitpos,top:win.bbox.top,bottom:win.bbox.bottom};box2={left:box1.right+win.pair_splitwidth,right:win.bbox.right,top:win.bbox.top,bottom:win.bbox.bottom};}
else{box1={top:win.bbox.top,bottom:win.pair_splitpos,left:win.bbox.left,right:win.bbox.right};box2={top:box1.bottom+win.pair_splitwidth,bottom:win.bbox.bottom,left:win.bbox.left,right:win.bbox.right};}
if(!win.pair_backward){ch1=win.child1;ch2=win.child2;}
else{ch1=win.child2;ch2=win.child1;}
gli_window_rearrange(ch1,box1);gli_window_rearrange(ch2,box2);break;}}
function gli_new_stream(type,readable,writable,rock){var str={};str.type=type;str.rock=rock;str.disprock=undefined;str.unicode=false;str.ref=null;str.win=null;str.file=null;str.buf=null;str.bufpos=0;str.buflen=0;str.bufeof=0;str.timer_id=null;str.flush_func=null;str.readcount=0;str.writecount=0;str.readable=readable;str.writable=writable;str.prev=null;str.next=gli_streamlist;gli_streamlist=str;if(str.next)
str.next.prev=str;if(window.GiDispa)
GiDispa.class_register('stream',str);return str;}
function gli_delete_stream(str){var prev,next;if(str===gli_currentstr){gli_currentstr=null;}
gli_windows_unechostream(str);if(str.type==strtype_Memory){if(window.GiDispa)
GiDispa.unretain_array(str.buf);}
if(window.GiDispa)
GiDispa.class_unregister('stream',str);prev=str.prev;next=str.next;str.prev=null;str.next=null;if(prev)
prev.next=next;else
gli_streamlist=next;if(next)
next.prev=prev;str.buf=null;str.readable=false;str.writable=false;str.ref=null;str.win=null;str.file=null;str.rock=null;str.disprock=null;}
function gli_stream_open_window(win){var str;str=gli_new_stream(strtype_Window,false,true,0);str.unicode=true;str.win=win;return str;}
function gli_stream_dirty_file(str){if(str.timer_id===null){if(str.flush_func===null){str.flush_func=function(){gli_stream_flush_file(str);};}
str.timer_id=setTimeout(str.flush_func,10000);}}
function gli_stream_flush_file(str){str.timer_id=null;Dialog.file_write(str.ref,str.buf);}
function gli_new_fileref(filename,usage,rock,ref){var fref={};fref.filename=filename;fref.rock=rock;fref.disprock=undefined;fref.textmode=((usage&Const.fileusage_TextMode)!=0);fref.filetype=(usage&Const.fileusage_TypeMask);fref.filetypename=FileTypeMap[fref.filetype];if(!fref.filetypename){fref.filetypename='xxx';}
if(!ref){var gameid='';if(fref.filetype==Const.fileusage_SavedGame)
gameid=VM.get_signature();ref=Dialog.file_construct_ref(fref.filename,fref.filetypename,gameid);}
fref.ref=ref;fref.prev=null;fref.next=gli_filereflist;gli_filereflist=fref;if(fref.next)
fref.next.prev=fref;if(window.GiDispa)
GiDispa.class_register('fileref',fref);return fref;}
function gli_delete_fileref(fref){var prev,next;if(window.GiDispa)
GiDispa.class_unregister('fileref',fref);prev=fref.prev;next=fref.next;fref.prev=null;fref.next=null;if(prev)
prev.next=next;else
gli_filereflist=next;if(next)
next.prev=prev;fref.filename=null;fref.ref=null;fref.rock=null;fref.disprock=null;}
function gli_put_char(str,ch){if(!str||!str.writable)
throw('gli_put_char: invalid stream');if(!str.unicode){if(ch<0||ch>=0x100)
ch=63;}
str.writecount+=1;switch(str.type){case strtype_File:gli_stream_dirty_file(str);case strtype_Memory:if(str.bufposstr.bufeof)
str.bufeof=str.bufpos;}
break;case strtype_Window:if(str.win.line_request)
throw('gli_put_char: window has pending line request');gli_window_put_string(str.win,CharToString(ch));if(str.win.echostr)
gli_put_char(str.win.echostr,ch);break;}}
function gli_put_array(str,arr,allbytes){var ix,len,val;if(!str||!str.writable)
throw('gli_put_array: invalid stream');if(!str.unicode&&!allbytes){arr=TrimArrayToBytes(arr);allbytes=true;}
str.writecount+=arr.length;switch(str.type){case strtype_File:gli_stream_dirty_file(str);case strtype_Memory:len=arr.length;if(len>str.buflen-str.bufpos)
len=str.buflen-str.bufpos;for(ix=0;ixstr.bufeof)
str.bufeof=str.bufpos;break;case strtype_Window:if(str.win.line_request)
throw('gli_put_array: window has pending line request');if(allbytes)
val=String.fromCharCode.apply(this,arr);else
val=UniArrayToString(arr);gli_window_put_string(str.win,val);if(str.win.echostr)
gli_put_array(str.win.echostr,arr,allbytes);break;}}
function gli_get_char(str,want_unicode){var ch;if(!str||!str.readable)
return-1;switch(str.type){case strtype_Resource:if(str.unicode){if(str.isbinary){if(str.bufpos>=str.bufeof)
return-1;ch=str.buf[str.bufpos];str.bufpos++;if(str.bufpos>=str.bufeof)
return-1;ch=(ch<<8)|(str.buf[str.bufpos]&0xFF);str.bufpos++;if(str.bufpos>=str.bufeof)
return-1;ch=(ch<<8)|(str.buf[str.bufpos]&0xFF);str.bufpos++;if(str.bufpos>=str.bufeof)
return-1;ch=(ch<<8)|(str.buf[str.bufpos]&0xFF);str.bufpos++;}
else{var val0,val1,val2,val3;if(str.bufpos>=str.bufeof)
return-1;val0=str.buf[str.bufpos];str.bufpos++;if(val0<0x80){ch=val0;}
else{if(str.bufpos>=str.bufeof)
return-1;val1=str.buf[str.bufpos];str.bufpos++;if((val1&0xC0)!=0x80)
return-1;if((val0&0xE0)==0xC0){ch=(val0&0x1F)<<6;ch|=(val1&0x3F);}
else{if(str.bufpos>=str.bufeof)
return-1;val2=str.buf[str.bufpos];str.bufpos++;if((val2&0xC0)!=0x80)
return-1;if((val0&0xF0)==0xE0){ch=(((val0&0xF)<<12)&0x0000F000);ch|=(((val1&0x3F)<<6)&0x00000FC0);ch|=(((val2&0x3F))&0x0000003F);}
else if((val0&0xF0)==0xF0){if(str.bufpos>=str.bufeof)
return-1;val3=str.buf[str.bufpos];str.bufpos++;if((val3&0xC0)!=0x80)
return-1;ch=(((val0&0x7)<<18)&0x1C0000);ch|=(((val1&0x3F)<<12)&0x03F000);ch|=(((val2&0x3F)<<6)&0x000FC0);ch|=(((val3&0x3F))&0x00003F);}
else{return-1;}}}}
str.readcount++;ch>>>=0;if(!want_unicode&&ch>=0x100)
return 63;return ch;}
case strtype_File:case strtype_Memory:if(str.bufpos=0x100)
return 63;return ch;}
else{return-1;}
default:return-1;}}
function gli_get_line(str,buf,want_unicode){if(!str||!str.readable)
return 0;var len=buf.length;var gotnewline;switch(str.type){case strtype_Resource:if(str.unicode){if(len==0)
return 0;len-=1;gotnewline=false;for(lx=0;lx=str.bufeof){len=0;}
else{if(str.bufpos+len>str.bufeof){len=str.bufeof-str.bufpos;}}
gotnewline=false;if(!want_unicode){for(lx=0;lx=0x100)
ch=63;buf[lx]=ch;gotnewline=(ch==10);}}
else{for(lx=0;lx=str.bufeof){len=0;}
else{if(str.bufpos+len>str.bufeof){len=str.bufeof-str.bufpos;}}
if(!want_unicode){for(lx=0;lx=0x100)
ch=63;buf[lx]=ch;}}
else{for(lx=0;lxstr.buflen-str.bufpos)
len=str.buflen-str.bufpos;if(str.unicode||allbytes){for(ix=0;ix=0x100)
ch=63;str.buf[str.bufpos+ix]=ch;}}
str.bufpos+=len;if(str.bufpos>str.bufeof)
str.bufeof=str.bufpos;break;case strtype_Window:if(str.win.line_request)
throw('glk_put_jstring: window has pending line request');gli_window_put_string(str.win,val);if(str.win.echostr)
glk_put_jstring_stream(str.win.echostr,val,allbytes);break;}}
function gli_set_style(str,val){if(!str||!str.writable)
throw('gli_set_style: invalid stream');if(val>=Const.style_NUMSTYLES)
val=0;if(str.type==strtype_Window){str.win.style=val;if(str.win.echostr)
gli_set_style(str.win.echostr,val);}}
function gli_set_hyperlink(str,val){if(!str||!str.writable)
throw('gli_set_hyperlink: invalid stream');if(str.type==strtype_Window){str.win.hyperlink=val;if(str.win.echostr)
gli_set_hyperlink(str.win.echostr,val);}}
function gli_timer_callback(){if(ui_disabled){if(has_exited){GlkOte.log("### dropping timer event...");gli_timer_id=null;return;}
else{GlkOte.log("### procrastinating timer event...");gli_timer_id=setTimeout(gli_timer_callback,500);return;}}
gli_timer_id=setTimeout(gli_timer_callback,gli_timer_interval);gli_timer_started=Date.now();GlkOte.extevent('timer');}
function glk_exit(){has_exited=true;ui_disabled=true;gli_selectref=null;return DidNotReturn;}
function glk_tick(){}
function glk_gestalt(sel,val){return glk_gestalt_ext(sel,val,null);}
function glk_gestalt_ext(sel,val,arr){switch(sel){case 0:return 0x00000704;case 1:if(val<=Const.keycode_Left&&val>=Const.keycode_End)
return 1;if(val>=0x100000000-Const.keycode_MAXVAL)
return 0;if(val>0x10FFFF)
return 0;if((val>=0&&val<32)||(val>=127&&val<160))
return 0;return 1;case 2:if(val>0x10FFFF)
return 0;if((val>=0&&val<32)||(val>=127&&val<160))
return 0;return 1;case 3:if((val>0x10FFFF)||(val>=0&&val<32)||(val>=127&&val<160)){if(arr)
arr[0]=1;return 0;}
if(arr)
arr[0]=1;return 2;case 4:if(val==Const.wintype_TextBuffer)
return 1;if(val==Const.wintype_Graphics&&has_canvas)
return 1;return 0;case 5:return 1;case 6:return 1;case 7:if(val==Const.wintype_TextBuffer)
return 1;if(val==Const.wintype_Graphics&&has_canvas)
return 1;return 0;case 8:return 0;case 9:return 0;case 10:return 0;case 11:return 1;case 12:if(val==3||val==4)
return 1;else
return 0;case 13:return 0;case 14:return 1;case 15:return 1;case 16:return 1;case 17:return 1;case 18:return 1;case 19:if(val==Const.keycode_Escape)
return 1;if(val>=Const.keycode_Func12&&val<=Const.keycode_Func1)
return 1;return 0;case 20:return 1;case 21:return 0;case 22:return 1;case 23:return 0;}
return 0;}
function glk_window_iterate(win,rockref){if(!win)
win=gli_windowlist;else
win=win.next;if(win){if(rockref)
rockref.set_value(win.rock);return win;}
if(rockref)
rockref.set_value(0);return null;}
function glk_window_get_rock(win){if(!win)
throw('glk_window_get_rock: invalid window');return win.rock;}
function glk_window_get_root(){return gli_rootwin;}
function glk_window_open(splitwin,method,size,wintype,rock){var oldparent,box,val;var pairwin,newwin;if(!gli_rootwin){if(splitwin)
throw('glk_window_open: splitwin must be null for first window');oldparent=null;box={left:content_metrics.outspacingx,top:content_metrics.outspacingy,right:content_metrics.width-content_metrics.outspacingx,bottom:content_metrics.height-content_metrics.outspacingy};}
else{if(!splitwin)
throw('glk_window_open: splitwin must not be null');val=(method&Const.winmethod_DivisionMask);if(val!=Const.winmethod_Fixed&&val!=Const.winmethod_Proportional)
throw('glk_window_open: invalid method (not fixed or proportional)');val=(method&Const.winmethod_DirMask);if(val!=Const.winmethod_Above&&val!=Const.winmethod_Below&&val!=Const.winmethod_Left&&val!=Const.winmethod_Right)
throw('glk_window_open: invalid method (bad direction)');box=splitwin.bbox;oldparent=splitwin.parent;if(oldparent&&oldparent.type!=Const.wintype_Pair)
throw('glk_window_open: parent window is not Pair');}
newwin=gli_new_window(wintype,rock);switch(newwin.type){case Const.wintype_TextBuffer:newwin.accum=[];newwin.accumstyle=null;newwin.accumhyperlink=0;newwin.content=[];newwin.clearcontent=false;break;case Const.wintype_TextGrid:newwin.gridwidth=0;newwin.gridheight=0;newwin.lines=[];newwin.cursorx=0;newwin.cursory=0;break;case Const.wintype_Graphics:if(!has_canvas){gli_delete_window(newwin);return null;}
newwin.content=[];break;case Const.wintype_Blank:break;case Const.wintype_Pair:throw('glk_window_open: cannot open pair window directly')
default:gli_delete_window(newwin);return null;}
if(!splitwin){gli_rootwin=newwin;gli_window_rearrange(newwin,box);}
else{pairwin=gli_new_window(Const.wintype_Pair,0);pairwin.pair_dir=method&Const.winmethod_DirMask;pairwin.pair_division=method&Const.winmethod_DivisionMask;pairwin.pair_key=newwin;pairwin.pair_keydamage=false;pairwin.pair_size=size;pairwin.pair_hasborder=((method&Const.winmethod_BorderMask)==Const.winmethod_Border);pairwin.pair_vertical=(pairwin.pair_dir==Const.winmethod_Left||pairwin.pair_dir==Const.winmethod_Right);pairwin.pair_backward=(pairwin.pair_dir==Const.winmethod_Left||pairwin.pair_dir==Const.winmethod_Above);pairwin.child1=splitwin;pairwin.child2=newwin;splitwin.parent=pairwin;newwin.parent=pairwin;pairwin.parent=oldparent;if(oldparent){if(oldparent.child1==splitwin)
oldparent.child1=pairwin;else
oldparent.child2=pairwin;}
else{gli_rootwin=pairwin;}
gli_window_rearrange(pairwin,box);}
return newwin;}
function glk_window_close(win,statsref){if(!win)
throw('glk_window_close: invalid window');if(win===gli_rootwin||!win.parent){gli_rootwin=null;gli_stream_fill_result(win.str,statsref);gli_window_close(win,true);}
else{var pairwin,grandparwin,sibwin,box,wx,keydamage_flag;pairwin=win.parent;if(win===pairwin.child1)
sibwin=pairwin.child2;else if(win===pairwin.child2)
sibwin=pairwin.child1;else
throw('glk_window_close: window tree is corrupted');box=pairwin.bbox;grandparwin=pairwin.parent;if(!grandparwin){gli_rootwin=sibwin;sibwin.parent=null;}
else{if(grandparwin.child1===pairwin)
grandparwin.child1=sibwin;else
grandparwin.child2=sibwin;sibwin.parent=grandparwin;}
gli_stream_fill_result(win.str,statsref);gli_window_close(win,true);if(win===pairwin.child1){pairwin.child1=null;}
else if(win===pairwin.child2){pairwin.child2=null;}
gli_window_close(pairwin,false);keydamage_flag=false;for(wx=sibwin;wx;wx=wx.parent){if(wx.type==Const.wintype_Pair){if(wx.pair_keydamage){keydamage_flag=true;wx.pair_keydamage=false;}}}
if(keydamage_flag){box=content_box;gli_window_rearrange(gli_rootwin,box);}
else{gli_window_rearrange(sibwin,box);}}}
function glk_window_get_size(win,widthref,heightref){if(!win)
throw('glk_window_get_size: invalid window');var wid=0;var hgt=0;var boxwidth,boxheight;switch(win.type){case Const.wintype_TextGrid:boxwidth=win.bbox.right-win.bbox.left;boxheight=win.bbox.bottom-win.bbox.top;wid=Math.max(0,Math.floor((boxwidth-content_metrics.gridmarginx)/content_metrics.gridcharwidth));hgt=Math.max(0,Math.floor((boxheight-content_metrics.gridmarginy)/content_metrics.gridcharheight));break;case Const.wintype_TextBuffer:boxwidth=win.bbox.right-win.bbox.left;boxheight=win.bbox.bottom-win.bbox.top;wid=Math.max(0,Math.floor((boxwidth-content_metrics.buffermarginx)/content_metrics.buffercharwidth));hgt=Math.max(0,Math.floor((boxheight-content_metrics.buffermarginy)/content_metrics.buffercharheight));break;case Const.wintype_Graphics:boxwidth=win.bbox.right-win.bbox.left;boxheight=win.bbox.bottom-win.bbox.top;wid=boxwidth-content_metrics.graphicsmarginx;hgt=boxheight-content_metrics.graphicsmarginy;break;}
if(widthref)
widthref.set_value(wid);if(heightref)
heightref.set_value(hgt);}
function glk_window_set_arrangement(win,method,size,keywin){var wx,newdir,newvertical,newbackward;if(!win)
throw('glk_window_set_arrangement: invalid window');if(win.type!=Const.wintype_Pair)
throw('glk_window_set_arrangement: not a pair window');if(keywin){if(keywin.type==Const.wintype_Pair)
throw('glk_window_set_arrangement: keywin cannot be a pair window');for(wx=keywin;wx;wx=wx.parent){if(wx==win)
break;}
if(!wx)
throw('glk_window_set_arrangement: keywin must be a descendant');}
newdir=method&Const.winmethod_DirMask;newvertical=(newdir==Const.winmethod_Left||newdir==Const.winmethod_Right);newbackward=(newdir==Const.winmethod_Left||newdir==Const.winmethod_Above);if(!keywin)
keywin=win.pair_key;if(newvertical&&!win.pair_vertical)
throw('glk_window_set_arrangement: split must stay horizontal');if(!newvertical&&win.pair_vertical)
throw('glk_window_set_arrangement: split must stay vertical');if(keywin&&keywin.type==Const.wintype_Blank&&(method&Const.winmethod_DivisionMask)==Const.winmethod_Fixed)
throw('glk_window_set_arrangement: a blank window cannot have a fixed size');if((newbackward&&!win.pair_backward)||(!newbackward&&win.pair_backward)){wx=win.child1;win.child1=win.child2;win.child2=wx;}
win.pair_dir=newdir;win.pair_division=(method&Const.winmethod_DivisionMask);win.pair_key=keywin;win.pair_size=size;win.pair_hasborder=((method&Const.winmethod_BorderMask)==Const.winmethod_Border);win.pair_vertical=(win.pair_dir==Const.winmethod_Left||win.pair_dir==Const.winmethod_Right);win.pair_backward=(win.pair_dir==Const.winmethod_Left||win.pair_dir==Const.winmethod_Above);gli_window_rearrange(win,win.bbox);}
function glk_window_get_arrangement(win,methodref,sizeref,keywinref){if(!win)
throw('glk_window_get_arrangement: invalid window');if(win.type!=Const.wintype_Pair)
throw('glk_window_get_arrangement: not a pair window');if(sizeref)
sizeref.set_value(win.pair_size);if(keywinref)
keywinref.set_value(win.pair_key);if(methodref)
methodref.set_value(win.pair_dir|win.pair_division|(win.pair_hasborder?Const.winmethod_Border:Const.winmethod_NoBorder));}
function glk_window_get_type(win){if(!win)
throw('glk_window_get_type: invalid window');return win.type;}
function glk_window_get_parent(win){if(!win)
throw('glk_window_get_parent: invalid window');return win.parent;}
function glk_window_clear(win){var ix,cx,lineobj;if(!win)
throw('glk_window_clear: invalid window');if(win.line_request){throw('glk_window_clear: window has pending line request');}
switch(win.type){case Const.wintype_TextBuffer:win.accum.length=0;win.accumstyle=null;win.accumhyperlink=0;win.content.length=0;win.clearcontent=true;break;case Const.wintype_TextGrid:win.cursorx=0;win.cursory=0;for(ix=0;ixstr.bufeof)
pos=str.bufeof;str.bufpos=pos;}}
function glk_stream_get_position(str){if(!str)
throw('glk_stream_get_position: invalid stream');switch(str.type){case strtype_File:case strtype_Resource:case strtype_Memory:return str.bufpos;default:return 0;}}
function glk_stream_set_current(str){gli_currentstr=str;}
function glk_stream_get_current(){return gli_currentstr;}
function glk_fileref_create_temp(usage,rock){var timestamp=new Date().getTime();var filename="_temp_"+timestamp+"_"+Math.random();filename=filename.replace('.','');fref=gli_new_fileref(filename,usage,rock,null);return fref;}
function glk_fileref_create_by_name(usage,filename,rock){fref=gli_new_fileref(filename,usage,rock,null);return fref;}
function glk_fileref_create_by_prompt(usage,fmode,rock){var modename;var filetype=(usage&Const.fileusage_TypeMask);var filetypename=FileTypeMap[filetype];if(!filetypename){filetypename='xxx';}
switch(fmode){case Const.filemode_Write:modename='write';break;case Const.filemode_ReadWrite:modename='readwrite';break;case Const.filemode_WriteAppend:modename='writeappend';break;case Const.filemode_Read:default:modename='read';break;}
var special={type:'fileref_prompt',filetype:filetypename,filemode:modename};var callback={usage:usage,rock:rock};if(filetype==Const.fileusage_SavedGame)
special.gameid=VM.get_signature();ui_specialinput=special;ui_specialcallback=callback;gli_selectref=null;return DidNotReturn;}
function gli_fileref_create_by_prompt_callback(obj){var ref=obj.value;var usage=ui_specialcallback.usage;var rock=ui_specialcallback.rock;var fref=null;if(ref){fref=gli_new_fileref(ref.filename,usage,rock,ref);}
ui_specialinput=null;ui_specialcallback=null;if(window.GiDispa)
GiDispa.prepare_resume(fref);VM.resume();}
function glk_fileref_destroy(fref){if(!fref)
throw('glk_fileref_destroy: invalid fileref');gli_delete_fileref(fref);}
function glk_fileref_iterate(fref,rockref){if(!fref)
fref=gli_filereflist;else
fref=fref.next;if(fref){if(rockref)
rockref.set_value(fref.rock);return fref;}
if(rockref)
rockref.set_value(0);return null;}
function glk_fileref_get_rock(fref){if(!fref)
throw('glk_fileref_get_rock: invalid fileref');return fref.rock;}
function glk_fileref_delete_file(fref){if(!fref)
throw('glk_fileref_delete_file: invalid fileref');Dialog.file_remove_ref(fref.ref);}
function glk_fileref_does_file_exist(fref){if(!fref)
throw('glk_fileref_does_file_exist: invalid fileref');if(Dialog.file_ref_exists(fref.ref))
return 1;else
return 0;}
function glk_fileref_create_from_fileref(usage,oldfref,rock){if(!oldfref)
throw('glk_fileref_create_from_fileref: invalid fileref');var fref=gli_new_fileref(oldfref.filename,usage,rock,null);return fref;}
function glk_put_char(ch){gli_put_char(gli_currentstr,ch&0xFF);}
function glk_put_char_stream(str,ch){gli_put_char(str,ch&0xFF);}
function glk_put_string(val){glk_put_jstring_stream(gli_currentstr,val,true);}
function glk_put_string_stream(str,val){glk_put_jstring_stream(str,val,true);}
function glk_put_buffer(arr){arr=TrimArrayToBytes(arr);gli_put_array(gli_currentstr,arr,true);}
function glk_put_buffer_stream(str,arr){arr=TrimArrayToBytes(arr);gli_put_array(str,arr,true);}
function glk_set_style(val){gli_set_style(gli_currentstr,val);}
function glk_set_style_stream(str,val){gli_set_style(str,val);}
function glk_get_char_stream(str){if(!str)
throw('glk_get_char_stream: invalid stream');return gli_get_char(str,false);}
function glk_get_line_stream(str,buf){if(!str)
throw('glk_get_line_stream: invalid stream');return gli_get_line(str,buf,false);}
function glk_get_buffer_stream(str,buf){if(!str)
throw('glk_get_buffer_stream: invalid stream');return gli_get_buffer(str,buf,false);}
function glk_char_to_lower(val){if(val>=0x41&&val<=0x5A)
return val+0x20;if(val>=0xC0&&val<=0xDE&&val!=0xD7)
return val+0x20;return val;}
function glk_char_to_upper(val){if(val>=0x61&&val<=0x7A)
return val-0x20;if(val>=0xE0&&val<=0xFE&&val!=0xF7)
return val-0x20;return val;}
function glk_stylehint_set(wintype,styl,hint,value){}
function glk_stylehint_clear(wintype,styl,hint){}
function glk_style_distinguish(win,styl1,styl2){return 0;}
function glk_style_measure(win,styl,hint,resultref){if(resultref)
resultref.set_value(0);return 0;}
function glk_select(eventref){gli_selectref=eventref;return DidNotReturn;}
function glk_select_poll(eventref){eventref.set_field(0,Const.evtype_None);eventref.set_field(1,null);eventref.set_field(2,0);eventref.set_field(3,0);if(gli_timer_interval&&!(gli_timer_id===null)){var now=Date.now();if(now-gli_timer_started>gli_timer_interval){clearTimeout(gli_timer_id);gli_timer_id=setTimeout(gli_timer_callback,gli_timer_interval);gli_timer_started=Date.now();eventref.set_field(0,Const.evtype_Timer);}}}
function glk_request_line_event(win,buf,initlen){if(!win)
throw('glk_request_line_event: invalid window');if(win.char_request||win.line_request)
throw('glk_request_line_event: window already has keyboard request');if(win.type==Const.wintype_TextBuffer||win.type==Const.wintype_TextGrid){if(initlen){var ls=buf.slice(0,initlen);if(!current_partial_outputs)
current_partial_outputs={};current_partial_outputs[win.disprock]=ByteArrayToString(ls);}
win.line_request=true;win.line_request_uni=false;if(win.type==Const.wintype_TextBuffer)
win.request_echo_line_input=win.echo_line_input;else
win.request_echo_line_input=true;win.input_generation=event_generation;win.linebuf=buf;if(window.GiDispa)
GiDispa.retain_array(buf);}
else{throw('glk_request_line_event: window does not support keyboard input');}}
function glk_cancel_line_event(win,eventref){if(!win)
throw('glk_cancel_line_event: invalid window');if(!win.line_request){if(eventref){eventref.set_field(0,Const.evtype_None);eventref.set_field(1,null);eventref.set_field(2,0);eventref.set_field(3,0);}
return;}
var input="";var ix,val;if(current_partial_inputs){val=current_partial_inputs[win.disprock];if(val)
input=val;}
if(input.length>win.linebuf.length)
input=input.slice(0,win.linebuf.length);if(win.request_echo_line_input){ix=win.style;gli_set_style(win.str,Const.style_Input);gli_window_put_string(win,input);if(win.echostr)
glk_put_jstring_stream(win.echostr,input);gli_set_style(win.str,ix);gli_window_put_string(win,"\n");if(win.echostr)
glk_put_jstring_stream(win.echostr,"\n");}
for(ix=0;ix=pos)
break;grpstart=ix;while(ix=2){for(jx=grpend-1;jx>grpstart;jx--){for(kx=grpstart;kxunicode_combin_table[arr[kx+1]]){tmp=arr[kx];arr[kx]=arr[kx+1];arr[kx+1]=tmp;}}}}}
return pos;}
function gli_buffer_canon_compose_uni(arr,numchars){var ix,jx,curch,newch,curclass,newclass,map,pos;if(numchars==0)
return 0;pos=0;curch=arr[0];curclass=unicode_combin_table[curch];if(curclass)
curclass=999;ix=1;jx=ix;while(true){if(jx>=numchars){arr[pos]=curch;pos=ix;break;}
newch=arr[jx];newclass=unicode_combin_table[newch];map=unicode_compo_table[curch];if(map!==undefined&&map[newch]!==undefined&&(!curclass||(newclass&&curclass>>0);usec=Math.floor((now%1000)*1000);if(usec<0)
usec=1000000+usec;timevalref.set_field(2,usec);}
function glk_current_simple_time(factor){var now=new Date().getTime();return Math.floor(now/(factor*1000));}
function glk_time_to_date_utc(timevalref,dateref){var now=timevalref.get_field(0)*4294967296000+timevalref.get_field(1)*1000+timevalref.get_field(2)/1000;var obj=new Date(now);dateref.set_field(0,obj.getUTCFullYear())
dateref.set_field(1,1+obj.getUTCMonth())
dateref.set_field(2,obj.getUTCDate())
dateref.set_field(3,obj.getUTCDay())
dateref.set_field(4,obj.getUTCHours())
dateref.set_field(5,obj.getUTCMinutes())
dateref.set_field(6,obj.getUTCSeconds())
dateref.set_field(7,1000*obj.getUTCMilliseconds())}
function glk_time_to_date_local(timevalref,dateref){var now=timevalref.get_field(0)*4294967296000+timevalref.get_field(1)*1000+timevalref.get_field(2)/1000;var obj=new Date(now);dateref.set_field(0,obj.getFullYear())
dateref.set_field(1,1+obj.getMonth())
dateref.set_field(2,obj.getDate())
dateref.set_field(3,obj.getDay())
dateref.set_field(4,obj.getHours())
dateref.set_field(5,obj.getMinutes())
dateref.set_field(6,obj.getSeconds())
dateref.set_field(7,1000*obj.getMilliseconds())}
function glk_simple_time_to_date_utc(time,factor,dateref){var now=time*(1000*factor);var obj=new Date(now);dateref.set_field(0,obj.getUTCFullYear())
dateref.set_field(1,1+obj.getUTCMonth())
dateref.set_field(2,obj.getUTCDate())
dateref.set_field(3,obj.getUTCDay())
dateref.set_field(4,obj.getUTCHours())
dateref.set_field(5,obj.getUTCMinutes())
dateref.set_field(6,obj.getUTCSeconds())
dateref.set_field(7,1000*obj.getUTCMilliseconds())}
function glk_simple_time_to_date_local(time,factor,dateref){var now=time*(1000*factor);var obj=new Date(now);dateref.set_field(0,obj.getFullYear())
dateref.set_field(1,1+obj.getMonth())
dateref.set_field(2,obj.getDate())
dateref.set_field(3,obj.getDay())
dateref.set_field(4,obj.getHours())
dateref.set_field(5,obj.getMinutes())
dateref.set_field(6,obj.getSeconds())
dateref.set_field(7,1000*obj.getMilliseconds())}
function glk_date_to_time_utc(dateref,timevalref){var obj=new Date(0);obj.setUTCFullYear(dateref.get_field(0));obj.setUTCMonth(dateref.get_field(1)-1);obj.setUTCDate(dateref.get_field(2));obj.setUTCHours(dateref.get_field(4));obj.setUTCMinutes(dateref.get_field(5));obj.setUTCSeconds(dateref.get_field(6));obj.setUTCMilliseconds(dateref.get_field(7)/1000);var now=obj.getTime();var usec;timevalref.set_field(0,Math.floor(now/4294967296000));timevalref.set_field(1,Math.floor(now/1000)>>>0);usec=Math.floor((now%1000)*1000);if(usec<0)
usec=1000000+usec;timevalref.set_field(2,usec);}
function glk_date_to_time_local(dateref,timevalref){var obj=new Date(dateref.get_field(0),dateref.get_field(1)-1,dateref.get_field(2),dateref.get_field(4),dateref.get_field(5),dateref.get_field(6),dateref.get_field(7)/1000);var now=obj.getTime();var usec;timevalref.set_field(0,Math.floor(now/4294967296000));timevalref.set_field(1,Math.floor(now/1000)>>>0);usec=Math.floor((now%1000)*1000);if(usec<0)
usec=1000000+usec;timevalref.set_field(2,usec);}
function glk_date_to_simple_time_utc(dateref,factor){var obj=new Date(0);obj.setUTCFullYear(dateref.get_field(0));obj.setUTCMonth(dateref.get_field(1)-1);obj.setUTCDate(dateref.get_field(2));obj.setUTCHours(dateref.get_field(4));obj.setUTCMinutes(dateref.get_field(5));obj.setUTCSeconds(dateref.get_field(6));obj.setUTCMilliseconds(dateref.get_field(7)/1000);var now=obj.getTime();return Math.floor(now/(factor*1000));}
function glk_date_to_simple_time_local(dateref,factor){var obj=new Date(dateref.get_field(0),dateref.get_field(1)-1,dateref.get_field(2),dateref.get_field(4),dateref.get_field(5),dateref.get_field(6),dateref.get_field(7)/1000);var now=obj.getTime();return Math.floor(now/(factor*1000));}
return{version:'2.1.1',init:init,update:update,fatal_error:fatal_error,byte_array_to_string:ByteArrayToString,uni_array_to_string:UniArrayToString,Const:Const,RefBox:RefBox,RefStruct:RefStruct,DidNotReturn:DidNotReturn,call_may_not_return:call_may_not_return,glk_put_jstring:glk_put_jstring,glk_put_jstring_stream:glk_put_jstring_stream,glk_exit:glk_exit,glk_tick:glk_tick,glk_gestalt:glk_gestalt,glk_gestalt_ext:glk_gestalt_ext,glk_window_iterate:glk_window_iterate,glk_window_get_rock:glk_window_get_rock,glk_window_get_root:glk_window_get_root,glk_window_open:glk_window_open,glk_window_close:glk_window_close,glk_window_get_size:glk_window_get_size,glk_window_set_arrangement:glk_window_set_arrangement,glk_window_get_arrangement:glk_window_get_arrangement,glk_window_get_type:glk_window_get_type,glk_window_get_parent:glk_window_get_parent,glk_window_clear:glk_window_clear,glk_window_move_cursor:glk_window_move_cursor,glk_window_get_stream:glk_window_get_stream,glk_window_set_echo_stream:glk_window_set_echo_stream,glk_window_get_echo_stream:glk_window_get_echo_stream,glk_set_window:glk_set_window,glk_window_get_sibling:glk_window_get_sibling,glk_stream_iterate:glk_stream_iterate,glk_stream_get_rock:glk_stream_get_rock,glk_stream_open_file:glk_stream_open_file,glk_stream_open_memory:glk_stream_open_memory,glk_stream_close:glk_stream_close,glk_stream_set_position:glk_stream_set_position,glk_stream_get_position:glk_stream_get_position,glk_stream_set_current:glk_stream_set_current,glk_stream_get_current:glk_stream_get_current,glk_fileref_create_temp:glk_fileref_create_temp,glk_fileref_create_by_name:glk_fileref_create_by_name,glk_fileref_create_by_prompt:glk_fileref_create_by_prompt,glk_fileref_destroy:glk_fileref_destroy,glk_fileref_iterate:glk_fileref_iterate,glk_fileref_get_rock:glk_fileref_get_rock,glk_fileref_delete_file:glk_fileref_delete_file,glk_fileref_does_file_exist:glk_fileref_does_file_exist,glk_fileref_create_from_fileref:glk_fileref_create_from_fileref,glk_put_char:glk_put_char,glk_put_char_stream:glk_put_char_stream,glk_put_string:glk_put_string,glk_put_string_stream:glk_put_string_stream,glk_put_buffer:glk_put_buffer,glk_put_buffer_stream:glk_put_buffer_stream,glk_set_style:glk_set_style,glk_set_style_stream:glk_set_style_stream,glk_get_char_stream:glk_get_char_stream,glk_get_line_stream:glk_get_line_stream,glk_get_buffer_stream:glk_get_buffer_stream,glk_char_to_lower:glk_char_to_lower,glk_char_to_upper:glk_char_to_upper,glk_stylehint_set:glk_stylehint_set,glk_stylehint_clear:glk_stylehint_clear,glk_style_distinguish:glk_style_distinguish,glk_style_measure:glk_style_measure,glk_select:glk_select,glk_select_poll:glk_select_poll,glk_request_line_event:glk_request_line_event,glk_cancel_line_event:glk_cancel_line_event,glk_request_char_event:glk_request_char_event,glk_cancel_char_event:glk_cancel_char_event,glk_request_mouse_event:glk_request_mouse_event,glk_cancel_mouse_event:glk_cancel_mouse_event,glk_request_timer_events:glk_request_timer_events,glk_image_get_info:glk_image_get_info,glk_image_draw:glk_image_draw,glk_image_draw_scaled:glk_image_draw_scaled,glk_window_flow_break:glk_window_flow_break,glk_window_erase_rect:glk_window_erase_rect,glk_window_fill_rect:glk_window_fill_rect,glk_window_set_background_color:glk_window_set_background_color,glk_schannel_iterate:glk_schannel_iterate,glk_schannel_get_rock:glk_schannel_get_rock,glk_schannel_create:glk_schannel_create,glk_schannel_destroy:glk_schannel_destroy,glk_schannel_play:glk_schannel_play,glk_schannel_play_ext:glk_schannel_play_ext,glk_schannel_stop:glk_schannel_stop,glk_schannel_set_volume:glk_schannel_set_volume,glk_schannel_create_ext:glk_schannel_create_ext,glk_schannel_play_multi:glk_schannel_play_multi,glk_schannel_pause:glk_schannel_pause,glk_schannel_unpause:glk_schannel_unpause,glk_schannel_set_volume_ext:glk_schannel_set_volume_ext,glk_sound_load_hint:glk_sound_load_hint,glk_set_hyperlink:glk_set_hyperlink,glk_set_hyperlink_stream:glk_set_hyperlink_stream,glk_request_hyperlink_event:glk_request_hyperlink_event,glk_cancel_hyperlink_event:glk_cancel_hyperlink_event,glk_buffer_to_lower_case_uni:glk_buffer_to_lower_case_uni,glk_buffer_to_upper_case_uni:glk_buffer_to_upper_case_uni,glk_buffer_to_title_case_uni:glk_buffer_to_title_case_uni,glk_buffer_canon_decompose_uni:glk_buffer_canon_decompose_uni,glk_buffer_canon_normalize_uni:glk_buffer_canon_normalize_uni,glk_put_char_uni:glk_put_char_uni,glk_put_string_uni:glk_put_string_uni,glk_put_buffer_uni:glk_put_buffer_uni,glk_put_char_stream_uni:glk_put_char_stream_uni,glk_put_string_stream_uni:glk_put_string_stream_uni,glk_put_buffer_stream_uni:glk_put_buffer_stream_uni,glk_get_char_stream_uni:glk_get_char_stream_uni,glk_get_buffer_stream_uni:glk_get_buffer_stream_uni,glk_get_line_stream_uni:glk_get_line_stream_uni,glk_stream_open_file_uni:glk_stream_open_file_uni,glk_stream_open_memory_uni:glk_stream_open_memory_uni,glk_request_char_event_uni:glk_request_char_event_uni,glk_request_line_event_uni:glk_request_line_event_uni,glk_set_echo_line_event:glk_set_echo_line_event,glk_set_terminators_line_event:glk_set_terminators_line_event,glk_current_time:glk_current_time,glk_current_simple_time:glk_current_simple_time,glk_time_to_date_utc:glk_time_to_date_utc,glk_time_to_date_local:glk_time_to_date_local,glk_simple_time_to_date_utc:glk_simple_time_to_date_utc,glk_simple_time_to_date_local:glk_simple_time_to_date_local,glk_date_to_time_utc:glk_date_to_time_utc,glk_date_to_time_local:glk_date_to_time_local,glk_date_to_simple_time_utc:glk_date_to_simple_time_utc,glk_date_to_simple_time_local:glk_date_to_simple_time_local,glk_stream_open_resource:glk_stream_open_resource,glk_stream_open_resource_uni:glk_stream_open_resource_uni};}();