Advertisement
Guest User

Example Worm Code

a guest
Jun 26th, 2017
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 14.21 KB | None | 0 0
  1. using System;
  2. using Microsoft.Xna.Framework;
  3. using Terraria;
  4. using Terraria.ModLoader;
  5. using Terraria.ID;
  6. namespace ExampleMod.NPCs
  7. {
  8. //ported from my tAPI mod because I'm lazy
  9. public abstract class Worm : ModNPC
  10. {
  11. /* ai[0] = follower
  12. * ai[1] = following
  13. * ai[2] = distanceFromTail
  14. * ai[3] = head
  15. */
  16. public bool head;
  17. public bool tail;
  18. public int minLength;
  19. public int maxLength;
  20. public int headType;
  21. public int bodyType;
  22. public int tailType;
  23. public bool flies = false;
  24. public bool directional = false;
  25. public float speed;
  26. public float turnSpeed;
  27.  
  28. public override void AI()
  29. {
  30. if (npc.localAI[1] == 0f)
  31. {
  32. npc.localAI[1] = 1f;
  33. Init();
  34. }
  35. if (npc.ai[3] > 0f)
  36. {
  37. npc.realLife = (int)npc.ai[3];
  38. }
  39. if (!head && npc.timeLeft < 300)
  40. {
  41. npc.timeLeft = 300;
  42. }
  43. if (npc.target < 0 || npc.target == 255 || Main.player[npc.target].dead)
  44. { npc.TargetClosest(true);
  45. }
  46. if (Main.player[npc.target].dead && npc.timeLeft > 300)
  47. {
  48. npc.timeLeft = 300;
  49. }
  50. if (Main.netMode != 1)
  51. {
  52. if (!tail && npc.ai[0] == 0f)
  53. {
  54. if (head)
  55. {
  56. npc.ai[3] = (float)npc.whoAmI;
  57. npc.realLife = npc.whoAmI;
  58. npc.ai[2] = (float)Main.rand.Next(minLength, maxLength + 1);
  59. npc.ai[0] = (float)NPC.NewNPC((int)(npc.position.X + (float)(npc.width / 2)), (int)(npc.position.Y +(float)npc.height), bodyType, npc.whoAmI);
  60. }
  61. else if (npc.ai[2] > 0f)
  62. {
  63. npc.ai[0] = (float)NPC.NewNPC((int)(npc.position.X + (float)(npc.width / 2)), (int)(npc.position.Y +(float)npc.height), npc.type, npc.whoAmI);
  64. }
  65. else
  66.  
  67. {
  68. npc.ai[0] = (float)NPC.NewNPC((int)(npc.position.X + (float)(npc.width / 2)), (int)(npc.position.Y + (float)npc.height), tailType, npc.whoAmI);
  69. }
  70. Main.npc[(int)npc.ai[0]].ai[3] = npc.ai[3];
  71. Main.npc[(int)npc.ai[0]].realLife = npc.realLife;
  72. Main.npc[(int)npc.ai[0]].ai[1] = (float)npc.whoAmI;
  73. Main.npc[(int)npc.ai[0]].ai[2] = npc.ai[2] - 1f;
  74. npc.netUpdate = true;
  75. }
  76. if (!head && (!Main.npc[(int)npc.ai[1]].active || (Main.npc[(int)npc.ai[1]].type != headType && Main.npc[(int)npc.ai[1]].type != bodyType)))
  77. {
  78. npc.life = 0;
  79. npc.HitEffect(0, 10.0);
  80. npc.active = false;
  81. }
  82. if (!tail && (!Main.npc[(int)npc.ai[0]].active || (Main.npc[(int)npc.ai[0]].type != bodyType && Main.npc[(int)npc.ai[0]].type != tailType)))
  83. {
  84. npc.life = 0;
  85. npc.HitEffect(0, 10.0);
  86. npc.active = false;
  87. }
  88. if (!npc.active && Main.netMode == 2)
  89. {
  90. NetMessage.SendData(28, -1, -1, "", npc.whoAmI, -1f, 0f, 0f, 0, 0, 0);
  91. }
  92. }
  93. int num180 = (int)(npc.position.X / 16f) - 1;
  94. int num181 = (int)((npc.position.X + (float)npc.width) / 16f) + 2;
  95. int num182 = (int)(npc.position.Y / 16f) - 1;
  96. int num183 = (int)((npc.position.Y + (float)npc.height) / 16f) + 2;
  97. if (num180 < 0)
  98. {
  99. num180 = 0;
  100. }
  101. if (num181 > Main.maxTilesX)
  102. {
  103. num181 = Main.maxTilesX;
  104. }
  105. if (num182 < 0)
  106. {
  107. num182 = 0;
  108. }
  109. if (num183 > Main.maxTilesY)
  110. {
  111. num183 = Main.maxTilesY;
  112. }
  113. bool flag18 = flies;
  114.  
  115. if (!flag18)
  116. {
  117. for (int num184 = num180; num184 < num181; num184++)
  118. {
  119. for (int num185 = num182; num185 < num183; num185++)
  120. {
  121. if (Main.tile[num184, num185] != null && ((Main.tile[num184, num185].nactive() && (Mai.tileSolid[(int)Main.tile[num184, num185].type] || (Main.tileSolidTop[(int)Main.tile[num184, num185].type] && Main.tile[num184, num185].frameY == 0))) || Main.tile[num184, num185].liquid > 64))
  122. {
  123. Vector2 vector17;
  124. vector17.X = (float)(num184 * 16);
  125. vector17.Y = (float)(num185 * 16);
  126. if (npc.position.X + (float)npc.width > vector17.X && npc.position.X < vector17.X + 16f && npc.position.Y + (float)npc.height > vector17.Y && npc.position.Y < vector17.Y + 16f)
  127. {
  128. flag18 = true;
  129. if (Main.rand.Next(100) == 0 && npc.behindTiles && Main.tile[num184, num185].nactive())
  130. {
  131. WorldGen.KillTile(num184, num185, true, true, false);
  132. }
  133. if (Main.netMode != 1 && Main.tile[num184, num185].type == 2)
  134. {
  135. ushort arg_BFCA_0 = Main.tile[num184, num185 - 1].type;
  136. }
  137. }
  138. }
  139. }
  140. }
  141. }
  142. if (!flag18 && head)
  143. {
  144. Rectangle rectangle = new Rectangle((int)npc.position.X, (int)npc.position.Y, npc.width, npc.height);
  145. int num186 = 1000;
  146. bool flag19 = true;
  147. for (int num187 = 0; num187 < 255; num187++)
  148. {
  149. if (Main.player[num187].active)
  150. {
  151. Rectangle rectangle2 = new Rectangle((int)Main.player[num187].position.X - num186, (int)Main.player[num187].position.Y - num186, num186 * 2, num186 * 2);
  152. if (rectangle.Intersects(rectangle2))
  153. {
  154. flag19 = false;
  155. break;
  156. }
  157. }
  158. }
  159. if (flag19)
  160. {
  161. flag18 = true;
  162. }
  163. }
  164. if (directional)
  165. {
  166. if (npc.velocity.X < 0f)
  167. {
  168. npc.spriteDirection = 1;
  169. }
  170. else if (npc.velocity.X > 0f)
  171. {
  172. npc.spriteDirection = -1;
  173. }
  174. }
  175. float num188 = speed;
  176. float num189 = turnSpeed;
  177. Vector2 vector18 = new Vector2(npc.position.X + (float)npc.width * 0.5f, npc.position.Y + (float)npc.height * 0.5f);
  178. float num191 = Main.player[npc.target].position.X + (float)(Main.player[npc.target].width / 2);
  179. float num192 = Main.player[npc.target].position.Y + (float)(Main.player[npc.target].height / 2);
  180. num191 = (float)((int)(num191 / 16f) * 16);
  181. num192 = (float)((int)(num192 / 16f) * 16);
  182. vector18.X = (float)((int)(vector18.X / 16f) * 16);
  183. vector18.Y = (float)((int)(vector18.Y / 16f) * 16);
  184. num191 -= vector18.X;
  185. num192 -= vector18.Y;
  186. float num193 = (float)System.Math.Sqrt((double)(num191 * num191 + num192 * num192));
  187. if (npc.ai[1] > 0f && npc.ai[1] < (float)Main.npc.Length)
  188. {
  189. try
  190. {
  191. vector18 = new Vector2(npc.position.X + (float)npc.width * 0.5f, npc.position.Y + (float)npc.height * 0.5f);
  192. num191 = Main.npc[(int)npc.ai[1]].position.X + (float)(Main.npc[(int)npc.ai[1]].width / 2) - vector18.X;
  193. num192 = Main.npc[(int)npc.ai[1]].position.Y + (float)(Main.npc[(int)npc.ai[1]].height / 2) - vector18.Y;
  194. }
  195. catch
  196. {
  197. }
  198. npc.rotation = (float)System.Math.Atan2((double)num192, (double)num191) + 1.57f;
  199. num193 = (float)System.Math.Sqrt((double)(num191 * num191 + num192 * num192));
  200. int num194 = npc.width;
  201. num193 = (num193 - (float)num194) / num193;
  202. num191 *= num193;
  203. num192 *= num193;
  204. npc.velocity = Vector2.Zero;
  205. npc.position.X = npc.position.X + num191;
  206. npc.position.Y = npc.position.Y + num192;
  207. if (directional)
  208. {
  209. if (num191 < 0f)
  210. {
  211. npc.spriteDirection = 1;
  212. }
  213. if (num191 > 0f)
  214. {
  215. npc.spriteDirection = -1;
  216. }
  217. }
  218. }
  219. else
  220. {
  221. if (!flag18)
  222. {
  223. npc.TargetClosest(true);
  224. npc.velocity.Y = npc.velocity.Y + 0.11f;
  225. if (npc.velocity.Y > num188)
  226. {
  227. npc.velocity.Y = num188;
  228. }
  229. if ((double)(System.Math.Abs(npc.velocity.X) + System.Math.Abs(npc.velocity.Y)) < (double)num188 * 0.4)
  230. {
  231. if (npc.velocity.X < 0f)
  232. {
  233. npc.velocity.X = npc.velocity.X - num189 * 1.1f;
  234. }
  235. else
  236. {
  237. npc.velocity.X = npc.velocity.X + num189 * 1.1f;
  238. }
  239. }
  240. else if (npc.velocity.Y == num188)
  241. {
  242. if (npc.velocity.X < num191)
  243. {
  244. npc.velocity.X = npc.velocity.X + num189;
  245. }
  246. else if (npc.velocity.X > num191)
  247. {
  248. npc.velocity.X = npc.velocity.X - num189;
  249. }
  250. }
  251. else if (npc.velocity.Y > 4f)
  252. {
  253. if (npc.velocity.X < 0f)
  254. {
  255. npc.velocity.X = npc.velocity.X + num189 * 0.9f;
  256. }
  257. else
  258. {
  259. npc.velocity.X = npc.velocity.X - num189 * 0.9f;
  260. }
  261. }
  262. }
  263. else
  264. {
  265. if (!flies && npc.behindTiles && npc.soundDelay == 0)
  266. {
  267. float num195 = num193 / 40f;
  268. if (num195 < 10f)
  269. {
  270. num195 = 10f;
  271. }
  272. if (num195 > 20f)
  273. {
  274. num195 = 20f;
  275. }
  276. npc.soundDelay = (int)num195;
  277. Main.PlaySound(SoundID.Roar, npc.position, 1);
  278. }
  279. num193 = (float)System.Math.Sqrt((double)(num191 * num191 + num192 * num192));
  280. float num196 = System.Math.Abs(num191);
  281. float num197 = System.Math.Abs(num192);
  282. float num198 = num188 / num193;
  283. num191 *= num198;
  284. num192 *= num198;
  285. if (ShouldRun())
  286. {
  287. bool flag20 = true;
  288. for (int num199 = 0; num199 < 255; num199++)
  289. {
  290. if (Main.player[num199].active && !Main.player[num199].dead && Main.player[num199].ZoneCorrupt)
  291. {
  292. flag20 = false;
  293. }
  294. }
  295. if (flag20)
  296. {
  297. if (Main.netMode != 1 && (double)(npc.position.Y / 16f) > (Main.rockLayer + (double)Main.maxTilesY) / 2.0)
  298. {
  299. npc.active = false;
  300. int num200 = (int)npc.ai[0];
  301. while (num200 > 0 && num200 < 200 && Main.npc[num200].active && Main.npc[num200].aiStyle == npc.iStyle)
  302. {
  303. int num201 = (int)Main.npc[num200].ai[0];
  304. Main.npc[num200].active = false;
  305. npc.life = 0;
  306. if (Main.netMode == 2)
  307. {
  308. NetMessage.SendData(23, -1, -1, "", num200, 0f, 0f, 0f, 0, 0, 0);
  309. }
  310. num200 = num201;
  311. }
  312. if (Main.netMode == 2)
  313. {
  314. NetMessage.SendData(23, -1, -1, "", npc.whoAmI, 0f, 0f, 0f, 0, 0, 0);
  315. }
  316. }
  317. num191 = 0f;
  318. num192 = num188;
  319. }
  320. }
  321. bool flag21 = false;
  322. if (npc.type == 87)
  323. {
  324. if (((npc.velocity.X > 0f && num191 < 0f) || (npc.velocity.X < 0f && num191 > 0f) || (npc.velocity.Y > 0f && num12 < 0f) || (npc.velocity.Y < 0f && num192 > 0f)) && System.Math.Abs(npc.velocity.X) + System.Math.Abs(npc.velocity.Y) > num189 / 2f && num193 < 300f)
  325. {
  326. flag21 = true;
  327. if (System.Math.Abs(npc.velocity.X) + System.Math.Abs(npc.velocity.Y) < num188)
  328. {
  329. npc.velocity *= 1.1f;
  330. }
  331. }
  332. if (npc.position.Y > Main.player[npc.target].position.Y || (double)(Main.player[npc.target].position.Y / 16f) > Main.worldSurface || Main.player[npc.target].dead)
  333. {
  334. flag21 = true;
  335. if (System.Math.Abs(npc.velocity.X) < num188 / 2f)
  336. {
  337. if (npc.velocity.X == 0f)
  338. {
  339. npc.velocity.X = npc.velocity.X - (float)npc.direction;
  340. }
  341. npc.velocity.X = npc.velocity.X * 1.1f;
  342. }
  343. else
  344. {
  345. if (npc.velocity.Y > -num188)
  346. {
  347. npc.velocity.Y = npc.velocity.Y - num189;
  348. }
  349. }
  350. }
  351. }
  352. if (!flag21)
  353. {
  354. if ((npc.velocity.X > 0f && num191 > 0f) || (npc.velocity.X < 0f && num191 < 0f) || (npc.velocity.Y > 0f && num192 > 0f) || (npc.velocity.Y < 0f && num192 < 0f))
  355. {
  356. if (npc.velocity.X < num191)
  357. {
  358. npc.velocity.X = npc.velocity.X + num189;
  359. }
  360. else
  361. {
  362. if (npc.velocity.X > num191)
  363. {
  364. npc.velocity.X = npc.velocity.X - num189;
  365.  
  366. }
  367. }
  368. if (npc.velocity.Y < num192)
  369. {
  370. npc.velocity.Y = npc.velocity.Y + num189;
  371. }
  372. else
  373. {
  374. if (npc.velocity.Y > num192)
  375. {
  376. npc.velocity.Y = npc.velocity.Y - num189;
  377. }
  378. }
  379. if ((double)System.Math.Abs(num192) < (double)num188 * 0.2 && ((npc.velocity.X > 0f && num191 < 0f) || (npc.velocity.X < 0f && num191 > 0f)))
  380. {
  381. if (npc.velocity.Y > 0f)
  382. {
  383. npc.velocity.Y = npc.velocity.Y + num189 * 2f;
  384. }
  385. else
  386. {
  387. npc.velocity.Y = npc.velocity.Y - num189 * 2f;
  388. }
  389. }
  390. if ((double)System.Math.Abs(num191) < (double)num188 * 0.2 && ((npc.velocity.Y > 0f && num192 < 0f) || (npc.velocity.Y < 0f && num192 > 0f)))
  391. {
  392. if (npc.velocity.X > 0f)
  393. {
  394. npc.velocity.X = npc.velocity.X + num189 * 2f;
  395. }
  396. else
  397. {
  398. npc.velocity.X = npc.velocity.X - num189 * 2f;
  399. }
  400. }
  401. }
  402. else
  403. {
  404. if (num196 > num197)
  405. {
  406. if (npc.velocity.X < num191)
  407. {
  408. npc.velocity.X = npc.velocity.X + num189 * 1.1f;
  409. }
  410. else if (npc.velocity.X > num191)
  411. {
  412. npc.velocity.X = npc.velocity.X - num189 * 1.1f;
  413. }
  414. if ((double)(System.Math.Abs(npc.velocity.X) + System.Math.Abs(npc.velocity.Y)) < (double)num188 * 0.5)
  415. {
  416. if (npc.velocity.Y > 0f)
  417. {
  418. npc.velocity.Y = npc.velocity.Y + num189;
  419. }
  420. else
  421. {
  422. npc.velocity.Y = npc.velocity.Y - num189;
  423. }
  424. }
  425. }
  426. else
  427. {
  428. if (npc.velocity.Y < num192)
  429. {
  430. npc.velocity.Y = npc.velocity.Y + num189 * 1.1f;
  431. }
  432. else if (npc.velocity.Y > num192)
  433. {
  434. npc.velocity.Y = npc.velocity.Y - num189 * 1.1f;
  435. }
  436. if ((double)(System.Math.Abs(npc.velocity.X) + System.Math.Abs(npc.velocity.Y)) < (double)num188 * 0.5)
  437. {
  438. if (npc.velocity.X > 0f)
  439. {
  440. npc.velocity.X = npc.velocity.X + num189;
  441. }
  442. else
  443. {
  444. npc.velocity.X = npc.velocity.X - num189;
  445. }
  446. }
  447. }
  448. }
  449. }
  450. }
  451. npc.rotation = (float)System.Math.Atan2((double)npc.velocity.Y, (double)npc.velocity.X) + 1.57f;
  452. if (head)
  453. {
  454. if (flag18)
  455. {
  456. if (npc.localAI[0] != 1f)
  457. {
  458. npc.netUpdate = true;
  459. }
  460. npc.localAI[0] = 1f;
  461. }
  462. else
  463. {
  464. if (npc.localAI[0] != 0f)
  465. {
  466. npc.netUpdate = true;
  467. }
  468. npc.localAI[0] = 0f;
  469. }
  470. if (((npc.velocity.X > 0f && npc.oldVelocity.X < 0f) || (npc.velocity.X < 0f && npc.oldVelocity.X > 0f) || (npc.velocity.Y > 0f && npc.oldVelocity.Y < 0f) || (npc.velocity.Y < 0f && npc.oldVelocity.Y > 0f)) && !npc.justHit)
  471. {
  472. npc.netUpdate = true;
  473. return;
  474. }
  475. }
  476. }
  477. CustomBehavior();
  478. }
  479.  
  480. public virtual void Init()
  481. {
  482. }
  483.  
  484. public virtual bool ShouldRun()
  485. {
  486. return false;
  487. }
  488.  
  489. public virtual void CustomBehavior()
  490. {
  491. }
  492. }
  493. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement