#!/usr/bin/gawk -f

# To run type: awk -f cloak_of_darkness_ru.awk

# Cloak of Darkness demo and GNU AWK Interactive Fiction engine
# Copyright 2005 by Nick Moffitt
# This code is released under the WTFPL: http://sam.zoy.org/wtfpl/
# Just do what you want with it!

# Russian translation and modifications by Alexey Galkin, august 2022

BEGIN {
   IGNORECASE = 1

   EAST = "^?$|??????"
   WEST = "^?$|?????"
   NORTH = "^?$|?????"
   SOUTH = "^?$|??"
   DROP = "???????|????????"
   TAKE = "?????|???????|???????"
   EXAMINE = "^?$|???|?????????|???????|????????"
   INVENTORY = "^?$|???|?????????"
       QUIT = "?????|?????|?????????"

   player = "The Player"
   dark[player] = "true"
   score = 0
}

#initial input sanitizing
$2 { $2 = cansee($2) }

#############################################################
# Game Data Begins Here
#############################################################

BEGIN {
   foyer = "???? ???????? ??????"
   description[foyer] = "?? ?????? ? ?????????? ????, ??????????? ?????????? ??????? ? ???????, ?? ??????????? ???????? ??? ???????. ???? ? ????? ? ?? ??????, ? ??????? ?????? ? ?? ??? ? ??????."
}
here == foyer && $1 ~ NORTH {
   print "?? ?????? ??????, ?, ????? ????, ?????? ???????, ??????, ????????."
   prompt()
}
here == foyer && $1 ~ WEST { go(cloakroom) ; prompt() }
here == foyer && $1 ~ SOUTH { go(bar) ; prompt() }

BEGIN {
   cloakroom = "????????"
   description[cloakroom] = "????? ???? ????????? ??????? ???? ?????-?? ???????? ??????, ???? ?????? ??????? ????? ????. ????? ? ????? ????? ?? ???????."
}
here == cloakroom && $1 ~ EAST { go(foyer) ; prompt() }

BEGIN {
   hook = "????????? ???????? ??????|????"
   location[hook] = cloakroom
   open[hook] = 1
   invisible[hook] = 1
}
$2 == hook && $1 ~ /?????/ { print "?? ?? ?????? ??? ?????!"; prompt()}
$2 == hook && $1 ~ EXAMINE {
   printf "????????? ???????? ?????? "
   if (location[cloak] == hook) {
       printf "?? ??????? ????? ????."
   } else {
       printf "????????? ? ?????."
   }
}

BEGIN {
   bar = "??? ? ????"
   description[bar] = "??? ???????? ??????? ????, ??? ?? ????????????, ????? ???? ??????? ???? ?? ??????, ? ?????????? ????. ???????, ? ??????? ?? ???? ????????? ?????-?? ????????."
   dark[bar] = 1
   bumblings = 0
}
here == bar && $1 ~ NORTH { go(foyer) ; prompt() }
dark[bar] && here == bar {
   print "? ???????? ?? ?????? ???????? ??????????!"
   bumblings++
   prompt()
}

BEGIN {
   cloak = "????????? ????"
   description[cloak] = "???????? ???? ?? ???????, ??????????? ??????? ? ?????? ???????????? ??????? ?????. ???? ????????? ??????, ??? ???????, ????? ?? ?????????? ???? ?? ???????."
   location[cloak] = player
}
$2 == cloak && ($1 ~ DROP || /????????|?????/) {
   if (here == cloakroom) {
       delete dark[bar]
       move(cloak, hook)
       print "?? ????????? ??????? ??????????? ????? ???? ?? ????????? ???????? ??????. ?? ?????????? ????... ???-?? ?????."
   } else {
       print "??? ?? ?????? ?????, ????? ???????? ???????? ????? ????? ?????????? ????!"
   }
   prompt()
}
$2 == cloak && $1 ~ TAKE || /??????/ {
   if (location[$2] == player ) {
       print "? ??? ??? ???? " $2 "."
       prompt()
   }
   move($2, player)
   dark[bar] = 1
   print $2 ": ?????."
   prompt()
}

BEGIN {
   message = "???????????? ?????????|????????|??????"
   location[message] = bar
   invisible[message] = 1
}

$2 == message && ($1 ~ EXAMINE || /??????/) {
   if (bumblings < 2) {
       print "????????? ?????????? ? ??????? ???????? ??????: <?? ????????>.\n"
       exit 0
   } else {
       print "???????? ?? ???? ?? ?????????????? ??????????, ?? ???????? ????????? ??? ??????????. ?? ?????????? ???? ????? <?? ?????????>.\n"
       exit 1
   }
}

BEGIN {
   here = foyer
   print "\033[1m???? ????\033[0m\n\n?? ??????? ?????? ????????? ?????????? ???? ? ?????????, ?????? ????? ???? ???????? ??????. ???????????, ??? ?????? ???, ??, ??, ??? ?? ??????? ?? ??????? ????-????...?"
   printf "\n\n> "
}


#############################################################
# Game Data Ends Here
#############################################################


$1 ~ EAST || $1 ~ WEST || $1 ~ NORTH || $1 ~ SOUTH {
   print "?? ?? ?????? ????? ? ???? ???????????."
   prompt()
}

$1 ~ TAKE {
   if (location[$2] == player ) {
       print "? ??? ??? ???? " $2 "."
       prompt()
   }
   move($2, player)
   print $2 ": ?????."
   prompt()
}

$1 ~ INVENTORY { inventory() ; prompt() }
$1 ~ EXAMINE && $2 { look($2) ; prompt()}
$1 ~ EXAMINE || ! $1 { look(here) ; prompt() } # ?????? ?????? - ????? ???????? ???????
$1 ~ QUIT {exit 1}

# Default verb confusion
{
   print "??????????? ?????? \"" $1 "\"."
   prompt()
}


function prompt() {
   printf "\n> "
   next
}

function move(object, destination) {
   location[object] = destination
}

function go(destination) {
   here = destination
   look(here)
}

function look(object) {
   if (object == here) {
       print "\033[1m" here "\033[0m"
   }
   if (dark[here]) {
       print "????? ????? ? ???? ???? ??????. ??? ????? ?????? ???."
       prompt()
   } else {
       print description[object]
   }
   if (object == here) {
       for (item in location) {
           if (location[item] == here && !invisible[item]) {
               print "\n????? ???? " item "."
           }
           if (location[item] == here && open[item]) {
               for (subitem in location) {
                   if (location[subitem] == item) {
                       print "\n????? ???? \033[33m" subitem "\033[0m."
                   }
               }
           }
       }
   }
}

function inventory() {
   print "? ??? ????: "
   for (item in location) {
       if (location[item] == player) {
           print "  " item
       }
   }
}


function cansee(object) {
   for (item in location) {
       if (location[item] == here || location[item] == player) {
           if (item ~ object) {
               return item
           }
           if (open[item]) {
               for (thing in location) {
                   if (location[thing] == item) {
                       if (thing ~ object) {
                           return thing
                       }
                   }
               }
           }
       }
   }
   print "? ?? ???? ????? \"" object "\"."
   prompt()
}

# ex:ts=4 sw=4 tw=72 expandtab ai