Accuracy does not work

Discussion of Lua and LuaWML support, development, and ideas.

Moderator: Forum Moderators

Post Reply
B099
Posts: 16
Joined: September 5th, 2024, 1:00 pm

Accuracy does not work

Post by B099 »

This code gets called whenever attack starts (to turn on) and ends (to turn off):

Code: Select all

local function guarantee_hit(unit, guarantee)
    for w =1, #unit.attacks do
        if guarantee == true then
            unit.attacks[w].accuracy = unit.attacks[w].accuracy + 100
            wesnoth.interface.add_chat_message("attack accuracy start" .. unit.attacks[w].accuracy)            
        else
            unit.attacks[w].accuracy = unit.attacks[w].accuracy - 100
            wesnoth.interface.add_chat_message("attack accuracy end " .. unit.attacks[w].accuracy)            
        end
    end
end
The problem: During the attck, unit will still use their normal accuracy, meaning they miss attacks, AND after the attack it says on their weapon "-100%", so they can not hit anything.
The intention is that the code guarantees hits by turning it up by 100, then back to normal after the attack.

It would seem like this line is never run..

Code: Select all

unit.attacks[w].accuracy = unit.attacks[w].accuracy + 100
But when I print the variable after this is run, accuracy is 100.

So what is going on? It seems like the variable is not saved onto the unit if it runs from the attack event.
white_haired_uncle
Posts: 1440
Joined: August 26th, 2018, 11:46 pm
Location: A country place, far outside the Wire

Re: Accuracy does not work

Post by white_haired_uncle »

B099 wrote: September 9th, 2024, 6:47 pm

So what is going on? It seems like the variable is not saved onto the unit if it runs from the attack event.
Yes, it does.

Where are is unit getting set?

After you change unit, are you pushing the changes back to the actual unit (e.g. wesnoth.units.to_map()) ?
Speak softly, and carry Doombringer.
User avatar
Celtic_Minstrel
Developer
Posts: 2332
Joined: August 3rd, 2012, 11:26 pm
Location: Canada
Contact:

Re: Accuracy does not work

Post by Celtic_Minstrel »

white_haired_uncle wrote: September 9th, 2024, 7:44 pm After you change unit, are you pushing the changes back to the actual unit (e.g. wesnoth.units.to_map()) ?
That's only necessary if you serialized the unit (using [store_unit] or __cfg).

Where is this code running? You said "whenever attack starts and ends", but exactly what does that mean? From your description, it sounds like guarantee(u, true) is never called, but guarantee(u, false) is.
Author of The Black Cross of Aleron campaign and Default++ era.
Former maintainer of Steelhive.
B099
Posts: 16
Joined: September 5th, 2024, 1:00 pm

Re: Accuracy does not work

Post by B099 »

I have tried wesnoth.units.to_map. guarantee(u, true) does get run, I can see the variable get printed correctly too.

But suddenly... when guarantee(u, false) is run, it goes from 100 to -100.. which should not be possible.

Here is my code (in order of execution):

Code: Select all

1. on_event("attack", attack_start_event) 

2. local function attack_start_event(ctx)
    local unit1 = wesnoth.get_unit(ctx.x1, ctx.y1)
    local unit2 = wesnoth.get_unit(ctx.x2, ctx.y2)
    [...]
    guarantee_hit(unit1, true)
    guarantee_hit(unit2, true)
end 

3. local function guarantee_hit(unit, guarantee)
    for w =1, #unit.attacks do
        if guarantee == true then
            unit.attacks[w].accuracy = unit.attacks[w].accuracy + 100
            wesnoth.interface.add_chat_message("attack accuracy start" .. unit.attacks[w].accuracy)           
        else
            unit.attacks[w].accuracy = unit.attacks[w].accuracy - 100
            wesnoth.interface.add_chat_message("attack accuracy end " .. unit.attacks[w].accuracy)            
        end
    end
end

4. on_event("attack end", remove_tmp_hits)

5. local function remove_tmp_hits(ctx)
    local unit1 = wesnoth.get_unit(ctx.x1, ctx.y1)
    local unit2 = wesnoth.get_unit(ctx.x2, ctx.y2)
    [...]
    guarantee_hit(unit1, false)
    guarantee_hit(unit2, false)
end

6 (see 3)

WHY DOESNT DOES THIS WORK?!! IS WML EVIL?!!
white_haired_uncle
Posts: 1440
Joined: August 26th, 2018, 11:46 pm
Location: A country place, far outside the Wire

Re: Accuracy does not work

Post by white_haired_uncle »

Just thinking out loud here...
But suddenly... when guarantee(u, false) is run, it goes from 100 to -100.. which should not be possible.
what happens if unit.attacks[w].accuracy is nil here?

Code: Select all

unit.attacks[w].accuracy = unit.attacks[w].accuracy - 100
(or 0 of course, but for some reason I keep looking at that line like it might be equivalent to:)

