Elin Decompiled Documentation EA 23.350.1 Nightly
Loading...
Searching...
No Matches
AI_Shopping.cs
Go to the documentation of this file.
1using System.Collections.Generic;
2using UnityEngine;
3
4public class AI_Shopping : AIAct
5{
6 public Card container;
7
8 public Card dest;
9
10 private static Dictionary<Card, List<Card>> _saleLists = new Dictionary<Card, List<Card>>();
11
12 private static Dictionary<Card, int> _salePrices = new Dictionary<Card, int>();
13
14 private static int _saleHour = -1;
15
16 public static bool TryShop(Chara c, bool realtime)
17 {
18 if (c.memberType != FactionMemberType.Guest || !EClass._zone.IsPCFaction || EClass._map.props.sales.Count == 0)
19 {
20 return false;
21 }
22 Card card = EClass._map.props.sales.RandomItem();
23 int c_allowance = c.c_allowance;
24 if (!EClass.debug.enable && c_allowance <= 0)
25 {
26 return false;
27 }
28 bool flag = card.IsContainer && card.things.Count > 0;
29 bool flag2 = !realtime && VirtualDate.IsActive;
30 if (flag2)
31 {
32 int raw = VirtualDate.current.GetRaw();
33 if (raw != _saleHour)
34 {
35 _saleHour = raw;
36 _saleLists.Clear();
37 _salePrices.Clear();
38 }
39 }
40 if (!flag2 || !_saleLists.TryGetValue(card, out var value))
41 {
42 value = new List<Card>();
43 if (flag)
44 {
45 foreach (Thing thing in card.things)
46 {
47 if (TraitSalesTag.CanTagSale(thing, insideContainer: true))
48 {
49 value.Add(thing);
50 }
51 }
52 }
53 else
54 {
55 value.Add(card);
56 }
57 if (flag2)
58 {
59 _saleLists[card] = value;
60 }
61 }
62 foreach (Card item in value)
63 {
64 if (item.isDestroyed)
65 {
66 continue;
67 }
68 int num = 25;
69 if (!flag2 || !_salePrices.TryGetValue(item, out var value2))
70 {
71 value2 = item.GetPrice(CurrencyType.Money, sell: true, PriceType.PlayerShop);
72 if (flag2)
73 {
74 _salePrices[item] = value2;
75 }
76 }
77 if (value2 >= c_allowance)
78 {
79 num = num * 10 * value2 / c_allowance;
80 }
81 if (value2 >= 10000)
82 {
83 num *= 15;
84 }
85 else if (value2 >= 5000)
86 {
87 num *= 10;
88 }
89 else if (value2 >= 1000)
90 {
91 num *= 6;
92 }
93 else if (value2 >= 200)
94 {
95 num *= 3;
96 }
97 if (EClass.Branch != null)
98 {
100 {
101 num = num * (300 + EClass.Branch.Evalue(2817)) / 100;
102 }
103 if (EClass.Branch.policies.IsActive(2816))
104 {
105 num = num * (1000 + EClass.Branch.Evalue(2816) * 2) / 100;
106 }
107 }
108 if (!realtime)
109 {
110 num /= 4;
111 }
112 if (EClass.debug.enable || EClass.rnd(num + 1) == 0)
113 {
114 if (realtime)
115 {
116 c.SetAI(new AI_Shopping
117 {
118 container = (flag ? card : null),
119 dest = item
120 });
121 return true;
122 }
123 Buy(c, item, realtime: false, flag ? card : null);
124 }
125 }
126 return false;
127 }
128
129 public static bool TryRestock(Chara c, bool realtime)
130 {
131 for (int i = 0; i < (realtime ? 1 : 10); i++)
132 {
133 bool flag = false;
134 foreach (Thing thing2 in EClass._map.things)
135 {
136 if (EClass.rnd(2) == 0 || !thing2.IsInstalled || !(thing2.trait is TraitSalesTag { IsOn: not false }) || thing2.GetStr(11).IsEmpty())
137 {
138 continue;
139 }
140 Thing thing = EClass._zone.TryGetRestock<TraitSpotStock>(thing2.GetStr(11));
141 if (thing != null)
142 {
143 if (realtime)
144 {
145 thing2.PlaySound("restock");
146 }
147 thing2.Destroy();
148 thing.isSale = true;
149 EClass._zone.AddCard(thing, thing2.pos).Install();
150 flag = true;
151 break;
152 }
153 }
154 if (!flag)
155 {
156 break;
157 }
158 }
159 return false;
160 }
161
162 public override IEnumerable<Status> Run()
163 {
164 Card _dest = container ?? dest;
165 if (_dest.ExistsOnMap)
166 {
167 yield return DoGoto(_dest.pos, 1);
168 }
169 if (!_dest.ExistsOnMap || !_dest.isSale)
170 {
171 yield return Success();
172 }
173 Buy(owner, dest, realtime: true, container);
174 yield return Success();
175 }
176
177 public static void SellChara(Chara c)
178 {
179 Msg.Say("sell_resident", c);
180 c.homeBranch.BanishMember(c, skipMsg: true);
182 }
183
184 public static void Buy(Chara c, Card dest, bool realtime, Card container)
185 {
186 Point point = dest.pos.Copy();
187 int price = dest.GetPrice(CurrencyType.Money, sell: true, PriceType.PlayerShop);
188 if ((dest.isThing && (dest.isDestroyed || dest.Thing.IsSharedContainer)) || (container == null && !dest.isSale) || (container != null && container.c_lockLv != 0))
189 {
190 return;
191 }
192 Card card = (dest.isChara ? dest : dest.Split(1));
193 EClass.Branch.incomeShop += price;
194 card.isSale = false;
195 if (card == dest)
196 {
197 EClass._map.props.sales.Remove(card);
198 }
199 if (realtime)
200 {
201 c.Talk("shop_buy");
202 c.PlaySound("money");
203 Msg.alwaysVisible = true;
204 Msg.Say("shop_buy", c, card, Lang._currency(price, "money"));
205 if (card.isThing)
206 {
207 c.AddCard(card);
208 c.c_allowance -= price;
209 if (!c.TryUse(card.Thing))
210 {
211 card.ModNum(-1);
212 }
213 }
214 else
215 {
216 SellChara(card.Chara);
217 }
218 }
219 else if (card.isThing)
220 {
221 card.ModNum(-1);
222 }
223 else
224 {
225 SellChara(card.Chara);
226 }
227 if (card.isStolen || card.isChara)
228 {
229 Guild.Thief.AddContribution((int)Mathf.Sqrt(price) / 2);
230 }
231 if (container != null)
232 {
233 if (container.things.Count == 0)
234 {
235 container.isSale = false;
237 }
238 }
239 else if (!dest.ExistsOnMap && dest.isThing)
240 {
241 Thing thing = ThingGen.Create("tag_sell");
242 thing.isOn = true;
243 thing.SetStr(11, dest.category.id);
244 EClass._zone.AddCard(thing, point).Install();
245 }
246 EClass.Branch.Log("shop_buy", c, card, Lang._currency(price, "money"));
247 }
248}
CurrencyType
Definition: CurrencyType.cs:2
static bool not(DramaManager dm, Dictionary< string, string > line, params string[] parameters)
FactionMemberType
PriceType
Definition: PriceType.cs:2
Definition: AIAct.cs:6
new Chara owner
Definition: AIAct.cs:14
Status DoGoto(Point pos, int dist=0, bool ignoreConnection=false, Func< Status > _onChildFail=null)
Definition: AIAct.cs:446
static void Buy(Chara c, Card dest, bool realtime, Card container)
Definition: AI_Shopping.cs:184
static bool TryRestock(Chara c, bool realtime)
Definition: AI_Shopping.cs:129
static Dictionary< Card, List< Card > > _saleLists
Definition: AI_Shopping.cs:10
static bool TryShop(Chara c, bool realtime)
Definition: AI_Shopping.cs:16
static int _saleHour
Definition: AI_Shopping.cs:14
static void SellChara(Chara c)
Definition: AI_Shopping.cs:177
static Dictionary< Card, int > _salePrices
Definition: AI_Shopping.cs:12
override IEnumerable< Status > Run()
Definition: AI_Shopping.cs:162
Card container
Definition: AI_Shopping.cs:6
Definition: Card.cs:11
bool isDestroyed
Definition: Card.cs:78
virtual bool isThing
Definition: Card.cs:2133
Thing Split(int a)
Definition: Card.cs:3665
SoundSource PlaySound(string id, float v=1f, bool spatial=true)
Definition: Card.cs:6645
virtual int GetPrice(CurrencyType currency=CurrencyType.Money, bool sell=false, PriceType priceType=PriceType.Default, Chara c=null)
Definition: Card.cs:7781
int c_lockLv
Definition: Card.cs:991
Card AddCard(Card c)
Definition: Card.cs:3266
bool isSale
Definition: Card.cs:869
void Talk(string idTopic, string ref1=null, string ref2=null, bool forceSync=false)
Definition: Card.cs:7206
bool ExistsOnMap
Definition: Card.cs:2137
string GetStr(string id, string defaultStr=null)
Definition: Card.cs:2595
Point pos
Definition: Card.cs:60
int c_allowance
Definition: Card.cs:1357
void Destroy()
Definition: Card.cs:5314
void SetStr(string id, string value=null)
Definition: Card.cs:2600
ThingContainer things
Definition: Card.cs:39
virtual Thing Thing
Definition: Card.cs:2110
Card Install()
Definition: Card.cs:3922
SourceCategory.Row category
Definition: Card.cs:2101
Definition: Chara.cs:10
bool TryUse(Thing t)
Definition: Chara.cs:8384
FactionMemberType memberType
Definition: Chara.cs:46
FactionBranch homeBranch
Definition: Chara.cs:1113
bool enable
Definition: CoreDebug.cs:302
int GetRaw(int offsetHours=0)
Definition: Date.cs:328
Definition: EClass.cs:6
static Zone _zone
Definition: EClass.cs:21
static Map _map
Definition: EClass.cs:19
static int rnd(long a)
Definition: EClass.cs:59
static FactionBranch Branch
Definition: EClass.cs:23
static Player player
Definition: EClass.cs:13
static CoreDebug debug
Definition: EClass.cs:49
int Evalue(int ele)
PolicyManager policies
string Log(string idLang, string ref1=null, string ref2=null, string ref3=null, string ref4=null)
void BanishMember(Chara c, bool skipMsg=false)
void AddContribution(int a)
Definition: FACTION.cs:318
Definition: Guild.cs:2
static GuildThief Thief
Definition: Guild.cs:60
Definition: Lang.cs:7
static string _currency(object a, string IDCurrency)
Definition: Lang.cs:161
List< Thing > things
Definition: Map.cs:52
PropsManager props
Definition: Map.cs:94
Definition: Msg.cs:5
static string Say(string idLang, string ref1, string ref2=null, string ref3=null, string ref4=null)
Definition: Msg.cs:58
void ModKarma(int a)
Definition: Player.cs:2636
Definition: Point.cs:9
Point Copy()
Definition: Point.cs:491
bool IsActive(int id, int days=-1)
List< Card > sales
Definition: PropsManager.cs:14
static Thing Create(string id, int idMat=-1, int lv=-1)
Definition: ThingGen.cs:53
Definition: Thing.cs:8
bool IsSharedContainer
Definition: Thing.cs:110
static bool CanTagSale(Card t, bool insideContainer=false)
Definition: TraitSalesTag.cs:9
static bool IsActive
Definition: VirtualDate.cs:13
static Date current
Definition: VirtualDate.cs:5
bool IsPCFaction
Definition: Zone.cs:480
Card AddCard(Card t, Point point)
Definition: Zone.cs:2151