-
Notifications
You must be signed in to change notification settings - Fork 0
/
Trader.cs
204 lines (177 loc) · 6.16 KB
/
Trader.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnityEngine;
using Tasharen;
using System.Collections;
using TNet;
using static TNet.GameClient;
public class Trader : MonoBehaviour
{
public static bool isRunning = false;
public static bool ENABLED = false;
private System.Collections.Generic.List<ShipConfiguration> tradeShips = new System.Collections.Generic.List<ShipConfiguration>();
void Start()
{
if (!isRunning)
{
isRunning = true;
UIGameChat.onCommand += ChatInput;
TNManager.onObjectCreated += UpdateToTradeShip;
TNManager.client.onPlayerLeft += RemovePlayer;
StartCoroutine(DoStatusUpdates());
StartCoroutine(DoUpdateNavigation());
StartCoroutine(DoUpdateTradeMissions());
}
}
private void RemovePlayer(Player p)
{
foreach (TradeShip tradeShip in GameWorld.FindObjectsOfType<TradeShip>().Where(x => x.Owner != null && x.Owner.id == p.id))
{
if (tradeShip.gameObject.GetComponent<GameShip>().tno.isMine)
{
tradeShip.UpdateTraderOwner(null);
}
}
}
IEnumerator DoUpdateTradeMissions()
{
for (; ; )
{
try
{
foreach (TradeShip tradeShip in GameWorld.FindObjectsOfType<TradeShip>().Where(x => x.Owner != null && x.Owner.id == MyPlayer.id))
{
tradeShip.UpdateTradeMissions();
}
}
catch (Exception e)
{
UnityEngine.Debug.Log(e);
}
yield return new WaitForSeconds(4f);
}
}
IEnumerator DoUpdateNavigation()
{
for (; ; )
{
try
{
foreach (TradeShip tradeShip in GameWorld.FindObjectsOfType<TradeShip>().Where(x => x.Owner != null && x.Owner.id == MyPlayer.id))
{
tradeShip.UpdateNavigation();
}
}
catch (Exception e)
{
UnityEngine.Debug.Log(e);
}
yield return new WaitForSeconds(1f);
}
}
IEnumerator DoStatusUpdates()
{
for (; ; )
{
try
{
foreach (TradeShip tradeShip in GameWorld.FindObjectsOfType<TradeShip>().Where(x => x.Owner != null && x.Owner.id == MyPlayer.id))
{
tradeShip.UpdateStatus();
}
}
catch (Exception e)
{
UnityEngine.Debug.Log(e);
}
yield return new WaitForSeconds(3f);
}
}
private void UpdateToTradeShip(GameObject go)
{
var gameMap = FindObjectOfType<UIGameMap>();
if(gameMap != null)
{
gameMap.overrideSprite = null;
}
if (GameZone.regionType == GameZone.RegionType.World || GameZone.regionType == GameZone.RegionType.Questing)
{
GameShip gameShip = go.GetComponent<GameShip>();
if (gameShip != null && gameShip.player == null)
{
go.AddMissingComponent<TradeShip>().Activate();
go.AddMissingComponent<TraderClickableScript>();
//Destroy(GetComponent<UIGameMapIcon>());
}
}
}
public static TradeShip FindTradeShip(int id)
{
return GameWorld.FindObjectsOfType<TradeShip>().Where(x => x.gameShip.id == id).FirstOrDefault(); ;
}
private void ChatInput(string msg, ref bool handled)
{
handled = false;
}
public void Create(System.Collections.Generic.List<ShipConfiguration> shipConfigurations)
{
if (tradeShips.Count > 0)
{
foreach (ShipConfiguration shipConfiguration in shipConfigurations)
{
TradeChat.Log("Trying to create " + shipConfiguration.name);
GameShip.Create(shipConfiguration.name, shipConfiguration.prefab, MyPlayer.factionID, MyPlayer.ship.position, 0f, 0, 1.5f, 0.75f, 0);
}
StartCoroutine(FindPlayerShip(shipConfigurations));
}
}
IEnumerator FindPlayerShip(System.Collections.Generic.List<ShipConfiguration> shipConfigurations)
{
yield return new WaitForSeconds(1f);
for (; ; )
{
if (MyPlayer.ship != null && MyPlayer.ship.position != null)
{
TradeChat.Log("Player ship found");
StartCoroutine(FindTrader(shipConfigurations));
StopCoroutine(FindPlayerShip(shipConfigurations));
}
yield return new WaitForSeconds(0.5f);
}
}
IEnumerator FindTrader(System.Collections.Generic.List<ShipConfiguration> shipConfigurations)
{
int instancesFound = 0;
for (; ; )
{
foreach (ShipConfiguration shipConfiguration in shipConfigurations)
{
foreach(GameShip g in GameWorld.FindObjectsOfType<GameShip>())
{
TradeChat.Log(g.name);
}
var possibleTrader = GameWorld.FindObjectsOfType<GameShip>().Where(x => x.name.Equals(shipConfiguration.name)).FirstOrDefault();
if (possibleTrader != null)
{
TradeChat.Log("Possible trader found: " + shipConfiguration.name);
TradeShip traderShip = possibleTrader.GetComponent<TradeShip>();
if (traderShip == null)
{
traderShip = possibleTrader.gameObject.AddMissingComponent<TradeShip>();
traderShip.Activate();
}
traderShip.UpdateTraderOwner(MyPlayer.id);
traderShip.SetTradeMissions(shipConfiguration.tradeMissions);
instancesFound++;
}
if (instancesFound == shipConfigurations.Count)
{
StopCoroutine(FindTrader(shipConfigurations));
}
}
yield return new WaitForSeconds(2f);
}
}
}