#Energy.bas
#run_and_exit
# Copyright (C) 2003 Bill St. Clair
# All Rights Reserved
gosub init
for i=0 to 17:s$(i)="":next i
s$(0)="OK=calc, Calc=load, Calc2=save"
s$(2)="Weight"
s$(4)="Velocity"
s$(6)="Diameter"
s$(10)="Energy"
s$(12)="Efficacy"
s$(14)="Sec. Dens."
s$(16)="Name"
while (1)
j = form(12,0, "Muzzle Energy")
if j=0
  end
elseif j=2
   gosub load
elseif j=3
  gosub save
endif
w=val(s$(3))
v=val(s$(5))
d=val(s$(7))
r=d/2
gps=6999*32.17
en=0.5*(w/gps)*v*v
ef=en*3.141592454*r*r
sd=(w/6999)/(d*d)
s$(11)=round(en)
s$(13)=round(ef)
s$(15)=round(sd,3)
wend
end

sub save
name$=s$(17)
if name$ = ""
  print("Blank name. Not saved.")
  return
endif
w$=s$(3)
v$=s$(5)
d$=s$(7)
c$=","
val$=w$+c$+v$+c$+d$
gosub readsaves
if i >= 0
  msg$ = "Overwrite " + name$ + "?"
  x = msgbox(msg$, "Question", 2)
  if x = 0 then return
  vals$(i)=val$
  gosub writesaves
  return
endif
if maxsave >= maxnames
  print "Save storage full"
endif
 if db.find("memo",mname$) < 0
  open new "memo", mname$ as #4
else
  open "memo",mname$ as #4
endif
print #4, name$+":"+val$
close #4
end sub

sub load
name$=s$(17)
if name$ = ""
  print("Blank name. Can't restore.")
  return
endif
gosub readsaves
if i < 0
  print("No saved message named " + name$)
  return
endif
s$(3) = field$(vals$(i), 1, ",")
s$(5) = field$(vals$(i), 2, ",")
s$(7) = field$(vals$(i), 3, ",")
end sub

sub readsaves
  if db.find("memo", mname$) >= 0
    open "memo",mname$ as #1
    input #1,text$
    i = 0
    while not eof
      input #1,text$
      names$(i) = field$(text$, 1, ":")
      if names$(i) <> ""
        vals$(i) = field$(text$, 2, ":")
        i = i + 1
      endif
      if i>maxnames then go finish
    wend
  endif
finish:
  maxsave=i-1
  close #1
  for i=0 to maxsave
    if names$(i) = name$ then return
  next i
  i = -1
end sub

sub writesaves
  n = db.find("memo",mname$)
  if n > 0 then kill "memo",(db.find("MemoDB")), n, -9
  open new "memo", mname$ as #4
  for i=0 to maxsave
    print #4, names$(i) + ":" + vals$(i)
  next i
  close #4
end sub

sub init
s$(0)="About Muzzle Energy"
s$(1)="billstclair.com/energy"
s$(2)="by Bill St. Clair"
s$(3)="bill@billstclair.com"
fn myabout()
dim names$(50)
dim vals$(50)
maxnames=50
maxsave=-1
mname$="#Energy"
end sub