Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Queue updates / refactor #262

Merged
merged 10 commits into from
Sep 16, 2024
Merged

Queue updates / refactor #262

merged 10 commits into from
Sep 16, 2024

Conversation

sean-gilliam
Copy link
Collaborator

@sean-gilliam sean-gilliam commented Aug 27, 2024

DO NOT MERGE!!!

Seriously do not merge.

I'm wanting to get some feedback on some queue refactoring that I'm doing. It compiles, but that's all I can guarantee atm. It'll probably segfault during runtime. I need to write a slew of tests and actual run it to verify.

Functions to take a look at in queue.h. These names will eventually be changed to their old named counterparts. Just for testing / compiling purposes :)

  • AddToNewQueue
  • ProcessNewQueue
  • HasNewQueuePending
  • DeleteNewQueuedEventsInvolving
  • GetCharacterData

All the old functions are still there to take a look and compare. Most of the new function parameters are unchanged except for adding to queue:

old way

RS.Queue.AddToNewQueue(speech->current_line->delay, 3, speech_handler, ch, mob, speech);

It took a timer, the number of arguments to the function, the function, and the arguments. It was limited on the number of function arguments that can be passed to it (max of 8).

new way

RS.Queue.AddToNewQueue(speech->current_line->delay, speech_handler, ch, mob, speech);

It takes a timer, the function, and the arguments. It is unlimited on the number of function arguments that can be passed to it.

Anyways, I'm looking for comments/suggestions. Do those methods look like an improvement over the old or not?

code/queue.h Outdated Show resolved Hide resolved
@github-actions github-actions bot added the tests label Aug 28, 2024
@sean-gilliam
Copy link
Collaborator Author

All tests are running green for my newly added methods.

Next step is to remove the old methods and then rename the new methods to the old methods.

@sean-gilliam
Copy link
Collaborator Author

The old C code was pretty fast and loose with how it handled parameters. (So many void pointers. So many.)

This new C++ code needs real types in order to do it's dispatch. Therefore, I'm making wrappers for methods that are placed on the queue. These wrappers basically take in the C++ parameters and calls the old method the C way.

Ex:

void act(const char *format, CHAR_DATA *ch, const void *arg1, const void *arg2, int type)
{
	act_new(format, ch, arg1, arg2, type, POS_RESTING);
}

void act_queue(std::string format, CHAR_DATA *ch, OBJ_DATA *arg1, CHAR_DATA *arg2, int type)
{
	act_new(format.c_str(), ch, (void*)arg1, (void*)arg2, type, POS_RESTING);
}

@sean-gilliam
Copy link
Collaborator Author

Latest commit has everything switched over to using the new queue methods.

Next step is to rename the new methods back to the old names.

Eg.

RS.Queue.AddToNewQueue(...)
...

to

RS.Queue.AddToQueue(...)
...

After that is complete, I'm going to have to run some extensive testing to ensure that the strict typing didn't break anything or any weird edge cases pop up.

@sean-gilliam
Copy link
Collaborator Author

Renamed the new methods back to the old names.

Removed the queue.c file and its entry in the CMake file. Goodbye old queue. You valiant warrior. You magical black box of void pointers. Thank you for your service, friend.

…ld cause undefined behavior.

Added a few more queue compliant functions
@sean-gilliam
Copy link
Collaborator Author

Alright with this last commit it should be good to test. I'm going to run some additional tests on my end, but it should all be good.

cc @rezalas @Psypher9

Copy link
Owner

@rezalas rezalas left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great, pulling down and building for local test run

@rezalas
Copy link
Owner

rezalas commented Sep 8, 2024

Good news, it's almost perfect! Bad news, we've got a bug.

When creating a new character, an assistant npc is assigned to the character. That mob speaks to the player frequently to give out good info that's useful to the player. That mob however doesn't speak now - he just does this: � ^�\

Sample:

You walk through a vast shimmering portal and find yourself somewhere else...
A Huge Mattress
  From the way it's been beaten almost completely flat, it's apparent that
this barely-padded floor serves as the entry point for people new to the
Academy.  A slight shimmering in the ceiling indicates the direction from
which those new to Shalar plummet.  The walls to the north, south, and east
are covered with signs and posters, mostly written in languages you can't
understand and pasted up on top of one another in such a manner as to make
them mostly illegible.  To your west, you see an intersection of hallways.  

[Exits: west]
A halfling recruit stands here greeting the "mattress fluffers."
A halfling recruit winks.

<56hp 165m 298mv -> 
�	^�\

@rezalas
Copy link
Owner

rezalas commented Sep 8, 2024

Looking at it further, not all NPC speech is broken - NPCs sending green text work (vendors speaking to you) and some NPCs using yellow text (guards using say in front of you) and I can see my own text when I use say whisper and tell. So only some NPC chat is broken, specifically assistants.

@sean-gilliam
Copy link
Collaborator Author

That's weird. Thank you for testing it out. Looks like I may have missed something or exposed something else.

At any rate, I've got a starting point.

@rezalas
Copy link
Owner

rezalas commented Sep 8, 2024

That's weird. Thank you for testing it out. Looks like I may have missed something or exposed something else.

At any rate, I've got a starting point.

Yeah this is great so far! I found another (probably related) issue: I cannot quit the game, because it says there are items pending for me. I'd bet it's queued up items from the assistant that need to be force removed.

<56hp 165m 292mv -> quit
You cannot quit while events requiring your presence are pending.

@sean-gilliam
Copy link
Collaborator Author

There was this weird thing where they were putting library calls in the queue

ex:

RS.Queue.AddToQueue(6, do_tell, mob, buf); 
RS.Queue.AddToQueue(6, sprintf, buf, (char *)"%s I see you,  mortal.", ch->name);

changed to

buffer = fmt::format("{} I see you,  mortal.", ch->name);
RS.Queue.AddToQueue(6, do_tell_queue, mob, buffer); 

another ex:

temp = palloc_string(row[0]);
RS.Queue.AddToQueue(inc * 5, free, temp);
RS.Queue.AddToQueue(inc * 5, say_to, fat, ch, (char *)"Just do yourself a favor, and watch out for $t.", temp);

changed to

temp = std::string(row[0]);
RS.Queue.AddToQueue(inc * 5, say_to_queue, fat, ch, "Just do yourself a favor, and watch out for $t.", temp);

I wonder if that has something to do with printing uninitialized / junk memory.

@sean-gilliam
Copy link
Collaborator Author

So this piece of text

You walk through a vast shimmering portal and find yourself somewhere else...

comes from do_enter from act_ente.c around line 149.

act("You walk through $p and find yourself somewhere else...", ch, portal, nullptr, TO_CHAR);

This piece of text

A halfling recruit winks.

looks to be from do_enter from act_ente.c around line 188.

/* Greet trigger for mobs  */
if (is_npc(fch) && IS_SET(fch->progtypes, MPROG_GREET))
	fch->pIndexData->mprogs->greet_prog(fch, ch);

The greet program in question comes from greet_prog_opening_greet from quest.c around line 932.

void greet_prog_opening_greet(CHAR_DATA *mob, CHAR_DATA *ch)

Haven't found any queue related functions around those parts yet. Still looking.

@rezalas
Copy link
Owner

rezalas commented Sep 9, 2024

I found the code for the assistant (referenced in code as 'academy pet' or 'pet') in mspec.c. That shows they're using a combination of message sending calls, including do_say and do_say_queue.

250: do_say(mob, "Shalar welcomes you.  I have been sent to ensure you have the chance to make your mark upon these "\
					"lands.  I will be guiding you for now.");
...
273: RS.Queue.AddToQueue(5, do_say_queue, mob, "To ask for my aid, direct your question to me.");

Looking at the text when I create a character, none of these become visible to the player, so I'm not sure which piece is broken.

@sean-gilliam
Copy link
Collaborator Author

Hmmm, do_say_queue is one of the functions I added to help fit do_say into the new queue.

I wonder if it's having trouble with the multi-line string syntax.

@sean-gilliam
Copy link
Collaborator Author

The last line in that function looks troubling. It's putting using do_say instead of do_say_queue on the queue.

It's also using one of those weird homemade string allocators.

@rezalas
Copy link
Owner

rezalas commented Sep 9, 2024

Unfortunately that didn't fix it, but it did cause me to look further into the problem - the pets may be having issues because they're not even fully generating in the player file. I checked both of the players, and maybe it's because it's not being fully composed. When it's being added to the player file now (it's stored in the bottom of the player file typically) it's not there - the NPC isn't being fully composed now for some reason.

A properly composed academy pet at the end of the player file:

#PET
Vnum 80
Name academy pet imp Thritak~
LogO 1724803694
ShD  Thritak~
LnD  A mischievous imp lurks in the shadows near Swami.
~
Desc Sliding through the shadows and out on thick, leathery wings, your eye seems to slide past this imp unless you concentrate closely.
~
Sex  1
HMV  89 89 100 100 100 100
Act  AIc
AfBy ST
Comm 0
Pos  8
Alig -1000
ACs  2 2 2 2
Attr 15 16 16 16 16
AMod 0 0 0 0 0
End
#END