Code: Select all

unit.attacks[w].accuracy =   - 100
Speak softly, and carry Doombringer.
B099
Posts: 16
Joined: September 5th, 2024, 1:00 pm

Re: Accuracy does not work

Post by B099 »

white_haired_uncle wrote: September 12th, 2024, 4:22 pm Just thinking out loud here...
But suddenly... when guarantee(u, false) is run, it goes from 100 to -100.. which should not be possible.
what happens if unit.attacks[w].accuracy is nil here?

Code: Select all

unit.attacks[w].accuracy = unit.attacks[w].accuracy - 100
(or 0 of course, but for some reason I keep looking at that line like it might be equivalent to:)

Code: Select all

unit.attacks[w].accuracy =   - 100
I have added the following, but it does NO THING!!!

Code: Select all

if unit.attacks[w].accuracy == nil then
    unit.attacks[w].accuracy = 0
end
I can upload the full code, so you can view the TRUE ERRORs if you're prepared enough.
gnombat
Posts: 808
Joined: June 10th, 2010, 8:49 pm

Re: Accuracy does not work

Post by gnombat »

B099 wrote: September 9th, 2024, 6:47 pm The problem: During the attck, unit will still use their normal accuracy, meaning they miss attacks
Are you sure about this? I tried running your code, and it seems like attacks never miss.
white_haired_uncle
Posts: 1440
Joined: August 26th, 2018, 11:46 pm
Location: A country place, far outside the Wire

Re: Accuracy does not work

Post by white_haired_uncle »

To clarify my last comment, you do this AFTER you change the accuracy:

Code: Select all

wesnoth.interface.add_chat_message("attack accuracy start" .. unit.attacks[w].accuracy) 
Why not check to make sure accuracy is what you think it should be BEFORE you make a change as well? From a couple of your comments, I'm not sure it is (it sounds like it's always initially 0 or nil, at least for you).
Speak softly, and carry Doombringer.
B099
Posts: 16
Joined: September 5th, 2024, 1:00 pm

Re: Accuracy does not work

Post by B099 »

white_haired_uncle wrote: September 13th, 2024, 6:32 pm To clarify my last comment, you do this AFTER you change the accuracy:

Code: Select all

wesnoth.interface.add_chat_message("attack accuracy start" .. unit.attacks[w].accuracy) 
Why not check to make sure accuracy is what you think it should be BEFORE you make a change as well? From a couple of your comments, I'm not sure it is (it sounds like it's always initially 0 or nil, at least for you).
Hi, I have done so.
In the first phase, the value is 0 before being run. It is 100 after being run.
In the second phase, it is 0 after being run. It is -100 after being on run.

That is, is that accuracy is not being saved. It seems like unit is suddenly a local variable? Is there a method to use pointers in Lua?
white_haired_uncle
Posts: 1440
Joined: August 26th, 2018, 11:46 pm
Location: A country place, far outside the Wire

Re: Accuracy does not work

Post by white_haired_uncle »

I'm not very good with lua, and worse with the unit type, but...

Assuming that's pass by value, "unit" is local to guarantee_hit, is it not?

I think I would take the contents of guarantee_hit() and move it directly into attack_start_event/remove_tmp_hits temporarily. This may be completely wrong and a waste of time for someone who knows lua, but it's what I would do in trying to isolate the issue. If that works, maybe you need to wesnoth.get_unit(unit) in guarantee_hit????
Speak softly, and carry Doombringer.
User avatar
Celtic_Minstrel
Developer
Posts: 2332
Joined: August 3rd, 2012, 11:26 pm
Location: Canada
Contact:

Re: Accuracy does not work

Post by Celtic_Minstrel »

The unit is a (non-light) userdata, so it's passed by reference.
Author of The Black Cross of Aleron campaign and Default++ era.
Former maintainer of Steelhive.
B099
Posts: 16
Joined: September 5th, 2024, 1:00 pm

Re: Accuracy does not work

Post by B099 »

Omg, this is crazy. Why does it not work? Is accuracy function broken? I remember it worked well the first time I used it
I have done ALL OPTIONS
Soliton
Site Administrator
Posts: 1711
Joined: April 5th, 2005, 3:25 pm
Location: #wesnoth-mp

Re: Accuracy does not work

Post by Soliton »

Post a complete test case that shows the issue and then someone can probably explain.
"If gameplay requires it, they can be made to live on Venus." -- scott
B099
Posts: 16
Joined: September 5th, 2024, 1:00 pm

Re: Accuracy does not work

Post by B099 »

It turns out that the problem was a part of another code I made which nullified damage, making it look like it missed.


Everybody, thanks to your advice and courageous encouragment, I now bear witness to the world to the add-on to define an era:

viewtopic.php?t=58727
Post Reply