Scripts:3rd Party HSI

From OHRRPGCE-Wiki
(Redirected from 3rd Party HSI Code)
Jump to navigation Jump to search

This pages hosts the actual code and keeps track of revisions to it.

Documentation is at What is the 3rd Party HSI?

#             3rd Party Include File                          |
# version 1.92 (Last updated 13/05/2017)                        |
#                                                             |
# Check the OHRRPGCE Wiki for documentation.                  |
# You can freely use this file in your game or redistribute   |
# it. Please do not distribute modified versions of this      |
# file without noting the changes made. Better yet, why not   |
# post your scripts on the wiki?                              |
#                                                             |
# This version is intended for OHRRPGCE v.Beezelbufo          |
#                                                             |
# The 3rd Party HSI was compiled and directed by FyreWulff    |
# Please consult the Wiki for contributing authors            |
#---------------------------------------------------------====/

#=====================#
#Constants Definitions#
#=====================#
define constant, begin

 #these are for use in FyreWulff's clearmap script
 1, clearwalls
 0, preserve walls

 #For use in FyreWulff's unequipparty script
 0, entire party
 1, active only
 2, reserve only
end 

#=====================#
#Operator  Definitions#
#=====================#

define operator, begin   #operators for your use, eg  '50 + 100, times sine, 50' 
  25, times sin, sin wrap 
  25, times cos, cos wrap 
  25, times tan, tan wrap 
  30, div, divideround  
end 

#=====================#
#   Global Variables  #  all Third Party HSI global variables start from 1024 and go back
#=====================#  to prevent conflicts. For now let's keep them between 1001 and 1024

#comment out the below lines if your game uses these global var IDs

global variable, begin

  1024, vehiclex #for use in savevechicleposition and etc
  1023, vehicley #  "        "          "              "
  1022, vehicledirection # saves vehicle direction, used in script above
  1021, vehicleid # saves which NPC is the vehicle

end

#=====================#
#     The Scripts     #
#=====================#

# shows text box with auto waitfortextbox
script, tb, textbox, begin
  showtextbox(textbox)
  waitfortextbox
end

script, distance, num1, num2, begin
  return (abs(num1 -- num2))
end

#xdistance(Hero#,NPC#) : distance between hero# and npc# x's
script, x distance, heronum, npcnum, begin #by FyreWulff
  return ( abs( herox(heronum) -- npcx(npcnum) ) )
end

#ydistance(Hero#,NPC#) : distance between hero# and npc# y's
script, y distance, heronum, npcnum, begin #by FyreWulff
  return ( abs( heroy(heronum) -- npcy(npcnum) ) )
end
  
script, trunc, n, o, begin #written by T-master & CodyWatts
  return((n -- (n, mod, o)) / o) 
end

script, resettime, begin
  setdaysofplay(0)
  sethoursofplay(0)
  setminutesofplay(0)
  setsecondsofplay(0) 
end 

# Clear tilemap with a tile,
# default walls will also be cleared unless "preserve walls" is used
script, clearmap, cleartile = 0, wallclear = true, begin
  variable(xtile, ytile, layer)
  for(xtile,0,mapwidth -- 1) do, begin
    for(ytile,0,mapheight -- 1) do, begin
      writemapblock(xtile,ytile,cleartile,0)
      for(layer,1,7) do, begin
        writemapblock(xtile,ytile,0,layer)
      end
      if (wallclear) then ( writepassblock(xtile,ytile,0) )
    end
  end
end

# Saves vehicle position
script, save vehicle, vechnpc, begin
   vehiclex := npcx(vechnpc)
   vehicley := npcy(vechnpc)
   vehicledirection := npcdirection(vechnpc)
   vehicleid := vechnpc
end

# Restores vehicle at its saved position
script, restore vehicle, begin
   setnpcposition(vehicleid, vehiclex,vehicley)
   setnpcdirection(vehicleid, vehicledirection)
end

# Compute n/m and round the result
script, divideround, n, m, begin 
  return ((n + sign(n) * sign(m) * m / 2) / m)
end

# Compute sin(angle_in_degrees) * mult
script, sine, angle, mult = 10000, begin 
  variable (val, sgn) 

  if (angle < 0) then (angle := abs(angle), sgn := 1) # sine is an odd function
  sgn := (sgn + angle / 180), mod, 2 
  angle := angle, mod, 180               #this part converts the angle to a value between 0 and 90. 
  if (angle > 90) then (angle := 180 -- angle) 

  if (angle == 90) then (
    val := mult
  ) else (

    if (angle < 45) then ( 
      if (angle < 22) then ( 
        val := sina (angle) 
      ) else ( 
        val := sinb (angle) 
      ) 
    ) else (
     if (angle < 67) then ( 
        val := sinc (angle) 
      ) else ( 
        val := sind (angle) 
      ) 
    )
    val := (val*mult + 16384)/32768
  )
  if (sgn) then (return (0 -- val)) else (return (val))
end

script, cosine, angle, mult = 10000, begin 
  return (sine (angle + 90, mult)) 
end 

# Multiple two numbers, return 2147483647 or -2147483647 if it overflows
script, bounded multiply, a, b, begin
  variable(ret)
  ret := a * b
  if (b <> 0 && ret / b <> a) then (
    return (2147483647 * sign(a) * sign(b))
  ) else (
    return (ret)
  )
end

# max error about 9% for certain values of mult like 700 or really large,
# but normally more like 1% or less (eg for 1000)
script, tan, angle, mult = 10000, begin
  variable(sgn, numerator, denominator, ret, temp)

  angle := angle, mod, 180
  if (angle < 0) then (angle := 0 -- angle, sgn := 1)
  if (mult < 0) then (mult := 0 -- mult, sgn := sgn, xor, 1)
  if (angle > 90) then (angle := 180 -- angle, sgn := sgn, xor, 1)

  if (angle == 0) then (exit returning (0))
  if (angle == 90) then (exit returning (2147483647))

  numerator := sine(angle, 32768)
  denominator := sine(angle + 90, 32768)

  temp := 1000 * numerator / denominator
  #r =  bounded_multiply(temp, mult)
  ret := temp * mult
  # Check for overflow
  if (temp <> 0 && ret / temp <> mult) then (
    # mult was > ~37000
    ret := bounded multiply((mult + 250) / 500, temp / 2)
  ) else (
    ret := ret / 1000
  )

  if (sgn) then (
    return (0 -- ret)
  ) else (
    return (ret)
  )
end

script, sina, angle, begin 
  if (angle < 11) then ( 
   if (angle < 5) then ( 
    if (angle < 2) then ( 
     if (angle == 0) then (return (0)) else (return (572)) 
    ) else ( 
     if (angle == 2) then (return (1144)) else ( 
      if (angle == 3) then (return (1715)) else (return (2286)) 
     ) 
    ) 
   ) else ( 
    if (angle < 8) then ( 
     if (angle == 5) then (return (2856)) else ( 
      if (angle == 6) then (return (3425)) else (return (3993)) 
     ) 
    ) else ( 
     if (angle == 8) then (return (4560)) else ( 
      if (angle == 9) then (return (5126)) else (return (5690)) 
     ) 
    ) 
   ) 
  ) else ( 
   if (angle < 16) then ( 
    if (angle < 13) then ( 
     if (angle == 11) then (return (6252)) else (return (6813)) 
    ) else ( 
     if (angle == 13) then (return (7371)) else ( 
      if (angle == 14) then (return (7927)) else (return (8481)) 
     ) 
    ) 
   ) else ( 
    if (angle < 19) then ( 
     if (angle == 16) then (return (9032)) else ( 
      if (angle == 17) then (return (9580)) else (return (10126)) 
     ) 
    ) else ( 
     if (angle == 19) then (return (10668)) else ( 
      if (angle == 20) then (return (11207)) else (return (11743)) 
     ) 
    ) 
   ) 
  ) 
end 

