Skip to content

Fix Npc Attacking - #406

Merged
AngeloTadeucci merged 1 commit into
masterfrom
npc-attack
Apr 3, 2025
Merged

Fix Npc Attacking#406
AngeloTadeucci merged 1 commit into
masterfrom
npc-attack

Conversation

@AngeloTadeucci

@AngeloTadeucci AngeloTadeucci commented Apr 2, 2025

Copy link
Copy Markdown
Collaborator

No more spazzing out npcs attack you!

Summary by CodeRabbit

  • New Features

    • Enhanced in-game navigation with an improved position update mechanism for smoother and more reliable movement.
  • Refactor

    • Streamlined movement and skill execution logic for more responsive character actions.
    • Consolidated state management to simplify handling of character conditions.
  • Chore

    • Cleaned up redundant code and removed unused dependencies for improved stability and maintainability.

No more spazzing out npcs attack you!
@coderabbitai

coderabbitai Bot commented Apr 2, 2025

Copy link
Copy Markdown
Contributor

Walkthrough

This pull request updates the navigation and movement logic within the game server. It adds a new overload for updating an agent's position, renames a method to better reflect its behavior, and streamlines error logging. Additionally, death state handling and movement update logic in actor state components have been simplified. Minor cleanups include removing redundant return statements and unused using directives, and refining conditional syntax in the NPC update method.

Changes

File(s) Change Summary
Maple2...AgentNavigation.cs Added new overload UpdatePosition(Vector3 position), renamed FindClosestPoint to FindRandomPointAround, and adjusted error logging in position updates.
Maple2...MovementState.cs,
Maple2...SkillCast.cs,
Maple2...Walk.cs
Removed the Died method and replaced health checking with IsDead; simplified movement updates by replacing search radius calls with direct UpdatePosition; optimized attack point lookup using TryGetValue.
Maple2...SkillCastTask.cs Removed an unnecessary blank line and an extraneous return; statement from the SkillCast method.
Maple2...FieldNpc.cs Refined the isSpawning check using pattern matching (is keyword with or) to simplify conditional logic.
Maple2...NpcControlPacket.cs Deleted unused using directives to remove unused dependencies.

Sequence Diagram(s)

sequenceDiagram
    participant Caller
    participant AgentNav as AgentNavigation
    participant Field
    participant NPC

    Caller->>AgentNav: UpdatePosition(newPosition)
    AgentNav->>Field: FindNearestPoly(newPosition)
    alt Valid position found
        Field-->>AgentNav: PolyData
        AgentNav->>NPC: Update npc.Position
        AgentNav->>AgentNav: Update agent.npos
        AgentNav-->>Caller: Return true
    else No valid position
        AgentNav->>Logger: Log error message
        AgentNav-->>Caller: Return false
    end
Loading

Possibly related PRs

Suggested reviewers

  • Zintixx

Poem

I’m a happy rabbit, hopping by,
With code that leaps both low and high.
New paths and updates fill my day,
No more errors to lead astray.
In fields of code my joy does bloom 🐇✨!

Happy hops and bug-free tunes!


📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 35e218f and a74a0ee.

📒 Files selected for processing (7)
  • Maple2.Server.Game/Manager/Field/AgentNavigation.cs (3 hunks)
  • Maple2.Server.Game/Model/Field/Actor/ActorStateComponent/MovementState.cs (1 hunks)
  • Maple2.Server.Game/Model/Field/Actor/ActorStateComponent/MovementStateStates/MovementState.SkillCast.cs (3 hunks)
  • Maple2.Server.Game/Model/Field/Actor/ActorStateComponent/MovementStateStates/MovementState.Walk.cs (1 hunks)
  • Maple2.Server.Game/Model/Field/Actor/ActorStateComponent/MovementStateTasks/MovementState.SkillCastTask.cs (0 hunks)
  • Maple2.Server.Game/Model/Field/Actor/FieldNpc.cs (1 hunks)
  • Maple2.Server.Game/Packets/NpcControlPacket.cs (0 hunks)
💤 Files with no reviewable changes (2)
  • Maple2.Server.Game/Model/Field/Actor/ActorStateComponent/MovementStateTasks/MovementState.SkillCastTask.cs
  • Maple2.Server.Game/Packets/NpcControlPacket.cs
🧰 Additional context used
🧬 Code Definitions (1)
Maple2.Server.Game/Model/Field/Actor/FieldNpc.cs (1)
Maple2.Server.Game/Model/Field/Actor/ActorStateComponent/MovementState.cs (2)
  • MovementState (11-310)
  • MovementState (37-48)
