-
Notifications
You must be signed in to change notification settings - Fork 0
/
TradeShip.cs
464 lines (407 loc) · 16.2 KB
/
TradeShip.cs
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnityEngine;
using Tasharen;
using System.Collections;
[RequireComponent(typeof(GameShip))]
public class TradeShip : TNBehaviour
{
public GameShip gameShip { get; set; }
const float RANGE = 8f;
private static readonly int DEFAULT_CARGO_SLOTS = 2;
public int cost;
public int profit = 0;
public TNet.Player Owner;
public int cargoSlots = 2;
[NonSerialized]
public List<TradeMission> TradeMissions = new List<TradeMission>();
protected override void OnEnable()
{
base.OnEnable();
this.gameShip = this.GetComponent<GameShip>();
this.gameShip.onDeath += new GameShip.DeathCallback(this.Died);
StartCoroutine(DoUpdateShipToPlayer());
}
protected virtual void OnDisable()
{
StopCoroutine(DoUpdateShipToPlayer());
}
private void Died(GameShip ship)
{
this.gameShip.onDeath -= new GameShip.DeathCallback(this.Died);
Dettach();
if (Owner != null && Owner.id == MyPlayer.id)
{
int cargoWorth = TradeMissions.Sum(m => !m.Completed ? m.PurchasePrice : 0);
if (cargoWorth == 0)
{
TradeChat.Chat(Localization.Format("Ship destroyed", this.gameShip.name));
}
else
{
TradeChat.Chat(Localization.Format("Ship destroyed with cargo", this.gameShip.name, Format.FormatGold(cargoWorth)));
MyPlayer.ModifyResource("gold", -cargoWorth, true);
MyPlayer.Sync();
}
}
}
public GameShip GetGameShip()
{
return gameShip;
}
public Boolean HasResource(string resource)
{
return TradeMissions.Where(x => x.StockedUp() && x.ResourceName.Equals(resource)).Any();
}
internal void SetTradeMissions(List<TradeMission> tradeMissions)
{
this.TradeMissions = tradeMissions;
}
public void UpdateNavigation()
{
var GameShip = GetGameShip();
if (GameShip != null && GameShip.position != null && GameShip.isActive)
{
TradeMission currentTradeMission = TradeMissions.FirstOrDefault();
if (currentTradeMission != null)
{
Boolean selling = currentTradeMission.StockedUp();
var destination = currentTradeMission.GetDestination();
var departure = currentTradeMission.GetDeparture();
GameTown currentDestinationTown = selling ? currentTradeMission.GetDestination() : currentTradeMission.GetDeparture();
if (selling && InRange(destination))
{
Sell(destination);
}
else if (!selling && InRange(departure))
{
Buy(departure);
}
else
{
AIShip aiShip = GameShip.GetComponent<AIShip>();
if (aiShip != null)
{
aiShip.NavigateTo(currentDestinationTown.dockPos, 2);
}
}
}
}
}
public void UpdateStatus()
{
var GameShip = GetGameShip();
if(GameShip.ai.state != AIShip.State.Combat) {
var tradeMissions = TradeMissions;
TradeMission currentTradeMission = tradeMissions.FirstOrDefault();
if (currentTradeMission != null)
{
Boolean selling = currentTradeMission.StockedUp();
GameTown currentDestinationTown = selling ? currentTradeMission.GetDestination() : currentTradeMission.GetDeparture();
AIShip aiShip = GameShip.GetComponent<AIShip>();
if (aiShip != null)
{
if (selling)
{
string[] resources = tradeMissions.Where(m => m.Destination == currentTradeMission.Destination && m.StockedUp())
.Select(m => Localization.Get(m.ResourceName))
.GroupBy(x => x)
.Select(g => g.Key + (g.Count() > 1 ? " (x" + g.Count() + ")" : "")).ToArray();
string resourcesCS = string.Join(", ", resources);
AddHudText(Localization.Format("Delivering",resourcesCS, Format.FormatTown(currentDestinationTown)), 1f);
}
else
{
string[] resources = tradeMissions.Where(m => m.Departure == currentTradeMission.Departure && !m.StockedUp()).Select(m => Localization.Get(m.ResourceName))
.GroupBy(x => x)
.Select(g => g.Key + (g.Count() > 1 ? " (x" + g.Count() + ")" : "")).ToArray();
string resourcesCS = string.Join(", ", resources);
AddHudText(Localization.Format("Buying", resourcesCS, Format.FormatTown(currentDestinationTown)), 1f);
}
}
} else
{
AddHudText("Where are the good deals ?", 1f);
}
}
}
internal void Activate()
{
var gameShip = GetGameShip();
tno.onDestroy += Dettach;
this.TradeMissions = new List<TradeMission>();
this.cargoSlots = CalculateCargoSlots(gameShip.prefabName);
}
public void AddHudText(string message, float duration)
{
GetGameShip().AddHudText(message, Color.gray, duration, false);
}
public void UpdateTradeMissions()
{
TradeMissions.ForEach(x => UpdateTradeMission(x));
TradeMissions.RemoveAll(x => !x.Valid);
if (TradeMissions.Count() < cargoSlots)
{
List<TradeMission> newTradeMissions = TradeMissionCalculator.FindTradeMissions(GetGameShip(), cargoSlots - TradeMissions.Count());
TradeMissions.AddRange(newTradeMissions);
}
}
private Boolean InRange(GameTown gameTown)
{
var GameShip = GetGameShip();
if (gameTown == null || gameTown.dockPos == null || GameShip == null || GameShip.position == null)
{
return false;
}
float range = Math.Max(Math.Max(
Math.Abs(gameTown.dockPos.x - GameShip.position.x),
Math.Abs(gameTown.dockPos.y - GameShip.position.y)),
Math.Abs(gameTown.dockPos.z - GameShip.position.z));
return range <= RANGE; ;
}
private void Sell(GameTown gameTown)
{
var GameShip = GetGameShip();
if (gameTown != null && GameShip.position != null)
{
List<TradeMission> tradeMissionsToRemove = new List<TradeMission>();
foreach (TradeMission tradeMission in TradeMissions.Where(m => m.Valid))
{
if (tradeMission.StockedUp() && tradeMission.Destination.Equals(gameTown.id))
{
int demand = gameTown.GetDemand(tradeMission.ResourceName);
if (demand > 0)
{
Sell(tradeMission.Destination, tradeMission.ResourceName, tradeMission.PurchasePrice);
tradeMissionsToRemove.Add(tradeMission);
}
else if (tradeMission.Destination == tradeMission.Departure)
{
BuyBack(tradeMission.Departure, tradeMission.ResourceName, tradeMission.PurchasePrice);
tradeMissionsToRemove.Add(tradeMission);
}
else
{
TradeMission newTradeMission = TradeMissionCalculator.FindATradeMissionForResource(tradeMission.ResourceName, tradeMission.GetDeparture(), GameShip.position);
if (newTradeMission != null)
{
tradeMission.Destination = newTradeMission.Destination;
}
else
{
//BuyBack
tradeMission.Destination = tradeMission.Departure;
}
}
}
}
if (tradeMissionsToRemove.Count() > 0)
{
RemoveTradeMissions(tradeMissionsToRemove);
}
}
}
public void BuyBack(int departureId, string resourceName, int purchasePrice)
{
if (Owner != null)
{
float multi = GameZone.rewardMultiplier;
var gameTown = GameTown.Find(departureId);
int price = gameTown.GetBuyBackOffer(resourceName, purchasePrice, multi);
gameTown.IncreaseProduction(resourceName);
MyPlayer.ModifyResource("gold", price - purchasePrice, true);
int profit = price - purchasePrice;
this.profit += profit;
var profitText = profit >= 0 ? Localization.Format("profit") : Localization.Format("loss") + " : " + Format.FormatGold(Math.Abs(profit));
TradeChat.Bad(Localization.Format("BuyBack", Format.FormatShip(GetGameShip()), Localization.Get(resourceName), Format.FormatTown(gameTown), profitText));
MyPlayer.saveNeeded = true;
MyPlayer.Sync();
}
}
public void Sell(int destination, string resourceName, int purchasePrice)
{
if (Owner != null)
{
var gameTown = GameTown.Find(destination);
var playerItem = ResourceFactory.CreatePlayerItemForResource(gameTown, resourceName, purchasePrice);
int salePrice = gameTown.Sell(playerItem);
if (salePrice > 0)
{
int profit = salePrice - purchasePrice;
MyPlayer.ModifyResource("gold", -profit, true);
MyPlayer.Sync();
string profitOrLoss = profit >= 0 ?
Localization.Format("profit") :
Localization.Format("loss");
string profitText = profitOrLoss + " : " + Format.FormatGold(Math.Abs(profit));
var text = Localization.Format(
"Sold",
Format.FormatShip(GetGameShip()),
Localization.Get(resourceName),
Format.FormatTown(gameTown),
profitText) ;
this.profit += profit;
if (profit > 0)
{
TradeChat.Chat(text);
}
else
{
TradeChat.Bad(text);
}
MyPlayer.saveNeeded = true;
MyPlayer.Sync();
}
}
}
private IEnumerator DoUpdateShipToPlayer()
{
for (; ; )
{
UpdateShip();
yield return new WaitForSeconds(5f);
}
}
private void UpdateShip()
{
if (Owner != null)
{
var player = GamePlayer.Find(Owner.id);
if (player != null && player.ship != null)
{
gameShip.factionID = player.factionID;
gameShip.ai.state = AIShip.State.Idle;
gameShip.symbolTex = player.ship.symbolTex;
gameShip.sailColor0 = player.ship.sailColor0;
gameShip.sailColor1 = player.ship.sailColor1;
gameShip.hullColor0 = player.ship.hullColor0;
gameShip.hullColor1 = player.ship.hullColor1;
}
}
}
public void Dettach()
{
OnDisable();
}
private void Buy(GameTown gameTown)
{
var GameShip = GetGameShip();
List<TradeMission> tradeMissionsToRemove = new List<TradeMission>();
var tradeMissions = TradeMissions;
foreach (TradeMission tradeMission in tradeMissions.Where(m => m.Valid))
{
var departure = tradeMission.GetDeparture();
if (!tradeMission.StockedUp() && departure.id.Equals(gameTown.id))
{
var production = departure.GetProduction(tradeMission.ResourceName);
if (production >= 1)
{
PlayerItem playerItem = ResourceFactory.CreatePlayerItemForResource(gameTown, tradeMission.ResourceName);
profit -= playerItem.gold;
TradeChat.Chat(Localization.Format("Buying at", Format.FormatShip(gameShip),playerItem.name , Format.FormatTown(gameTown), Format.FormatGold(playerItem.gold)));
tradeMission.GetDeparture().ReduceProduction(tradeMission.ResourceName);
tradeMission.PurchasePrice = playerItem.gold;
} else
{
tradeMissionsToRemove.Add(tradeMission);
}
}
}
if (tradeMissionsToRemove.Count() > 0)
{
RemoveTradeMissions(tradeMissionsToRemove);
}
}
private void RemoveTradeMissions(List<TradeMission> tradeMissionsToRemove)
{
foreach (TradeMission tradeMissionToRemove in tradeMissionsToRemove)
{
TradeMissions.Remove(tradeMissionToRemove);
}
}
private static int CalculateCargoSlots(string name)
{
TNet.DataNode ships = GameConfig.ships;
if (ships == null || ships.children.size == 0)
{
return DEFAULT_CARGO_SLOTS;
}
TNet.DataNode child1 = ships.GetChild(name);
if (child1 == null)
return DEFAULT_CARGO_SLOTS;
return child1.GetChild<int>("cargo");
}
public TNet.Player GetOwner()
{
return Owner;
}
public void UpdateTraderOwner(int? playerId)
{
if (playerId.HasValue)
{
tno.Send("OnUpdateTraderOwner", TNet.Target.AllSaved, playerId.Value);
} else
{
tno.Send("OnClearTraderOwner", TNet.Target.AllSaved);
}
}
[TNet.RFC]
protected void OnUpdateTraderOwner(int playerId)
{
GamePlayer gamePlayer = GamePlayer.Find(playerId);
if (gamePlayer != null) {
this.Owner = gamePlayer.netPlayer;
UpdateShip();
}
}
[TNet.RFC]
protected void OnClearTraderOwner()
{
this.Owner = null;
}
public void UpdateTradeMission(TradeMission tradeMission)
{
if (tradeMission.StockedUp())
{
var destinationTown = tradeMission.GetDestination();
if (destinationTown == null || !destinationTown.NeedsResource(tradeMission.ResourceName) || !GameTownIsAlly(gameShip, destinationTown))
{
if(destinationTown == null)
{
TradeChat.Bad("destination is null");
}
if (!GameTownIsAlly(gameShip, destinationTown))
{
TradeChat.Bad(Localization.Format("Not an ally", Format.FormatShip(gameShip), Format.FormatTown(destinationTown)));
}
if (!destinationTown.NeedsResource(tradeMission.ResourceName))
{
TradeChat.Bad(Localization.Format("Does not need resource", Format.FormatShip(gameShip), Format.FormatTown(destinationTown),Localization.Get(tradeMission.ResourceName)));
}
tradeMission.Valid = false;
}
}
else
{
var departure = tradeMission.GetDeparture();
if (!departure.HasResource(tradeMission.ResourceName) || !GameTownIsAlly(gameShip, departure))
{
if (!GameTownIsAlly(gameShip, departure))
{
TradeChat.Bad(Localization.Format("Not an ally", Format.FormatShip(gameShip), Format.FormatTown(departure)));
}
if (!departure.HasResource(tradeMission.ResourceName))
{
TradeChat.Bad(Localization.Format("Does not have resource", Format.FormatShip(gameShip), Format.FormatTown(departure), Localization.Get(tradeMission.ResourceName)));
}
tradeMission.Valid = false;
}
}
}
public static Boolean GameTownIsAlly(GameShip gameShip, GameTown gameTown)
{
return (gameTown.factionID == 0 && gameShip.factionID == 0) || (gameShip.factionID > 0 && gameTown.factionID > 0);
}
}