script, sinb, angle, begin 
  if (angle < 33) then ( 
   if (angle < 27) then ( 
    if (angle < 24) then ( 
     if (angle == 22) then (return (12275)) else (return (12803)) 
    ) else ( 
     if (angle == 24) then (return (13328)) else ( 
      if (angle == 25) then (return (13848)) else (return (14365)) 
     ) 
    ) 
   ) else ( 
    if (angle < 30) then ( 
     if (angle == 27) then (return (14876)) else ( 
      if (angle == 28) then (return (15384)) else (return (15886)) 
     ) 
    ) else ( 
     if (angle == 30) then (return (16384)) else ( 
      if (angle == 31) then (return (16877)) else (return (17364)) 
     ) 
    ) 
   ) 
  ) else ( 
   if (angle < 39) then ( 
    if (angle < 36) then ( 
     if (angle == 33) then (return (17847)) else ( 
      if (angle == 34) then (return (18324)) else (return (18795)) 
     ) 
    ) else ( 
     if (angle == 36) then (return (19261)) else ( 
      if (angle == 37) then (return (19720)) else (return (20174)) 
     ) 
    ) 
   ) else ( 
    if (angle < 42) then ( 
     if (angle == 39) then (return (20622)) else ( 
      if (angle == 40) then (return (21063)) else (return (21498)) 
     ) 
    ) else ( 
     if (angle == 42) then (return (21926)) else ( 
      if (angle == 43) then (return (22348)) else (return (22763)) 
     ) 
    ) 
   ) 
  ) 
end 

script, sinc, angle, begin 
  if (angle < 56) then ( 
   if (angle < 50) then ( 
    if (angle < 47) then ( 
     if (angle == 45) then (return (23170)) else (return (23571)) 
    ) else ( 
     if (angle == 47) then (return (23965)) else ( 
      if (angle == 48) then (return (24351)) else (return (24730)) 
     ) 
    ) 
   ) else ( 
    if (angle < 53) then ( 
     if (angle == 50) then (return (25102)) else ( 
      if (angle == 51) then (return (25466)) else (return (25822)) 
     ) 
    ) else ( 
     if (angle == 53) then (return (26170)) else ( 
      if (angle == 54) then (return (26510)) else (return (26842)) 
     ) 
    ) 
   ) 
  ) else ( 
   if (angle < 61) then ( 
    if (angle < 58) then ( 
     if (angle == 56) then (return (27166)) else (return (27482)) 
    ) else ( 
     if (angle == 58) then (return (27789)) else ( 
      if (angle == 59) then (return (28088)) else (return (28378)) 
     ) 
    ) 
   ) else ( 
    if (angle < 64) then ( 
     if (angle == 61) then (return (28660)) else ( 
      if (angle == 62) then (return (28932)) else (return (29197)) 
     ) 
    ) else ( 
     if (angle == 64) then (return (29452)) else ( 
      if (angle == 65) then (return (29698)) else (return (29935)) 
     ) 
    ) 
   ) 
  ) 
end 

script, sind, angle, begin 
  if (angle < 78) then ( 
   if (angle < 72) then ( 
    if (angle < 69) then ( 
     if (angle == 67) then (return (30163)) else (return (30382)) 
    ) else ( 
     if (angle == 69) then (return (30592)) else ( 
      if (angle == 70) then (return (30792)) else (return (30983)) 
     ) 
    ) 
   ) else ( 
    if (angle < 75) then ( 
     if (angle == 72) then (return (31164)) else ( 
      if (angle == 73) then (return (31336)) else (return (31499)) 
     ) 
    ) else ( 
     if (angle == 75) then (return (31651)) else ( 
      if (angle == 76) then (return (31795)) else (return (31928)) 
     ) 
    ) 
   ) 
  ) else ( 
   if (angle < 84) then ( 
    if (angle < 81) then ( 
     if (angle == 78) then (return (32052)) else ( 
      if (angle == 79) then (return (32166)) else (return (32270)) 
     ) 
    ) else ( 
     if (angle == 81) then (return (32365)) else ( 
      if (angle == 82) then (return (32449)) else (return (32524)) 
     ) 
    ) 
   ) else ( 
    if (angle < 87) then ( 
     if (angle == 84) then (return (32588)) else ( 
      if (angle == 85) then (return (32643)) else (return (32688)) 
     ) 
    ) else ( 
     if (angle == 87) then (return (32723)) else ( 
      if (angle == 88) then (return (32748)) else (return (32763)) 
     ) 
    )
   )
  )
end 

