From cdf9bf73a9f634c76272227335b2f6906e59874d Mon Sep 17 00:00:00 2001 From: sneak Date: Mon, 6 Jul 2026 19:34:36 +0200 Subject: [PATCH] =?UTF-8?q?go:=20port=20item=20effects=20=E2=80=94=20potio?= =?UTF-8?q?ns,=20scrolls,=20options,=20call=5Fit?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - potions.c completed: quaff with all 14 potion effects, the PACT standard-fuse table (do_pot), raise_level; the see-invisible fruit- juice message is computed at quaff time as in C - scrolls.c in full: read_scroll with all 18 scroll effects including the magic-mapping map rewrite and identify dispatch, uncurse - options.c in full: the interactive options screen, get_bool/get_str/ get_inv_t/get_sf editors, ROGUEOPTS parsing (prefix matching, no- prefixed booleans, ~ expansion), strucpy - misc.c call_it via the get_str line editor - turn_see gets a DaemonID (C casts it to a fuse callback for the monster-detection potion) Tests: healing/confusion potions with fuse burn-down, enchant armor, hold monster, slow-monster zap, ROGUEOPTS parsing, and a regression test documenting the C wake_monster ISGREED/ISHELD quirk the port keeps faithfully. --- game/daemon.go | 1 + game/daemons.go | 2 + game/effects_test.go | 170 ++++++++++++++++++++ game/misc.go | 15 ++ game/options.go | 375 +++++++++++++++++++++++++++++++++++++++++++ game/potions.go | 209 +++++++++++++++++++++++- game/scrolls.go | 262 ++++++++++++++++++++++++++++++ 7 files changed, 1032 insertions(+), 2 deletions(-) create mode 100644 game/effects_test.go create mode 100644 game/options.go create mode 100644 game/scrolls.go diff --git a/game/daemon.go b/game/daemon.go index db4b506..ffa3672 100644 --- a/game/daemon.go +++ b/game/daemon.go @@ -26,6 +26,7 @@ const ( DVisuals DaemonID = 10 DComeDown DaemonID = 11 DLand DaemonID = 12 + DTurnSee DaemonID = 13 // potions.c casts turn_see to a fuse callback ) // Scheduling phases and slot states (daemon.c). diff --git a/game/daemons.go b/game/daemons.go index b0fcb54..842b68a 100644 --- a/game/daemons.go +++ b/game/daemons.go @@ -30,6 +30,8 @@ func (g *RogueGame) runDaemon(id DaemonID, arg int) { g.comeDown(arg) case DLand: g.land(arg) + case DTurnSee: + g.turnSee(arg != 0) default: // Callbacks are added to this switch as their subsystems are // ported; reaching one that isn't here is a porting bug. diff --git a/game/effects_test.go b/game/effects_test.go new file mode 100644 index 0000000..32bca00 --- /dev/null +++ b/game/effects_test.go @@ -0,0 +1,170 @@ +package game + +import "testing" + +// mkGameInput builds a headless game whose input plays the given script. +func mkGameInput(t *testing.T, seed int32, input string) *RogueGame { + t.Helper() + g := NewGame(Config{Seed: seed, Term: &testTerm{input: []byte(input)}}) + g.NewLevel() + g.Oldpos = g.Player.Pos + g.Oldrp = g.roomin(g.Player.Pos) + return g +} + +// give puts an object straight into the pack and returns its pack letter. +func give(g *RogueGame, obj *Object) byte { + obj.Count = 1 + g.addPack(obj, true) + return obj.PackCh +} + +func TestQuaffHealingPotion(t *testing.T) { + g := mkGameInput(t, 5, "") + pot := newObject() + pot.Type = Potion + pot.Which = PHealing + ch := give(g, pot) + g.scr.term.(*testTerm).input = []byte{ch} + + g.Player.Stats.HP = 1 + g.quaff() + if g.Player.Stats.HP <= 1 { + t.Error("healing potion did not heal") + } + if !g.Items.Potions[PHealing].Know { + t.Error("healing potion not identified after drinking") + } + if len(g.Player.Pack) != 5 { + t.Errorf("potion not consumed: %d items", len(g.Player.Pack)) + } +} + +func TestQuaffConfusionSetsFlagAndFuse(t *testing.T) { + g := mkGameInput(t, 5, "") + pot := newObject() + pot.Type = Potion + pot.Which = PConfuse + ch := give(g, pot) + g.scr.term.(*testTerm).input = []byte{ch} + + g.quaff() + if !g.Player.On(IsHuh) { + t.Error("confusion potion did not confuse") + } + if g.findSlot(DUnconfuse) == nil { + t.Error("no unconfuse fuse pending") + } + // Let the fuse burn down + for range 30 { + g.DoFuses(After) + } + if g.Player.On(IsHuh) { + t.Error("confusion never wore off") + } +} + +func TestReadEnchantArmor(t *testing.T) { + g := mkGameInput(t, 5, "") + scr := newObject() + scr.Type = Scroll + scr.Which = SArmor + ch := give(g, scr) + g.scr.term.(*testTerm).input = []byte{ch} + + before := g.Player.CurArmor.Arm + g.readScroll() + if g.Player.CurArmor.Arm != before-1 { + t.Errorf("enchant armor: AC %d -> %d, want %d", + before, g.Player.CurArmor.Arm, before-1) + } +} + +func TestReadHoldMonsterFreezesAdjacent(t *testing.T) { + // Note: this must not use a greedy monster ('O' orc, ISGREED): the C + // wake_monster gold-guarding check has no ISHELD guard, so the + // look(TRUE) at the end of read_scroll immediately re-wakes greedy + // monsters. The port reproduces that quirk faithfully — see + // TestHoldScrollGreedyMonsterQuirk. + g := mkGameInput(t, 5, "") + tp := spawnAdjacent(g, 'Z') + tp.Flags.Set(IsRun) + scr := newObject() + scr.Type = Scroll + scr.Which = SHold + ch := give(g, scr) + g.scr.term.(*testTerm).input = []byte{ch} + + g.readScroll() + t.Logf("after scroll: flags=%o huh=%q", tp.Flags, g.Msgs.Huh) + if tp.On(IsRun) || !tp.On(IsHeld) { + t.Error("hold monster scroll did not hold the adjacent monster") + } +} + +// TestHoldScrollGreedyMonsterQuirk documents a C behavior the port keeps: +// wake_monster's ISGREED branch lacks an ISHELD guard, so a greedy monster +// (orc) held by a scroll is re-woken by the look(TRUE) that read_scroll +// performs, ending up both held and running again. +func TestHoldScrollGreedyMonsterQuirk(t *testing.T) { + g := mkGameInput(t, 5, "") + tp := spawnAdjacent(g, 'O') + tp.Flags.Set(IsRun) + scr := newObject() + scr.Type = Scroll + scr.Which = SHold + ch := give(g, scr) + g.scr.term.(*testTerm).input = []byte{ch} + + g.readScroll() + t.Logf("orc after scroll: flags=%o (IsRun=%v IsHeld=%v)", + tp.Flags, tp.On(IsRun), tp.On(IsHeld)) + if !tp.On(IsHeld) { + t.Error("orc lost IsHeld entirely") + } + if !tp.On(IsRun) { + t.Error("quirk changed: greedy monster stayed held; if this is a " + + "deliberate fix, update this test and ARCHITECTURE.md") + } +} + +func TestZapSlowMonster(t *testing.T) { + g := mkGameInput(t, 5, "") + tp := spawnAdjacent(g, 'Z') + stick := newObject() + stick.Type = Stick + stick.Which = WsSlowM + g.fixStick(stick) + ch := give(g, stick) + g.scr.term.(*testTerm).input = []byte{ch} + g.Delta = Coord{X: 1, Y: 0} // aim at the monster + + charges := stick.Charges() + g.doZap() + if !tp.On(IsSlow) { + t.Error("slow monster wand did not slow") + } + if stick.Charges() != charges-1 { + t.Error("zap did not use a charge") + } +} + +func TestParseOpts(t *testing.T) { + g := NewGame(Config{Seed: 1}) + g.ParseOpts("terse,nojump,name=Conan,fruit=mango,inven=slow") + if !g.Options.Terse { + t.Error("terse not set") + } + if g.Options.Jump { + t.Error("nojump not honored") + } + if g.Whoami != "Conan" { + t.Errorf("name = %q", g.Whoami) + } + if g.Fruit != "mango" { + t.Errorf("fruit = %q", g.Fruit) + } + if g.Options.InvType != InvSlow { + t.Errorf("inven = %d", g.Options.InvType) + } +} diff --git a/game/misc.go b/game/misc.go index e04d738..2cd4355 100644 --- a/game/misc.go +++ b/game/misc.go @@ -416,6 +416,21 @@ func (g *RogueGame) getDir() bool { return true } +// callIt calls an object something after use (misc.c call_it). +func (g *RogueGame) callIt(info *ObjInfo) { + if info.Know { + info.Guess = "" + } else if info.Guess == "" { + g.msg("%s", g.chooseTerse("call it: ", "what do you want to call it? ")) + buf := "" + if g.getStr(&buf, g.scr.Std) == Norm { + if buf != "" { + info.Guess = buf + } + } + } +} + // thingList is misc.c rnd_thing()'s static table. var thingList = []byte{ Potion, Scroll, Ring, Stick, Food, Weapon, Armor, Stairs, Gold, Amulet, diff --git a/game/options.go b/game/options.go new file mode 100644 index 0000000..86914ba --- /dev/null +++ b/game/options.go @@ -0,0 +1,375 @@ +package game + +import "strings" + +// options.c — the option command and ROGUEOPTS parsing. + +// optKind selects the get/put behavior of an option (the C function +// pointers o_putfunc/o_getfunc). +type optKind int + +const ( + optBool optKind = iota + optSeeFloor + optInvT + optStr +) + +// optDesc describes an option (options.c OPTION). +type optDesc struct { + name string + prompt string + kind optKind + boolP *bool + intP *int + strP *string +} + +// optList builds the options.c optlist for this game. +func (g *RogueGame) optList() []optDesc { + o := &g.Options + return []optDesc{ + {"terse", "Terse output", optBool, &o.Terse, nil, nil}, + {"flush", "Flush typeahead during battle", optBool, &o.FightFlush, nil, nil}, + {"jump", "Show position only at end of run", optBool, &o.Jump, nil, nil}, + {"seefloor", "Show the lamp-illuminated floor", optSeeFloor, &o.SeeFloor, nil, nil}, + {"passgo", "Follow turnings in passageways", optBool, &o.PassGo, nil, nil}, + {"tombstone", "Print out tombstone when killed", optBool, &o.Tombstone, nil, nil}, + {"inven", "Inventory style", optInvT, nil, &o.InvType, nil}, + {"name", "Name", optStr, nil, nil, &g.Whoami}, + {"fruit", "Fruit", optStr, nil, nil, &g.Fruit}, + {"file", "Save file", optStr, nil, nil, &g.FileName}, + } +} + +// option prints and then sets options from the terminal (options.c option). +func (g *RogueGame) option() { + hw := g.scr.Hw + optlist := g.optList() + hw.Clear() + // Display current values of options + for i := range optlist { + op := &optlist[i] + g.prOptname(op) + g.putOpt(op) + hw.AddCh('\n') + } + // Set values + hw.Move(0, 0) + for i := 0; i < len(optlist); i++ { + op := &optlist[i] + g.prOptname(op) + retval := g.getOpt(op) + if retval != Norm { + if retval == Quit { + break + } + // MINUS: back up one option + if i > 0 { + hw.Move(i-1, 0) + i -= 2 + } else { // trying to back up beyond the top + hw.Move(0, 0) + i-- + } + } + } + // Switch back to original screen + hw.MvAddStr(NumLines-1, 0, "--Press space to continue--") + g.scr.RefreshWin(hw) + g.waitFor(' ') + g.refresh() + g.After = false +} + +// prOptname prints out the option name prompt (options.c pr_optname). +func (g *RogueGame) prOptname(op *optDesc) { + g.scr.Hw.Printw("%s (\"%s\"): ", op.prompt, op.name) +} + +// putOpt prints an option's current value (options.c put_bool/put_str/ +// put_inv_t). +func (g *RogueGame) putOpt(op *optDesc) { + hw := g.scr.Hw + switch op.kind { + case optBool, optSeeFloor: + hw.AddStr(boolStr(*op.boolP)) + case optInvT: + hw.AddStr(invTName[*op.intP]) + case optStr: + hw.AddStr(*op.strP) + } +} + +func boolStr(b bool) string { + if b { + return "True" + } + return "False" +} + +// getOpt reads a new value for an option (options.c get_bool/get_sf/ +// get_inv_t/get_str dispatch). +func (g *RogueGame) getOpt(op *optDesc) int { + switch op.kind { + case optBool: + return g.getBool(op.boolP) + case optSeeFloor: + return g.getSf(op.boolP) + case optInvT: + return g.getInvT(op.intP) + default: + return g.getStr(op.strP, g.scr.Hw) + } +} + +// getBool allows changing a boolean option and prints it out (options.c +// get_bool). +func (g *RogueGame) getBool(bp *bool) int { + win := g.scr.Hw + oy, ox := win.GetYX() + win.AddStr(boolStr(*bp)) + for { + win.Move(oy, ox) + g.scr.RefreshWin(win) + switch g.readchar() { + case 't', 'T': + *bp = true + case 'f', 'F': + *bp = false + case '\n', '\r': + case Escape: + return Quit + case '-': + return Minus + default: + win.Move(oy, ox+10) + win.AddStr("(T or F)") + continue + } + break + } + win.Move(oy, ox) + win.AddStr(boolStr(*bp)) + win.AddCh('\n') + return Norm +} + +// getSf changes see_floor and handles the display transition (options.c +// get_sf). +func (g *RogueGame) getSf(bp *bool) int { + wasSf := g.Options.SeeFloor + retval := g.getBool(bp) + if retval == Quit { + return Quit + } + if wasSf != g.Options.SeeFloor { + if !g.Options.SeeFloor { + g.Options.SeeFloor = true + g.eraseLamp(g.Player.Pos, g.Player.Room) + g.Options.SeeFloor = false + } else { + g.look(false) + } + } + return Norm +} + +// getStr sets a string option (options.c get_str). win selects between the +// options window and the message line (C's stdscr), which changes the '-' +// and mpos behavior. +func (g *RogueGame) getStr(opt *string, win *Window) int { + onStd := win == g.scr.Std + oy, ox := win.GetYX() + g.scr.RefreshWin(win) + // loop reading in the string, and put it in a temporary buffer + var buf []byte + var c byte + for { + c = g.readchar() + if c == '\n' || c == '\r' || c == Escape { + break + } + if c == 8 || c == 0x7f { // erase character + if len(buf) > 0 { + buf = buf[:len(buf)-1] + win.Move(oy, ox+len(displayStr(buf))) + } + win.Clrtoeol() + g.scr.RefreshWin(win) + continue + } + if c == CTRL('U') { // kill character + buf = buf[:0] + win.Move(oy, ox) + win.Clrtoeol() + g.scr.RefreshWin(win) + continue + } + if len(buf) == 0 { + if c == '-' && !onStd { + break + } + if c == '~' { + buf = append(buf, g.Home...) + win.AddStr(g.Home) + win.Clrtoeol() + g.scr.RefreshWin(win) + continue + } + } + if len(buf) >= MaxInp || !(isPrint(c) || c == ' ') { + continue // C beeps here + } + buf = append(buf, c) + win.AddStr(unctrl(c)) + win.Clrtoeol() + g.scr.RefreshWin(win) + } + if len(buf) > 0 { // only change option if something has been typed + *opt = strucpy(string(buf)) + } + win.MvPrintw(oy, ox, "%s\n", *opt) + g.scr.RefreshWin(win) + if onStd { + g.Msgs.Mpos += len(buf) + } + switch c { + case '-': + return Minus + case Escape: + return Quit + default: + return Norm + } +} + +// displayStr renders a buffer the way the input echo did. +func displayStr(buf []byte) string { + var sb strings.Builder + for _, c := range buf { + sb.WriteString(unctrl(c)) + } + return sb.String() +} + +// getInvT gets an inventory type name (options.c get_inv_t). +func (g *RogueGame) getInvT(ip *int) int { + win := g.scr.Hw + oy, ox := win.GetYX() + win.AddStr(invTName[*ip]) + for { + win.Move(oy, ox) + g.scr.RefreshWin(win) + switch g.readchar() { + case 'o', 'O': + *ip = InvOver + case 's', 'S': + *ip = InvSlow + case 'c', 'C': + *ip = InvClear + case '\n', '\r': + case Escape: + return Quit + case '-': + return Minus + default: + win.Move(oy, ox+15) + win.AddStr("(O, S, or C)") + continue + } + break + } + win.MvPrintw(oy, ox, "%s\n", invTName[*ip]) + return Norm +} + +// ParseOpts parses options from a string, usually taken from the +// environment: comma separated values, booleans stated as "name" (true) or +// "noname" (false), strings as "name=..." (options.c parse_opts). +func (g *RogueGame) ParseOpts(str string) { + optlist := g.optList() + for str != "" { + // Get option name + i := 0 + for i < len(str) && isAlpha(str[i]) { + i++ + } + name := str[:i] + rest := str[i:] + matched := false + for oi := range optlist { + op := &optlist[oi] + isBoolOpt := op.kind == optBool || op.kind == optSeeFloor + if strings.HasPrefix(op.name, name) && name != "" { + matched = true + if isBoolOpt { + *op.boolP = true + } else { + // Skip to start of string value + for rest != "" && rest[0] == '=' { + rest = rest[1:] + } + val := rest + var prefix string + if val != "" && val[0] == '~' { + prefix = g.Home + val = val[1:] + for val != "" && val[0] == '/' { + val = val[1:] + } + } + // Skip to end of string value + end := strings.IndexByte(val, ',') + if end < 0 { + end = len(val) + } + word := val[:end] + rest = val[end:] + if op.kind == optInvT { + // check for type of inventory + w := word + if w != "" { + w = string(toUpper(w[0])) + w[1:] + } + for ti, tn := range invTName { + if strings.HasPrefix(tn, w) { + *op.intP = ti + break + } + } + } else { + *op.strP = prefix + strucpy(word) + } + } + break + } else if isBoolOpt && strings.HasPrefix(name, "no") && + strings.HasPrefix(op.name, name[2:]) { + matched = true + *op.boolP = false + break + } + } + _ = matched + // skip to start of next option name + for rest != "" && !isAlpha(rest[0]) { + rest = rest[1:] + } + str = rest + } +} + +// strucpy copies a string keeping only printable characters, capped at +// MAXINP (options.c strucpy). +func strucpy(s string) string { + if len(s) > MaxInp { + s = s[:MaxInp] + } + var sb strings.Builder + for i := 0; i < len(s); i++ { + if isPrint(s[i]) || s[i] == ' ' { + sb.WriteByte(s[i]) + } + } + return sb.String() +} diff --git a/game/potions.go b/game/potions.go index be22871..f49b408 100644 --- a/game/potions.go +++ b/game/potions.go @@ -1,7 +1,212 @@ package game -// potions.c — the visibility-related helpers arrive first; quaff and the -// potion effect dispatch come with the effects phase. +import "fmt" + +// potions.c — functions for dealing with potions. + +// pact describes a standard fuse-based potion (potions.c PACT). +type pact struct { + flags CreatureFlags + daemon DaemonID + time int + high string // message when hallucinating + straight string +} + +// pActions is potions.c p_actions[]. The P_SEEINVIS message is dynamic +// (it names the fruit) and is computed in doPot. +var pActions = [MaxPotions]pact{ + PConfuse: {IsHuh, DUnconfuse, HuhDuration, + "what a tripy feeling!", + "wait, what's going on here. Huh? What? Who?"}, + PLSD: {IsHalu, DComeDown, SeeDuration, + "Oh, wow! Everything seems so cosmic!", + "Oh, wow! Everything seems so cosmic!"}, + PSeeInvis: {CanSee, DUnsee, SeeDuration, "", ""}, + PBlind: {IsBlind, DSight, SeeDuration, + "oh, bummer! Everything is dark! Help!", + "a cloak of darkness falls around you"}, + PLevit: {IsLevit, DLand, HealTime, + "oh, wow! You're floating in the air!", + "you start to float in the air"}, +} + +// quaff drinks a potion from the pack (potions.c quaff). +func (g *RogueGame) quaff() { + p := &g.Player + obj := g.getItem("quaff", int(Potion)) + // Make certain that it is something that we want to drink + if obj == nil { + return + } + if obj.Type != Potion { + if !g.Options.Terse { + g.msg("yuk! Why would you want to drink that?") + } else { + g.msg("that's undrinkable") + } + return + } + if obj == p.CurWeapon { + p.CurWeapon = nil + } + + // Calculate the effect it has on the poor guy. + trip := p.On(IsHalu) + g.leavePack(obj, false, false) + switch obj.Which { + case PConfuse: + g.doPot(PConfuse, !trip) + case PPoison: + g.Items.Potions[PPoison].Know = true + if p.IsWearing(RSustStr) { + g.msg("you feel momentarily sick") + } else { + g.chgStr(-(g.rnd(3) + 1)) + g.msg("you feel very sick now") + g.comeDown(0) + } + case PHealing: + g.Items.Potions[PHealing].Know = true + if p.Stats.HP += g.roll(p.Stats.Lvl, 4); p.Stats.HP > p.Stats.MaxHP { + p.Stats.MaxHP++ + p.Stats.HP = p.Stats.MaxHP + } + g.sight(0) + g.msg("you begin to feel better") + case PStrength: + g.Items.Potions[PStrength].Know = true + g.chgStr(1) + g.msg("you feel stronger, now. What bulging muscles!") + case PMFind: + p.Flags.Set(SeeMonst) + g.Fuse(DTurnSee, 1, HuhDuration, After) + if !g.turnSee(false) { + g.msg("you have a %s feeling for a moment, then it passes", + g.chooseStr("normal", "strange")) + } + case PTFind: + // Potion of magic detection. Show the potions and scrolls + show := false + if len(g.Level.Objects) > 0 { + g.scr.Hw.Clear() + for _, tp := range g.Level.Objects { + if tp.isMagic() { + show = true + g.scr.Hw.MvAddCh(tp.Pos.Y, tp.Pos.X, Magic) + g.Items.Potions[PTFind].Know = true + } + } + for _, mp := range g.Level.Monsters { + for _, tp := range mp.Pack { + if tp.isMagic() { + show = true + g.scr.Hw.MvAddCh(mp.Pos.Y, mp.Pos.X, Magic) + } + } + } + } + if show { + g.Items.Potions[PTFind].Know = true + g.showWin("You sense the presence of magic on this level.--More--") + } else { + g.msg("you have a %s feeling for a moment, then it passes", + g.chooseStr("normal", "strange")) + } + case PLSD: + if !trip { + if p.On(SeeMonst) { + g.turnSee(false) + } + g.StartDaemon(DVisuals, 0, Before) + g.SeenStairs = g.seenStairs() + } + g.doPot(PLSD, true) + case PSeeInvis: + show := p.On(CanSee) + g.doPot(PSeeInvis, false) + if !show { + g.invisOn() + } + g.sight(0) + case PRaise: + g.Items.Potions[PRaise].Know = true + g.msg("you suddenly feel much more skillful") + g.raiseLevel() + case PXHeal: + g.Items.Potions[PXHeal].Know = true + if p.Stats.HP += g.roll(p.Stats.Lvl, 8); p.Stats.HP > p.Stats.MaxHP { + if p.Stats.HP > p.Stats.MaxHP+p.Stats.Lvl+1 { + p.Stats.MaxHP++ + } + p.Stats.MaxHP++ + p.Stats.HP = p.Stats.MaxHP + } + g.sight(0) + g.comeDown(0) + g.msg("you begin to feel much better") + case PHaste: + g.Items.Potions[PHaste].Know = true + g.After = false + if g.addHaste(true) { + g.msg("you feel yourself moving much faster") + } + case PRestore: + if p.IsRing(Left, RAddStr) { + addStr(&p.Stats.Str, -p.CurRing[Left].Arm) + } + if p.IsRing(Right, RAddStr) { + addStr(&p.Stats.Str, -p.CurRing[Right].Arm) + } + if p.Stats.Str < p.MaxStats.Str { + p.Stats.Str = p.MaxStats.Str + } + if p.IsRing(Left, RAddStr) { + addStr(&p.Stats.Str, p.CurRing[Left].Arm) + } + if p.IsRing(Right, RAddStr) { + addStr(&p.Stats.Str, p.CurRing[Right].Arm) + } + g.msg("hey, this tastes great. It make you feel warm all over") + case PBlind: + g.doPot(PBlind, true) + case PLevit: + g.doPot(PLevit, true) + } + g.status() + // Throw the item away + g.callIt(&g.Items.Potions[obj.Which]) +} + +// raiseLevel: the guy just magically went up a level (potions.c +// raise_level). +func (g *RogueGame) raiseLevel() { + g.Player.Stats.Exp = eLevels[g.Player.Stats.Lvl-1] + 1 + g.checkLevel() +} + +// doPot does a potion with standard setup: it uses a fuse and turns on a +// flag (potions.c do_pot). +func (g *RogueGame) doPot(typ int, knowit bool) { + pp := &pActions[typ] + if !g.Items.Potions[typ].Know { + g.Items.Potions[typ].Know = knowit + } + t := g.spread(pp.time) + if !g.Player.On(pp.flags) { + g.Player.Flags.Set(pp.flags) + g.Fuse(pp.daemon, 0, t, After) + g.look(false) + } else { + g.Lengthen(pp.daemon, t) + } + high, straight := pp.high, pp.straight + if typ == PSeeInvis { + s := fmt.Sprintf("this potion tastes like %s juice", g.Fruit) + high, straight = s, s + } + g.msg("%s", g.chooseStr(high, straight)) +} // isMagic reports whether an object radiates magic (potions.c is_magic). func (o *Object) isMagic() bool { diff --git a/game/scrolls.go b/game/scrolls.go new file mode 100644 index 0000000..29e0cbf --- /dev/null +++ b/game/scrolls.go @@ -0,0 +1,262 @@ +package game + +// scrolls.c — read a scroll and let it happen. + +// idType maps identify scrolls to the type they identify (scrolls.c +// static id_type). +var idType = [SIDRorS + 1]int{ + SIDPotion: int(Potion), + SIDScroll: int(Scroll), + SIDWeapon: int(Weapon), + SIDArmor: int(Armor), + SIDRorS: RorS, +} + +// readScroll reads a scroll from the pack and does the appropriate thing +// (scrolls.c read_scroll). +func (g *RogueGame) readScroll() { + p := &g.Player + obj := g.getItem("read", int(Scroll)) + if obj == nil { + return + } + if obj.Type != Scroll { + if !g.Options.Terse { + g.msg("there is nothing on it to read") + } else { + g.msg("nothing to read") + } + return + } + // Calculate the effect it has on the poor guy. + if obj == p.CurWeapon { + p.CurWeapon = nil + } + // Get rid of the thing + g.leavePack(obj, false, false) + + switch obj.Which { + case SConfuse: + // Scroll of monster confusion. Give him that power. + p.Flags.Set(CanHuh) + g.msg("your hands begin to glow %s", g.pickColor("red")) + case SArmor: + if p.CurArmor != nil { + p.CurArmor.Arm-- + p.CurArmor.Flags.Clear(IsCursed) + g.msg("your armor glows %s for a moment", g.pickColor("silver")) + } + case SHold: + // Hold monster scroll. Stop all monsters within two spaces from + // chasing after the hero. + held := 0 + for x := p.Pos.X - 2; x <= p.Pos.X+2; x++ { + if x < 0 || x >= NumCols { + continue + } + for y := p.Pos.Y - 2; y <= p.Pos.Y+2; y++ { + if y < 0 || y > NumLines-1 { + continue + } + if mp := g.Level.MonsterAt(y, x); mp != nil && mp.On(IsRun) { + mp.Flags.Clear(IsRun) + mp.Flags.Set(IsHeld) + held++ + } + } + } + if held > 0 { + g.addmsg("the monster") + if held > 1 { + g.addmsg("s around you") + } + g.addmsg(" freeze") + if held == 1 { + g.addmsg("s") + } + g.endmsg() + g.Items.Scrolls[SHold].Know = true + } else { + g.msg("you feel a strange sense of loss") + } + case SSleep: + // Scroll which makes you fall asleep + g.Items.Scrolls[SSleep].Know = true + g.NoCommand += g.rnd(g.spread(5)) + 4 // SLEEPTIME + p.Flags.Clear(IsRun) + g.msg("you fall asleep") + case SCreate: + // Create a monster: first look in a circle around him, next try + // his room, otherwise give up + i := 0 + var mp Coord + for y := p.Pos.Y - 1; y <= p.Pos.Y+1; y++ { + for x := p.Pos.X - 1; x <= p.Pos.X+1; x++ { + // Don't put a monster on top of the player. + if y == p.Pos.Y && x == p.Pos.X { + continue + } + // Or anything else nasty + if ch := g.Level.VisibleChar(y, x); stepOk(ch) { + if ch == Scroll { + if fo := g.findObj(y, x); fo != nil && fo.Which == SScare { + continue + } + } + if i++; g.rnd(i) == 0 { + mp = Coord{Y: y, X: x} + } + } + } + } + if i == 0 { + g.msg("you hear a faint cry of anguish in the distance") + } else { + tp := &Monster{} + g.newMonster(tp, g.randMonster(false), mp) + } + case SIDPotion, SIDScroll, SIDWeapon, SIDArmor, SIDRorS: + // Identify, let him figure something out + g.Items.Scrolls[obj.Which].Know = true + g.msg("this scroll is an %s scroll", g.Items.Scrolls[obj.Which].Name) + g.whatis(true, idType[obj.Which]) + case SMap: + // Scroll of magic mapping. + g.Items.Scrolls[SMap].Know = true + g.msg("oh, now this scroll has a map on it") + // take all the things we want to keep hidden out of the window + for y := 1; y < NumLines-1; y++ { + for x := 0; x < NumCols; x++ { + pp := g.Level.At(y, x) + ch := pp.Ch + pass := false + switch ch { + case Door, Stairs: + case '-', '|': + if !pp.Flags.Has(FReal) { + ch = Door + pp.Ch = Door + pp.Flags.Set(FReal) + } + case ' ': + if pp.Flags.Has(FReal) { + // def: hidden things in walls stay hidden + if pp.Flags.Has(FPass) { + pass = true + } else { + ch = ' ' + } + } else { + pp.Flags.Set(FReal) + pp.Ch = Passage + pass = true + } + case Passage: + pass = true + case Floor: + if pp.Flags.Has(FReal) { + ch = ' ' + } else { + ch = Trap + pp.Ch = Trap + pp.Flags.Set(FSeen | FReal) + } + default: + if pp.Flags.Has(FPass) { + pass = true + } else { + ch = ' ' + } + } + if pass { + if !pp.Flags.Has(FReal) { + pp.Ch = Passage + } + pp.Flags.Set(FSeen | FReal) + ch = Passage + } + if ch != ' ' { + if tp := pp.Monst; tp != nil { + tp.OldCh = ch + if !p.On(SeeMonst) { + g.mvaddch(y, x, ch) + } + } else { + g.mvaddch(y, x, ch) + } + } + } + } + case SFDet: + // Food detection + found := false + g.scr.Hw.Clear() + for _, fo := range g.Level.Objects { + if fo.Type == Food { + found = true + g.scr.Hw.MvAddCh(fo.Pos.Y, fo.Pos.X, Food) + } + } + if found { + g.Items.Scrolls[SFDet].Know = true + g.showWin("Your nose tingles and you smell food.--More--") + } else { + g.msg("your nose tingles") + } + case STelep: + // Scroll of teleportation: make him disappear and reappear + curRoom := p.Room + g.teleport() + if curRoom != p.Room { + g.Items.Scrolls[STelep].Know = true + } + case SEnch: + if p.CurWeapon == nil || p.CurWeapon.Type != Weapon { + g.msg("you feel a strange sense of loss") + } else { + p.CurWeapon.Flags.Clear(IsCursed) + if g.rnd(2) == 0 { + p.CurWeapon.HPlus++ + } else { + p.CurWeapon.DPlus++ + } + g.msg("your %s glows %s for a moment", + g.Items.Weapons[p.CurWeapon.Which].Name, g.pickColor("blue")) + } + case SScare: + // Reading it is a mistake and produces laughter at her poor boo + // boo. + g.msg("you hear maniacal laughter in the distance") + case SRemove: + uncurse(p.CurArmor) + uncurse(p.CurWeapon) + uncurse(p.CurRing[Left]) + uncurse(p.CurRing[Right]) + g.msg("%s", g.chooseStr("you feel in touch with the Universal Onenes", + "you feel as if somebody is watching over you")) + case SAggr: + // This scroll aggravates all the monsters on the current level + // and sets them running towards the hero + g.aggravate() + g.msg("you hear a high pitched humming noise") + case SProtect: + if p.CurArmor != nil { + p.CurArmor.Flags.Set(IsProt) + g.msg("your armor is covered by a shimmering %s shield", + g.pickColor("gold")) + } else { + g.msg("you feel a strange sense of loss") + } + } + g.look(true) // put the result of the scroll on the screen + g.status() + + g.callIt(&g.Items.Scrolls[obj.Which]) +} + +// uncurse uncurses an item (scrolls.c uncurse). +func uncurse(obj *Object) { + if obj != nil { + obj.Flags.Clear(IsCursed) + } +}