Latest Posts

Topic: Improving the AI

Tibor

Joined: 2009-03-23, 23:24
Posts: 1377
Ranking
One Elder of Players
Location: Slovakia
Posted at: 2019-12-04, 08:05

Is this OK?:

for (auto cw : site.bo->positions) {
    const Worker* cw = site.site->working_positions()[0].worker
        if (cw) {

The second line is always referring to first position.... What about this?

for (auto cwp : site.bo->positions) {
     if (cwp.worker) {

Top Quote
hessenfarmer
Avatar
Joined: 2014-12-11, 23:16
Posts: 2646
Ranking
One Elder of Players
Location: Bavaria
Posted at: 2019-12-04, 10:07

Tibor wrote:

Is this OK?:

it is partly ok. the for loop is iterating over the worker indexes in the building observer of this building type. this is the worker defined for the position in lua. the second line is evaluating the actual worker of the site working in this position. As this can be empty we need the if (cw) part.

for (auto cw : site.bo->positions) {
    const Worker* cw = site.site->working_positions()[0].worker
        if (cw) {

The second line is always referring to first position.... What about this?

Yes I need to replace the [0] with a counter variable. However I need to have a way to iterate over site.bo->positions as well.

for (auto cwp : site.bo->positions) {
     if (cwp.worker) {

this won't work as bo.positions does not have .worker I believe furthermore we need to have the actual worker here rather then the standard worker.

after a short nightsleep I got the idea how to do it. I'll iterate ofer the actual positions of the site now as we can skip if a position is not manned should be more efficient. if position is manned i will compare with bo.positions.at(i) with i being the iterator variable. this should do it. If the code is working properly I'll copy it to the check_mines function as well.

finally it should look like this

    for (uint8 i = 0, i <  site.site->working_positions().size, i++) {  // iterate over all working positions of the actual productionsite
        const Worker* cw = site.site->working_positions()[i].worker   // get the pointer to the worker assigned to the actual position
            if (cw) {  // a worker is assigned to the position
                DescriptionIndex current_worker = cw->descr().worker_index();   // get the descritpion index of the worker assigned on this position
                                // if description indexes of assigned worker and normal worker differ (an experienced worker is assigned to the position)
                                // and we have none of the experienced workers on stock 
                if (current_worker != site.bo->positions.at(i) && calculate_stocklevel(current_worker, WareWorker::kWorker) < 1) {  
                    game().send_player_evict_worker(*site.site->working_positions()[i].worker);  // kick out the worker
                    return true;
                }
            }
    }   

but I can't test this until tonight. Thanks for all the support

Edited: 2019-12-04, 10:08

Top Quote
Tibor

Joined: 2009-03-23, 23:24
Posts: 1377
Ranking
One Elder of Players
Location: Slovakia
Posted at: 2019-12-04, 10:28

The final code looks good to me, but back to this:

for (auto cw : site.bo->positions) {
    const Worker* cw = site.site->working_positions()[0].worker

I think you first get 'cw' and immediately overwrite it in second line


Top Quote
hessenfarmer
Avatar
Joined: 2014-12-11, 23:16
Posts: 2646
Ranking
One Elder of Players
Location: Bavaria
Posted at: 2019-12-04, 10:43

Tibor wrote:

The final code looks good to me, but back to this:

for (auto cw : site.bo->positions) {
    const Worker* cw = site.site->working_positions()[0].worker

I think you first get 'cw' and immediately overwrite it in second line

I don't know where you found this line. I always had for( auto worker : ... ) in my code. If it were like the above you would have been right of course.


Top Quote
Tibor

Joined: 2009-03-23, 23:24
Posts: 1377
Ranking
One Elder of Players
Location: Slovakia
Posted at: 2019-12-04, 10:49

hessenfarmer wrote:

Tibor wrote:

The final code looks good to me, but back to this:

for (auto cw : site.bo->positions) {
    const Worker* cw = site.site->working_positions()[0].worker

I think you first get 'cw' and immediately overwrite it in second line

I don't know where you found this line. I always had for( auto worker : ... ) in my code. If it were like the above you would have been right of course.

Oh, sorry, I messed something badly, probably during copying/retyping face-sad.png


Top Quote
hessenfarmer
Avatar
Joined: 2014-12-11, 23:16
Posts: 2646
Ranking
One Elder of Players
Location: Bavaria
Posted at: 2019-12-04, 10:53

No worries. will test this tonight on my atlanteans branch and if it works there I'll pull out a new branch for this, toghether with the crude statistics fix and the malus for unoccupied sites. How can I add you to the review? Are you enlisted in the git developers already?


Top Quote
Tibor

Joined: 2009-03-23, 23:24
Posts: 1377
Ranking
One Elder of Players
Location: Slovakia
Posted at: 2019-12-04, 10:57

I have an account on github, so you should be able to find me there.


Top Quote
hessenfarmer
Avatar
Joined: 2014-12-11, 23:16
Posts: 2646
Ranking
One Elder of Players
Location: Bavaria
Posted at: 2019-12-04, 11:07

Tibor wrote:

I have an account on github, so you should be able to find me there.

glad to hear that. So I will set you as the first reviewer. Await some work face-wink.png


Top Quote
hessenfarmer
Avatar
Joined: 2014-12-11, 23:16
Posts: 2646
Ranking
One Elder of Players
Location: Bavaria
Posted at: 2019-12-05, 12:43

ok pull request with AI changes is up


Top Quote
hessenfarmer
Avatar
Joined: 2014-12-11, 23:16
Posts: 2646
Ranking
One Elder of Players
Location: Bavaria
Posted at: 2019-12-05, 14:58

Damn, travis complains about code format but I don't know why as I didn't change the format.


Top Quote