A stable modding interface between Techblox and mods https://mod.exmods.org/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

TweakableEngine.cs 20KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  1. using RobocraftX.Blocks;
  2. using Gamecraft.Wires;
  3. using Svelto.ECS;
  4. using GamecraftModdingAPI.Engines;
  5. namespace GamecraftModdingAPI.Blocks
  6. {
  7. public class TweakableEngine : IApiEngine
  8. {
  9. public string Name { get; } = "GamecraftModdingAPITweakableGameEngine";
  10. public EntitiesDB entitiesDB { set; private get; }
  11. public bool isRemovable => false;
  12. public bool IsInGame = false;
  13. public void Dispose()
  14. {
  15. IsInGame = false;
  16. }
  17. public void Ready()
  18. {
  19. IsInGame = true;
  20. }
  21. // Implementations for Tweakable static class
  22. public T GetStatAny<T>(EGID blockID, TweakableStat stat)
  23. {
  24. switch (stat)
  25. {
  26. case TweakableStat.TopSpeed:
  27. if (entitiesDB.Exists<MotorReadOnlyStruct>(blockID))
  28. {
  29. return (T)(object)entitiesDB.QueryEntity<MotorReadOnlyStruct>(blockID).maxVelocity;
  30. }
  31. break;
  32. case TweakableStat.Torque:
  33. if (entitiesDB.Exists<MotorReadOnlyStruct>(blockID))
  34. {
  35. return (T)(object)entitiesDB.QueryEntity<MotorReadOnlyStruct>(blockID).maxForce;
  36. }
  37. break;
  38. case TweakableStat.MaxExtension:
  39. if (entitiesDB.Exists<PistonReadOnlyStruct>(blockID))
  40. {
  41. return (T)(object)entitiesDB.QueryEntity<PistonReadOnlyStruct>(blockID).maxDeviation;
  42. }
  43. break;
  44. case TweakableStat.MinAngle:
  45. if (entitiesDB.Exists<ServoReadOnlyStruct>(blockID))
  46. {
  47. return (T)(object)entitiesDB.QueryEntity<ServoReadOnlyStruct>(blockID).minDeviation;
  48. }
  49. break;
  50. case TweakableStat.MaxAngle:
  51. if (entitiesDB.Exists<ServoReadOnlyStruct>(blockID))
  52. {
  53. return (T)(object)entitiesDB.QueryEntity<ServoReadOnlyStruct>(blockID).maxDeviation;
  54. }
  55. break;
  56. case TweakableStat.Reverse:
  57. if (entitiesDB.Exists<MotorReadOnlyStruct>(blockID))
  58. {
  59. return (T)(object)entitiesDB.QueryEntity<MotorReadOnlyStruct>(blockID).reverse;
  60. }
  61. else if (entitiesDB.Exists<ServoReadOnlyStruct>(blockID))
  62. {
  63. return (T)(object)entitiesDB.QueryEntity<ServoReadOnlyStruct>(blockID).reverse;
  64. }
  65. break;
  66. case TweakableStat.StartValue:
  67. if (entitiesDB.Exists<SignalGeneratorEntityStruct>(blockID))
  68. {
  69. return (T)(object)entitiesDB.QueryEntity<SignalGeneratorEntityStruct>(blockID).startValue;
  70. }
  71. break;
  72. }
  73. return default(T);
  74. }
  75. public T GetStatAny<T>(uint blockID, TweakableStat stat)
  76. {
  77. return GetStatAny<T>(new EGID(blockID, BlockIdentifiers.OWNED_BLOCKS), stat);
  78. }
  79. public dynamic GetStatDynamic(EGID blockID, TweakableStat stat)
  80. {
  81. switch (stat)
  82. {
  83. case TweakableStat.TopSpeed:
  84. if (entitiesDB.Exists<MotorReadOnlyStruct>(blockID))
  85. {
  86. return entitiesDB.QueryEntity<MotorReadOnlyStruct>(blockID).maxVelocity;
  87. }
  88. break;
  89. case TweakableStat.Torque:
  90. if (entitiesDB.Exists<MotorReadOnlyStruct>(blockID))
  91. {
  92. return entitiesDB.QueryEntity<MotorReadOnlyStruct>(blockID).maxForce;
  93. }
  94. break;
  95. case TweakableStat.MaxExtension:
  96. if (entitiesDB.Exists<PistonReadOnlyStruct>(blockID))
  97. {
  98. return entitiesDB.QueryEntity<PistonReadOnlyStruct>(blockID).maxDeviation;
  99. }
  100. break;
  101. case TweakableStat.MinAngle:
  102. if (entitiesDB.Exists<ServoReadOnlyStruct>(blockID))
  103. {
  104. return entitiesDB.QueryEntity<ServoReadOnlyStruct>(blockID).minDeviation;
  105. }
  106. break;
  107. case TweakableStat.MaxAngle:
  108. if (entitiesDB.Exists<ServoReadOnlyStruct>(blockID))
  109. {
  110. return entitiesDB.QueryEntity<ServoReadOnlyStruct>(blockID).maxDeviation;
  111. }
  112. break;
  113. case TweakableStat.Reverse:
  114. if (entitiesDB.Exists<MotorReadOnlyStruct>(blockID))
  115. {
  116. return entitiesDB.QueryEntity<MotorReadOnlyStruct>(blockID).reverse;
  117. }
  118. else if (entitiesDB.Exists<ServoReadOnlyStruct>(blockID))
  119. {
  120. return entitiesDB.QueryEntity<ServoReadOnlyStruct>(blockID).reverse;
  121. }
  122. break;
  123. case TweakableStat.StartValue:
  124. if (entitiesDB.Exists<SignalGeneratorEntityStruct>(blockID))
  125. {
  126. return entitiesDB.QueryEntity<SignalGeneratorEntityStruct>(blockID).startValue;
  127. }
  128. break;
  129. }
  130. return null;
  131. }
  132. public dynamic GetStatDynamic(uint blockID, TweakableStat stat)
  133. {
  134. return GetStatDynamic(new EGID(blockID, BlockIdentifiers.OWNED_BLOCKS), stat);
  135. }
  136. public T SetStatAny<T>(EGID blockID, TweakableStat stat, T value)
  137. {
  138. switch (stat)
  139. {
  140. case TweakableStat.TopSpeed:
  141. if (entitiesDB.Exists<MotorReadOnlyStruct>(blockID))
  142. {
  143. ref MotorReadOnlyStruct refStruct = ref entitiesDB.QueryEntity<MotorReadOnlyStruct>(blockID);
  144. refStruct.maxVelocity = (float)(object)value;
  145. return (T)(object)refStruct.maxVelocity;
  146. }
  147. break;
  148. case TweakableStat.Torque:
  149. if (entitiesDB.Exists<MotorReadOnlyStruct>(blockID))
  150. {
  151. ref MotorReadOnlyStruct refStruct = ref entitiesDB.QueryEntity<MotorReadOnlyStruct>(blockID);
  152. refStruct.maxForce = (float)(object)value;
  153. return (T)(object)refStruct.maxForce;
  154. }
  155. break;
  156. case TweakableStat.MaxExtension:
  157. if (entitiesDB.Exists<PistonReadOnlyStruct>(blockID))
  158. {
  159. ref PistonReadOnlyStruct refStruct = ref entitiesDB.QueryEntity<PistonReadOnlyStruct>(blockID);
  160. refStruct.maxDeviation = (float)(object)value;
  161. return (T)(object)refStruct.maxDeviation;
  162. }
  163. break;
  164. case TweakableStat.MinAngle:
  165. if (entitiesDB.Exists<ServoReadOnlyStruct>(blockID))
  166. {
  167. ref ServoReadOnlyStruct refStruct = ref entitiesDB.QueryEntity<ServoReadOnlyStruct>(blockID);
  168. refStruct.minDeviation = (float)(object)value;
  169. return (T)(object)refStruct.minDeviation;
  170. }
  171. break;
  172. case TweakableStat.MaxAngle:
  173. if (entitiesDB.Exists<ServoReadOnlyStruct>(blockID))
  174. {
  175. ref ServoReadOnlyStruct refStruct = ref entitiesDB.QueryEntity<ServoReadOnlyStruct>(blockID);
  176. refStruct.maxDeviation = (float)(object)value;
  177. return (T)(object)refStruct.maxDeviation;
  178. }
  179. break;
  180. case TweakableStat.Reverse:
  181. if (entitiesDB.Exists<MotorReadOnlyStruct>(blockID))
  182. {
  183. ref MotorReadOnlyStruct refStruct = ref entitiesDB.QueryEntity<MotorReadOnlyStruct>(blockID);
  184. refStruct.reverse = (bool)(object)value;
  185. return (T)(object)refStruct.reverse;
  186. }
  187. else if (entitiesDB.Exists<ServoReadOnlyStruct>(blockID))
  188. {
  189. ref ServoReadOnlyStruct refStruct = ref entitiesDB.QueryEntity<ServoReadOnlyStruct>(blockID);
  190. refStruct.reverse = (bool)(object)value;
  191. return (T)(object)refStruct.reverse;
  192. }
  193. break;
  194. case TweakableStat.StartValue:
  195. if (entitiesDB.Exists<SignalGeneratorEntityStruct>(blockID))
  196. {
  197. ref SignalGeneratorEntityStruct refStruct = ref entitiesDB.QueryEntity<SignalGeneratorEntityStruct>(blockID);
  198. refStruct.startValue = (float)(object)value;
  199. return (T)(object)refStruct.startValue;
  200. }
  201. break;
  202. }
  203. return default(T);
  204. }
  205. public T SetStatAny<T>(uint blockID, TweakableStat stat, T value)
  206. {
  207. return SetStatAny<T>(new EGID(blockID, BlockIdentifiers.OWNED_BLOCKS), stat, value);
  208. }
  209. public dynamic SetStatDynamic(EGID blockID, TweakableStat stat, dynamic value)
  210. {
  211. switch (stat)
  212. {
  213. case TweakableStat.TopSpeed:
  214. if (entitiesDB.Exists<MotorReadOnlyStruct>(blockID))
  215. {
  216. ref MotorReadOnlyStruct refStruct = ref entitiesDB.QueryEntity<MotorReadOnlyStruct>(blockID);
  217. refStruct.maxVelocity = value;
  218. return refStruct.maxVelocity;
  219. }
  220. break;
  221. case TweakableStat.Torque:
  222. if (entitiesDB.Exists<MotorReadOnlyStruct>(blockID))
  223. {
  224. ref MotorReadOnlyStruct refStruct = ref entitiesDB.QueryEntity<MotorReadOnlyStruct>(blockID);
  225. refStruct.maxForce = value;
  226. return refStruct.maxForce;
  227. }
  228. break;
  229. case TweakableStat.MaxExtension:
  230. if (entitiesDB.Exists<PistonReadOnlyStruct>(blockID))
  231. {
  232. ref PistonReadOnlyStruct refStruct = ref entitiesDB.QueryEntity<PistonReadOnlyStruct>(blockID);
  233. refStruct.maxDeviation = value;
  234. return refStruct.maxDeviation;
  235. }
  236. break;
  237. case TweakableStat.MinAngle:
  238. if (entitiesDB.Exists<ServoReadOnlyStruct>(blockID))
  239. {
  240. ref ServoReadOnlyStruct refStruct = ref entitiesDB.QueryEntity<ServoReadOnlyStruct>(blockID);
  241. refStruct.minDeviation = value;
  242. return refStruct.minDeviation;
  243. }
  244. break;
  245. case TweakableStat.MaxAngle:
  246. if (entitiesDB.Exists<ServoReadOnlyStruct>(blockID))
  247. {
  248. ref ServoReadOnlyStruct refStruct = ref entitiesDB.QueryEntity<ServoReadOnlyStruct>(blockID);
  249. refStruct.maxDeviation = value;
  250. return refStruct.maxDeviation;
  251. }
  252. break;
  253. case TweakableStat.Reverse:
  254. if (entitiesDB.Exists<MotorReadOnlyStruct>(blockID))
  255. {
  256. ref MotorReadOnlyStruct refStruct = ref entitiesDB.QueryEntity<MotorReadOnlyStruct>(blockID);
  257. refStruct.reverse = value;
  258. return refStruct.reverse;
  259. }
  260. else if (entitiesDB.Exists<ServoReadOnlyStruct>(blockID))
  261. {
  262. ref ServoReadOnlyStruct refStruct = ref entitiesDB.QueryEntity<ServoReadOnlyStruct>(blockID);
  263. refStruct.reverse = value;
  264. return refStruct.reverse;
  265. }
  266. break;
  267. case TweakableStat.StartValue:
  268. if (entitiesDB.Exists<SignalGeneratorEntityStruct>(blockID))
  269. {
  270. ref SignalGeneratorEntityStruct refStruct = ref entitiesDB.QueryEntity<SignalGeneratorEntityStruct>(blockID);
  271. refStruct.startValue = value;
  272. return refStruct.startValue;
  273. }
  274. break;
  275. }
  276. return null;
  277. }
  278. public dynamic SetStatDynamic(uint blockID, TweakableStat stat, dynamic value)
  279. {
  280. return SetStatDynamic(new EGID(blockID, BlockIdentifiers.OWNED_BLOCKS), stat, value);
  281. }
  282. public T AddStatAny<T>(EGID blockID, TweakableStat stat, T value)
  283. {
  284. switch (stat)
  285. {
  286. case TweakableStat.TopSpeed:
  287. if (entitiesDB.Exists<MotorReadOnlyStruct>(blockID))
  288. {
  289. ref MotorReadOnlyStruct refStruct = ref entitiesDB.QueryEntity<MotorReadOnlyStruct>(blockID);
  290. refStruct.maxVelocity += (float)(object)value;
  291. return (T)(object)refStruct.maxVelocity;
  292. }
  293. break;
  294. case TweakableStat.Torque:
  295. if (entitiesDB.Exists<MotorReadOnlyStruct>(blockID))
  296. {
  297. ref MotorReadOnlyStruct refStruct = ref entitiesDB.QueryEntity<MotorReadOnlyStruct>(blockID);
  298. refStruct.maxForce += (float)(object)value;
  299. return (T)(object)refStruct.maxForce;
  300. }
  301. break;
  302. case TweakableStat.MaxExtension:
  303. if (entitiesDB.Exists<PistonReadOnlyStruct>(blockID))
  304. {
  305. ref PistonReadOnlyStruct refStruct = ref entitiesDB.QueryEntity<PistonReadOnlyStruct>(blockID);
  306. refStruct.maxDeviation += (float)(object)value;
  307. return (T)(object)refStruct.maxDeviation;
  308. }
  309. break;
  310. case TweakableStat.MinAngle:
  311. if (entitiesDB.Exists<ServoReadOnlyStruct>(blockID))
  312. {
  313. ref ServoReadOnlyStruct refStruct = ref entitiesDB.QueryEntity<ServoReadOnlyStruct>(blockID);
  314. refStruct.minDeviation += (float)(object)value;
  315. return (T)(object)refStruct.minDeviation;
  316. }
  317. break;
  318. case TweakableStat.MaxAngle:
  319. if (entitiesDB.Exists<ServoReadOnlyStruct>(blockID))
  320. {
  321. ref ServoReadOnlyStruct refStruct = ref entitiesDB.QueryEntity<ServoReadOnlyStruct>(blockID);
  322. refStruct.maxDeviation += (float)(object)value;
  323. return (T)(object)refStruct.maxDeviation;
  324. }
  325. break;
  326. case TweakableStat.Reverse:
  327. // '+' is associated with logical OR in some fields, so it technically isn't invalid to "add" booleans
  328. if (entitiesDB.Exists<MotorReadOnlyStruct>(blockID))
  329. {
  330. ref MotorReadOnlyStruct refStruct = ref entitiesDB.QueryEntity<MotorReadOnlyStruct>(blockID);
  331. refStruct.reverse = refStruct.reverse || (bool)(object)value;
  332. return (T)(object)refStruct.reverse;
  333. }
  334. else if (entitiesDB.Exists<ServoReadOnlyStruct>(blockID))
  335. {
  336. ref ServoReadOnlyStruct refStruct = ref entitiesDB.QueryEntity<ServoReadOnlyStruct>(blockID);
  337. refStruct.reverse = refStruct.reverse || (bool)(object)value;
  338. return (T)(object)refStruct.reverse;
  339. }
  340. break;
  341. case TweakableStat.StartValue:
  342. if (entitiesDB.Exists<SignalGeneratorEntityStruct>(blockID))
  343. {
  344. ref SignalGeneratorEntityStruct refStruct = ref entitiesDB.QueryEntity<SignalGeneratorEntityStruct>(blockID);
  345. refStruct.startValue += (float)(object)value;
  346. return (T)(object)refStruct.startValue;
  347. }
  348. break;
  349. }
  350. return default(T);
  351. }
  352. public T AddStatAny<T>(uint blockID, TweakableStat stat, T value)
  353. {
  354. return AddStatAny<T>(new EGID(blockID, BlockIdentifiers.OWNED_BLOCKS), stat, value);
  355. }
  356. public dynamic AddStatDynamic(EGID blockID, TweakableStat stat, dynamic value)
  357. {
  358. switch (stat)
  359. {
  360. case TweakableStat.TopSpeed:
  361. if (entitiesDB.Exists<MotorReadOnlyStruct>(blockID))
  362. {
  363. ref MotorReadOnlyStruct refStruct = ref entitiesDB.QueryEntity<MotorReadOnlyStruct>(blockID);
  364. refStruct.maxVelocity += value;
  365. return refStruct.maxVelocity;
  366. }
  367. break;
  368. case TweakableStat.Torque:
  369. if (entitiesDB.Exists<MotorReadOnlyStruct>(blockID))
  370. {
  371. ref MotorReadOnlyStruct refStruct = ref entitiesDB.QueryEntity<MotorReadOnlyStruct>(blockID);
  372. refStruct.maxForce += value;
  373. return refStruct.maxForce;
  374. }
  375. break;
  376. case TweakableStat.MaxExtension:
  377. if (entitiesDB.Exists<PistonReadOnlyStruct>(blockID))
  378. {
  379. ref PistonReadOnlyStruct refStruct = ref entitiesDB.QueryEntity<PistonReadOnlyStruct>(blockID);
  380. refStruct.maxDeviation += value;
  381. return refStruct.maxDeviation;
  382. }
  383. break;
  384. case TweakableStat.MinAngle:
  385. if (entitiesDB.Exists<ServoReadOnlyStruct>(blockID))
  386. {
  387. ref ServoReadOnlyStruct refStruct = ref entitiesDB.QueryEntity<ServoReadOnlyStruct>(blockID);
  388. refStruct.minDeviation += value;
  389. return refStruct.minDeviation;
  390. }
  391. break;
  392. case TweakableStat.MaxAngle:
  393. if (entitiesDB.Exists<ServoReadOnlyStruct>(blockID))
  394. {
  395. ref ServoReadOnlyStruct refStruct = ref entitiesDB.QueryEntity<ServoReadOnlyStruct>(blockID);
  396. refStruct.maxDeviation += value;
  397. return refStruct.maxDeviation;
  398. }
  399. break;
  400. case TweakableStat.Reverse:
  401. // '+' is associated with logical OR in some fields, so it technically isn't invalid to "add" booleans
  402. if (entitiesDB.Exists<MotorReadOnlyStruct>(blockID))
  403. {
  404. ref MotorReadOnlyStruct refStruct = ref entitiesDB.QueryEntity<MotorReadOnlyStruct>(blockID);
  405. refStruct.reverse = refStruct.reverse || value;
  406. return refStruct.reverse;
  407. }
  408. else if (entitiesDB.Exists<ServoReadOnlyStruct>(blockID))
  409. {
  410. ref ServoReadOnlyStruct refStruct = ref entitiesDB.QueryEntity<ServoReadOnlyStruct>(blockID);
  411. refStruct.reverse = refStruct.reverse || value;
  412. return refStruct.reverse;
  413. }
  414. break;
  415. case TweakableStat.StartValue:
  416. if (entitiesDB.Exists<SignalGeneratorEntityStruct>(blockID))
  417. {
  418. ref SignalGeneratorEntityStruct refStruct = ref entitiesDB.QueryEntity<SignalGeneratorEntityStruct>(blockID);
  419. refStruct.startValue += value;
  420. return refStruct.startValue;
  421. }
  422. break;
  423. }
  424. return null;
  425. }
  426. public dynamic AddStatDynamic(uint blockID, TweakableStat stat, dynamic value)
  427. {
  428. return AddStatDynamic(new EGID(blockID, BlockIdentifiers.OWNED_BLOCKS), stat, value);
  429. }
  430. }
  431. }