Advertisement
Guest User

Untitled

a guest
Sep 2nd, 2016
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 18.24 KB | None | 0 0
  1. // -- Initializing a Battle --
  2. // create()
  3. // setX(real x) - Sets the x location to battle in (center)
  4. // setY(real y) - Sets the y location to battle in (center)
  5. // setLocation(Location loc) - Sets x/y of arena to Loc
  6. // setEnemies(group enemies) - Declares which group are the
  7. // enemies to fight in battle
  8. // setHeroes(group heroes) - Same for heroes.
  9. // (note: these groups are NOT destroyed after use)
  10. // setArenaSize(real x, real y) - Sets the dimensions of
  11. // the arena to use.
  12. // start() - Initiates the battle. Moves heroes/enemies in,
  13. // and starts the first move.
  14. //
  15. // -- Battle Flow --
  16. // pause() - Stops battling units from acting
  17. // resume() - Resumes action - same unit may go again
  18. // next() - Allows the next unit in order to act
  19. //
  20. // -- Ending a Battle --
  21. // boolean isBattleOver() - Returns whether or not battle has ended
  22. // Boolean hasPlayerWon() - Returns true if player won the battle
  23. // (so, if (not)hasPlayerWon() and isBattleOver(), then AI won)
  24. // Destroy() - Call after battle is no longer used to free resources
  25.  
  26. struct Battle
  27.  
  28. /**
  29. * Some general constants. You should edit these.
  30. * ENEMY is self explanatory.
  31. * The characters that belong to the enemy.
  32.  
  33. * SELECTOR is the unit-type of the command selector dummy
  34. * Players use this unit to select what they want to do.
  35. *
  36. * The size of the orders array plus one is the limit of
  37. * units in the arena. Same thing for the array heroList,
  38. * but for max heroes in arena. MAX_HEROES is this index + 1
  39. */
  40. public static constant player ENEMY = Player(11)
  41. private static constant integer SELECTOR = 'h002'
  42. private unit array order[12]
  43.  
  44. /**
  45. * Gets a unit speed to determine turn order
  46. * Should be edited by user. This is just an example function.
  47. */
  48. private static method getUnitSpeed takes unit u returns integer
  49. if(GetOwningPlayer(u)!=ENEMY)then
  50. return (GetUnitPointValue(u) + (GetHeroAgi(u,true)/20))+1
  51. else
  52. return (GetUnitPointValue(u) + (GetHeroAgi(u,true)/20))
  53. endif
  54. endmethod
  55.  
  56. /** Do not edit below **/
  57.  
  58. /**
  59. * Results
  60. */
  61. private boolean battleOver = false
  62. private boolean playerWon = false
  63.  
  64. /**
  65. * For internal trigger work.
  66. */
  67. private static Battle array battles
  68. private static integer battleCount = 0
  69. private static boolean triggersExist = false
  70.  
  71. /**
  72. * Instance variables
  73. */
  74. public group enemies
  75. public group heroes
  76. public integer turn = 1
  77. public real arenaX = 0
  78. public real arenaY = 0
  79. public real x = 0
  80. public real y = 0
  81. private real maxHeroes = 4
  82. private real maxEnemies = 4
  83. private real enemyIndex = 0
  84. private real heroIndex = 0
  85. private integer nextUnit = 0
  86. private unit orderingUnit = null
  87. private unit actionSelector
  88.  
  89.  
  90. //---------- SETTERS ----------//
  91.  
  92. public method setEnemies takes group g returns nothing
  93. set .enemies = g
  94. endmethod
  95.  
  96. public method setHeroes takes group g returns nothing
  97. set .heroes = g
  98. endmethod
  99.  
  100. public method setArenaX takes real arenaX returns nothing
  101. set .arenaX = arenaX
  102. endmethod
  103.  
  104. public method setArenaY takes real arenaY returns nothing
  105. set .arenaY = arenaY
  106. endmethod
  107.  
  108. public method setArenaSize takes real x, real y returns nothing
  109. call .setArenaX(x)
  110. call .setArenaY(y)
  111. endmethod
  112.  
  113. public method setX takes real x returns nothing
  114. set .x = x
  115. endmethod
  116.  
  117. public method setY takes real y returns nothing
  118. set .y = y
  119. endmethod
  120.  
  121. public method setLocation takes location loc returns nothing
  122. call .setX(GetLocationX(loc))
  123. call .setY(GetLocationY(loc))
  124. endmethod
  125.  
  126. public method setEnemyCapacity takes integer i returns nothing
  127. set .maxEnemies = i
  128. endmethod
  129.  
  130. public method setHeroCapacity takes integer i returns nothing
  131. set .maxHeroes = i
  132. endmethod
  133.  
  134. public method isBattleOver takes nothing returns boolean
  135. return battleOver
  136. endmethod
  137.  
  138. public method hasPlayerWon takes nothing returns boolean
  139. return playerWon
  140. endmethod
  141.  
  142. //---------- HELPER METHODS ----------//
  143.  
  144. public method unitInBattle takes unit u returns boolean
  145. return(IsUnitInGroup(u,.heroes) or IsUnitInGroup(u,.enemies))
  146. endmethod
  147.  
  148. /**
  149. * Moves the selected unit to its appropriate battle spot.
  150. * Top area for enemies, bottom area for heroes.
  151. */
  152. private method moveUnitToBattle takes unit u, boolean enemy returns nothing
  153. local real d = 0
  154. local real theta = 0
  155. local real x1 = .x
  156. local real y1 = .y
  157. call PauseUnit(u,true)
  158. call SetUnitVertexColor(u,100,100,100,200)
  159.  
  160. //Move up/down
  161. set d = arenaY/3
  162. if(enemy)then
  163. set theta = 90
  164. else
  165. set theta = 270
  166. endif
  167. set y1 = Position_polarProjectY(.y,d,theta)
  168.  
  169. //Move to the far end of the right side
  170. if(enemy)then
  171. if(maxEnemies>1)then
  172. set d = arenaX/4
  173. set theta = 180
  174. set x1 = Position_polarProjectX(.x,d,theta)
  175. endif
  176. elseif(maxHeroes>1)then
  177. set d = arenaX/4
  178. set theta = 180
  179. set x1 = Position_polarProjectX(.x,d,theta)
  180. endif
  181.  
  182. //Move left by intervals, for each unit added.
  183. set theta = 0
  184. if(enemy)then
  185. if(maxEnemies>1)then
  186. set d = (arenaX/2) * ((enemyIndex)/(maxEnemies))
  187. set enemyIndex = enemyIndex + 1
  188. set d = d * 1.3
  189. set x1 = Position_polarProjectX(x1,d,theta)
  190. endif
  191. elseif(maxHeroes>1)then
  192. set d = (arenaX/2) * ((heroIndex)/(maxHeroes))
  193. set heroIndex = heroIndex + 1
  194. set d = d * 1.3
  195. set x1 = Position_polarProjectX(x1,d,theta)
  196. endif
  197.  
  198. //Move unit to final position
  199. call SetUnitX(u,x1)
  200. call SetUnitY(u,y1)
  201. if(enemy)then
  202. call SetUnitFacing(u,270)
  203. else
  204. call SetUnitFacing(u,90)
  205. endif
  206. endmethod
  207.  
  208. /**
  209. * Obtains the fastest unit from a group.
  210. * Does not edit initial list.
  211. */
  212. private method getFastestUnitFromGroup takes group g returns unit
  213. local group temp = CreateGroup()
  214. local unit fog = null
  215. local integer highestSpeed = 0
  216. local unit fastestUnit = null
  217. local integer speed = 0
  218. call GroupAddGroup(g,temp)
  219. loop
  220. set fog = FirstOfGroup(temp)
  221. exitwhen fog == null
  222. set speed = getUnitSpeed(fog)
  223. if(speed>highestSpeed)then
  224. set highestSpeed = speed
  225. set fastestUnit = fog
  226. endif
  227. call GroupRemoveUnit(temp,fog)
  228. endloop
  229. call DestroyGroup(temp)
  230. set fog = null
  231. set temp = null
  232. return fastestUnit
  233. endmethod
  234.  
  235. /**
  236. * Sorts units by speed and adds to order (array)
  237. * Group will be destroyed after completion
  238. */
  239. private method populateActionList takes group g returns nothing
  240. local integer i = 1
  241. local integer count = CountUnitsInGroup(g)
  242. local unit u = null
  243. loop
  244. exitwhen i>count
  245. set u=getFastestUnitFromGroup(g)
  246. set order[i]=u
  247. //call BJDebugMsg("Unit " + I2S(i) + ": " + GetUnitName(u) + " of " + GetPlayerName(GetOwningPlayer(u)))
  248. call GroupRemoveUnit(g,u)
  249. set i = i + 1
  250. endloop
  251. call DestroyGroup(g)
  252. set g = null
  253. set u = null
  254. endmethod
  255.  
  256. //--------- PARSING ORDERS ---------//
  257.  
  258. /**
  259. * Gives all items from src to target
  260. */
  261. private method exchangeItems takes unit src, unit target returns nothing
  262. local integer i = 0
  263. loop
  264. exitwhen i > 5
  265. if(GetItemType(UnitItemInSlot(target, i)) != ITEM_TYPE_PERMANENT )then
  266. call UnitAddItem(target,UnitRemoveItemFromSlot(src,i))
  267. endif
  268. set i = i + 1
  269. endloop
  270. endmethod
  271.  
  272. /**
  273. * Forces AI to take an action
  274. * Uses behavior from AI List scope
  275. */
  276. private method aiAction takes nothing returns nothing
  277. local AI temp
  278. set temp = AIList_getAIOfUnitType(GetUnitTypeId(orderingUnit))
  279. if(temp!=0)then
  280. call temp.run(orderingUnit,this)
  281. else
  282. call DisplayTextToForce(GetPlayersAll(),"AI not found: " + GetUnitName(orderingUnit))
  283. call next()
  284. endif
  285. endmethod
  286.  
  287. /**
  288. * Create a command selector for the player.
  289. */
  290. private method createMenu takes nothing returns nothing
  291. local integer i = 0
  292. local unit selector = CreateUnit(GetOwningPlayer(orderingUnit),SELECTOR,.x,.y,0)
  293. call exchangeItems(orderingUnit,selector)
  294. call SelectUnitForPlayerSingle(selector,GetOwningPlayer(orderingUnit))
  295. call UnitRemoveAbility(selector,'Amov')
  296. call UnitRemoveAbility(selector,'Astp')
  297. set .actionSelector = selector
  298. call Spells_giveAbilities(orderingUnit,selector)
  299. endmethod
  300.  
  301. //--------- FLOW - SELECTION OF UNITS, ALLOWING CAST---------//
  302.  
  303. /**
  304. * Checks whether the battle is over, and if so, who won.
  305. */
  306. private method battleStatus takes nothing returns nothing
  307. local unit fog = null
  308. local boolean playerDead = true
  309. local boolean aiDead = true
  310. local group tempHeroes = CreateGroup()
  311. local group tempEnemies = CreateGroup()
  312.  
  313. call GroupAddGroup(.heroes,tempHeroes)
  314. call GroupAddGroup(.enemies,tempEnemies)
  315.  
  316. //check if player wiped
  317. loop
  318. set fog = FirstOfGroup(tempHeroes)
  319. exitwhen fog==null
  320. if(IsUnitAliveBJ(fog))then
  321. set playerDead=false
  322. endif
  323. call GroupRemoveUnit(tempHeroes,fog)
  324. endloop
  325. //now for AI
  326. set fog = null
  327. loop
  328. set fog = FirstOfGroup(tempEnemies)
  329. exitwhen fog==null
  330. if(IsUnitAliveBJ(fog))then
  331. set aiDead=false
  332. endif
  333. call GroupRemoveUnit(tempEnemies,fog)
  334. endloop
  335.  
  336. if(aiDead or playerDead)then
  337. set battleOver = true
  338. set playerWon = aiDead
  339. endif
  340.  
  341. set fog = null
  342. call DestroyGroup(tempHeroes)
  343. call DestroyGroup(tempEnemies)
  344. set tempHeroes = null
  345. set tempEnemies = null
  346. endmethod
  347.  
  348. /**
  349. * Stops all units from ordering, until next or resume is called.
  350. */
  351. public method pause takes nothing returns nothing
  352. call exchangeItems(actionSelector,orderingUnit)
  353. call RemoveUnit(actionSelector)
  354. endmethod
  355.  
  356. /**
  357. * Allows the same unit to attack again
  358. * Might be useful for spells that allow you to move again after
  359. */
  360. public method resume takes nothing returns nothing
  361. call battleStatus()
  362. if(battleOver==false)then
  363. call SetUnitVertexColor(orderingUnit,255,255,255,255)
  364. if(GetOwningPlayer(orderingUnit)==ENEMY)then
  365. call aiAction()
  366. else
  367. call createMenu()
  368. endif
  369. endif
  370. endmethod
  371.  
  372. /**
  373. * Allows the next unit in order to attack
  374. */
  375. public method next takes nothing returns nothing
  376. set Spells_turnUnit = orderingUnit
  377. call ExecuteFunc("Behavior_onTurnEnd")
  378. call battleStatus()
  379. if(battleOver==false)then
  380. call SetUnitVertexColor(orderingUnit,100,100,100,200)
  381.  
  382. set orderingUnit = order[nextUnit]
  383. set nextUnit = nextUnit + 1
  384. loop
  385. //filter out dead units
  386. exitwhen orderingUnit != null and IsUnitAliveBJ(orderingUnit) and General_checkAct(orderingUnit)
  387. set orderingUnit = order[nextUnit]
  388. set nextUnit = nextUnit + 1
  389. if(nextUnit>=12)then
  390. set nextUnit=0
  391. set turn = turn + 1
  392. endif
  393. endloop
  394. //call BJDebugMsg(I2S(nextUnit))
  395.  
  396. call battleStatus()
  397. if(battleOver==false)then
  398. //call BJDebugMsg("Ordering: " + GetUnitName(orderingUnit) + " of " + GetPlayerName(GetOwningPlayer(orderingUnit)))
  399. call SetUnitVertexColor(orderingUnit,255,255,255,255)
  400. if(GetOwningPlayer(orderingUnit)==ENEMY)then
  401. call aiAction()
  402. else
  403. call createMenu()
  404. endif
  405. endif
  406.  
  407. endif
  408. endmethod
  409.  
  410. //---------- USER METHODS ----------//
  411.  
  412. /**
  413. * Move all units to their respective areas in the battle arena.
  414. * Pauses and changes their coloring
  415. */
  416. public method start takes nothing returns nothing
  417. local unit fog = null
  418. local group allUnits = CreateGroup()
  419. local group tempHeroes = CreateGroup()
  420. local group tempEnemies = CreateGroup()
  421.  
  422. call GroupAddGroup(.enemies,allUnits)
  423. call GroupAddGroup(.heroes,allUnits)
  424. call GroupAddGroup(.heroes,tempHeroes)
  425. call GroupAddGroup(.enemies,tempEnemies)
  426.  
  427. loop
  428. set fog = FirstOfGroup(tempEnemies)
  429. exitwhen fog == null
  430. call moveUnitToBattle(fog,true)
  431. call GroupRemoveUnit(tempEnemies,fog)
  432. endloop
  433. loop
  434. set fog = FirstOfGroup(tempHeroes)
  435. exitwhen fog == null
  436. call moveUnitToBattle(fog,false)
  437. call GroupRemoveUnit(tempHeroes,fog)
  438. endloop
  439.  
  440. //note: this call destroys and nulls allUnits
  441. call .populateActionList(allUnits)
  442. call .next()
  443.  
  444. set fog = null
  445. call DestroyGroup(tempHeroes)
  446. call DestroyGroup(tempEnemies)
  447. set tempHeroes = null
  448. set tempEnemies = null
  449. endmethod
  450.  
  451. //---------- PROCESSING BATTLE FLOW ----------//
  452.  
  453. /**
  454. * Confirms that the caster is a command selector
  455. */
  456. private static method isSelector takes nothing returns boolean
  457. return(GetUnitTypeId(GetTriggerUnit())==thistype.SELECTOR)
  458. endmethod
  459.  
  460. /**
  461. * Parses the command that the user gave, and executes it.
  462. */
  463. private static method onCast takes nothing returns nothing
  464. local Battle whichBattle = 0
  465. local integer count = 0
  466. local unit caster = GetTriggerUnit()
  467. local Action whichAction = 0
  468. local unit target = GetSpellTargetUnit()
  469. loop
  470. exitwhen count > battleCount
  471. if(caster==battles[count].actionSelector)then
  472. set whichBattle=battles[count]
  473. endif
  474. set count = count + 1
  475. endloop
  476. if(whichBattle!=0)then
  477. set whichAction = Spells_convertAbilityIdToAction(GetSpellAbilityId())
  478. if(whichAction!=0)then
  479. call whichAction.run(whichBattle.orderingUnit,target,whichBattle)
  480. else
  481. call DisplayTextToForce(GetPlayersAll(),"Unknown action: " + I2S(GetSpellAbilityId()))
  482. endif
  483. endif
  484. set caster = null
  485. set target = null
  486. set whichAction = 0
  487. set whichBattle = 0
  488. endmethod
  489.  
  490. /**
  491. *
  492. */
  493. private static method onDeath takes nothing returns nothing
  494. call purge(GetDyingUnit())
  495. endmethod
  496.  
  497. /**
  498. * Create basic triggers that all instances use.
  499. */
  500. private method makeTriggers takes nothing returns nothing
  501. local trigger t = CreateTrigger()
  502. local trigger t2 = CreateTrigger()
  503. call TriggerAddAction(t,function thistype.onCast)
  504. call TriggerRegisterAnyUnitEventBJ(t,EVENT_PLAYER_UNIT_SPELL_EFFECT)
  505. call TriggerAddCondition(t,function thistype.isSelector)
  506. call TriggerAddAction(t2,function thistype.onDeath)
  507. call TriggerRegisterAnyUnitEventBJ(t2,EVENT_PLAYER_UNIT_DEATH)
  508. set t = null
  509. set t2 = null
  510. endmethod
  511.  
  512. //---------- CONSTRUCTOR/DESTRUCTOR ----------//
  513.  
  514. /**
  515. * Required constructor.
  516. * Creates triggers if none exist currently
  517. * Saves reference to this battle in an array,
  518. * and counts how many battles exist.
  519. */
  520. public static method create takes nothing returns thistype
  521. local thistype new = thistype.allocate()
  522. if(thistype.triggersExist==false)then
  523. call new.makeTriggers()
  524. set triggersExist=true
  525. endif
  526. set battles[battleCount]=new
  527. set battleCount=battleCount+1
  528. return new
  529. endmethod
  530.  
  531. /**
  532. * Destroys this battle
  533. * Note: does NOT destroy the heroes/enemies unit group.
  534. * Since the user might want to keep their group intact
  535. * after the battle ends.
  536. */
  537. public method destroy takes nothing returns nothing
  538. local unit u = null
  539. local integer i = 0
  540. local unit fog = null
  541.  
  542. loop
  543. set fog = FirstOfGroup(.enemies)
  544. exitwhen fog == null
  545. call RemoveUnit(fog)
  546. call GroupRemoveUnit(.enemies,fog)
  547. endloop
  548.  
  549. call RemoveUnit(actionSelector)
  550. set actionSelector = null
  551. set orderingUnit = null
  552. call DestroyGroup(.enemies)
  553. set heroes = null
  554. set enemies = null
  555. loop
  556. set u = order[i]
  557. exitwhen u == null
  558. set order[i] = null
  559. set i = i + 1
  560. endloop
  561. call this.deallocate()
  562. endmethod
  563.  
  564. endstruct
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement