| | 1 | | using System.Collections; |
| | 2 | | using System.Collections.Generic; |
| | 3 | | using DCL.Controllers; |
| | 4 | | using DCL.Interface; |
| | 5 | | using DCL.Models; |
| | 6 | | using DCL.Helpers; |
| | 7 | | using Google.Protobuf; |
| | 8 | | using UnityEngine; |
| | 9 | | using DCL.Configuration; |
| | 10 | | using Random = UnityEngine.Random; |
| | 11 | |
|
| | 12 | | namespace DCL.Bots |
| | 13 | | { |
| | 14 | | /// <summary> |
| | 15 | | /// Bots Tool: BotsController |
| | 16 | | /// |
| | 17 | | /// Used to spawn bots/avatarShapes for debugging and profiling purposes. |
| | 18 | | /// </summary> |
| | 19 | | public class BotsController : IBotsController |
| | 20 | | { |
| | 21 | | private ParcelScene globalScene; |
| 531 | 22 | | private List<string> randomizedCollections = new List<string>(); |
| 531 | 23 | | private List<string> instantiatedBots = new List<string>(); |
| 531 | 24 | | private List<string> eyesWearableIds = new List<string>(); |
| 531 | 25 | | private List<string> eyebrowsWearableIds = new List<string>(); |
| 531 | 26 | | private List<string> mouthWearableIds = new List<string>(); |
| 531 | 27 | | private List<string> hairWearableIds = new List<string>(); |
| 531 | 28 | | private List<string> facialWearableIds = new List<string>(); |
| 531 | 29 | | private List<string> upperBodyWearableIds = new List<string>(); |
| 531 | 30 | | private List<string> lowerBodyWearableIds = new List<string>(); |
| 531 | 31 | | private List<string> feetWearableIds = new List<string>(); |
| 531 | 32 | | private List<string> bodyshapeWearableIds = new List<string>(); |
| | 33 | |
|
| | 34 | | /// <summary> |
| | 35 | | /// Makes sure the Catalogue with all the wearables has already been loaded, otherwise it loads it |
| | 36 | | /// </summary> |
| | 37 | | private IEnumerator EnsureGlobalSceneAndCatalog() |
| | 38 | | { |
| 0 | 39 | | if (globalScene != null) |
| 0 | 40 | | yield break; |
| | 41 | |
|
| 0 | 42 | | globalScene = Environment.i.world.state.loadedScenes[Environment.i.world.state.globalSceneIds[0]] as ParcelS |
| | 43 | |
|
| 0 | 44 | | CatalogController.wearableCatalog.Clear(); |
| | 45 | |
|
| 0 | 46 | | yield return WearablesFetchingHelper.GetRandomCollections(20, true, randomizedCollections); |
| | 47 | |
|
| 0 | 48 | | List<WearableItem> wearableItems = new List<WearableItem>(); |
| 0 | 49 | | yield return WearablesFetchingHelper.GetWearableItems(BuildRandomizedCollectionsURL(), wearableItems); |
| | 50 | |
|
| 0 | 51 | | PopulateCatalog(wearableItems); |
| 0 | 52 | | } |
| | 53 | |
|
| | 54 | | string BuildRandomizedCollectionsURL() |
| | 55 | | { |
| 0 | 56 | | if (randomizedCollections.Count == 0) |
| 0 | 57 | | return null; |
| | 58 | |
|
| 0 | 59 | | string finalUrl = WearablesFetchingHelper.WEARABLES_FETCH_URL; |
| | 60 | |
|
| 0 | 61 | | finalUrl += "collectionId=" + randomizedCollections[0]; |
| 0 | 62 | | for (int i = 1; i < randomizedCollections.Count; i++) |
| | 63 | | { |
| 0 | 64 | | finalUrl += "&collectionId=" + randomizedCollections[i]; |
| | 65 | | } |
| | 66 | |
|
| 0 | 67 | | return finalUrl; |
| | 68 | | } |
| | 69 | |
|
| | 70 | | /// <summary> |
| | 71 | | /// Populates the catalogue and internal avatar-part divided collections for optimized randomization |
| | 72 | | /// </summary> |
| | 73 | | /// <param name="newWearables">The list of WearableItem objects to be added to the catalog</param> |
| | 74 | | private void PopulateCatalog(List<WearableItem> newWearables) |
| | 75 | | { |
| 0 | 76 | | foreach (var wearableItem in newWearables) |
| | 77 | | { |
| 0 | 78 | | switch (wearableItem.data.category) |
| | 79 | | { |
| | 80 | | case WearableLiterals.Categories.EYES: |
| 0 | 81 | | eyesWearableIds.Add(wearableItem.id); |
| 0 | 82 | | break; |
| | 83 | | case WearableLiterals.Categories.EYEBROWS: |
| 0 | 84 | | eyebrowsWearableIds.Add(wearableItem.id); |
| 0 | 85 | | break; |
| | 86 | | case WearableLiterals.Categories.MOUTH: |
| 0 | 87 | | mouthWearableIds.Add(wearableItem.id); |
| 0 | 88 | | break; |
| | 89 | | case WearableLiterals.Categories.FEET: |
| 0 | 90 | | feetWearableIds.Add(wearableItem.id); |
| 0 | 91 | | break; |
| | 92 | | case WearableLiterals.Categories.HAIR: |
| 0 | 93 | | hairWearableIds.Add(wearableItem.id); |
| 0 | 94 | | break; |
| | 95 | | case WearableLiterals.Categories.FACIAL: |
| 0 | 96 | | facialWearableIds.Add(wearableItem.id); |
| 0 | 97 | | break; |
| | 98 | | case WearableLiterals.Categories.LOWER_BODY: |
| 0 | 99 | | lowerBodyWearableIds.Add(wearableItem.id); |
| 0 | 100 | | break; |
| | 101 | | case WearableLiterals.Categories.UPPER_BODY: |
| 0 | 102 | | upperBodyWearableIds.Add(wearableItem.id); |
| 0 | 103 | | break; |
| | 104 | | case WearableLiterals.Categories.BODY_SHAPE: |
| 0 | 105 | | bodyshapeWearableIds.Add(wearableItem.id); |
| | 106 | | break; |
| | 107 | | } |
| | 108 | | } |
| | 109 | |
|
| 0 | 110 | | CatalogController.i.AddWearablesToCatalog(newWearables); |
| 0 | 111 | | } |
| | 112 | |
|
| | 113 | | /// <summary> |
| | 114 | | /// Instantiates bots using the config file param values. It defaults some uninitialized values using the player |
| | 115 | | /// </summary> |
| | 116 | | /// <param name="config">The config file to be used</param> |
| | 117 | | public IEnumerator InstantiateBotsAtWorldPos(WorldPosInstantiationConfig config) |
| | 118 | | { |
| 0 | 119 | | yield return EnsureGlobalSceneAndCatalog(); |
| | 120 | |
|
| 0 | 121 | | if (config.xPos == EnvironmentSettings.UNINITIALIZED_FLOAT) |
| | 122 | | { |
| 0 | 123 | | Log($"X Position value wasn't provided... using player's current X Position."); |
| 0 | 124 | | config.xPos = DCLCharacterController.i.characterPosition.unityPosition.x; |
| | 125 | | } |
| | 126 | |
|
| 0 | 127 | | if (config.yPos == EnvironmentSettings.UNINITIALIZED_FLOAT) |
| | 128 | | { |
| 0 | 129 | | Log($"Y Position value wasn't provided... using player's current Y Position."); |
| 0 | 130 | | config.yPos = DCLCharacterController.i.characterPosition.unityPosition.y; |
| | 131 | | } |
| | 132 | |
|
| 0 | 133 | | if (config.zPos == EnvironmentSettings.UNINITIALIZED_FLOAT) |
| | 134 | | { |
| 0 | 135 | | Log($"Z Position value wasn't provided... using player's current Z Position."); |
| 0 | 136 | | config.zPos = DCLCharacterController.i.characterPosition.unityPosition.z; |
| | 137 | | } |
| | 138 | |
|
| 0 | 139 | | Log($"Instantiating {config.amount} randomized avatars inside a {config.areaWidth}x{config.areaDepth} area p |
| | 140 | |
|
| 0 | 141 | | Vector3 randomizedAreaPosition = new Vector3(); |
| 0 | 142 | | for (int i = 0; i < config.amount; i++) |
| | 143 | | { |
| 0 | 144 | | randomizedAreaPosition.Set(Random.Range(config.xPos, config.xPos + config.areaWidth), config.yPos, Rando |
| 0 | 145 | | InstantiateBot(randomizedAreaPosition); |
| | 146 | | } |
| | 147 | |
|
| 0 | 148 | | Log($"Finished instantiating {config.amount} avatars. They may take some time to appear while their wearable |
| 0 | 149 | | } |
| | 150 | |
|
| | 151 | | /// <summary> |
| | 152 | | /// Instantiates bots using the config file param values. It defaults some uninitialized values using the player |
| | 153 | | /// </summary> |
| | 154 | | /// <param name="config">The config file to be used</param> |
| | 155 | | public IEnumerator InstantiateBotsAtCoords(CoordsInstantiationConfig config) |
| | 156 | | { |
| 0 | 157 | | if (config.xCoord == EnvironmentSettings.UNINITIALIZED_FLOAT) |
| | 158 | | { |
| 0 | 159 | | Log($"X Coordinate value wasn't provided... using player's current scene base X coordinate."); |
| 0 | 160 | | config.xCoord = Mathf.Floor(DCLCharacterController.i.characterPosition.worldPosition.x / ParcelSettings. |
| | 161 | | } |
| | 162 | |
|
| 0 | 163 | | if (config.yCoord == EnvironmentSettings.UNINITIALIZED_FLOAT) |
| | 164 | | { |
| 0 | 165 | | Log($"Y Coordinate value wasn't provided... using player's current scene base Y coordinate."); |
| 0 | 166 | | config.yCoord = Mathf.Floor(DCLCharacterController.i.characterPosition.worldPosition.z / ParcelSettings. |
| | 167 | | } |
| | 168 | |
|
| 0 | 169 | | var worldPosConfig = new WorldPosInstantiationConfig() |
| | 170 | | { |
| | 171 | | amount = config.amount, |
| | 172 | | xPos = config.xCoord * ParcelSettings.PARCEL_SIZE, |
| | 173 | | yPos = DCLCharacterController.i.characterPosition.unityPosition.y - DCLCharacterController.i.characterCo |
| | 174 | | zPos = config.yCoord * ParcelSettings.PARCEL_SIZE, |
| | 175 | | areaWidth = config.areaWidth, |
| | 176 | | areaDepth = config.areaDepth |
| | 177 | | }; |
| | 178 | |
|
| 0 | 179 | | Log($"Instantiating {config.amount} randomized avatars inside a {config.areaWidth}x{config.areaDepth} area p |
| | 180 | |
|
| 0 | 181 | | yield return InstantiateBotsAtWorldPos(worldPosConfig); |
| 0 | 182 | | } |
| | 183 | |
|
| | 184 | | /// <summary> |
| | 185 | | /// Instantiates an entity with an AvatarShape component, with randomized wearables, at the given position |
| | 186 | | /// </summary> |
| | 187 | | /// <param name="position">The world position of the randomized bot</param> |
| | 188 | | void InstantiateBot(Vector3 position) |
| | 189 | | { |
| 0 | 190 | | string entityId = "BOT-" + instantiatedBots.Count; |
| | 191 | |
|
| 0 | 192 | | AvatarModel avatarModel = new AvatarModel() |
| | 193 | | { |
| | 194 | | name = entityId, |
| | 195 | | hairColor = Color.white, |
| | 196 | | eyeColor = Color.white, |
| | 197 | | skinColor = Color.white, |
| | 198 | | bodyShape = Random.Range(0, 2) == 0 ? WearableLiterals.BodyShapes.FEMALE : WearableLiterals.BodyShapes.M |
| | 199 | | wearables = GetRandomizedWearablesSet() |
| | 200 | | }; |
| | 201 | |
|
| 0 | 202 | | globalScene.CreateEntity(entityId); |
| 0 | 203 | | globalScene.EntityComponentCreateOrUpdateWithModel(entityId, CLASS_ID_COMPONENT.AVATAR_SHAPE, avatarModel); |
| 0 | 204 | | UpdateEntityTransform(globalScene, entityId, position, Quaternion.identity, Vector3.one); |
| | 205 | |
|
| 0 | 206 | | instantiatedBots.Add(entityId); |
| 0 | 207 | | } |
| | 208 | |
|
| | 209 | | /// <summary> |
| | 210 | | ///Removes an instantiated bot. Every bot has its ID as its avatar name. |
| | 211 | | /// </summary> |
| | 212 | | /// <param name="targetEntityId">The target bot ID. Every bot has its ID as its avatar name.</param> |
| | 213 | | public void RemoveBot(string targetEntityId) |
| | 214 | | { |
| 0 | 215 | | if (!instantiatedBots.Contains(targetEntityId)) |
| 0 | 216 | | return; |
| | 217 | |
|
| 0 | 218 | | globalScene.RemoveEntity(targetEntityId); |
| 0 | 219 | | instantiatedBots.Remove(targetEntityId); |
| 0 | 220 | | } |
| | 221 | |
|
| | 222 | | /// <summary> |
| | 223 | | /// Removes all instantiated bots. |
| | 224 | | /// </summary> |
| | 225 | | public void ClearBots() |
| | 226 | | { |
| 0 | 227 | | while (instantiatedBots.Count > 0) |
| | 228 | | { |
| 0 | 229 | | RemoveBot(instantiatedBots[0]); |
| | 230 | | } |
| 0 | 231 | | } |
| | 232 | |
|
| | 233 | | /// <summary> |
| | 234 | | /// Randomizes a whole avatar set of wearables and returns a list with all the wearable IDs |
| | 235 | | /// </summary> |
| | 236 | | List<string> GetRandomizedWearablesSet() |
| | 237 | | { |
| 0 | 238 | | var wearablesSet = new List<string>(); |
| | 239 | |
|
| 0 | 240 | | if (eyesWearableIds.Count > 0) |
| 0 | 241 | | wearablesSet.Add(eyesWearableIds[Random.Range(0, eyesWearableIds.Count)]); |
| | 242 | |
|
| 0 | 243 | | if (eyebrowsWearableIds.Count > 0) |
| 0 | 244 | | wearablesSet.Add(eyebrowsWearableIds[Random.Range(0, eyebrowsWearableIds.Count)]); |
| | 245 | |
|
| 0 | 246 | | if (mouthWearableIds.Count > 0) |
| 0 | 247 | | wearablesSet.Add(mouthWearableIds[Random.Range(0, mouthWearableIds.Count)]); |
| | 248 | |
|
| 0 | 249 | | if (hairWearableIds.Count > 0) |
| 0 | 250 | | wearablesSet.Add(hairWearableIds[Random.Range(0, hairWearableIds.Count)]); |
| | 251 | |
|
| 0 | 252 | | if (facialWearableIds.Count > 0) |
| 0 | 253 | | wearablesSet.Add(facialWearableIds[Random.Range(0, facialWearableIds.Count)]); |
| | 254 | |
|
| 0 | 255 | | if (upperBodyWearableIds.Count > 0) |
| 0 | 256 | | wearablesSet.Add(upperBodyWearableIds[Random.Range(0, upperBodyWearableIds.Count)]); |
| | 257 | |
|
| 0 | 258 | | if (lowerBodyWearableIds.Count > 0) |
| 0 | 259 | | wearablesSet.Add(lowerBodyWearableIds[Random.Range(0, lowerBodyWearableIds.Count)]); |
| | 260 | |
|
| 0 | 261 | | if (feetWearableIds.Count > 0) |
| 0 | 262 | | wearablesSet.Add(feetWearableIds[Random.Range(0, feetWearableIds.Count)]); |
| | 263 | |
|
| 0 | 264 | | if (bodyshapeWearableIds.Count > 0) |
| 0 | 265 | | wearablesSet.Add(bodyshapeWearableIds[Random.Range(0, bodyshapeWearableIds.Count)]); |
| | 266 | |
|
| 0 | 267 | | return wearablesSet; |
| | 268 | | } |
| | 269 | |
|
| | 270 | | void UpdateEntityTransform(ParcelScene scene, string entityId, Vector3 position, Quaternion rotation, Vector3 sc |
| | 271 | | { |
| 0 | 272 | | PB_Transform pB_Transform = GetPBTransform(position, rotation, scale); |
| 0 | 273 | | scene.EntityComponentCreateOrUpdate( |
| | 274 | | entityId, |
| | 275 | | CLASS_ID_COMPONENT.TRANSFORM, |
| | 276 | | System.Convert.ToBase64String(pB_Transform.ToByteArray()) |
| | 277 | | ); |
| 0 | 278 | | } |
| | 279 | |
|
| | 280 | | public PB_Transform GetPBTransform(Vector3 position, Quaternion rotation, Vector3 scale) |
| | 281 | | { |
| 0 | 282 | | PB_Transform pbTranf = new PB_Transform(); |
| 0 | 283 | | pbTranf.Position = new PB_Vector3(); |
| 0 | 284 | | pbTranf.Position.X = position.x; |
| 0 | 285 | | pbTranf.Position.Y = position.y; |
| 0 | 286 | | pbTranf.Position.Z = position.z; |
| 0 | 287 | | pbTranf.Rotation = new PB_Quaternion(); |
| 0 | 288 | | pbTranf.Rotation.X = rotation.x; |
| 0 | 289 | | pbTranf.Rotation.Y = rotation.y; |
| 0 | 290 | | pbTranf.Rotation.Z = rotation.z; |
| 0 | 291 | | pbTranf.Rotation.W = rotation.w; |
| 0 | 292 | | pbTranf.Scale = new PB_Vector3(); |
| 0 | 293 | | pbTranf.Scale.X = scale.x; |
| 0 | 294 | | pbTranf.Scale.Y = scale.y; |
| 0 | 295 | | pbTranf.Scale.Z = scale.z; |
| 0 | 296 | | return pbTranf; |
| | 297 | | } |
| | 298 | |
|
| | 299 | | /// <summary> |
| | 300 | | /// Logs the tool messages in console regardless of the "Debug.unityLogger.logEnabled" value. |
| | 301 | | /// </summary> |
| | 302 | | private void Log(string message) |
| | 303 | | { |
| 0 | 304 | | bool originalLogEnabled = Debug.unityLogger.logEnabled; |
| 0 | 305 | | Debug.unityLogger.logEnabled = true; |
| | 306 | |
|
| 0 | 307 | | Debug.Log("BotsController - " + message); |
| | 308 | |
|
| 0 | 309 | | Debug.unityLogger.logEnabled = originalLogEnabled; |
| 0 | 310 | | } |
| | 311 | | } |
| | 312 | | } |