from core import *
from util import *
import event

class Civ(Actor):
    ld = 0 # last drink
    cash = 100
    def __init__(S):
        super(Civ,S).__init__()
        S.init()

    def init(S):
        event.adde(randint(10,15),0,nup,[S])

    def __setstate__(S,s):
        super(Civ,S).__setstate__(s)
        S.init()

    # ranks different drinks in order of interest
    def rankall(S):
        ranks = []
        for o in Thing.all:
            if o.shop:
                for z in o.shop.inv:
                    if is_a(z[0],Drink):
                        ranks += [[S.rank(o.room,*z),o.room,o.shop,z]]
        return sorted(ranks,reverse=1)

    # How much do I want a particular drink?
    # This formula is really lousy. Fix it.
    def rank(S,r,x,q,pr):
        s = randint(0,10)            # starting score
        if not q: s -= 999           # out of stock
        if S.ld == x.fls: s -= 200   # already drank one
        if r == S.room: s += 10      # no walking == good
        s += (S.cash/pr)**0.3 * 10   # Price matters
        c = '/'.join(x.fls)

        # Try something new? Familiar flavors help.
        # Ideally, there would be a slight chance that NPCs would go try out
        # new drinks, but the better way would be advertising and free samples
        if c not in S.tastes:
            s += 100 + 2*sum([S.tastes[fl] for fl in x.fls if fl in S.tastes])
        else:
            s += S.tastes[c] * 5
        return s

class Pie(Item):
    dam = 16
    def on_splat(S,v,by):
        Act(S,v).vict("$SN hits you in the face!").room("$SN splatters on $VN's face!")
        by.rhit(S.dam,"pie throw")
        S.goto(0)

class Drink(Item):
    fls = ()
    dam = 12
    def on_splat(S,v,by):
        Act(S,v).vict("$SN spills all over you!").room("$SN spills all over $VN!")
        by.rhit(S.dam,"spillage")
        S.goto(0)

    def on_drink(S,by):
        Act(by,S).subj("You drink $VN.").room("$SN drinks $VN.")
        if is_a(by,Civ):
            by.ld = S.fls
            c = '/'.join(S.fls) # combo name
            if c not in by.tastes: # new combo based on avg of old tastes
                by.tastes[c] = gauss(0,20)
                for fl in S.fls:
                    by.tastes[c] += by.tastes.setdefault(fl,gauss(10,5)) / len(S.fls)
        S.goto(0)

class Vial(Item):
    def on_drop(S,by): Item.on_drop(S,by); S.on_splat(0,by)
    def on_splat(S,v,by): # sorry, not a weapon to make opponent smell bad
        Act(S).room("$SN shatters, releasing a foul gas.")
        # random npcs flee
        as = [a for a in S.room.people if a.npc and not a.foe]
        for a in sample(as,randint(0,len(as))): a.flee()
        S.goto(0)

start = wload()