Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/Saludame.activity/events.py
blob: b2c978e2d1e6c7fe2fb70394c94b435863af1fc9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# -*- coding: utf-8 -*-

MAX_BAR_VALUE = 100.0 #maximo valor que puede alcanzar una barra
                      #necesario para el calculo de probabilidades

class Event:
    
    def __init__(self, directory_path, name, description, impact, trigger, time_span, conditioned_bars, effect, library_link, level, preferred_mood):
        
        if not time_span:
            time_span = 999
        
        self.directory_path = directory_path
        self.name = name
        self.description = description
        
        self.impact = impact
        self.preferred_mood = preferred_mood
        
        self.trigger = trigger          # "random" - it's selected at random by it's probability,
                                        # "triggered" - it should appear only when it's triggered as a consequence of an action and it's probability is higher than zero
        self.time_span = time_span
        self.time_left = time_span

        self.effect = effect
        
        self.operator = conditioned_bars[0]
        self.condicioned_bars = conditioned_bars[1]
        self.condicioned_probability = 0.0 # starts in 0.0
        self.level = level      # Starting level, in levels prior to this one, the event is not triggered

        self.library_link = library_link
        
        self.restrictions = {}
    
    def check_restrictions(self, restrictions):
        for restriction_id, values in self.restrictions.items():
            value = restrictions[restriction_id]
            if not value in values:
                return False
        return True
        
    def update_probability(self, bars_value_dic, restrictions, triggered = False):
        """
        Updates event probability
        """
        
        self.condicioned_probability = 0.0
        
        if not self.check_restrictions(restrictions):
            return 0.0
            
        probs = []
        
        if self.condicioned_bars:
            
            for bar_con in self.condicioned_bars:
                bar_id, probability_type, threshold, max_prob = bar_con
                
                bar_value = bars_value_dic[bar_id]
                
                prob = 0.0
                if probability_type == "direct":
                    if bar_value >= threshold:
                        prob = max_prob * ((bar_value - threshold) / (MAX_BAR_VALUE - threshold))
                    
                elif probability_type == "indirect":
                    if bar_value <= threshold:
                        prob = max_prob * ((threshold - bar_value) / threshold)
                    
                elif probability_type == "constant+":
                    if bar_value >= threshold :
                        prob = float(max_prob)
                
                elif probability_type == "constant-":
                    if bar_value <= threshold :
                        prob = float(max_prob)

                elif probability_type == "range":
                    rMin, rMax = threshold
                    pMin, pMax = max_prob
                    if rMin <= bar_value and bar_value <= rMax:
                        prob = pMin + (bar_value-rMin)*(pMax-pMin)/(rMax-rMin)
                
                if self.operator == "all" and prob == 0.0:
                    return 0.0
                    
                probs.append(prob)
        
        if probs:
            self.condicioned_probability = sum(probs) / len(probs)
        
        return self.condicioned_probability
    
    def get_probability(self):
        #eventually we just take the conditioned
        #probability
        return int(self.condicioned_probability)
        
        
    def perform(self):
        if self.time_left is None or self.time_left:
            if self.effect:
                self.effect.activate(1)
                
            if not self.time_left is None:
                self.time_left -= 1
    
    def reset(self):
        self.time_left = self.time_span
    
    def add_restriction(self, restriction_id, values):
        """ Adds a restriction for this event to be triggered
            place: []
            weather: []
            time: []
            clothes: []
        """
        self.restrictions[restriction_id] = values
        
class PersonalEvent(Event):
    def __init__(self, picture, kid_animation_path, name, description, impact, trigger, time_span, conditioned_bars, effect, kid_message, library_link, level=1, preferred_mood=9):
        Event.__init__(self, picture, name, description, impact, trigger, time_span, conditioned_bars, effect, library_link, level, preferred_mood)
        
        self.kid_animation_path = kid_animation_path
    
        # Messages at ballon
        self.kid_message = kid_message
        self.message_time_span = 250
    
    
class SocialEvent(Event):
    
    def __init__(self, picture, person_path, name, description, impact, trigger, time_span, conditioned_bars, effect, message, library_link, level=1, preferred_mood=9):
        Event.__init__(self, picture, name, description, impact, trigger, time_span, conditioned_bars, effect, library_link, level, preferred_mood)
        
        self.time_left = time_span
        
        self.person_path = person_path
        self.person_message = message
        self.message_time_span = 250