script, sin wrap, mult, angle (return (sine (angle, mult))) 
script, cos wrap, mult, angle (return (cosine (angle, mult))) 
script, tan wrap, mult, angle (return (tan (angle, mult)))

# Result in thousands of a radian, between 3142 and -3142.
# Original floating point version had |error| < 5 mRad, this would be more than that.
script, atan2 milliradians, y, x, begin
  variable(atan, z, pi)
  pi := 3142
  if (x == 0) then (
    if (y > 0) then (return(pi/2))
    else if (y == 0) then (return(0))
    else (return(pi/-2))
  )
  else(
    z := 1000 * y / x
    if (abs(z) < 1000)
    then(
      atan := 1000*z/(1000 + z*z*28/100/1000)
      if (x < 0) then(
        if (y < 0) then (exitreturning(atan -- pi))
        exitreturning(atan + pi)
      )
    )
    else(
      if (abs(z) > 65000) then(
        # prevent overflow if z > sqrt(2 * 2**31)
        atan := pi/2
      )
      else(
        atan := pi/2 -- 1000*z/(z/2*z/500 + 280)  # pi/2 - z/(z*z + 0.28)
      )
      if (y < 0) then (exitreturning(atan -- pi))
    )
    return(atan)
  )
end

# Result in degrees. Returns mult*atan(y,x). Probably accurate to about half a degree.
# Returns a value between 180 and -180:
#  180: left
#  90: up
#  0: right
#  -90: down
#  -180: left
script, atan2, y, x, mult = 1, begin
  return (divideround(atan2 milliradians(y, x) * mult * 180, 3142))  # *180/pi
end

# Returns max(1, e^(n/1000) * 1000): always returns a positive value
# Delete the +1 at the end if you want it to reach 0
# For n > 0, maximum error in the result is 0.3%
script, exp1000, n, begin
  if (n >= 7291) then (
    if (n >= 14579) then (
      # Prevent overflow
      exitreturning(2145750645)
    )
    # exp(7.291) = 1467.04
    exitreturning(exp1000(n -- 7291) * 1467)
  )
  variable(mult, val)
  mult := 1000
  while (n <= -414) do (
    # exp(-0.414) = 0.661001
    mult := mult * 661 / 1000
    n += 414
  )
  while (n >= 540) do (
    # exp(0.540) = 1.716007
    mult := mult * 1716 / 1000
    n -= 540
  )

  #val := 1 + n + n**2 / 2 + n**3 / 6 + n**4 / 24
  val := 1000 + n + n^2 / 2000 + n^3 / 6000000 + (n^3 / 1000) * n / 24000000
  return(val * mult / 1000 + 1)
end

script, number of npcs, low = 0, high = 199, begin 
  variable (total, i) 
  for (i, low, high) do (increment (total, npc copy count (i))) 
  return (total) 
end 

# Returns a digit 'dgtno' in number 'value'.
script, digit, value, dgtno, begin 
  if (dgtno > 9) then (exit returning(0)) 
  return ((value / 10 ^ (dgtno -- 1)), mod, 10) 
end 

# Takes away all of the party's equipment.
# type can be entireparty, activeonly, or reserveonly
script, unequip party, type = entire party, begin
  variable(heroslot), heroslot := 0
  variable(itemslot), itemslot := 0
  variable(min hero slot) , min hero slot := 0
  variable(max hero slot) , max hero slot := 40

  if( type == active only ) then( max hero slot := 3 , min hero slot := 0 )
  if( type == reserve only ) then( max hero slot := 40 , min hero slot := 4)

  for(heroslot,min hero slot,max hero slot) do, begin
    for(itemslot,1,5) do ( unequip( heroslot, itemslot ) )
  end     
end

# Takes away all items in player's inventory
script, delete all items, begin
  variable(slot)
  for (slot, 0, getinventorysize -- 1) do (
    setitemcountinslot(slot, 0)
  )
end

#OBSOLETE
script, reset tags, begin
  variable(tagloop)  
  for(tagloop,2,2002) do ( settag(tagloop, false) ) 
end

# Teleport to a position on a map
script, fake door, map, x, y, begin
   suspendplayer
   fadescreenout
   wait(2)
   teleporttomap(map,x,y)
   wait(1)
   fadescreenin
   resumeplayer
end
# EOF