⏰ Context from checks skipped due to timeout of 90000ms (1)
  • GitHub Check: build
🔇 Additional comments (8)
Maple2.Server.Game/Model/Field/Actor/FieldNpc.cs (1)

154-154: Refined Spawning Check with Pattern Matching

The change refactors the spawning check by replacing the traditional logical OR comparison with a more concise pattern matching expression (is ActorState.Spawn or ActorState.Regen). This improves readability without changing the original behavior. Just ensure the project targets C# 9 or later so that the new syntax is supported.

Maple2.Server.Game/Manager/Field/AgentNavigation.cs (3)

173-182: Good addition of a new position update method!

This new overload for UpdatePosition that accepts a Vector3 position parameter provides a more direct way to update an agent's position, which will help with the NPC movement issues. The implementation correctly finds the nearest polygon for the provided position and updates both the agent and NPC positions.


243-243: Method rename improves clarity

Renaming this method from FindClosestPoint to FindRandomPointAround better reflects what the method actually does - it finds a random point around the specified location rather than the closest point. This matches the underlying implementation using FindRandomPointAroundCircle.


258-259: Method signature update for consistency

The method signature has been appropriately updated to match the renamed method. The implementation correctly calls the more detailed version of the method, passing the agent's current position as the fallback.

Maple2.Server.Game/Model/Field/Actor/ActorStateComponent/MovementState.cs (1)

191-191: Improved death state check

Changing from checking if health is zero to using the actor.IsDead property is a better approach. This encapsulates the death logic in a single property, making the code more maintainable and ensuring consistent death state checking throughout the codebase.

Maple2.Server.Game/Model/Field/Actor/ActorStateComponent/MovementStateStates/MovementState.Walk.cs (1)

73-73: Simplified position update logic

The previous implementation likely calculated a search radius and then used FindClosestPoint to determine the new position. This direct call to UpdatePosition with the newly calculated position simplifies the code and aligns with the new method added to the AgentNavigation class. This change will help prevent NPCs from "spazzing out" during movement.

Maple2.Server.Game/Model/Field/Actor/ActorStateComponent/MovementStateStates/MovementState.SkillCast.cs (2)

30-30: Improved dictionary access pattern

Using TryGetValue instead of ContainsKey followed by direct indexing is more efficient and safer. This approach avoids a potential second dictionary lookup and handles the case where the key might not exist.


125-125: Streamlined position update during skill casting

Similar to the change in the Movement.Walk class, this direct call to UpdatePosition with the new position simplifies the code and eliminates the need for calculating a search radius or finding the closest point. This improvement helps prevent NPCs from exhibiting erratic behavior during combat skills.

✨ Finishing Touches
  • 📝 Generate Docstrings

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai plan to trigger planning for file edits and PR creation.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@AngeloTadeucci
AngeloTadeucci requested a review from Copilot April 2, 2025 04:32

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull Request Overview

This PR aims to fix erratic NPC attacking behavior by refining movement, state management, and navigation logic. Key changes include:

  • Removing unused dependencies and consolidating usings.
  • Updating state checks using pattern matching and streamlining NPC spawning logic.
  • Refactoring navigation updates and renaming helper functions to better reflect their purpose.

Reviewed Changes

Copilot reviewed 7 out of 7 changed files in this pull request and generated no comments.

Show a summary per file
File Description
Maple2.Server.Game/Packets/NpcControlPacket.cs Removed unused using statements to clean up dependencies.
Maple2.Server.Game/Model/Field/Actor/FieldNpc.cs Updated the NPC spawning check to use pattern matching.
Maple2.Server.Game/Model/Field/Actor/ActorStateComponent/MovementStateTasks/MovementState.SkillCastTask.cs Removed a redundant return statement in a void method.
Maple2.Server.Game/Model/Field/Actor/ActorStateComponent/MovementStateStates/MovementState.Walk.cs Simplified navigation update by integrating the position directly.
Maple2.Server.Game/Model/Field/Actor/ActorStateComponent/MovementStateStates/MovementState.SkillCast.cs Improved safety by using TryGetValue for attack point lookup and simplified navigation update logic.
Maple2.Server.Game/Model/Field/Actor/ActorStateComponent/MovementState.cs Removed the Died method and replaced health checks with an IsDead property.
Maple2.Server.Game/Manager/Field/AgentNavigation.cs Introduced an overloaded UpdatePosition method and renamed functions to better reflect their functionality.

@AngeloTadeucci
AngeloTadeucci merged commit b57a6d6 into master Apr 3, 2025
@AngeloTadeucci
AngeloTadeucci deleted the npc-attack branch April 4, 2025 04:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants