Cloudrip Siege: I need help with my code


#1

Hi everyone can you help me with this code please.

def valueOverDistance(item):
    return item.value / hero.distanceTo(item)

def findBestItem(items):
    bestItem = None
    bestValue = 0
    itemsIndex = 0
    
    while itemsIndex < len(items):
        if valueOverDistance(items[itemsIndex]) > bestValue:
            bestValue = valueOverDistance(items[itemsIndex])
            bestItem = items[itemsIndex]
        
        itemsIndex += 1
    return bestItem
while True:
    coins = hero.findItems()
    coin = None
    coin = findBestItem(coins)
    if coin and hero.isReady("jump"):
        hero.jumpTo(coin)
    elif coin:
        hero.moveXY(coin.pos.x, coin.pos.y)
    hero.buildTypes = ["decoy", "arrow-tower"]
    buildType = buildTypes[len(hero.built) % len(buildTypes)]
    if hero.gold >= hero.costOf(buildType):
        hero.buildXY(buildType, hero.pos.x, hero.pos.y)
    if hero.canCast("summon-undead"):
        hero.cast("summon-undead")
    enemy = hero.findNearestEnemy()
    if hero.canCast("chain-lightning") and enemy and hero.distanceTo(enemy) < hero.canCastRange:
        if enemy:
            hero.cast("chain-lightning", enemy)
    enemy = hero.findNearestEnemy()
    if hero.canCast("fear") and enemy and hero.distanceTo(enemy) < hero.canCastRange:
        if enemy:
            hero.cast("fear", enemy)
    enemy = hero.findNearestEnemy()
    if hero.canCast("poison-cloud") and enemy and hero.distanceTo(enemy) < hero.canCastRange:
        if enemy:
            hero.cast("poison-cloud", enemy)
    if hero.canCast("raise-dead"):
        enemy = hero.findNearestEnemy()
        if enemy and hero.distanceTo(enemy) < 5:
            hero.cast("raise-dead", enemy)
    enemy = hero.findNearestEnemy()
    if hero.canCast("devour") and enemy and hero.distanceTo(enemy) < hero.canCastRange:
        if enemy:
            hero.devour(enemy)

I am getting this error some where here

hero.buildTypes = ["decoy", "arrow-tower"]
    buildType = buildTypes[len(hero.built) % len(buildTypes)]
    if hero.gold >= hero.costOf(buildType):
        hero.buildXY(buildType, hero.pos.x, hero.pos.y)

And I think that error is from this line if hero.gold >= hero.costOf(buildType):I could be wrong. Also this is the hero I’m using
And here is what my hero is using-


#2

I don’t think that you can create/modify/play around with a list called hero.buildTypes, because it’s already reserved by the game. Otherwise, you might be able to ‘create’ a build type (like Ritic or something), and just build it with the hammer (convenient!). Go for hero.buildTypes1 or hero.bTypes or something like that.

I think that’s what the error message means; hope it helps.


#3

Am still getting the same error message.


#4

The error message is still that bTypes (or whatever) is protected property? Could it be referencing some other part of the code where you’re still using buildTypes?


#5

It could be. I think the error is coming from some where here

hero.buildTypes = ["decoy", "arrow-tower"]
    buildType = buildTypes[len(hero.built) % len(buildTypes)]
    if hero.gold >= hero.costOf(buildType):
        hero.buildXY(buildType, hero.pos.x, hero.pos.y)

#6

It highlight this part of the code


#7

I understand, but is the error message identical when you change the name of the list to something else?


#8

Yes(sorry I took so long to reply that message.)


#9

I think you cannot change values of your hero object directly. Try a variable name without hero. or better: without any dot (.) .


#10

It still giving me the same error.:cry:


#11

It could be a bug.(20character)


#12

@Minh Can you post the new, edited code?


#13

Ok

def valueOverDistance(item):
    return item.value / hero.distanceTo(item)

def findBestItem(items):
    bestItem = None
    bestValue = 0
    itemsIndex = 0
    
    while itemsIndex < len(items):
        if valueOverDistance(items[itemsIndex]) > bestValue:
            bestValue = valueOverDistance(items[itemsIndex])
            bestItem = items[itemsIndex]
        
        itemsIndex += 1
    return bestItem
while True:
    coins = hero.findItems()
    coin = None
    coin = findBestItem(coins)
    if coin and hero.isReady("jump"):
        hero.jumpTo(coin)
    elif coin:
        hero.moveXY(coin.pos.x, coin.pos.y)
    hero.buildTypes = ["decoy", "arrow-tower"]
    buildType = buildTypes[len(hero.built) % len(buildTypes)]
    if hero.gold >= buildType:
        hero.buildXY("buildType", hero.pos.x, hero.pos.y)
    if hero.canCast("summon-undead"):
        hero.cast("summon-undead")
    enemy = hero.findNearestEnemy()
    if hero.canCast("chain-lightning") and enemy and hero.distanceTo(enemy) < hero.canCastRange:
        if enemy:
            hero.cast("chain-lightning", enemy)
    enemy = hero.findNearestEnemy()
    if hero.canCast("fear") and enemy and hero.distanceTo(enemy) < hero.canCastRange:
        if enemy:
            hero.cast("fear", enemy)
    enemy = hero.findNearestEnemy()
    if hero.canCast("poison-cloud") and enemy and hero.distanceTo(enemy) < hero.canCastRange:
        if enemy:
            hero.cast("poison-cloud", enemy)
    if hero.canCast("raise-dead"):
        enemy = hero.findNearestEnemy()
        if enemy and hero.distanceTo(enemy) < 5:
            hero.cast("raise-dead", enemy)
    enemy = hero.findNearestEnemy()
    if hero.canCast("devour") and enemy and hero.distanceTo(enemy) < hero.canCastRange:
        if enemy:
            hero.devour(enemy)

The only part of the code that I edited was if hero.gold >= buildType:.


#14

I’ve only just started the mountain island, so I’m not familiar with the buildTypes method, as I’ve never used it. But, could the issue be this section of code?

buildType = buildTypes[len(hero.built) % len(buildTypes)]

would it not need to be something like this?

buildType = hero.buildTypes[len(hero.built) % len(buildTypes)]

or even

buildType = hero.buildTypes[len(hero.built) % len(hero.buildTypes)]

Sorry if this doesn’t help, just spitballing.


#15

Try to change

hero.buildTypes = ["decoy", "arrow-tower"]

to

buildTypes = ["decoy", "arrow-tower"]

Does this remove the error?

Regards


#16

I believe it should be hero.buildTypes. (This is unlocked when you equip a Catrop belt or trap belt)


#17

Thank it remove the error but it not building any things. Even If I have enough gold.


#18

@Minh I’m not up to Cloudrip at all (still in the desert), but looking over your code, I’m a bit stuck on this part:

hero.buildTypes = ["decoy", "arrow-tower"]

buildType = buildTypes[len(hero.built) % len(buildTypes)]
if hero.gold >= buildType:

(some problems with the formatting there...)

I'm assuming that you took @user1's edit, and that's what took out the error message.

This should mean that you have a list with two strings, decoy and arrow-tower. You then make a new variable, `buildType`, which should be the modulo (`%`) of the length of some function called `hero.built` and the length of buildTypes.  What is `hero.built`? I'm assuming it's a built-in function that shows what the hero has built already, but I don't know it. Perhaps this is part of the problem?

Also, the line `buildType = buildTypes[len(hero.built) % len(buildTypes)]` is, essentially supposed to be yielding the value of the object you want to build (I think), since that's what you're comparing to `hero.gold` in the next line (the last one I quoted). I'm not figuring out how it should be doing that, though, perhaps because I'm a total newbie, though perhaps part of the problem is there; if you're code isn't returning the right number there, I would assume the hero won't be building at the right times.

Hope something in here helps.

#19

I don’t really understand this part of the code buildType = buildTypes[len(hero.built) % len(buildTypes)].hero.buildTypes = ["decoy", "arrow-tower"]is ment to be buildTypes = ["decoy", "arrow-tower"]. This part of the code buildTypes = ["decoy", "arrow-tower"] mean you can list what you you want to build and if you have enough gold it will build the first one in the list and if you have enough gold again it will build the second one in the list and so on. (Sorry it take me a long time to reply because I sometime not on CodeCombat Discourse for a long time and tell me if you don’t understand somethings because I can try to be clearer.)

I hope this help you understand a little bit more about my code.:slightly_smiling_face:


#20

Just don’t use hero. anything useless you’re accessing a list or variable because it might bug out the preprogramed stuff. Just use a normal list I don’t see why not.