The end of a new player after the upgrade:

#O
Vnum 24507
Nest 0
Owner none~
Cond 0
Wear 1
Cost 35
End

#O
Vnum 24537
Nest 0
Owner none~
Cond 0
Wear -1
Cost 50
End

#END

@sean-gilliam
Copy link
Collaborator Author

sean-gilliam commented Sep 9, 2024

PET information is written to the player file by

fwrite_pet which is called from save_char_obj in save.c

save_char_obj is called by multiple callers. Need to investigate further.

@sean-gilliam
Copy link
Collaborator Author

Making a bit of headway. It's processing queue events now, not segfaulting and I can exit. Still an issue with passing some parameters.

server:

[warning] Add => nanny added create_academy_pet with timer 3
[warning] Processing delete ==> 2
[warning] Processing delete ==> 1
[warning] Processing funcs ==> from nanny func create_academy_pet timer 0
[info] entered create_academy_pet
[info] entered create_academy_pet free
[info] entered create_academy_pet switch name
[info] entered create_academy_pet switch align
[info] entered create_academy_pet setup
[info] entered create_academy_pet greetings
[info] entered create_academy_pet queue
[warning] Add => create_academy_pet added do_say_queue_1 with timer 3
[warning] Add => create_academy_pet added do_say_queue_2 with timer 5
[warning] Add => create_academy_pet added do_say_queue_3 with timer 8
[warning] Processing delete ==> 0
[warning] Processing delete ==> 3
[warning] Processing delete ==> 5
[warning] Processing delete ==> 8
[warning] Processing delete ==> -1
[warning] Processing delete ==> 2
[warning] Processing delete ==> 4
[warning] Processing delete ==> 7
[warning] Processing delete ==> 1
[warning] Processing delete ==> 3
[warning] Processing delete ==> 6
[warning] Processing funcs ==> from create_academy_pet func do_say_queue_1 timer 0
[info] entered do_say_queue args => �G�5]
[warning] Processing delete ==> 0
[warning] Processing delete ==> 2
[warning] Processing delete ==> 5
[warning] Processing delete ==> -1
[warning] Processing delete ==> 1
[warning] Processing delete ==> 4
[warning] Processing funcs ==> from create_academy_pet func do_say_queue_2 timer 0
[info] entered do_say_queue args => �G�5]
[warning] Processing delete ==> 0
[warning] Processing delete ==> 3
[warning] Processing delete ==> -1
[warning] Processing delete ==> 2
[warning] Processing delete ==> 1
[warning] Processing funcs ==> from create_academy_pet func do_say_queue_3 timer 0
[info] entered do_say_queue args => ��5]o find food.
[warning] Processing delete ==> 0
[warning] Processing delete ==> -1
[info] Besto@localhost has quit. [2 played, 10 (0) obj]

client

<56hp 165m 180mv -> 
Quilik says 'Shalar welcomes you.  I have been sent to ensure you have the chance to make your mark upon these lands.  I will be guiding you for now.'
Quilik now follows you.

<56hp 165m 180mv -> 
You say '�G�5]'

<56hp 165m 180mv -> 
You say '�G�5]'

<56hp 165m 180mv -> 
You say '��5]'

<56hp 165m 180mv -> 

@sean-gilliam
Copy link
Collaborator Author

With this latest commit, I believe I have all the bugs ironed out. Tested it locally and game seems to be acting like it should. Going to remove the WIP tag and let you all do a proper review.

Side note: I've definitely learned a lot more about C++ debugging this PR. Maybe learned too much ;) Thanks for putting up with me while working through this.

@sean-gilliam sean-gilliam removed the WIP label Sep 16, 2024
@sean-gilliam sean-gilliam changed the title [WIP] Queue updates / refactor Queue updates / refactor Sep 16, 2024
@sean-gilliam
Copy link
Collaborator Author

cc @rezalas @Psypher9

@rezalas
Copy link
Owner

rezalas commented Sep 16, 2024

Looking now!

Copy link
Owner

@rezalas rezalas left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks great!

@rezalas rezalas merged commit 03df94c into rezalas:main Sep 16, 2024
3 checks passed
@sean-gilliam sean-gilliam deleted the queue-refactor branch September 16, 2024 23:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants