MMO Visible Lattice algorithm

Source: Internet
Author: User

Look at the notes, it's clear.

Using system;using system.collections.generic;using system.diagnostics;using system.linq;using System.Text;using System.threading.tasks;namespace zonetest{public class zone{public int mid;public float width;public float Height; Public list<zone> visiblezonelist = new list<zone> ();p ublic Zone (int id, float width, float height) {mId = Id;w Idth = width; Height = height;} public void Addvisiblezone (zone zone) {Visiblezonelist.add (zone);}} public class Scene{private List<zone> mzonelist = new list<zone> ();p ublic Scene (float w,float h) {Mscenewidth = W;msceneheight = h;} Public Const FLOAT Zone_side = 1.0f;//scene width height private float mscenewidth;private float msceneheight;//grid column number and row number private int Mzone columncount;private int mzonelinecount;public Zone getzone (int id) {if (id < 0 | | ID > mzonelist.count) {return null;} return mzonelist[id];}  zone/*| Mscenewidth |-------------------| | | | | Zone_side-----------------| | | | | -----------------msceneheight| | | | |   -------------------  -----------------Line 0-----------------Line 1-----------------line 2-----------------line 3column0 column1 Column2   column3| | | |   ||||   |||| |||| * */public bool Initzone () {//Calculate the Zone row and column in the scene mzonelinecount = (int) math.ceiling ((double) msceneheight/(double) zone_side) ; mzonecolumncount = (int) math.ceiling (double) mscenewidth/(double) zone_side);//create zonefor (int i = 0; i < Mzonelineco Unt i++) {for (int j = 0; J < Mzonecolumncount; J + +) {int id = i * mzonecolumncount + j; Zone Zone = new zone (ID, zone_side, zone_side); Mzonelist.add (zone);}} The maximum number of squares around is visible maxnearbyzonenumber*maxnearbyzonenumber a const int maxnearbyzonenumber = 3;//The starting offset of the visible lattice const int offset = maxnearbyzonenumber/2;//Add a list of visible squares around */*-------------* |0|1|2| *-------------* |3|4|5| *-------------* |6|7|8| *-------------*///0 visible lattice for the 0134//4 visible lattice for the 0123456789//7 visible lattice for 345678//traverse all lattice rows for (int i = 0; i < Mzonelinecount; i++) {/ /Traverse all lattice for (int j = 0; J < Mzonecolumncount; J + +) {//the current lattice Idint ID to determine visibility = I * Mzonecolumncount + j; Zone zone = mzonelist[id];//traverse around Maxnearbyzonenumber*maxnearbyzonenumber lattice//Determine [x, Y] This lattice is out of bounds//line for (int n = 0; n < Max Nearbyzonenumber; n++) {int y = I-offset + n;if (Y < 0 | | y >= mzonelinecount) continue;//y out of bounds//column for (int m = 0; M < Maxnearbyzonen umber; m++) {int x = J-offset + m;if (x < 0 | | x >= mzonecolumncount) continue;//x out of bounds//count current lattice idint Visiblezoneid = y * mzone ColumnCount + x;//if (Visiblezoneid = = ID) continue;//is their own lattice also to join, oneself belongs to the visible lattice zone. Addvisiblezone (Mzonelist[visiblezoneid]);}}} return true;} public void Print () {for (int i = 0; i < Mzonelinecount; i++) {String str = "";//Traverse all lattice for (int j = 0; J < Mzonecolumncount; J + +) {str + = (I * mzonecolumncount + j). ToString () + "\ T";} Debug.Print (str);}} public void Printzone (int i) {Debug.Print ("[{0}]---------------------", i); var zone = mzonelist[i];string str = ""; foreach (Var z in zone.) Visiblezonelist) {str+=z.mid.tostring () + "\ T";} Debug.Print (str);D ebug. Print ("[{0}]---------------------", i);}}class program{static void Main (string[] args) {Scene scene = new Scene (5,7); scene. Initzone (); scene. Print (); int oldid = 5;int NewId = 10; Zone oldzone = scene. Getzone (oldid); Zone newzone = scene. Getzone (newId);/*//comparison calculation should delete my zonelist<zone> Deletemezone = new list<zone> (); foreach (Var Zone in Oldzone.visiblezonelist) {if (!newzone.visiblezonelist.contains (zone)) {Deletemezone.add (zone);D ebug. Print (Zone.mId.ToString ());}} Debug.Print ("-----------");//comparison calculation should create my list<zone> Createmezone = new list<zone> (); foreach (Var Zone in Newzone.visiblezonelist) {if (!oldzone.visiblezonelist.contains (zone)) {Createmezone.add (zone);D ebug. Print (Zone.mId.ToString ());}} */scene. Printzone (4); scene. Printzone (+); scene. Printzone (); scene. Printzone (30);}}

  

MMO Visible Lattice algorithm

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.