habi.py 60 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247
  1. # ---------------------------------------------------------------------
  2. # neobots 1.0.5 -------------------------------------------------------
  3. # Made by txtsd -------------------------------------------------------
  4. # Based on raredaredevil's/DarkByte's HabiBot/NeoAuto -----------------
  5. # Open Source Neopets Automation --------------------------------------
  6. # ---------------------------------------------------------------------
  7. # Import needed classes
  8. import time # Time module, used for sleeping
  9. import datetime # Time for ticks
  10. import random # Used to randomise numbers and summon one from between a range
  11. import sys
  12. import threading
  13. # Begin Class
  14. class habi:
  15. # Checked
  16. def __init__(self, acc, thegateway, build_housecount,
  17. build_nestcount, build_storagecount, build_hospitalcount):
  18. self.lastexp = 0
  19. self.lastlevel = 0
  20. # A flag for to detect if this is the first run of the loop or not
  21. self.isfirstrun = 1
  22. # Buffer to store the last time we checked for gems on stage
  23. self.LAST_GEM_CHECK_TIME = 1
  24. # Buffer to store the last time we checked for eggs on stage
  25. self.LAST_EGG_CHECK_TIME = 1
  26. self.LAST_UPGRADE_CHECK_TIME = 1
  27. # Set amfs opener to our accounts (share the login/cookie)
  28. # self.theopener = acc.opener.open
  29. self.gateway = thegateway
  30. # Only load the services once when class inits, per tick just adds
  31. # more room for failure
  32. self.player_service = self.amfgetservice("PlayerService")
  33. self.event_service = self.amfgetservice("EventService")
  34. self.scene_service = self.amfgetservice("SceneService")
  35. self.Inventory_Service = self.amfgetservice("InventoryService")
  36. self.store_service = self.amfgetservice("StoreService")
  37. self.upgradeservice = self.amfgetservice("UpgradeService")
  38. print "-------init-------"
  39. self.mapdata = []
  40. self.themapid = '-1'
  41. self.amf_levelinfo = ""
  42. self.build_nestcount = build_nestcount
  43. self.build_housecount = build_housecount
  44. self.build_storagecount = build_storagecount
  45. self.build_hospitalcount = build_hospitalcount
  46. # Urgently buy this item id, because we need it to complete our map
  47. # (Set in build item function, if we dont have the item needed)
  48. self.urgentbuy = 0
  49. self.mapary = []
  50. self.dumpWorker = ""
  51. # with open("./storeItems.txt", "w") as f:
  52. # f.write(str(self.store_service.storeItems()))
  53. # self.itemcollection = self.scene_service.sceneItems()
  54. # for x in self.itemcollection:
  55. # if x['sceneItemType'] == "Structure":
  56. # print "Struc"
  57. # if x['x'] == "5" and x['y'] == "2":
  58. # print "5,2"
  59. # self.Inventory_Service.moveToItemBag(str(x["m_id"]))
  60. # print "moved it to bag"
  61. # Checked
  62. def checkworkers(self):
  63. workerid = self.findnonbusyworker()
  64. doneone = 0
  65. if workerid > -1:
  66. lowestres = self.getlowestresource()
  67. if not (lowestres == "none"):
  68. print "[lowestRes:" + lowestres + "]"
  69. self.setworkercollect(workerid, lowestres)
  70. # Checked
  71. def checknonbuiltbuildings(self):
  72. doneone = 0
  73. pets = self.countpets()
  74. P3type = ""
  75. buildingdata = self.findnoncompletebuilding()
  76. if not buildingdata == -1:
  77. buildid = buildingdata["m_id"]
  78. badx = buildingdata["x"]
  79. bady = buildingdata["y"]
  80. if (pets[3] > 0):
  81. builderid = self.findnonbusysoldier()
  82. P3type = "soldier"
  83. else:
  84. builderid = self.findnonbusyworker()
  85. P3type = "worker"
  86. if builderid > -1:
  87. emptytile = self.findemptytilearound(badx, bady)
  88. if (emptytile): # Empty tile found
  89. doneone = 1
  90. self.scene_service.moveItem(
  91. str(builderid), str(emptytile[0]), str(emptytile[1]))
  92. print "[" + P3type + "#" + str(builderid) + "] now building unbuilt structure at [" + str(emptytile[0]) + "," + str(emptytile[1]) + "]"
  93. return doneone
  94. # Temp
  95. def threadcollectgem(self, gemid):
  96. self.scene_service.collectGem(gemid)
  97. # Checked
  98. def checkgems(self):
  99. self.itemcollection = self.scene_service.sceneItems()
  100. print "Looking for gems on stage"
  101. for x in self.itemcollection:
  102. if (x['sceneItemType'] == "Gem"):
  103. strid = str(x['m_id'])
  104. try:
  105. threading.Thread(target=self.threadcollectgem, args=(strid,)).start()
  106. except:
  107. # print "Gem thread Timeout Error"
  108. threading.Thread(target=self.threadcollectgem, args=(strid,)).start()
  109. # if str((self.scene_service.collectGem(str(x['m_id']))) ==
  110. # "True"):
  111. print "[gem#" + str(x['m_id']) + "] collected!"
  112. # Checked
  113. def countmapitems(self):
  114. counter = [0, 0, 0, 0, 0]
  115. for x in self.itemcollection:
  116. if (x['sceneItemType'] == "Structure"):
  117. counter[0] += 1
  118. if (x['type'] == "House"):
  119. counter[1] += 1
  120. elif (x['type'] == "Storage"):
  121. counter[2] += 1
  122. elif (x['type'] == "Nest"):
  123. counter[3] += 1
  124. elif (x['type'] == "Hospital"):
  125. counter[4] += 1
  126. return counter
  127. # Checked
  128. def readmapfile(self, thefolder):
  129. print str(thefolder)
  130. with open(thefolder) as f:
  131. lines = f.readlines()
  132. lines2 = []
  133. for x in lines:
  134. # Fix EOL
  135. lines2.append(x.replace("\n", ""))
  136. return lines2
  137. # Checked
  138. def loadmapdata(self, mapid):
  139. maps = self.readmapfile("./data/habimaps/" + mapid + ".txt")
  140. return maps
  141. # Checked
  142. def autoreshack(self):
  143. # Automatially perform reshack
  144. doneone = 1
  145. while doneone == 1:
  146. self.amf_playerinfo = self.player_service.playerinfo()
  147. resp = self.amf_playerinfo
  148. if int(resp['isSetup']) == 0:
  149. print "New habi detected, sending setup packet"
  150. self.player_service.skillSetup() # Sets up a brand new habi
  151. print "Resetting player"
  152. self.player_service.reset()
  153. rand = random.randrange(0, 100)
  154. if (rand < 50):
  155. self.themapid = '0'
  156. else:
  157. self.themapid = '2'
  158. print "Choosing map " + self.themapid
  159. self.scene_service.setupHabitarium(self.themapid)
  160. print "Setting tutorial to complete"
  161. self.player_service.setTutorialProgress("-1")
  162. print "Sending Update"
  163. self.event_service.update('40', '40')
  164. self.event_service.simulate()
  165. self.scene_service.collectGem("16")
  166. self.event_service.update('40', '40')
  167. self.itembag = self.Inventory_Service.itemBag()
  168. doneone = 0
  169. print "[#houses:" + str(self.countinvitems("House")) + "]"
  170. if (self.countinvitems("House") < self.build_housecount):
  171. # Reshack is generating a house
  172. doneone = 1
  173. print "Resource Hack Generating a house"
  174. self.store_service.buyItem("15") # 15h, 20s, 22n
  175. self.event_service.update('40', '40')
  176. self.event_service.simulate()
  177. print "Resource Hack Generated a house"
  178. print "[#storages:" + str(self.countinvitems("Storage")) + "]"
  179. if doneone == 0:
  180. if (self.countinvitems("Storage") < self.build_storagecount):
  181. doneone = 1
  182. print "Resource Hack Generating a Storage Center"
  183. self.store_service.buyItem("20") # 15h, 20s, 22n
  184. self.event_service.update('40', '40')
  185. self.event_service.simulate()
  186. print "Resource Hack Generated a Storage Center"
  187. print "[#nests:" + str(self.countinvitems("Nest")) + "]"
  188. if doneone == 0:
  189. if (self.countinvitems("Nest") < 20):
  190. doneone = 1
  191. print "Resource Hack Generating a Nest"
  192. self.store_service.buyItem("22") # 15h, 20s, 22n
  193. self.event_service.update('40', '40')
  194. self.event_service.simulate()
  195. print "Resource Hack Generated a Nest"
  196. if doneone == 0:
  197. # Nothing left to do break
  198. print "Done"
  199. break
  200. return 1
  201. # Checked
  202. def findinvitem(self, itemname):
  203. retid = 0
  204. for x in self.itembag:
  205. if (x[1]['sceneItemType'] == "Structure"):
  206. if (x[1]['type'] == itemname):
  207. if (x[1]['isBuilt'] is False):
  208. retid = x[1]['m_id']
  209. return retid
  210. return retid
  211. # Checked
  212. def findinveggprof(self, profname):
  213. retid = 0
  214. for x in self.itembag:
  215. if (x[1]['sceneItemType'] == "Egg"):
  216. if profname in x[1]['name']:
  217. retid = x[1]['m_id']
  218. return retid
  219. return retid
  220. # Checked
  221. def countinvitems(self, itemname):
  222. retid = 0
  223. # print "entered countinvitems" + str(retid)
  224. for x in self.itembag:
  225. # print "loop #" + str(x[0])
  226. if (x[1]['sceneItemType'] == "Structure"):
  227. if (x[1]['type'] == str(itemname)):
  228. # print "true"
  229. retid += 1
  230. # print "retid" + str(retid)
  231. return retid
  232. # Checked
  233. def countinveggs(self):
  234. counter = [0, 0, 0, 0]
  235. for x in self.itembag:
  236. if (x[1]['sceneItemType'] == "Egg"):
  237. counter[0] += 1
  238. if "Worker" in x[1]['name']:
  239. counter[1] += 1
  240. elif "Nester" in x[1]['name']:
  241. counter[2] += 1
  242. elif "Soldier" in x[1]['name']:
  243. counter[3] += 1
  244. return counter
  245. # Checked
  246. def setDumpWorker(self):
  247. for x in self.itemcollection:
  248. if (x['sceneItemType'] == "Character"):
  249. if (x['profession'] == "worker"):
  250. self.dumpWorker = x['m_id']
  251. break
  252. # print "[worker#" + str(self.dumpWorker) + "] has been set as nestdump
  253. # builder"
  254. return x
  255. # ---------------------------------------------------------------------
  256. # DoLoop()
  257. # ---------------------------------------------------------------------
  258. def DoLoop(self):
  259. try:
  260. print "\n----Begin Loop----"
  261. # self.gateway.opener = self.theopener
  262. self.itemcollection = self.scene_service.sceneItems()
  263. self.amf_playerinfo = self.player_service.playerinfo()
  264. playerlevel = self.amf_playerinfo['habitariumLevel']
  265. self.itembag = self.Inventory_Service.itemBag()
  266. if (playerlevel < 2):
  267. if not ((self.countinvitems("House") >= self.build_housecount) and (self.countinvitems("Storage") >= self.build_storagecount) and (self.countinvitems("Nest") >= 20)):
  268. self.autoreshack()
  269. print "---End ResHack----"
  270. return 0
  271. # Check if first run; if so, load some config
  272. if self.isfirstrun == 1:
  273. # Force check reshack
  274. # self.needResHack()
  275. print "First run of tick, loading files"
  276. # This is the first run of this code..
  277. # Players map id
  278. for x in self.itemcollection:
  279. if (x['sceneItemType'] == 'Resource'):
  280. if (x['x'] == 1) and (x['y'] == 1):
  281. if (x['type'] == 'pollen'):
  282. self.themapid = '0'
  283. print 'found pollen at [1,1]'
  284. break
  285. if (x['x'] == 1) and (x['y'] == 2):
  286. if (x['type'] == 'water'):
  287. self.themapid = '1'
  288. print 'found water at [1,2]'
  289. break
  290. if (x['x'] == 0) and (x['y'] == 4):
  291. if (x['type'] == 'stone'):
  292. self.themapid = '2'
  293. print 'found stone at [0,4]'
  294. break
  295. # load the map file
  296. self.mapary = self.loadmapdata(self.themapid)
  297. self.isfirstrun = 0
  298. print "-----End Loop-----"
  299. return 0
  300. # End firstRun check
  301. if (self.themapid == '-1'):
  302. print 'Map not loaded. Re-run client.'
  303. sys.exit()
  304. self.amf_levelinfo = self.event_service.simulate() # Level info
  305. self.updateinfo = self.event_service.update('40', '40')
  306. maxxp = self.amf_levelinfo['player']['levelInfo']['nextXP']
  307. thisexp = self.amf_levelinfo['player']['xp'] - self.lastexp
  308. self.lastexp = self.amf_levelinfo['player']['xp']
  309. if (self.amf_playerinfo['habitariumLevel'] > self.lastlevel):
  310. self.player_service.nextLevelGains()
  311. self.lastlevel = self.amf_playerinfo['habitariumLevel']
  312. # Set counts for feed
  313. self.stonecount = int(self.updateinfo['resources']['stone'])
  314. self.mudcount = int(self.updateinfo['resources']['mud'])
  315. self.woodcount = int(self.updateinfo['resources']['wood'])
  316. self.pollencount = int(self.updateinfo['resources']['pollen'])
  317. self.grasscount = int(self.updateinfo['resources']['grass'])
  318. self.watercount = int(self.updateinfo['resources']['water'])
  319. if (playerlevel < 50):
  320. print "[" + str(datetime.datetime.now().time())[:-3] + "] [" + str(self.amf_playerinfo['neoUsername']) + "] [level:" + str(self.amf_playerinfo['habitariumLevel']) + "] [+exp:" + str(thisexp) + "] [totalExp:" + str(self.amf_levelinfo['player']['xp']) + "/" + str(maxxp) + "]"
  321. else:
  322. print "[" + str(datetime.datetime.now().time())[:-3] + "] [" + str(self.amf_playerinfo['neoUsername']) + "] [level:" + str(self.amf_playerinfo['habitariumLevel']) + "] [nextGem:" + str((self.amf_levelinfo['player']['cappedXP']) % 1000) + "/1000] [lvl50gemsCollected:" + str(self.amf_levelinfo['player']['cappedXPRewards']) + "]"
  323. print "[#stone:" + str(self.stonecount) + "] [#mud:" + str(self.mudcount) + "] [#wood:" + str(self.woodcount) + "] [#pollen:" + str(self.pollencount) + "] [#grass:" + str(self.grasscount) + "] [#water:" + str(self.watercount) + "]"
  324. # Counts and prints the amount of each type of P3
  325. # pets = self.countpets()
  326. # print "[#workers:" + str(pets[1]) + "] [#nesters:" + str(pets[2]) + "] [#soldiers:" + str(pets[3]) + "]"
  327. # Do timer dependent code
  328. self.tilecollection = self.amf_levelinfo['map']['tiles']
  329. if (time.time() - float(self.LAST_GEM_CHECK_TIME) > 120): # 2min
  330. self.LAST_GEM_CHECK_TIME = time.time() # Update time
  331. self.checkgems() # Check for any gems and collect them
  332. if self.checknonbuiltbuildings() == 0:
  333. if self.findnonhealthybuildings() == 0:
  334. self.checkworkers()
  335. self.buildmap(playerlevel)
  336. self.checkhungry()
  337. # Check for full houses with healed pets and move them out
  338. self.checkhouses()
  339. self.checkhospitals()
  340. self.checknesters()
  341. # Check for eggs, harvest a bunch, then hatch/discard
  342. self.checkeggs()
  343. # We dont urgently need to buy a item
  344. if self.urgentbuy == 0:
  345. # Check for upgrades based on current player level.
  346. self.checkupgrades(playerlevel)
  347. # print "Done checking"
  348. if (playerlevel >= 30):
  349. if (self.stonecount > 20000) and (self.woodcount > 20000) and (self.mudcount > 20000):
  350. print "Buying a dump nest"
  351. self.store_service.buyItem("22")
  352. if (playerlevel >= 40):
  353. if (self.stonecount > 8250) and (self.woodcount > 6900) and (self.mudcount > 4850):
  354. self.dumpNest()
  355. else:
  356. # Urgentbuy
  357. self.urgentBuy()
  358. # print "Done spending"
  359. print "-----End Loop-----"
  360. except KeyboardInterrupt:
  361. print "\n------------------"
  362. print "neobots was interrupted via keyboard"
  363. print "------------------"
  364. sys.exit()
  365. except:
  366. print "------------------"
  367. print "-Something broke--"
  368. print "------------------"
  369. time.sleep(10)
  370. # ---------------------------------------------------------------------
  371. # End DoLoop()
  372. # ---------------------------------------------------------------------
  373. # Checked - Made this myself
  374. def dumpNest(self):
  375. if not (self.itemexistat(0, 9)):
  376. if (self.countinvitems("Nest") > 3):
  377. getnest = self.findinvitem("Nest")
  378. # hardcoded location change later
  379. if (getnest != 0):
  380. self.scene_service.moveItem(str(getnest), "0", "9")
  381. print "[dumpNest#" + str(getnest) + "] placed at [0,9]"
  382. else:
  383. for x in self.itemcollection:
  384. if (x['sceneItemType'] == "Structure"):
  385. if (x['x'] == "0") and (x['y'] == "9"):
  386. if (x['isBuilt'] is False):
  387. if (self.countpets()[3] > 0):
  388. soldier = self.findnonbusysoldier()
  389. if soldier > -1:
  390. # hardcoded location change later
  391. print "[soldier#" + str(soldier) + "] now building a dump nest"
  392. self.scene_service.moveItem(
  393. str(soldier), "1", "9")
  394. else:
  395. dudex = self.setDumpWorker()
  396. if not (dudex['attachedTo'] == x['m_id']):
  397. print "[worker#" + str(dudex['m_id']) + "] now building a dump nest"
  398. self.scene_service.moveItem(
  399. str(dudex['m_id']), "1", "9")
  400. else:
  401. if (x['itemLevel'] == 1):
  402. if (self.mudcount > 850):
  403. if (self.stonecount > 1050):
  404. if (self.woodcount > 800):
  405. print "[dumpNest#" + str(x['m_id']) + "] upgraded to lvl2"
  406. self.upgradeservice.buyUpgrade(
  407. "22", str(x['m_id']))
  408. elif (x['itemLevel'] == 2):
  409. if (self.mudcount > 950):
  410. if (self.stonecount > 1300):
  411. if (self.woodcount > 900):
  412. print "[dumpNest#" + str(x['m_id']) + "] upgraded to lvl3"
  413. self.upgradeservice.buyUpgrade(
  414. "23", str(x['m_id']))
  415. elif (x['itemLevel'] == 3):
  416. print "[dumpNest#" + str(x['m_id']) + "] has served its purpose"
  417. self.Inventory_Service.moveToItemBag(
  418. str(x['m_id']))
  419. print "Moved [dumpNest#" + str(x['m_id']) + "] to bag"
  420. self.Inventory_Service.deleteItem(
  421. str(x['m_id']))
  422. print "Deleted [dumpNest#" + str(x['m_id']) + "] from bag"
  423. def buildmap(self, playerlevel):
  424. structures = self.countmapitems()
  425. print "[tStrc,tHous,tStor,tNest,tHosp] " + str(structures)
  426. if (structures[1] < self.build_housecount):
  427. print "Trying to build a House"
  428. invitem = self.findinvitem("House")
  429. if (invitem == 0):
  430. self.urgentbuy = 15
  431. print "Do not have a needed item in iventory; item will be bought"
  432. return 0
  433. else:
  434. print "House found in inventory so building it"
  435. shuffledmap = self.mapary
  436. random.shuffle(shuffledmap)
  437. for currenttile in shuffledmap:
  438. tempdata = currenttile.split(":")
  439. tilecoords = tempdata[0]
  440. tiletype = tempdata[1]
  441. tilecoords = tilecoords.split(",")
  442. tileX = tilecoords[0]
  443. tileY = tilecoords[1]
  444. if tiletype == "build":
  445. if self.itemexistat(tileX, tileY) is False:
  446. self.scene_service.moveItem(
  447. str(invitem), str(tileX), str(tileY))
  448. self.urgentbuy = 0
  449. return 1
  450. if (structures[2] < self.build_storagecount):
  451. print "Trying to build a Storage"
  452. invitem = self.findinvitem("Storage")
  453. if (invitem == 0):
  454. self.urgentbuy = 20
  455. print "Do not have a needed item in iventory; item will be bought"
  456. return 0
  457. else:
  458. print "Storage found in inventory so building it"
  459. shuffledmap = self.mapary
  460. random.shuffle(shuffledmap)
  461. for currenttile in shuffledmap:
  462. tempdata = currenttile.split(":")
  463. tilecoords = tempdata[0]
  464. tiletype = tempdata[1]
  465. tilecoords = tilecoords.split(",")
  466. tileX = tilecoords[0]
  467. tileY = tilecoords[1]
  468. if tiletype == "build":
  469. if self.itemexistat(tileX, tileY) is False:
  470. self.scene_service.moveItem(
  471. str(invitem), str(tileX), str(tileY))
  472. self.urgentbuy = 0
  473. return 1
  474. if (structures[3] < self.build_nestcount):
  475. print "Trying to build a Nest"
  476. invitem = self.findinvitem("Nest")
  477. if (invitem == 0):
  478. self.urgentbuy = 22
  479. print "Do not have a needed item in iventory; item will be bought"
  480. return 0
  481. else:
  482. print "Nest found in inventory so building it"
  483. shuffledmap = self.mapary
  484. random.shuffle(shuffledmap)
  485. for currenttile in shuffledmap:
  486. tempdata = currenttile.split(":")
  487. tilecoords = tempdata[0]
  488. tiletype = tempdata[1]
  489. tilecoords = tilecoords.split(",")
  490. tileX = tilecoords[0]
  491. tileY = tilecoords[1]
  492. if tiletype == "build":
  493. if self.itemexistat(tileX, tileY) is False:
  494. self.scene_service.moveItem(
  495. str(invitem), str(tileX), str(tileY))
  496. self.urgentbuy = 0
  497. return 1
  498. if (playerlevel >= 15):
  499. if (structures[4] < self.build_hospitalcount):
  500. print "Trying to build a Hospital"
  501. invitem = self.findinvitem("Hospital")
  502. if (invitem == 0):
  503. self.urgentbuy = 41
  504. print "Do not have a needed item in iventory; item will be bought"
  505. return 0
  506. else:
  507. print "Hospital found in inventory so building it"
  508. shuffledmap = self.mapary
  509. random.shuffle(shuffledmap)
  510. for currenttile in shuffledmap:
  511. tempdata = currenttile.split(":")
  512. tilecoords = tempdata[0]
  513. tiletype = tempdata[1]
  514. tilecoords = tilecoords.split(",")
  515. tileX = tilecoords[0]
  516. tileY = tilecoords[1]
  517. if tiletype == "build":
  518. if self.itemexistat(tileX, tileY) is False:
  519. self.scene_service.moveItem(
  520. str(invitem), str(tileX), str(tileY))
  521. self.urgentbuy = 0
  522. return 1
  523. # Checked - AMF stuff makes no sense to me
  524. def amfgetservice(self, servicename):
  525. # self.gateway.opener = self.theopener
  526. theret = self.gateway.getService(servicename) # Get The service
  527. return theret
  528. # Checked - Modified to flow better
  529. def checkupgrades(self, playerlevel):
  530. print "Searching for upgrades"
  531. ret = 0 # return 0 if none done
  532. # Get list of all upgrades player has
  533. # first give them a default
  534. got_nester_lvl_1_upgrade = 0
  535. got_worker_lvl_1_upgrade = 0
  536. got_nester_lvl_2_upgrade = 0
  537. got_worker_lvl_2_upgrade = 0
  538. got_soldier_lvl_1_upgrade = 0
  539. got_soldier_lvl_2_upgrade = 0
  540. playersupgrades = self.upgradeservice.playerUpgrades()
  541. # with open("./playerUpgrades.txt", "w") as f:
  542. # f.write(str(playersupgrades))
  543. # print "upgradeservice.playerUpgrades()"
  544. # availableupgrades = self.upgradeservice.availableUpgrades()
  545. # with open("./availableUpgrades.txt", "w") as g:
  546. # g.write(str(availableupgrades))
  547. # print "upgradeservice.availableUpgrades()"
  548. for x in playersupgrades:
  549. if x['m_id'] == 1: # Level 1 worker upgrade
  550. got_worker_lvl_1_upgrade = 1
  551. if x['m_id'] == 2: # Level 2 worker upgrade
  552. got_worker_lvl_2_upgrade = 1
  553. if x['m_id'] == 6: # Level 1 nester upgrade
  554. got_nester_lvl_1_upgrade = 1
  555. if x['m_id'] == 7: # Level 2 nester upgrade
  556. got_nester_lvl_2_upgrade = 1
  557. if x['m_id'] == 11: # Level 1 soldier upgrade
  558. got_soldier_lvl_1_upgrade = 1
  559. if x['m_id'] == 12: # Level 1 soldier upgrade
  560. got_soldier_lvl_2_upgrade = 1
  561. # dont even waste my cpu if no upgrades available for this level (11+
  562. # for upgrades)
  563. if playerlevel >= 11:
  564. # print "playerlevel > 10"
  565. for x in self.itemcollection:
  566. # level 1 workers
  567. if got_worker_lvl_1_upgrade == 0: # only if we havent got all
  568. if (x['sceneItemType'] == "Character"):
  569. # if (x['unlockLevel']) == 1:
  570. if x['profession'] == "worker":
  571. if self.pollencount > 25:
  572. if self.grasscount > 25:
  573. if self.watercount > 50:
  574. upgradeid = x['m_id']
  575. print "Upgraded workers from lvl1 to lvl2 [upgradeID#" + str(upgradeid) + "]"
  576. # print pollencount
  577. self.upgradeservice.buyUpgrade(
  578. "1", str(upgradeid))
  579. return 1
  580. # level 1 nesters
  581. if got_nester_lvl_1_upgrade == 0: # only if we havent got all
  582. if (x['sceneItemType'] == "Character"):
  583. # if (x['unlockLevel']) == 1:
  584. if x['profession'] == "nester":
  585. if self.pollencount > 25:
  586. if self.grasscount > 25:
  587. if self.watercount > 50:
  588. upgradeid = x['m_id']
  589. print "Upgraded nesters from lvl1 to lvl2 [upgradeID#" + str(upgradeid) + "]"
  590. # print pollencount
  591. self.upgradeservice.buyUpgrade(
  592. "6", str(upgradeid))
  593. return 1
  594. # level 1 soldiers - this only executes if the player has a soldier on
  595. # stage but if so its a nice little +200 exp tweak
  596. if got_soldier_lvl_1_upgrade == 0: # only if we havent got all
  597. if (x['sceneItemType'] == "Character"):
  598. # if (x['unlockLevel']) == 1:
  599. if x['profession'] == "soldier":
  600. if self.pollencount > 25:
  601. if self.grasscount > 25:
  602. if self.watercount > 50:
  603. upgradeid = x['m_id']
  604. print "Upgraded soldiers from lvl1 to lvl2 [upgradeID#" + str(upgradeid) + "]"
  605. self.upgradeservice.buyUpgrade(
  606. "11", str(upgradeid))
  607. return 1
  608. if playerlevel >= 13:
  609. # print "playerlevel > 12"
  610. if (x['sceneItemType'] == 'Structure'):
  611. if (x['type'] == 'House'):
  612. upgradeid = x['m_id']
  613. if (x['itemLevel'] == 1):
  614. if (self.mudcount > 600):
  615. if (self.stonecount > 1300):
  616. if (self.woodcount > 950):
  617. print "[house#" + str(upgradeid) + "] upgraded to lvl2"
  618. self.upgradeservice.buyUpgrade(
  619. "16", str(upgradeid))
  620. return 1
  621. if playerlevel >= 17:
  622. # print "playerlevel > 16"
  623. if (x['sceneItemType'] == 'Structure'):
  624. if (x['type'] == 'Nest'):
  625. upgradeid = x['m_id']
  626. if (x['itemLevel'] == 1):
  627. if (self.mudcount > 850):
  628. if (self.stonecount > 1050):
  629. if (self.woodcount > 800):
  630. print "[nest#" + str(upgradeid) + "] upgraded to lvl2"
  631. self.upgradeservice.buyUpgrade(
  632. "22", str(upgradeid))
  633. return 1
  634. if playerlevel >= 21:
  635. # print "playerlevel > 20"
  636. if (x['sceneItemType'] == 'Structure'):
  637. if (x['type'] == 'Storage'):
  638. upgradeid = x['m_id']
  639. if (x['itemLevel'] == 1):
  640. if (self.mudcount > 1250):
  641. if (self.stonecount > 1800):
  642. if (self.woodcount > 3400):
  643. print "[storage#" + str(upgradeid) + "] upgraded to lvl2"
  644. self.upgradeservice.buyUpgrade(
  645. "19", str(upgradeid))
  646. return 1
  647. if playerlevel >= 25:
  648. # print "playerlevel > 24"
  649. if (x['sceneItemType'] == 'Structure'):
  650. if (x['type'] == 'Hospital'):
  651. upgradeid = x['m_id']
  652. if (x['itemLevel'] == 1):
  653. if (self.mudcount > 2100):
  654. if (self.stonecount > 4800):
  655. if (self.woodcount > 3900):
  656. print "[hospital#" + str(upgradeid) + "] upgraded to lvl2"
  657. self.upgradeservice.buyUpgrade(
  658. "28", str(upgradeid))
  659. return 1
  660. if playerlevel >= 31:
  661. # print "playerlevel > 30"
  662. # level 2 workers
  663. # only if we havent got all
  664. if got_worker_lvl_2_upgrade == 0:
  665. if (x['sceneItemType'] == "Character"):
  666. # if (x['unlockLevel']) == 1:
  667. if x['profession'] == "worker":
  668. if self.pollencount > 30:
  669. if self.grasscount > 30:
  670. if self.watercount > 60:
  671. upgradeid = x['m_id']
  672. print "Upgraded workers from lvl2 to lvl3 [upgradeID#" + str(upgradeid) + "]"
  673. self.upgradeservice.buyUpgrade(
  674. "2", str(upgradeid))
  675. return 1
  676. # level 2 nesters
  677. # only if we havent got all
  678. if got_nester_lvl_2_upgrade == 0:
  679. if (x['sceneItemType'] == "Character"):
  680. # if (x['unlockLevel']) == 1:
  681. if x['profession'] == "nester":
  682. if self.pollencount > 30:
  683. if self.grasscount > 30:
  684. if self.watercount > 60:
  685. upgradeid = x['m_id']
  686. print "Upgraded nesters from lvl2 to lvl3 [upgradeID#" + str(upgradeid) + "]"
  687. self.upgradeservice.buyUpgrade(
  688. "7", str(upgradeid))
  689. return 1
  690. # level 2 soldiers
  691. # only if we havent got all
  692. if got_soldier_lvl_2_upgrade == 0:
  693. if (x['sceneItemType'] == "Character"):
  694. # if (x['unlockLevel']) == 1:
  695. if x['profession'] == "soldier":
  696. if self.pollencount > 30:
  697. if self.grasscount > 30:
  698. if self.watercount > 60:
  699. upgradeid = x['m_id']
  700. print "Upgraded soldiers from lvl2 to lvl3 [upgradeID#" + str(upgradeid) + "]"
  701. self.upgradeservice.buyUpgrade(
  702. "12", str(upgradeid))
  703. return 1
  704. if playerlevel >= 36:
  705. # print "playerlevel > 35"
  706. if (x['sceneItemType'] == 'Structure'):
  707. if (x['type'] == 'House'):
  708. upgradeid = x['m_id']
  709. if (x['itemLevel'] == 2):
  710. if (self.mudcount > 850):
  711. if (self.stonecount > 2200):
  712. if (self.woodcount > 1150):
  713. print "[house#" + str(upgradeid) + "] upgraded to lvl3"
  714. self.upgradeservice.buyUpgrade(
  715. "17", str(upgradeid))
  716. return 1
  717. if playerlevel >= 40:
  718. # print "playerlevel > 39"
  719. if (x['sceneItemType'] == 'Structure'):
  720. if (x['type'] == 'Nest'):
  721. upgradeid = x['m_id']
  722. if (x['itemLevel'] == 2):
  723. if (self.mudcount > 950):
  724. if (self.stonecount > 1300):
  725. if (self.woodcount > 900):
  726. print "[nest#" + str(upgradeid) + "] upgraded to lvl3"
  727. self.upgradeservice.buyUpgrade(
  728. "23", str(upgradeid))
  729. return 1
  730. if playerlevel >= 44:
  731. # print "playerlevel > 43"
  732. if (x['sceneItemType'] == 'Structure'):
  733. if (x['type'] == 'Storage'):
  734. upgradeid = x['m_id']
  735. if (x['itemLevel'] == 2):
  736. if (self.mudcount > 1300):
  737. if (self.stonecount > 1850):
  738. if (self.woodcount > 3800):
  739. print "[storage#" + str(upgradeid) + "] upgraded to lvl3"
  740. self.upgradeservice.buyUpgrade(
  741. "20", str(upgradeid))
  742. return 1
  743. if playerlevel >= 48:
  744. # print "playerlevel > 43"
  745. if (x['sceneItemType'] == 'Structure'):
  746. if (x['type'] == 'Hospital'):
  747. upgradeid = x['m_id']
  748. if (x['itemLevel'] == 2):
  749. if (self.mudcount > 2100):
  750. if (self.stonecount > 5250):
  751. if (self.woodcount > 4400):
  752. print "[hospital#" + str(upgradeid) + "] upgraded to lvl3"
  753. self.upgradeservice.buyUpgrade(
  754. "29", str(upgradeid))
  755. return 1
  756. # Checked
  757. def findnonhealthybuildings(self):
  758. doneone = 0
  759. pets = self.countpets()
  760. print "Checking for unhealthy buildings"
  761. for x in self.itemcollection:
  762. if (x['sceneItemType'] == "Structure"):
  763. # Buildings have a heath and a decay level
  764. # Only use decay levels in this version, it's better this way
  765. if (x['decayLevel'] > 1):
  766. buildid = x['m_id']
  767. if (pets[3] > 0):
  768. builderid = self.findnonbusysoldier()
  769. else:
  770. builderid = self.findnonbusyworker()
  771. if not int(builderid) == -1:
  772. badx = self.getbuildingx(buildid)
  773. bady = self.getbuildingy(buildid)
  774. emptytile = self.findemptytilearound(badx, bady)
  775. if emptytile: # tile found
  776. doneone = 1
  777. self.scene_service.moveItem(
  778. str(builderid), str(badx), str(bady))
  779. print "[P3#" + str(builderid) + "] now fixing unhealthy building at [" + str(badx) + "," + str(bady) + "]"
  780. return doneone
  781. return doneone
  782. # Checked - Set about 5k for gpw
  783. def getlowestresource(self):
  784. print "Finding lowest resource"
  785. lowestres = "none"
  786. lowestrescount = 0
  787. lowestrescount = self.woodcount
  788. lowestres = "wood"
  789. if (self.mudcount < lowestrescount):
  790. lowestrescount = self.mudcount
  791. lowestres = "mud"
  792. if (self.stonecount < lowestrescount):
  793. lowestrescount = self.stonecount
  794. lowestres = "stone"
  795. # if (self.grasscount < lowestrescount):
  796. # lowestrescount = self.grasscount
  797. # lowestres = "grass"
  798. # if (self.pollencount < lowestrescount):
  799. # lowestrescount = self.pollencount
  800. # lowestres = "pollen"
  801. # if (self.watercount < lowestrescount):
  802. # lowestrescount = self.watercount
  803. # lowestres = "water"
  804. return lowestres
  805. # Checked
  806. def checkhouses(self):
  807. for y in self.itemcollection:
  808. if (y['sceneItemType'] == "Structure"):
  809. if (y['type'] == "House"):
  810. for x in self.itemcollection:
  811. if (x['sceneItemType'] == "Character"):
  812. if((x['tenantOf']) == y['m_id']):
  813. maxhunger = x['maxAttributes']['hunger']
  814. maxrest = x['maxAttributes']['rest']
  815. if (int(x['hunger']) == int(maxhunger)) and (int(x['rest']) == int(maxrest)):
  816. print "Moving fully healthy [" + str(x['profession']) + "#" + str(x['m_id']) + "] out of [house#" + str(y['m_id']) + "]"
  817. self.scene_service.moveItem(
  818. str(x['m_id']), str(6), str(6))
  819. # Checked
  820. def checkhospitals(self):
  821. for y in self.itemcollection:
  822. if (y['sceneItemType'] == "Structure"):
  823. if (y['type'] == "Hospital"):
  824. for x in self.itemcollection:
  825. if (x['sceneItemType'] == "Character"):
  826. if((x['tenantOf']) == y['m_id']):
  827. maxhunger = x['maxAttributes']['hunger']
  828. maxrest = x['maxAttributes']['rest']
  829. if (int(x['hunger']) == int(maxhunger)) and (int(x['rest']) == int(maxrest)):
  830. print "Moving fully healthy [" + str(x['profession']) + "#" + str(x['m_id']) + "] out of [hospital#" + str(y['m_id']) + "]"
  831. self.scene_service.moveItem(
  832. str(x['m_id']), str(6), str(6))
  833. # Checked
  834. def findemptyhouse(self):
  835. for y in self.itemcollection:
  836. if (y['sceneItemType'] == 'Structure'):
  837. if (y['type'] == 'House'):
  838. maxchars = y['characterCapacity']
  839. tencount = self.findtenantcount(y['m_id'])
  840. if (int(tencount) < int(maxchars)):
  841. return y
  842. # Checked
  843. def findemptyhospital(self):
  844. for y in self.itemcollection:
  845. if (y['sceneItemType'] == 'Structure'):
  846. if (y['type'] == 'Hospital'):
  847. maxchars = y['characterCapacity']
  848. tencount = self.findtenantcount(y['m_id'])
  849. if (int(tencount) < int(maxchars)):
  850. return y
  851. # Checked
  852. def findtenantcount(self, searchid):
  853. counter = 0
  854. for x in self.itemcollection:
  855. if (x['sceneItemType'] == "Character"):
  856. if((x['tenantOf']) == searchid):
  857. counter += 1
  858. return counter
  859. # Checked
  860. def checkhungry(self):
  861. hosp = False
  862. print "Checking for tired or hungry P3s"
  863. for x in self.itemcollection:
  864. if (x['sceneItemType'] == "Structure"):
  865. if (x['type'] == "Hospital"):
  866. if (x['isBuilt'] is True):
  867. hosp = True
  868. break
  869. for x in self.itemcollection:
  870. if (x['sceneItemType'] == "Character"):
  871. if (int(x['tenantOf']) == -1):
  872. if (int(x['hunger']) < 50) or (int(x['rest']) < 50):
  873. if (int(x['m_id']) > 0):
  874. if hosp:
  875. currhosp = self.findemptyhospital()
  876. if not (currhosp is None):
  877. result = self.scene_service.makeTenant(
  878. (str(x['m_id'])), str(currhosp['m_id']))
  879. if (result is True):
  880. print "Placed [" + str(x['profession']) + "#" + str(x['m_id']) + "] in [hospital#" + str(currhosp['m_id']) + "]"
  881. break
  882. else:
  883. currhouse = self.findemptyhouse()
  884. if not (currhouse == None):
  885. result = self.scene_service.makeTenant(
  886. (str(x['m_id'])), str(currhouse['m_id']))
  887. if (result is True):
  888. print "Placed [" + str(x['profession']) + "#" + str(x['m_id']) + "] in [house#" + str(currhouse['m_id']) + "]"
  889. break
  890. # Checked
  891. def setworkercollect(self, cid, lowestres):
  892. if (self.themapid == '0'):
  893. if (lowestres == "wood"):
  894. print "[worker#" + str(cid) + "] is now collecting wood"
  895. self.scene_service.moveItem(str(cid), "2", "8")
  896. elif (lowestres == "stone"):
  897. print "[worker#" + str(cid) + "] is now collecting stone"
  898. self.scene_service.moveItem(str(cid), "0", "4")
  899. elif (lowestres == "mud"):
  900. print "[worker#" + str(cid) + "] is now collecting mud"
  901. self.scene_service.moveItem(str(cid), "9", "3")
  902. elif (lowestres == "grass"):
  903. print "[worker#" + str(cid) + "] is now collecting grass"
  904. self.scene_service.moveItem(str(cid), "4", "1")
  905. elif (lowestres == "pollen"):
  906. print "[worker#" + str(cid) + "] is now collecting pollen"
  907. self.scene_service.moveItem(str(cid), "2", "1")
  908. elif (lowestres == "water"):
  909. print "[worker#" + str(cid) + "] is now collecting water"
  910. self.scene_service.moveItem(str(cid), "3", "3")
  911. if (self.themapid == '1'):
  912. if (lowestres == "wood"):
  913. print "[worker#" + str(cid) + "] is now collecting wood"
  914. self.scene_service.moveItem(str(cid), "1", "8")
  915. elif (lowestres == "stone"):
  916. print "[worker#" + str(cid) + "] is now collecting stone"
  917. self.scene_service.moveItem(str(cid), "4", "9")
  918. elif (lowestres == "mud"):
  919. print "[worker#" + str(cid) + "] is now collecting mud"
  920. self.scene_service.moveItem(str(cid), "8", "9")
  921. elif (lowestres == "grass"):
  922. print "[worker#" + str(cid) + "] is now collecting grass"
  923. self.scene_service.moveItem(str(cid), "1", "9")
  924. elif (lowestres == "pollen"):
  925. print "[worker#" + str(cid) + "] is now collecting pollen"
  926. self.scene_service.moveItem(str(cid), "8", "6")
  927. elif (lowestres == "water"):
  928. print "[worker#" + str(cid) + "] is now collecting water"
  929. self.scene_service.moveItem(str(cid), "0", "3")
  930. if (self.themapid == '2'):
  931. if (lowestres == "wood"):
  932. print "[worker#" + str(cid) + "] is now collecting wood"
  933. self.scene_service.moveItem(str(cid), "5", "9")
  934. elif (lowestres == "stone"):
  935. print "[worker#" + str(cid) + "] is now collecting stone"
  936. self.scene_service.moveItem(str(cid), "0", "5")
  937. elif (lowestres == "mud"):
  938. print "[worker#" + str(cid) + "] is now collecting mud"
  939. self.scene_service.moveItem(str(cid), "9", "3")
  940. elif (lowestres == "grass"):
  941. print "[worker#" + str(cid) + "] is now collecting grass"
  942. self.scene_service.moveItem(str(cid), "3", "7")
  943. elif (lowestres == "pollen"):
  944. print "[worker#" + str(cid) + "] is now collecting pollen"
  945. self.scene_service.moveItem(str(cid), "1", "6")
  946. elif (lowestres == "water"):
  947. print "[worker#" + str(cid) + "] is now collecting water"
  948. self.scene_service.moveItem(str(cid), "3", "4")
  949. # Checked
  950. def itemexistat(self, thex, they):
  951. theret = False
  952. for x in self.itemcollection:
  953. if (x['x'] == str(thex)):
  954. if (x['y'] == str(they)):
  955. theret = True
  956. return theret
  957. # Checked
  958. def findemptytilearound(self, thex, they):
  959. # Find a empty tile next to a certain tile, used for adjacent placement
  960. # Checks tile to left, to right, above and below could be improved with
  961. # diagonal but not really needed
  962. foundone = []
  963. for x in self.tilecollection:
  964. searchx = int(thex) - 1
  965. searchy = int(they)
  966. if (x['x'] == int(searchx)):
  967. if (x['y'] == int(searchy)):
  968. if (x['walkable'] is True):
  969. if self.itemexistat(searchx, searchy) is False:
  970. foundone = [searchx, searchy]
  971. return foundone
  972. searchx = int(thex) + 1
  973. searchy = int(they)
  974. if (x['x'] == int(searchx)):
  975. if (x['y'] == int(searchy)):
  976. if (x['walkable'] is True):
  977. if self.itemexistat(searchx, searchy) is False:
  978. foundone = [searchx, searchy]
  979. return foundone
  980. searchx = int(thex)
  981. searchy = int(they) - 1
  982. if (x['x'] == int(searchx)):
  983. if (x['y'] == int(searchy)):
  984. if (x['walkable'] is True):
  985. if self.itemexistat(searchx, searchy) is False:
  986. foundone = [searchx, searchy]
  987. return foundone
  988. searchx = int(thex)
  989. searchy = int(they) + 1
  990. if (x['x'] == int(searchx)):
  991. if (x['y'] == int(searchy)):
  992. if (x['walkable'] is True):
  993. if self.itemexistat(searchx, searchy) is False:
  994. foundone = [searchx, searchy]
  995. return foundone
  996. return foundone
  997. # Checked - Combine with below
  998. def getbuildingx(self, buildingid):
  999. # Get building X coord (should be updated to single function for both
  1000. # coords)
  1001. for x in self.itemcollection:
  1002. if (x['sceneItemType'] == "Structure"):
  1003. if (x['m_id'] == buildingid):
  1004. return (x['x'])
  1005. # Checked - Combine with above
  1006. def getbuildingy(self, buildingid):
  1007. # Get building Y coord (should be updated to single function for both
  1008. # coords)
  1009. for x in self.itemcollection:
  1010. if (x['sceneItemType'] == "Structure"):
  1011. if (x['m_id'] == buildingid):
  1012. return (x['y'])
  1013. # Checked - returns an array [#char, #work, #nest, #sold]
  1014. def countpets(self):
  1015. counter = [0, 0, 0, 0]
  1016. for x in self.itemcollection:
  1017. if (x['sceneItemType'] == "Character"):
  1018. counter[0] += 1
  1019. if (str(x['profession']) == "worker"):
  1020. counter[1] += 1
  1021. elif (str(x['profession']) == "nester"):
  1022. counter[2] += 1
  1023. elif (str(x['profession']) == "soldier"):
  1024. counter[3] += 1
  1025. return counter
  1026. # Checked - Rewritten for minimal CPU usage
  1027. def checkeggs(self):
  1028. self.itembag = self.Inventory_Service.itemBag()
  1029. # self.itemcollection = self.scene_service.sceneItems()
  1030. # print "entered checkeggs()"
  1031. # with open("./itembag.txt", "w") as f:
  1032. # f.write(str(self.itembag))
  1033. # with open("./itemcollection.txt", "w") as g:
  1034. # g.write(str(self.itemcollection))
  1035. # print "done writing"
  1036. eggcount = self.countinveggs()
  1037. print "[tEggs,wEggs,nEggs,sEggs] " + str(eggcount)
  1038. pets = self.countpets()
  1039. print "[tP3s, tWork,tNest,tSold] " + str(pets)
  1040. maxpetcount = self.amf_playerinfo['maxPopulation']
  1041. # Discard extra stored eggs from bag
  1042. if (eggcount[3] > 0):
  1043. # Store in soldierID to show id in printmsg
  1044. self.scene_service.moveItem(
  1045. str(self.findinveggprof("Soldier")), "6", "6")
  1046. print "Discarded Soldier Egg from inventory. Goddamn free eggs."
  1047. if (eggcount[1] > 43):
  1048. self.scene_service.moveItem(
  1049. str(self.findinveggprof("Worker")), "6", "6")
  1050. print "Discarded Worker Egg from inventory [MaxStoredWorkers]"
  1051. if (eggcount[2] > 3):
  1052. self.scene_service.moveItem(
  1053. str(self.findinveggprof("Nester")), "6", "6")
  1054. print "Discarded Nester Egg from inventory [MaxStoredNesters]"
  1055. # Check eggs on stage
  1056. for x in self.itemcollection:
  1057. if (x['sceneItemType'] == "Egg") and (not (x['species'] == None)):
  1058. if (x['profession'] == "soldier"):
  1059. self.scene_service.discardEgg(str(x['m_id']))
  1060. print "[egg#" + str(x['m_id']) + "] discarded [soldier]"
  1061. if (x['profession'] == "nester"):
  1062. if (pets[2] < 3) and (pets[0] < maxpetcount):
  1063. self.scene_service.hatchEgg(str(x['m_id']))
  1064. print "[egg#" + str(x['m_id']) + "] hatched [" + str(x['profession']) + "]"
  1065. return 0
  1066. elif (eggcount[2] >= 3):
  1067. self.scene_service.discardEgg(str(x['m_id']))
  1068. print "[egg#" + str(x['m_id']) + "] discarded [maxNesterEgg]"
  1069. return 0
  1070. else:
  1071. self.scene_service.harvestEgg(str(x['m_id']))
  1072. print "Harvested [egg#" + str(x['m_id']) + "] [nester]"
  1073. return 0
  1074. if (x['profession'] == "worker"):
  1075. if (pets[2] >= 3) and (pets[0] < maxpetcount):
  1076. self.scene_service.hatchEgg(str(x['m_id']))
  1077. print "[egg#" + str(x['m_id']) + "] hatched [" + str(x['profession']) + "]"
  1078. return 0
  1079. elif (eggcount[1] >= 43):
  1080. self.scene_service.discardEgg(str(x['m_id']))
  1081. print "[egg#" + str(x['m_id']) + "] discarded [maxWorkerEgg]"
  1082. return 0
  1083. else:
  1084. self.scene_service.harvestEgg(str(x['m_id']))
  1085. print "Harvested [egg#" + str(x['m_id']) + "] [worker]"
  1086. return 0
  1087. # Get egg from bag
  1088. if (pets[0] < maxpetcount):
  1089. if (eggcount[2] > 0):
  1090. if (pets[2] < 3):
  1091. print "Moving nester egg from bag to hatch on next tick"
  1092. self.scene_service.moveItem(
  1093. str(self.findinveggprof("Nester")), "6", "6")
  1094. if (eggcount[1] > 0):
  1095. if (pets[2] >= 3):
  1096. print "Moving worker egg from bag to hatch on next tick"
  1097. self.scene_service.moveItem(
  1098. str(self.findinveggprof("Worker")), "6", "6")
  1099. # Checked
  1100. def checknesters(self):
  1101. for x in self.itemcollection:
  1102. if (x['sceneItemType'] == "Character"):
  1103. if (str(x['profession']) == "nester"):
  1104. if (int(x['tenantOf'] == -1)):
  1105. if (int(x['hunger']) > 50) and (int(x['rest']) > 50):
  1106. nestid = self.findemptynest()
  1107. if not (nestid == -1):
  1108. result = self.scene_service.makeTenant(
  1109. (str(x['m_id'])), str(nestid))
  1110. print "Placed inactive [nester#" + str(x['m_id']) + "] in [nest#" + str(nestid) + "]"
  1111. break
  1112. # Checked - Add check for decay level in addition to health
  1113. def findemptynest(self):
  1114. ret = -1
  1115. for x in self.itemcollection:
  1116. if (x['sceneItemType'] == 'Structure'):
  1117. if (x['type'] == 'Nest'):
  1118. if (str(x['isBuilt']) == 'True'):
  1119. if (int(x['health']) > 50):
  1120. tencount = self.findtenantcount(x['m_id'])
  1121. if (tencount == 0):
  1122. ret = x['m_id']
  1123. return ret
  1124. # Checked
  1125. def findnoncompletebuilding(self):
  1126. theret = -1
  1127. for x in self.itemcollection:
  1128. if (x['sceneItemType'] == "Structure"):
  1129. if (x['isBuilt'] is False):
  1130. theret = x
  1131. return theret
  1132. break
  1133. return theret
  1134. # Checked
  1135. def urgentBuy(self):
  1136. if (self.urgentbuy == 15):
  1137. if (self.mudcount > 1200):
  1138. if (self.stonecount > 950):
  1139. if (self.woodcount > 1200):
  1140. print "----urgentBuy----"
  1141. print "Buying Urgently needed house"
  1142. self.store_service.buyItem(str(self.urgentbuy))
  1143. print "Bought Urgently needed house"
  1144. self.urgentbuy = 0
  1145. return 1
  1146. if (self.urgentbuy == 20):
  1147. if (self.mudcount > 1550):
  1148. if (self.stonecount > 1450):
  1149. if (self.woodcount > 1850):
  1150. print "----urgentBuy----"
  1151. print "Buying Urgently needed storage"
  1152. self.store_service.buyItem(str(self.urgentbuy))
  1153. print "Bought Urgently needed storage"
  1154. self.urgentbuy = 0
  1155. return 1
  1156. if (self.urgentbuy == 22):
  1157. if (self.mudcount > 950):
  1158. if (self.stonecount > 650):
  1159. if (self.woodcount > 800):
  1160. print "----urgentBuy----"
  1161. print "Buying Urgently needed nest"
  1162. self.store_service.buyItem(str(self.urgentbuy))
  1163. print "Bought Urgently needed nest"
  1164. self.urgentbuy = 0
  1165. return 1
  1166. if (self.urgentbuy == 41):
  1167. if (self.mudcount > 1800):
  1168. if (self.stonecount > 6750):
  1169. if (self.woodcount > 2450):
  1170. print "----urgentBuy----"
  1171. print "Buying Urgently needed hospital"
  1172. self.store_service.buyItem(str(self.urgentbuy))
  1173. print "Bought Urgently needed hospital"
  1174. self.urgentbuy = 0
  1175. return 1
  1176. # Checked
  1177. def findnonbusyworker(self):
  1178. theid = -1
  1179. viableworkers = []
  1180. for x in self.itemcollection:
  1181. if (x['sceneItemType'] == "Character"):
  1182. if (x['profession'] == "worker"):
  1183. if (x['tenantOf'] == -1):
  1184. if (x['attachedTo'] == -1):
  1185. viableworkers.append(x['m_id'])
  1186. random.shuffle(viableworkers)
  1187. theid = viableworkers[0]
  1188. return theid
  1189. # Checked
  1190. def findnonbusysoldier(self):
  1191. theid = -1
  1192. viablesoldiers = []
  1193. for x in self.itemcollection:
  1194. if (x['sceneItemType'] == "Character"):
  1195. if (x['profession'] == "soldier"):
  1196. if (x['tenantOf'] == -1):
  1197. if (x['attachedTo'] == -1):
  1198. viablesoldiers.append(x['m_id'])
  1199. random.shuffle(viablesoldiers)
  1200. theid = viablesoldiers[0]
  1201. return theid